Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simix] Use std::string for s_smx_process_arg
[simgrid.git] / src / simix / ContextThread.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/internal_config.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/ContextThread.hpp"
18
19 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
20
21 static xbt_os_sem_t smx_ctx_thread_sem = nullptr;
22
23 namespace simgrid {
24 namespace simix {
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_process_t process)
53 {
54   return this->new_context<ThreadContext>(std::move(code), cleanup, process, !code);
55 }
56
57 void ThreadContextFactory::run_all()
58 {
59   if (smx_ctx_thread_sem == nullptr) {
60     // Serial execution
61     smx_process_t process;
62     unsigned int cursor;
63     xbt_dynar_foreach(simix_global->process_to_run, cursor, process) {
64       XBT_DEBUG("Handling %p",process);
65       ThreadContext* context = static_cast<ThreadContext*>(process->context);
66       xbt_os_sem_release(context->begin_);
67       xbt_os_sem_acquire(context->end_);
68     }
69   } else {
70     // Parallel execution
71     unsigned int index;
72     smx_process_t process;
73     xbt_dynar_foreach(simix_global->process_to_run, index, process)
74       xbt_os_sem_release(static_cast<ThreadContext*>(process->context)->begin_);
75     xbt_dynar_foreach(simix_global->process_to_run, index, process)
76        xbt_os_sem_acquire(static_cast<ThreadContext*>(process->context)->end_);
77   }
78 }
79
80 ThreadContext* ThreadContextFactory::self()
81 {
82   return static_cast<ThreadContext*>(xbt_os_thread_get_extra_data());
83 }
84
85 ThreadContext* ThreadContextFactory::attach(void_pfn_smxprocess_t cleanup_func, smx_process_t process)
86 {
87   return this->new_context<ThreadContext>(
88     std::function<void()>(), cleanup_func, process, false);
89 }
90
91 ThreadContext* ThreadContextFactory::create_maestro(std::function<void()> code, smx_process_t process)
92 {
93     return this->new_context<ThreadContext>(std::move(code), nullptr, process, true);
94 }
95
96 ThreadContext::ThreadContext(std::function<void()> code,
97     void_pfn_smxprocess_t cleanup, smx_process_t process, bool maestro)
98   : AttachContext(std::move(code), cleanup, process)
99 {
100   // We do not need the semaphores when maestro is in main, but we create them anyway for simplicity wrt the case where maestro is externalized
101   this->begin_ = xbt_os_sem_init(0);
102   this->end_ = xbt_os_sem_init(0);
103
104   /* If the user provided a function for the process then use it */
105   if (has_code()) {
106     if (smx_context_stack_size_was_set)
107       xbt_os_thread_setstacksize(smx_context_stack_size);
108     if (smx_context_guard_size_was_set)
109       xbt_os_thread_setguardsize(smx_context_guard_size);
110
111     /* create and start the process */
112     /* NOTE: The first argument to xbt_os_thread_create used to be the process *
113     * name, but now the name is stored at SIMIX level, so we pass a null  */
114     this->thread_ =
115       xbt_os_thread_create(NULL,
116         maestro ? ThreadContext::maestro_wrapper : ThreadContext::wrapper,
117         this, this);
118     /* wait the starting of the newly created process */
119     xbt_os_sem_acquire(this->end_);
120   }
121
122   /* Otherwise, we attach to the current thread */
123   else {
124     xbt_os_thread_set_extra_data(this);
125   }
126 }
127
128 ThreadContext::~ThreadContext()
129 {
130   if (this->thread_) /* If there is a thread (maestro don't have any), wait for its termination */
131     xbt_os_thread_join(this->thread_, nullptr);
132
133   /* destroy the synchronization objects */
134   xbt_os_sem_destroy(this->begin_);
135   xbt_os_sem_destroy(this->end_);
136 }
137
138 void *ThreadContext::wrapper(void *param)
139 {
140   ThreadContext* context = static_cast<ThreadContext*>(param);
141
142 #ifndef WIN32
143   /* Install alternate signal stack, for SIGSEGV handler. */
144   stack_t stack;
145   stack.ss_sp = sigsegv_stack;
146   stack.ss_size = sizeof sigsegv_stack;
147   stack.ss_flags = 0;
148   sigaltstack(&stack, nullptr);
149 #endif
150   /* Tell the maestro we are starting, and wait for its green light */
151   xbt_os_sem_release(context->end_);
152
153   xbt_os_sem_acquire(context->begin_);
154   if (smx_ctx_thread_sem)       /* parallel run */
155     xbt_os_sem_acquire(smx_ctx_thread_sem);
156
157   (*context)();
158   context->stop();
159
160   return nullptr;
161 }
162
163 void *ThreadContext::maestro_wrapper(void *param)
164 {
165   ThreadContext* context = static_cast<ThreadContext*>(param);
166
167 #ifndef WIN32
168   /* Install alternate signal stack, for SIGSEGV handler. */
169   stack_t stack;
170   stack.ss_sp = sigsegv_stack;
171   stack.ss_size = sizeof sigsegv_stack;
172   stack.ss_flags = 0;
173   sigaltstack(&stack, nullptr);
174 #endif
175   /* Tell the caller we are starting */
176   xbt_os_sem_release(context->end_);
177
178   // Wait for the caller to give control back to us:
179   xbt_os_sem_acquire(context->begin_);
180   (*context)();
181
182   // Tell main that we have finished:
183   xbt_os_sem_release(context->end_);
184
185   return nullptr;
186 }
187
188 void ThreadContext::start()
189 {
190   xbt_os_sem_acquire(this->begin_);
191   if (smx_ctx_thread_sem)       /* parallel run */
192     xbt_os_sem_acquire(smx_ctx_thread_sem);
193 }
194
195 void ThreadContext::stop()
196 {
197   Context::stop();
198   if (smx_ctx_thread_sem)
199     xbt_os_sem_release(smx_ctx_thread_sem);
200
201   // Signal to the maestro that it has finished:
202   xbt_os_sem_release(this->end_);
203
204   xbt_os_thread_exit(NULL);
205 }
206
207 void ThreadContext::suspend()
208 {
209   if (smx_ctx_thread_sem)
210     xbt_os_sem_release(smx_ctx_thread_sem);
211   xbt_os_sem_release(this->end_);
212   xbt_os_sem_acquire(this->begin_);
213   if (smx_ctx_thread_sem)
214     xbt_os_sem_acquire(smx_ctx_thread_sem);
215 }
216
217 void ThreadContext::attach_start()
218 {
219   // We're breaking the layers here by depending on the upper layer:
220   ThreadContext* maestro = (ThreadContext*) simix_global->maestro_process->context;
221   xbt_os_sem_release(maestro->begin_);
222   this->start();
223 }
224
225 void ThreadContext::attach_stop()
226 {
227   if (smx_ctx_thread_sem)
228     xbt_os_sem_release(smx_ctx_thread_sem);
229   xbt_os_sem_release(this->end_);
230
231   ThreadContext* maestro = (ThreadContext*) simix_global->maestro_process->context;
232   xbt_os_sem_acquire(maestro->end_);
233
234   xbt_os_thread_set_extra_data(nullptr);
235 }
236
237 }
238 }