Logo AND Algorithmique Numérique Distribuée

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