Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Simplify parameter passing between EngineImpl and ContextFactory
[simgrid.git] / src / kernel / context / ContextThread.cpp
1 /* Copyright (c) 2009-2022. 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(std::vector<actor::ActorImpl*> const& actors_list)
50 {
51   if (is_parallel())
52     ParallelThreadContext::run_all(actors_list);
53
54   else
55     SerialThreadContext::run_all(actors_list);
56 }
57
58 // ThreadContext
59
60 ThreadContext::ThreadContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
61     : AttachContext(std::move(code), actor, 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 start 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->stop();
100   } catch (ForcefulKillException const&) {
101     XBT_DEBUG("Caught a ForcefulKillException in Thread::wrapper");
102     xbt_assert(not context->is_maestro(), "Maestro shall not receive ForcefulKillExceptions, even when detached.");
103   } catch (simgrid::Exception const& e) {
104     XBT_INFO("Actor killed by an uncaught exception %s", boost::core::demangle(typeid(e).name()).c_str());
105     throw;
106   }
107   // Signal to the caller (normally the maestro) that we have finished:
108   context->yield();
109
110 #ifndef WIN32
111   install_sigsegv_stack(nullptr, false);
112 #endif
113   XBT_DEBUG("Terminating");
114   Context::set_current(nullptr);
115 }
116
117 void ThreadContext::release()
118 {
119   this->begin_.release();
120 }
121
122 void ThreadContext::wait()
123 {
124   this->end_.acquire();
125 }
126
127 void ThreadContext::start()
128 {
129   this->begin_.acquire();
130   this->start_hook();
131 }
132
133 void ThreadContext::yield()
134 {
135   this->yield_hook();
136   this->end_.release();
137 }
138
139 void ThreadContext::suspend()
140 {
141   this->yield();
142   this->start();
143 }
144
145 void ThreadContext::attach_start()
146 {
147   // We're breaking the layers here by depending on the upper layer:
148   auto* maestro = static_cast<ThreadContext*>(EngineImpl::get_instance()->get_maestro()->context_.get());
149   maestro->begin_.release();
150   xbt_assert(not this->is_maestro());
151   this->start();
152 }
153
154 void ThreadContext::attach_stop()
155 {
156   xbt_assert(not this->is_maestro());
157   this->yield();
158
159   auto* maestro = static_cast<ThreadContext*>(EngineImpl::get_instance()->get_maestro()->context_.get());
160   maestro->end_.acquire();
161
162   Context::set_current(nullptr);
163 }
164
165 // SerialThreadContext
166
167 void SerialThreadContext::run_all(std::vector<actor::ActorImpl*> const& actors_list)
168 {
169   for (smx_actor_t const& actor : actors_list) {
170     XBT_DEBUG("Handling %p", actor);
171     auto* context = static_cast<ThreadContext*>(actor->context_.get());
172     context->release();
173     context->wait();
174   }
175 }
176
177 // ParallelThreadContext
178
179 xbt::OsSemaphore* ParallelThreadContext::thread_sem_ = nullptr;
180
181 void ParallelThreadContext::initialize()
182 {
183   thread_sem_ = new xbt::OsSemaphore(get_nthreads());
184 }
185
186 void ParallelThreadContext::finalize()
187 {
188   delete thread_sem_;
189   thread_sem_ = nullptr;
190 }
191
192 void ParallelThreadContext::run_all(std::vector<actor::ActorImpl*> const& actors_list)
193 {
194   for (smx_actor_t const& actor : actors_list)
195     static_cast<ThreadContext*>(actor->context_.get())->release();
196
197   for (smx_actor_t const& actor : actors_list)
198     static_cast<ThreadContext*>(actor->context_.get())->wait();
199 }
200
201 void ParallelThreadContext::start_hook()
202 {
203   if (not is_maestro()) /* parallel run */
204     thread_sem_->acquire();
205 }
206
207 void ParallelThreadContext::yield_hook()
208 {
209   if (not is_maestro()) /* parallel run */
210     thread_sem_->release();
211 }
212
213 XBT_PRIVATE ContextFactory* thread_factory()
214 {
215   XBT_VERB("Activating thread context factory");
216   return new ThreadContextFactory();
217 }
218 } // namespace context
219 } // namespace kernel
220 } // namespace simgrid