Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b8bc3d1c82a76930ee06cc4af523a47eb76336a1
[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/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_sigsegv_stack(nullptr, true);
91 #endif
92   // Tell the caller (normally the maestro) we are starting, and wait for its green light
93   context->end_.release();
94   context->start();
95
96   try {
97     (*context)();
98     if (not context->is_maestro()) { // Just in case somebody detached maestro
99       context->Context::stop();
100       context->stop_hook();
101     }
102   } catch (ForcefulKillException const&) {
103     XBT_DEBUG("Caught a ForcefulKillException in Thread::wrapper");
104     xbt_assert(not context->is_maestro(), "Maestro shall not receive ForcefulKillExceptions, even when detached.");
105   } catch (simgrid::Exception const& e) {
106     XBT_INFO("Actor killed by an uncaught exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
107     throw;
108   }
109   // Signal to the caller (normally the maestro) that we have finished:
110   context->yield();
111
112 #ifndef WIN32
113   install_sigsegv_stack(nullptr, false);
114 #endif
115   XBT_DEBUG("Terminating");
116   Context::set_current(nullptr);
117 }
118
119 void ThreadContext::release()
120 {
121   this->begin_.release();
122 }
123
124 void ThreadContext::wait()
125 {
126   this->end_.acquire();
127 }
128
129 void ThreadContext::start()
130 {
131   this->begin_.acquire();
132   this->start_hook();
133 }
134
135 void ThreadContext::yield()
136 {
137   this->yield_hook();
138   this->end_.release();
139 }
140
141 void ThreadContext::stop()
142 {
143   Context::stop();
144   stop_hook();
145   throw ForcefulKillException();
146 }
147
148 void ThreadContext::suspend()
149 {
150   this->yield();
151   this->start();
152 }
153
154 void ThreadContext::attach_start()
155 {
156   // We're breaking the layers here by depending on the upper layer:
157   auto* maestro = static_cast<ThreadContext*>(simix_global->maestro_->context_.get());
158   maestro->begin_.release();
159   xbt_assert(not this->is_maestro());
160   this->start();
161 }
162
163 void ThreadContext::attach_stop()
164 {
165   xbt_assert(not this->is_maestro());
166   this->yield();
167
168   auto* maestro = static_cast<ThreadContext*>(simix_global->maestro_->context_.get());
169   maestro->end_.acquire();
170
171   Context::set_current(nullptr);
172 }
173
174 // SerialThreadContext
175
176 void SerialThreadContext::run_all()
177 {
178   for (smx_actor_t const& actor : simix_global->actors_to_run) {
179     XBT_DEBUG("Handling %p", actor);
180     auto* context = static_cast<ThreadContext*>(actor->context_.get());
181     context->release();
182     context->wait();
183   }
184 }
185
186 // ParallelThreadContext
187
188 xbt::OsSemaphore* ParallelThreadContext::thread_sem_ = nullptr;
189
190 void ParallelThreadContext::initialize()
191 {
192   thread_sem_ = new xbt::OsSemaphore(SIMIX_context_get_nthreads());
193 }
194
195 void ParallelThreadContext::finalize()
196 {
197   delete thread_sem_;
198   thread_sem_ = nullptr;
199 }
200
201 void ParallelThreadContext::run_all()
202 {
203   for (smx_actor_t const& actor : simix_global->actors_to_run)
204     static_cast<ThreadContext*>(actor->context_.get())->release();
205   for (smx_actor_t const& actor : simix_global->actors_to_run)
206     static_cast<ThreadContext*>(actor->context_.get())->wait();
207 }
208
209 void ParallelThreadContext::start_hook()
210 {
211   if (not is_maestro()) /* parallel run */
212     thread_sem_->acquire();
213 }
214
215 void ParallelThreadContext::yield_hook()
216 {
217   if (not is_maestro()) /* parallel run */
218     thread_sem_->release();
219 }
220
221 XBT_PRIVATE ContextFactory* thread_factory()
222 {
223   XBT_VERB("Activating thread context factory");
224   return new ThreadContextFactory();
225 }
226 } // namespace context
227 } // namespace kernel
228 } // namespace simgrid