Logo AND Algorithmique Numérique Distribuée

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