Logo AND Algorithmique Numérique Distribuée

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