Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move maestro to EngineImpl. breaks a unit-test
[simgrid.git] / src / kernel / context / ContextThread.cpp
1 /* Copyright (c) 2009-2021. 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/kernel/EngineImpl.hpp"
11 #include "src/simix/smx_private.hpp"
12 #include "xbt/function_types.h"
13 #include "xbt/xbt_modinter.h" /* prototype of os thread module's init/exit in XBT */
14
15 #include <boost/core/demangle.hpp>
16 #include <functional>
17 #include <typeinfo>
18 #include <utility>
19
20 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
21
22 namespace simgrid {
23 namespace kernel {
24 namespace context {
25
26 // ThreadContextFactory
27
28 ThreadContextFactory::ThreadContextFactory() : ContextFactory()
29 {
30   if (smx_context_stack_size != 8 * 1024 * 1024)
31     XBT_INFO("Stack size modifications are ignored by thread factory.");
32   if (SIMIX_context_is_parallel())
33     ParallelThreadContext::initialize();
34 }
35
36 ThreadContextFactory::~ThreadContextFactory()
37 {
38   if (SIMIX_context_is_parallel())
39     ParallelThreadContext::finalize();
40 }
41
42 ThreadContext* ThreadContextFactory::create_context(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
43 {
44   if (SIMIX_context_is_parallel())
45     return this->new_context<ParallelThreadContext>(std::move(code), actor, maestro);
46   else
47     return this->new_context<SerialThreadContext>(std::move(code), actor, maestro);
48 }
49
50 void ThreadContextFactory::run_all()
51 {
52   if (SIMIX_context_is_parallel()) {
53     // Parallel execution
54     ParallelThreadContext::run_all();
55   } else {
56     // Serial execution
57     SerialThreadContext::run_all();
58   }
59 }
60
61 // ThreadContext
62
63 ThreadContext::ThreadContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
64     : AttachContext(std::move(code), actor), is_maestro_(maestro)
65 {
66   /* If the user provided a function for the actor then use it */
67   if (has_code()) {
68     /* create and start the actor */
69     this->thread_ = new std::thread(ThreadContext::wrapper, this);
70     /* wait the starting of the newly created actor */
71     this->end_.acquire();
72   }
73
74   /* Otherwise, we attach to the current thread */
75   else {
76     Context::set_current(this);
77   }
78 }
79
80 ThreadContext::~ThreadContext()
81 {
82   if (this->thread_) { /* Maestro don't have any thread */
83     thread_->join();
84     delete thread_;
85   }
86 }
87
88 void ThreadContext::wrapper(ThreadContext* context)
89 {
90   Context::set_current(context);
91
92 #ifndef WIN32
93   install_sigsegv_stack(nullptr, true);
94 #endif
95   // Tell the caller (normally the maestro) we are starting, and wait for its green light
96   context->end_.release();
97   context->start();
98
99   try {
100     (*context)();
101     if (not context->is_maestro()) { // Just in case somebody detached maestro
102       context->Context::stop();
103       context->stop_hook();
104     }
105   } catch (ForcefulKillException const&) {
106     XBT_DEBUG("Caught a ForcefulKillException in Thread::wrapper");
107     xbt_assert(not context->is_maestro(), "Maestro shall not receive ForcefulKillExceptions, even when detached.");
108   } catch (simgrid::Exception const& e) {
109     XBT_INFO("Actor killed by an uncaught exception %s", boost::core::demangle(typeid(e).name()).c_str());
110     throw;
111   }
112   // Signal to the caller (normally the maestro) that we have finished:
113   context->yield();
114
115 #ifndef WIN32
116   install_sigsegv_stack(nullptr, false);
117 #endif
118   XBT_DEBUG("Terminating");
119   Context::set_current(nullptr);
120 }
121
122 void ThreadContext::release()
123 {
124   this->begin_.release();
125 }
126
127 void ThreadContext::wait()
128 {
129   this->end_.acquire();
130 }
131
132 void ThreadContext::start()
133 {
134   this->begin_.acquire();
135   this->start_hook();
136 }
137
138 void ThreadContext::yield()
139 {
140   this->yield_hook();
141   this->end_.release();
142 }
143
144 void ThreadContext::stop()
145 {
146   Context::stop();
147   stop_hook();
148   throw ForcefulKillException();
149 }
150
151 void ThreadContext::suspend()
152 {
153   this->yield();
154   this->start();
155 }
156
157 void ThreadContext::attach_start()
158 {
159   // We're breaking the layers here by depending on the upper layer:
160   auto* maestro = static_cast<ThreadContext*>(EngineImpl::get_instance()->get_maestro()->context_.get());
161   maestro->begin_.release();
162   xbt_assert(not this->is_maestro());
163   this->start();
164 }
165
166 void ThreadContext::attach_stop()
167 {
168   xbt_assert(not this->is_maestro());
169   this->yield();
170
171   auto* maestro = static_cast<ThreadContext*>(EngineImpl::get_instance()->get_maestro()->context_.get());
172   maestro->end_.acquire();
173
174   Context::set_current(nullptr);
175 }
176
177 // SerialThreadContext
178
179 void SerialThreadContext::run_all()
180 {
181   const auto& to_run = EngineImpl::get_instance()->get_actors_to_run();
182   for (smx_actor_t const& actor : to_run) {
183     XBT_DEBUG("Handling %p", actor);
184     auto* context = static_cast<ThreadContext*>(actor->context_.get());
185     context->release();
186     context->wait();
187   }
188 }
189
190 // ParallelThreadContext
191
192 xbt::OsSemaphore* ParallelThreadContext::thread_sem_ = nullptr;
193
194 void ParallelThreadContext::initialize()
195 {
196   thread_sem_ = new xbt::OsSemaphore(SIMIX_context_get_nthreads());
197 }
198
199 void ParallelThreadContext::finalize()
200 {
201   delete thread_sem_;
202   thread_sem_ = nullptr;
203 }
204
205 void ParallelThreadContext::run_all()
206 {
207   const auto& to_release = EngineImpl::get_instance()->get_actors_to_run();
208   for (smx_actor_t const& actor : to_release)
209     static_cast<ThreadContext*>(actor->context_.get())->release();
210   const auto& to_wait = EngineImpl::get_instance()->get_actors_to_run();
211   for (smx_actor_t const& actor : to_wait)
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