Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix maestro-set
[simgrid.git] / src / kernel / context / ContextThread.cpp
1 /* Copyright (c) 2009-2018. 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 #include "xbt/xbt_os_thread.h"
14
15 #include <functional>
16 #include <utility>
17
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
19
20 namespace simgrid {
21 namespace kernel {
22 namespace context {
23
24 // ThreadContextFactory
25
26 ThreadContextFactory::ThreadContextFactory()
27     : ContextFactory("ThreadContextFactory"), parallel_(SIMIX_context_is_parallel())
28 {
29   if (parallel_)
30     ParallelThreadContext::initialize();
31 }
32
33 ThreadContextFactory::~ThreadContextFactory()
34 {
35   if (parallel_)
36     ParallelThreadContext::finalize();
37 }
38
39 ThreadContext* ThreadContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup,
40                                                     smx_actor_t process, bool maestro)
41 {
42   if (parallel_)
43     return this->new_context<ParallelThreadContext>(std::move(code), cleanup, process, maestro);
44   else
45     return this->new_context<SerialThreadContext>(std::move(code), cleanup, process, maestro);
46 }
47
48 void ThreadContextFactory::run_all()
49 {
50   if (parallel_) {
51     // Parallel execution
52     ParallelThreadContext::run_all();
53   } else {
54     // Serial execution
55     SerialThreadContext::run_all();
56   }
57 }
58
59 // ThreadContext
60
61 ThreadContext::ThreadContext(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process,
62                              bool maestro)
63     : AttachContext(std::move(code), cleanup, process), is_maestro_(maestro)
64 {
65   // We do not need the semaphores when maestro is in main,
66   // but creating them anyway simplifies things when maestro is externalized
67   this->begin_ = xbt_os_sem_init(0);
68   this->end_ = xbt_os_sem_init(0);
69
70   /* If the user provided a function for the process then use it */
71   if (has_code()) {
72     if (smx_context_stack_size_was_set)
73       xbt_os_thread_setstacksize(smx_context_stack_size);
74     if (smx_context_guard_size_was_set)
75       xbt_os_thread_setguardsize(smx_context_guard_size);
76
77     /* create and start the process */
78     /* NOTE: The first argument to xbt_os_thread_create used to be the process *
79     * name, but now the name is stored at SIMIX level, so we pass a null  */
80     this->thread_ = xbt_os_thread_create(nullptr, ThreadContext::wrapper, this, this);
81     /* wait the starting of the newly created process */
82     xbt_os_sem_acquire(this->end_);
83   }
84
85   /* Otherwise, we attach to the current thread */
86   else {
87     xbt_os_thread_set_extra_data(this);
88   }
89 }
90
91 ThreadContext::~ThreadContext()
92 {
93   if (this->thread_) /* If there is a thread (maestro don't have any), wait for its termination */
94     xbt_os_thread_join(this->thread_, nullptr);
95
96   /* destroy the synchronization objects */
97   xbt_os_sem_destroy(this->begin_);
98   xbt_os_sem_destroy(this->end_);
99 }
100
101 void *ThreadContext::wrapper(void *param)
102 {
103   ThreadContext* context = static_cast<ThreadContext*>(param);
104
105 #ifndef WIN32
106   /* Install alternate signal stack, for SIGSEGV handler. */
107   stack_t stack;
108   stack.ss_sp = sigsegv_stack;
109   stack.ss_size = sizeof sigsegv_stack;
110   stack.ss_flags = 0;
111   sigaltstack(&stack, nullptr);
112 #endif
113   // Tell the caller (normally the maestro) we are starting, and wait for its green light
114   xbt_os_sem_release(context->end_);
115   context->start();
116
117   try {
118     (*context)();
119   } catch (StopRequest const&) {
120     XBT_DEBUG("Caught a StopRequest");
121     xbt_assert(not context->is_maestro(), "Maestro shall not receive StopRequests, even when detached.");
122   } catch (simgrid::Exception const& e) {
123     XBT_INFO("Actor killed by an uncatched exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
124     throw;
125   }
126   if (not context->is_maestro()) // Just in case somebody detached maestro
127     context->Context::stop();
128
129   // Signal to the caller (normally the maestro) that we have finished:
130   context->yield();
131
132 #ifndef WIN32
133   stack.ss_flags = SS_DISABLE;
134   sigaltstack(&stack, nullptr);
135 #endif
136   return nullptr;
137 }
138
139 void ThreadContext::release()
140 {
141   xbt_os_sem_release(this->begin_);
142 }
143
144 void ThreadContext::wait()
145 {
146   xbt_os_sem_acquire(this->end_);
147 }
148
149 void ThreadContext::start()
150 {
151   xbt_os_sem_acquire(this->begin_);
152   this->start_hook();
153 }
154
155 void ThreadContext::yield()
156 {
157   this->yield_hook();
158   xbt_os_sem_release(this->end_);
159 }
160
161 void ThreadContext::stop()
162 {
163   Context::stop();
164   throw StopRequest();
165 }
166
167 void ThreadContext::suspend()
168 {
169   this->yield();
170   this->start();
171 }
172
173 void ThreadContext::attach_start()
174 {
175   // We're breaking the layers here by depending on the upper layer:
176   ThreadContext* maestro = (ThreadContext*)simix_global->maestro_process->context_;
177   xbt_os_sem_release(maestro->begin_);
178   xbt_assert(not this->is_maestro());
179   this->start();
180 }
181
182 void ThreadContext::attach_stop()
183 {
184   xbt_assert(not this->is_maestro());
185   this->yield();
186
187   ThreadContext* maestro = (ThreadContext*)simix_global->maestro_process->context_;
188   xbt_os_sem_acquire(maestro->end_);
189
190   xbt_os_thread_set_extra_data(nullptr);
191 }
192
193 // SerialThreadContext
194
195 void SerialThreadContext::run_all()
196 {
197   for (smx_actor_t const& process : simix_global->process_to_run) {
198     XBT_DEBUG("Handling %p", process);
199     ThreadContext* context = static_cast<ThreadContext*>(process->context_);
200     context->release();
201     context->wait();
202   }
203 }
204
205 // ParallelThreadContext
206
207 xbt_os_sem_t ParallelThreadContext::thread_sem_ = nullptr;
208
209 void ParallelThreadContext::initialize()
210 {
211   thread_sem_ = xbt_os_sem_init(SIMIX_context_get_nthreads());
212 }
213
214 void ParallelThreadContext::finalize()
215 {
216   xbt_os_sem_destroy(thread_sem_);
217   thread_sem_ = nullptr;
218 }
219
220 void ParallelThreadContext::run_all()
221 {
222   for (smx_actor_t const& process : simix_global->process_to_run)
223     static_cast<ThreadContext*>(process->context_)->release();
224   for (smx_actor_t const& process : simix_global->process_to_run)
225     static_cast<ThreadContext*>(process->context_)->wait();
226 }
227
228 void ParallelThreadContext::start_hook()
229 {
230   if (not is_maestro()) /* parallel run */
231     xbt_os_sem_acquire(thread_sem_);
232 }
233
234 void ParallelThreadContext::yield_hook()
235 {
236   if (not is_maestro()) /* parallel run */
237     xbt_os_sem_release(thread_sem_);
238 }
239
240 XBT_PRIVATE ContextFactory* thread_factory()
241 {
242   XBT_VERB("Activating thread context factory");
243   return new ThreadContextFactory();
244 }
245 }}} // namespace