Logo AND Algorithmique Numérique Distribuée

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