Logo AND Algorithmique Numérique Distribuée

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