Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Use unsigned char* for context stacks.
[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, smx_actor_t actor, bool maestro)
38 {
39   if (parallel_)
40     return this->new_context<ParallelThreadContext>(std::move(code), actor, maestro);
41   else
42     return this->new_context<SerialThreadContext>(std::move(code), actor, maestro);
43 }
44
45 void ThreadContextFactory::run_all()
46 {
47   if (parallel_) {
48     // Parallel execution
49     ParallelThreadContext::run_all();
50   } else {
51     // Serial execution
52     SerialThreadContext::run_all();
53   }
54 }
55
56 // ThreadContext
57
58 ThreadContext::ThreadContext(std::function<void()> code, smx_actor_t actor, bool maestro)
59     : AttachContext(std::move(code), actor), is_maestro_(maestro)
60 {
61   /* If the user provided a function for the actor then use it */
62   if (has_code()) {
63     /* create and start the actor */
64     this->thread_ = new std::thread(ThreadContext::wrapper, this);
65     /* wait the starting of the newly created actor */
66     this->end_.acquire();
67   }
68
69   /* Otherwise, we attach to the current thread */
70   else {
71     Context::set_current(this);
72   }
73 }
74
75 ThreadContext::~ThreadContext()
76 {
77   if (this->thread_) { /* Maestro don't have any thread */
78     thread_->join();
79     delete thread_;
80   }
81 }
82
83 void *ThreadContext::wrapper(void *param)
84 {
85   ThreadContext* context = static_cast<ThreadContext*>(param);
86   Context::set_current(context);
87
88 #ifndef WIN32
89   /* Install alternate signal stack, for SIGSEGV handler. */
90   stack_t stack;
91   stack.ss_sp = sigsegv_stack;
92   stack.ss_size = sizeof sigsegv_stack;
93   stack.ss_flags = 0;
94   sigaltstack(&stack, nullptr);
95 #endif
96   // Tell the caller (normally the maestro) we are starting, and wait for its green light
97   context->end_.release();
98   context->start();
99
100   try {
101     (*context)();
102     if (not context->is_maestro()) { // Just in case somebody detached maestro
103       context->Context::stop();
104       context->stop_hook();
105     }
106   } catch (ForcefulKillException const&) {
107     XBT_DEBUG("Caught a ForcefulKillException in Thread::wrapper");
108     xbt_assert(not context->is_maestro(), "Maestro shall not receive ForcefulKillExceptions, even when detached.");
109   } catch (simgrid::Exception const& e) {
110     XBT_INFO("Actor killed by an uncatched exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
111     throw;
112   }
113   // Signal to the caller (normally the maestro) that we have finished:
114   context->yield();
115
116 #ifndef WIN32
117   stack.ss_flags = SS_DISABLE;
118   sigaltstack(&stack, nullptr);
119 #endif
120   XBT_DEBUG("Terminating");
121   Context::set_current(nullptr);
122   return nullptr;
123 }
124
125 void ThreadContext::release()
126 {
127   this->begin_.release();
128 }
129
130 void ThreadContext::wait()
131 {
132   this->end_.acquire();
133 }
134
135 void ThreadContext::start()
136 {
137   this->begin_.acquire();
138   this->start_hook();
139 }
140
141 void ThreadContext::yield()
142 {
143   this->yield_hook();
144   this->end_.release();
145 }
146
147 void ThreadContext::stop()
148 {
149   Context::stop();
150   stop_hook();
151   throw ForcefulKillException();
152 }
153
154 void ThreadContext::suspend()
155 {
156   this->yield();
157   this->start();
158 }
159
160 void ThreadContext::attach_start()
161 {
162   // We're breaking the layers here by depending on the upper layer:
163   ThreadContext* maestro = (ThreadContext*)simix_global->maestro_process->context_;
164   maestro->begin_.release();
165   xbt_assert(not this->is_maestro());
166   this->start();
167 }
168
169 void ThreadContext::attach_stop()
170 {
171   xbt_assert(not this->is_maestro());
172   this->yield();
173
174   ThreadContext* maestro = (ThreadContext*)simix_global->maestro_process->context_;
175   maestro->end_.acquire();
176
177   Context::set_current(nullptr);
178 }
179
180 // SerialThreadContext
181
182 void SerialThreadContext::run_all()
183 {
184   for (smx_actor_t const& actor : simix_global->actors_to_run) {
185     XBT_DEBUG("Handling %p", actor);
186     ThreadContext* context = static_cast<ThreadContext*>(actor->context_);
187     context->release();
188     context->wait();
189   }
190 }
191
192 // ParallelThreadContext
193
194 xbt::OsSemaphore* ParallelThreadContext::thread_sem_ = nullptr;
195
196 void ParallelThreadContext::initialize()
197 {
198   thread_sem_ = new xbt::OsSemaphore(SIMIX_context_get_nthreads());
199 }
200
201 void ParallelThreadContext::finalize()
202 {
203   delete thread_sem_;
204   thread_sem_ = nullptr;
205 }
206
207 void ParallelThreadContext::run_all()
208 {
209   for (smx_actor_t const& actor : simix_global->actors_to_run)
210     static_cast<ThreadContext*>(actor->context_)->release();
211   for (smx_actor_t const& actor : simix_global->actors_to_run)
212     static_cast<ThreadContext*>(actor->context_)->wait();
213 }
214
215 void ParallelThreadContext::start_hook()
216 {
217   if (not is_maestro()) /* parallel run */
218     thread_sem_->acquire();
219 }
220
221 void ParallelThreadContext::yield_hook()
222 {
223   if (not is_maestro()) /* parallel run */
224     thread_sem_->release();
225 }
226
227 XBT_PRIVATE ContextFactory* thread_factory()
228 {
229   XBT_VERB("Activating thread context factory");
230   return new ThreadContextFactory();
231 }
232 }}} // namespace