Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Replace redundant type with "auto" (include/ and src/).
[simgrid.git] / src / kernel / context / ContextThread.cpp
1 /* Copyright (c) 2009-2020. 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
14 #include <functional>
15 #include <utility>
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() : ContextFactory()
26 {
27   if (smx_context_stack_size != 8 * 1024 * 1024)
28     XBT_INFO("Stack size modifications are ignored by thread factory.");
29   if (SIMIX_context_is_parallel())
30     ParallelThreadContext::initialize();
31 }
32
33 ThreadContextFactory::~ThreadContextFactory()
34 {
35   if (SIMIX_context_is_parallel())
36     ParallelThreadContext::finalize();
37 }
38
39 ThreadContext* ThreadContextFactory::create_context(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
40 {
41   if (SIMIX_context_is_parallel())
42     return this->new_context<ParallelThreadContext>(std::move(code), actor, maestro);
43   else
44     return this->new_context<SerialThreadContext>(std::move(code), actor, maestro);
45 }
46
47 void ThreadContextFactory::run_all()
48 {
49   if (SIMIX_context_is_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, actor::ActorImpl* actor, bool maestro)
61     : AttachContext(std::move(code), actor), is_maestro_(maestro)
62 {
63   /* If the user provided a function for the actor then use it */
64   if (has_code()) {
65     /* create and start the actor */
66     this->thread_ = new std::thread(ThreadContext::wrapper, this);
67     /* wait the starting of the newly created actor */
68     this->end_.acquire();
69   }
70
71   /* Otherwise, we attach to the current thread */
72   else {
73     Context::set_current(this);
74   }
75 }
76
77 ThreadContext::~ThreadContext()
78 {
79   if (this->thread_) { /* Maestro don't have any thread */
80     thread_->join();
81     delete thread_;
82   }
83 }
84
85 void ThreadContext::wrapper(ThreadContext* context)
86 {
87   Context::set_current(context);
88
89 #ifndef WIN32
90   /* Install alternate signal stack, for SIGSEGV handler. */
91   stack_t stack;
92   stack.ss_sp = sigsegv_stack;
93   stack.ss_size = sizeof sigsegv_stack;
94   stack.ss_flags = 0;
95   sigaltstack(&stack, nullptr);
96 #endif
97   // Tell the caller (normally the maestro) we are starting, and wait for its green light
98   context->end_.release();
99   context->start();
100
101   try {
102     (*context)();
103     if (not context->is_maestro()) { // Just in case somebody detached maestro
104       context->Context::stop();
105       context->stop_hook();
106     }
107   } catch (ForcefulKillException const&) {
108     XBT_DEBUG("Caught a ForcefulKillException in Thread::wrapper");
109     xbt_assert(not context->is_maestro(), "Maestro shall not receive ForcefulKillExceptions, even when detached.");
110   } catch (simgrid::Exception const& e) {
111     XBT_INFO("Actor killed by an uncaught exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
112     throw;
113   }
114   // Signal to the caller (normally the maestro) that we have finished:
115   context->yield();
116
117 #ifndef WIN32
118   stack.ss_flags = SS_DISABLE;
119   sigaltstack(&stack, nullptr);
120 #endif
121   XBT_DEBUG("Terminating");
122   Context::set_current(nullptr);
123 }
124
125 void ThreadContext::release()
126 {
127   this->begin_.release();
128 }
129
130 void ThreadContext::wait()
131 {
132   this->end_.acquire();
133 }
134
135 void ThreadContext::start()
136 {
137   this->begin_.acquire();
138   this->start_hook();
139 }
140
141 void ThreadContext::yield()
142 {
143   this->yield_hook();
144   this->end_.release();
145 }
146
147 void ThreadContext::stop()
148 {
149   Context::stop();
150   stop_hook();
151   throw ForcefulKillException();
152 }
153
154 void ThreadContext::suspend()
155 {
156   this->yield();
157   this->start();
158 }
159
160 void ThreadContext::attach_start()
161 {
162   // We're breaking the layers here by depending on the upper layer:
163   auto* maestro = static_cast<ThreadContext*>(simix_global->maestro_->context_.get());
164   maestro->begin_.release();
165   xbt_assert(not this->is_maestro());
166   this->start();
167 }
168
169 void ThreadContext::attach_stop()
170 {
171   xbt_assert(not this->is_maestro());
172   this->yield();
173
174   auto* maestro = static_cast<ThreadContext*>(simix_global->maestro_->context_.get());
175   maestro->end_.acquire();
176
177   Context::set_current(nullptr);
178 }
179
180 // SerialThreadContext
181
182 void SerialThreadContext::run_all()
183 {
184   for (smx_actor_t const& actor : simix_global->actors_to_run) {
185     XBT_DEBUG("Handling %p", actor);
186     auto* context = static_cast<ThreadContext*>(actor->context_.get());
187     context->release();
188     context->wait();
189   }
190 }
191
192 // ParallelThreadContext
193
194 xbt::OsSemaphore* ParallelThreadContext::thread_sem_ = nullptr;
195
196 void ParallelThreadContext::initialize()
197 {
198   thread_sem_ = new xbt::OsSemaphore(SIMIX_context_get_nthreads());
199 }
200
201 void ParallelThreadContext::finalize()
202 {
203   delete thread_sem_;
204   thread_sem_ = nullptr;
205 }
206
207 void ParallelThreadContext::run_all()
208 {
209   for (smx_actor_t const& actor : simix_global->actors_to_run)
210     static_cast<ThreadContext*>(actor->context_.get())->release();
211   for (smx_actor_t const& actor : simix_global->actors_to_run)
212     static_cast<ThreadContext*>(actor->context_.get())->wait();
213 }
214
215 void ParallelThreadContext::start_hook()
216 {
217   if (not is_maestro()) /* parallel run */
218     thread_sem_->acquire();
219 }
220
221 void ParallelThreadContext::yield_hook()
222 {
223   if (not is_maestro()) /* parallel run */
224     thread_sem_->release();
225 }
226
227 XBT_PRIVATE ContextFactory* thread_factory()
228 {
229   XBT_VERB("Activating thread context factory");
230   return new ThreadContextFactory();
231 }
232 } // namespace context
233 } // namespace kernel
234 } // namespace simgrid