Logo AND Algorithmique Numérique Distribuée

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