Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove unused typedefs.
[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/xbt_os_thread.h"
14
15 #include "src/kernel/context/ContextThread.hpp"
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
18
19 namespace simgrid {
20 namespace kernel {
21 namespace context {
22
23 // ThreadContextFactory
24
25 ThreadContextFactory::ThreadContextFactory()
26     : ContextFactory("ThreadContextFactory"), parallel_(SIMIX_context_is_parallel())
27 {
28   if (parallel_)
29     ParallelThreadContext::initialize();
30 }
31
32 ThreadContextFactory::~ThreadContextFactory()
33 {
34   if (parallel_)
35     ParallelThreadContext::finalize();
36 }
37
38 ThreadContext* ThreadContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup,
39                                                     smx_actor_t process, bool maestro)
40 {
41   if (parallel_)
42     return this->new_context<ParallelThreadContext>(std::move(code), cleanup, process, maestro);
43   else
44     return this->new_context<SerialThreadContext>(std::move(code), cleanup, process, maestro);
45 }
46
47 void ThreadContextFactory::run_all()
48 {
49   if (parallel_) {
50     // Parallel execution
51     ParallelThreadContext::run_all();
52   } else {
53     // Serial execution
54     SerialThreadContext::run_all();
55   }
56 }
57
58 // ThreadContext
59
60 ThreadContext::ThreadContext(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process,
61                              bool maestro)
62     : AttachContext(std::move(code), cleanup, process), 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     if (not context->isMaestro()) // really?
119       context->Context::stop();
120   } catch (StopRequest const&) {
121     XBT_DEBUG("Caught a StopRequest");
122     xbt_assert(not context->isMaestro(), "I'm not supposed to be maestro here.");
123   }
124
125   // Signal to the caller (normally the maestro) that we have finished:
126   context->yield();
127
128 #ifndef WIN32
129   stack.ss_flags = SS_DISABLE;
130   sigaltstack(&stack, nullptr);
131 #endif
132   return nullptr;
133 }
134
135 void ThreadContext::release()
136 {
137   xbt_os_sem_release(this->begin_);
138 }
139
140 void ThreadContext::wait()
141 {
142   xbt_os_sem_acquire(this->end_);
143 }
144
145 void ThreadContext::start()
146 {
147   xbt_os_sem_acquire(this->begin_);
148   this->start_hook();
149 }
150
151 void ThreadContext::yield()
152 {
153   this->yield_hook();
154   xbt_os_sem_release(this->end_);
155 }
156
157 void ThreadContext::stop()
158 {
159   Context::stop();
160   throw StopRequest();
161 }
162
163 void ThreadContext::suspend()
164 {
165   this->yield();
166   this->start();
167 }
168
169 void ThreadContext::attach_start()
170 {
171   // We're breaking the layers here by depending on the upper layer:
172   ThreadContext* maestro = (ThreadContext*) simix_global->maestro_process->context;
173   xbt_os_sem_release(maestro->begin_);
174   xbt_assert(not this->isMaestro());
175   this->start();
176 }
177
178 void ThreadContext::attach_stop()
179 {
180   xbt_assert(not this->isMaestro());
181   this->yield();
182
183   ThreadContext* maestro = (ThreadContext*) simix_global->maestro_process->context;
184   xbt_os_sem_acquire(maestro->end_);
185
186   xbt_os_thread_set_extra_data(nullptr);
187 }
188
189 // SerialThreadContext
190
191 void SerialThreadContext::run_all()
192 {
193   for (smx_actor_t const& process : simix_global->process_to_run) {
194     XBT_DEBUG("Handling %p", process);
195     ThreadContext* context = static_cast<ThreadContext*>(process->context);
196     context->release();
197     context->wait();
198   }
199 }
200
201 // ParallelThreadContext
202
203 xbt_os_sem_t ParallelThreadContext::thread_sem_ = nullptr;
204
205 void ParallelThreadContext::initialize()
206 {
207   thread_sem_ = xbt_os_sem_init(SIMIX_context_get_nthreads());
208 }
209
210 void ParallelThreadContext::finalize()
211 {
212   xbt_os_sem_destroy(thread_sem_);
213   thread_sem_ = nullptr;
214 }
215
216 void ParallelThreadContext::run_all()
217 {
218   for (smx_actor_t const& process : simix_global->process_to_run)
219     static_cast<ThreadContext*>(process->context)->release();
220   for (smx_actor_t const& process : simix_global->process_to_run)
221     static_cast<ThreadContext*>(process->context)->wait();
222 }
223
224 void ParallelThreadContext::start_hook()
225 {
226   if (not isMaestro()) /* parallel run */
227     xbt_os_sem_acquire(thread_sem_);
228 }
229
230 void ParallelThreadContext::yield_hook()
231 {
232   if (not isMaestro()) /* parallel run */
233     xbt_os_sem_release(thread_sem_);
234 }
235
236 XBT_PRIVATE ContextFactory* thread_factory()
237 {
238   XBT_VERB("Activating thread context factory");
239   return new ThreadContextFactory();
240 }
241 }}} // namespace