Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Improve process termination in Java world.
[simgrid.git] / src / kernel / context / ContextThread.cpp
1 /* Copyright (c) 2009-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/kernel/context/ContextThread.hpp"
7
8 #include "simgrid/Exception.hpp"
9 #include "src/internal_config.h" /* loads context system definitions */
10 #include "src/simix/smx_private.hpp"
11 #include "src/xbt_modinter.h" /* prototype of os thread module's init/exit in XBT */
12 #include "xbt/function_types.h"
13
14 #include <functional>
15 #include <utility>
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
18
19 namespace simgrid {
20 namespace kernel {
21 namespace context {
22
23 // ThreadContextFactory
24
25 ThreadContextFactory::ThreadContextFactory() : ContextFactory(), parallel_(SIMIX_context_is_parallel())
26 {
27   if (parallel_)
28     ParallelThreadContext::initialize();
29 }
30
31 ThreadContextFactory::~ThreadContextFactory()
32 {
33   if (parallel_)
34     ParallelThreadContext::finalize();
35 }
36
37 ThreadContext* ThreadContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup,
38                                                     smx_actor_t actor, bool maestro)
39 {
40   if (parallel_)
41     return this->new_context<ParallelThreadContext>(std::move(code), cleanup, actor, maestro);
42   else
43     return this->new_context<SerialThreadContext>(std::move(code), cleanup, actor, maestro);
44 }
45
46 void ThreadContextFactory::run_all()
47 {
48   if (parallel_) {
49     // Parallel execution
50     ParallelThreadContext::run_all();
51   } else {
52     // Serial execution
53     SerialThreadContext::run_all();
54   }
55 }
56
57 // ThreadContext
58
59 ThreadContext::ThreadContext(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t actor, bool maestro)
60     : AttachContext(std::move(code), cleanup, actor), is_maestro_(maestro)
61 {
62   /* If the user provided a function for the actor then use it */
63   if (has_code()) {
64     /* create and start the actor */
65     this->thread_ = new std::thread(ThreadContext::wrapper, this);
66     /* wait the starting of the newly created actor */
67     this->end_.acquire();
68   }
69
70   /* Otherwise, we attach to the current thread */
71   else {
72     Context::set_current(this);
73   }
74 }
75
76 ThreadContext::~ThreadContext()
77 {
78   if (this->thread_) { /* Maestro don't have any thread */
79     thread_->join();
80     delete thread_;
81   }
82 }
83
84 void *ThreadContext::wrapper(void *param)
85 {
86   ThreadContext* context = static_cast<ThreadContext*>(param);
87   Context::set_current(context);
88
89 #ifndef WIN32
90   /* Install alternate signal stack, for SIGSEGV handler. */
91   stack_t stack;
92   stack.ss_sp = sigsegv_stack;
93   stack.ss_size = sizeof sigsegv_stack;
94   stack.ss_flags = 0;
95   sigaltstack(&stack, nullptr);
96 #endif
97   // Tell the caller (normally the maestro) we are starting, and wait for its green light
98   context->end_.release();
99   context->start();
100
101   try {
102     (*context)();
103     if (not context->is_maestro()) { // Just in case somebody detached maestro
104       context->Context::stop();
105       context->stop_hook();
106     }
107   } catch (StopRequest const&) {
108     XBT_DEBUG("Caught a StopRequest in Thread::wrapper");
109     xbt_assert(not context->is_maestro(), "Maestro shall not receive StopRequests, even when detached.");
110   } catch (simgrid::Exception const& e) {
111     XBT_INFO("Actor killed by an uncatched exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
112     throw;
113   }
114   // Signal to the caller (normally the maestro) that we have finished:
115   context->yield();
116
117 #ifndef WIN32
118   stack.ss_flags = SS_DISABLE;
119   sigaltstack(&stack, nullptr);
120 #endif
121   XBT_DEBUG("Terminating");
122   Context::set_current(nullptr);
123   return nullptr;
124 }
125
126 void ThreadContext::release()
127 {
128   this->begin_.release();
129 }
130
131 void ThreadContext::wait()
132 {
133   this->end_.acquire();
134 }
135
136 void ThreadContext::start()
137 {
138   this->begin_.acquire();
139   this->start_hook();
140 }
141
142 void ThreadContext::yield()
143 {
144   this->yield_hook();
145   this->end_.release();
146 }
147
148 void ThreadContext::stop()
149 {
150   Context::stop();
151   stop_hook();
152   throw StopRequest();
153 }
154
155 void ThreadContext::suspend()
156 {
157   this->yield();
158   this->start();
159 }
160
161 void ThreadContext::attach_start()
162 {
163   // We're breaking the layers here by depending on the upper layer:
164   ThreadContext* maestro = (ThreadContext*)simix_global->maestro_process->context_;
165   maestro->begin_.release();
166   xbt_assert(not this->is_maestro());
167   this->start();
168 }
169
170 void ThreadContext::attach_stop()
171 {
172   xbt_assert(not this->is_maestro());
173   this->yield();
174
175   ThreadContext* maestro = (ThreadContext*)simix_global->maestro_process->context_;
176   maestro->end_.acquire();
177
178   Context::set_current(nullptr);
179 }
180
181 // SerialThreadContext
182
183 void SerialThreadContext::run_all()
184 {
185   for (smx_actor_t const& actor : simix_global->process_to_run) {
186     XBT_DEBUG("Handling %p", actor);
187     ThreadContext* context = static_cast<ThreadContext*>(actor->context_);
188     context->release();
189     context->wait();
190   }
191 }
192
193 // ParallelThreadContext
194
195 xbt::OsSemaphore* ParallelThreadContext::thread_sem_ = nullptr;
196
197 void ParallelThreadContext::initialize()
198 {
199   thread_sem_ = new xbt::OsSemaphore(SIMIX_context_get_nthreads());
200 }
201
202 void ParallelThreadContext::finalize()
203 {
204   delete thread_sem_;
205   thread_sem_ = nullptr;
206 }
207
208 void ParallelThreadContext::run_all()
209 {
210   for (smx_actor_t const& actor : simix_global->process_to_run)
211     static_cast<ThreadContext*>(actor->context_)->release();
212   for (smx_actor_t const& actor : simix_global->process_to_run)
213     static_cast<ThreadContext*>(actor->context_)->wait();
214 }
215
216 void ParallelThreadContext::start_hook()
217 {
218   if (not is_maestro()) /* parallel run */
219     thread_sem_->acquire();
220 }
221
222 void ParallelThreadContext::yield_hook()
223 {
224   if (not is_maestro()) /* parallel run */
225     thread_sem_->release();
226 }
227
228 XBT_PRIVATE ContextFactory* thread_factory()
229 {
230   XBT_VERB("Activating thread context factory");
231   return new ThreadContextFactory();
232 }
233 }}} // namespace