Logo AND Algorithmique Numérique Distribuée

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