Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3f93201506e88325f5a69056673d5b0c018bc9f8
[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);
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     SIMIX_context_set_current(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   SIMIX_context_set_current(context);
95
96 #ifndef WIN32
97   /* Install alternate signal stack, for SIGSEGV handler. */
98   stack_t stack;
99   stack.ss_sp = sigsegv_stack;
100   stack.ss_size = sizeof sigsegv_stack;
101   stack.ss_flags = 0;
102   sigaltstack(&stack, nullptr);
103 #endif
104   // Tell the caller (normally the maestro) we are starting, and wait for its green light
105   context->end_.release();
106   context->start();
107
108   try {
109     (*context)();
110   } catch (StopRequest const&) {
111     XBT_DEBUG("Caught a StopRequest");
112     xbt_assert(not context->is_maestro(), "Maestro shall not receive StopRequests, even when detached.");
113   } catch (simgrid::Exception const& e) {
114     XBT_INFO("Actor killed by an uncatched exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
115     throw;
116   }
117   if (not context->is_maestro()) // Just in case somebody detached maestro
118     context->Context::stop();
119
120   // Signal to the caller (normally the maestro) that we have finished:
121   context->yield();
122
123 #ifndef WIN32
124   stack.ss_flags = SS_DISABLE;
125   sigaltstack(&stack, nullptr);
126 #endif
127   return nullptr;
128 }
129
130 void ThreadContext::release()
131 {
132   this->begin_.release();
133 }
134
135 void ThreadContext::wait()
136 {
137   this->end_.acquire();
138 }
139
140 void ThreadContext::start()
141 {
142   this->begin_.acquire();
143   this->start_hook();
144 }
145
146 void ThreadContext::yield()
147 {
148   this->yield_hook();
149   this->end_.release();
150 }
151
152 void ThreadContext::stop()
153 {
154   Context::stop();
155   throw StopRequest();
156 }
157
158 void ThreadContext::suspend()
159 {
160   this->yield();
161   this->start();
162 }
163
164 void ThreadContext::attach_start()
165 {
166   // We're breaking the layers here by depending on the upper layer:
167   ThreadContext* maestro = (ThreadContext*)simix_global->maestro_process->context_;
168   maestro->begin_.release();
169   xbt_assert(not this->is_maestro());
170   this->start();
171 }
172
173 void ThreadContext::attach_stop()
174 {
175   xbt_assert(not this->is_maestro());
176   this->yield();
177
178   ThreadContext* maestro = (ThreadContext*)simix_global->maestro_process->context_;
179   maestro->end_.acquire();
180
181   SIMIX_context_set_current(nullptr);
182 }
183
184 // SerialThreadContext
185
186 void SerialThreadContext::run_all()
187 {
188   for (smx_actor_t const& process : simix_global->process_to_run) {
189     XBT_DEBUG("Handling %p", process);
190     ThreadContext* context = static_cast<ThreadContext*>(process->context_);
191     context->release();
192     context->wait();
193   }
194 }
195
196 // ParallelThreadContext
197
198 xbt::OsSemaphore* ParallelThreadContext::thread_sem_ = nullptr;
199
200 void ParallelThreadContext::initialize()
201 {
202   thread_sem_ = new xbt::OsSemaphore(SIMIX_context_get_nthreads());
203 }
204
205 void ParallelThreadContext::finalize()
206 {
207   delete thread_sem_;
208   thread_sem_ = nullptr;
209 }
210
211 void ParallelThreadContext::run_all()
212 {
213   for (smx_actor_t const& process : simix_global->process_to_run)
214     static_cast<ThreadContext*>(process->context_)->release();
215   for (smx_actor_t const& process : simix_global->process_to_run)
216     static_cast<ThreadContext*>(process->context_)->wait();
217 }
218
219 void ParallelThreadContext::start_hook()
220 {
221   if (not is_maestro()) /* parallel run */
222     thread_sem_->acquire();
223 }
224
225 void ParallelThreadContext::yield_hook()
226 {
227   if (not is_maestro()) /* parallel run */
228     thread_sem_->release();
229 }
230
231 XBT_PRIVATE ContextFactory* thread_factory()
232 {
233   XBT_VERB("Activating thread context factory");
234   return new ThreadContextFactory();
235 }
236 }}} // namespace