1 /* Copyright (c) 2009-2023. The SimGrid Team. All rights reserved. */
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. */
6 #include "src/kernel/context/ContextThread.hpp"
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 */
14 #include <boost/core/demangle.hpp>
19 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ker_context);
21 namespace simgrid::kernel::context {
23 // ThreadContextFactory
25 ThreadContextFactory::ThreadContextFactory() : ContextFactory()
27 if (Context::stack_size != 8 * 1024 * 1024)
28 XBT_INFO("Stack size modifications are ignored by thread factory.");
29 if (Context::is_parallel())
30 ParallelThreadContext::initialize();
33 ThreadContextFactory::~ThreadContextFactory()
35 if (Context::is_parallel())
36 ParallelThreadContext::finalize();
39 ThreadContext* ThreadContextFactory::create_context(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
41 if (Context::is_parallel())
42 return this->new_context<ParallelThreadContext>(std::move(code), actor, maestro);
44 return this->new_context<SerialThreadContext>(std::move(code), actor, maestro);
47 void ThreadContextFactory::run_all(std::vector<actor::ActorImpl*> const& actors_list)
49 if (Context::is_parallel())
50 ParallelThreadContext::run_all(actors_list);
53 SerialThreadContext::run_all(actors_list);
58 ThreadContext::ThreadContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
59 : AttachContext(std::move(code), actor, maestro)
61 /* If the user provided a function for the actor then use it */
63 /* create and start the actor */
64 this->thread_ = new std::thread(ThreadContext::wrapper, this);
65 /* wait the start of the newly created actor */
69 /* Otherwise, we attach to the current thread */
71 Context::set_current(this);
75 ThreadContext::~ThreadContext()
77 if (this->thread_) { /* Maestro don't have any thread */
83 void ThreadContext::wrapper(ThreadContext* context)
85 Context::set_current(context);
87 install_sigsegv_stack(true);
88 // Tell the caller (normally the maestro) we are starting, and wait for its green light
89 context->end_.release();
94 if (not context->is_maestro()) // Just in case somebody detached maestro
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());
103 // Signal to the caller (normally the maestro) that we have finished:
106 install_sigsegv_stack(false);
107 XBT_DEBUG("Terminating");
108 Context::set_current(nullptr);
111 void ThreadContext::release()
113 this->begin_.release();
116 void ThreadContext::wait()
118 this->end_.acquire();
121 void ThreadContext::start()
123 this->begin_.acquire();
127 void ThreadContext::yield()
130 this->end_.release();
133 void ThreadContext::suspend()
139 void ThreadContext::attach_start()
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());
148 void ThreadContext::attach_stop()
150 xbt_assert(not this->is_maestro());
153 auto* maestro = static_cast<ThreadContext*>(EngineImpl::get_instance()->get_maestro()->context_.get());
154 maestro->end_.acquire();
156 Context::set_current(nullptr);
159 // SerialThreadContext
161 void SerialThreadContext::run_all(std::vector<actor::ActorImpl*> const& actors_list)
163 for (auto const* actor : actors_list) {
164 XBT_DEBUG("Handling %p", actor);
165 auto* context = static_cast<ThreadContext*>(actor->context_.get());
171 // ParallelThreadContext
173 xbt::OsSemaphore* ParallelThreadContext::thread_sem_ = nullptr;
175 void ParallelThreadContext::initialize()
177 thread_sem_ = new xbt::OsSemaphore(get_nthreads());
180 void ParallelThreadContext::finalize()
183 thread_sem_ = nullptr;
186 void ParallelThreadContext::run_all(std::vector<actor::ActorImpl*> const& actors_list)
188 for (auto const* actor : actors_list)
189 static_cast<ThreadContext*>(actor->context_.get())->release();
191 for (auto const* actor : actors_list)
192 static_cast<ThreadContext*>(actor->context_.get())->wait();
195 void ParallelThreadContext::start_hook()
197 if (not is_maestro()) /* parallel run */
198 thread_sem_->acquire();
201 void ParallelThreadContext::yield_hook()
203 if (not is_maestro()) /* parallel run */
204 thread_sem_->release();
207 XBT_PRIVATE ContextFactory* thread_factory()
209 XBT_VERB("Activating thread context factory");
210 return new ThreadContextFactory();
212 } // namespace simgrid::kernel::context