Logo AND Algorithmique Numérique Distribuée

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