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 (stack_size != 8 * 1024 * 1024)
28 XBT_INFO("Stack size modifications are ignored by thread factory.");
30 ParallelThreadContext::initialize();
33 ThreadContextFactory::~ThreadContextFactory()
36 ParallelThreadContext::finalize();
39 ThreadContext* ThreadContextFactory::create_context(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
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)
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);
88 install_sigsegv_stack(nullptr, true);
90 // Tell the caller (normally the maestro) we are starting, and wait for its green light
91 context->end_.release();
96 if (not context->is_maestro()) // Just in case somebody detached maestro
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());
105 // Signal to the caller (normally the maestro) that we have finished:
109 install_sigsegv_stack(nullptr, false);
111 XBT_DEBUG("Terminating");
112 Context::set_current(nullptr);
115 void ThreadContext::release()
117 this->begin_.release();
120 void ThreadContext::wait()
122 this->end_.acquire();
125 void ThreadContext::start()
127 this->begin_.acquire();
131 void ThreadContext::yield()
134 this->end_.release();
137 void ThreadContext::suspend()
143 void ThreadContext::attach_start()
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());
152 void ThreadContext::attach_stop()
154 xbt_assert(not this->is_maestro());
157 auto* maestro = static_cast<ThreadContext*>(EngineImpl::get_instance()->get_maestro()->context_.get());
158 maestro->end_.acquire();
160 Context::set_current(nullptr);
163 // SerialThreadContext
165 void SerialThreadContext::run_all(std::vector<actor::ActorImpl*> const& actors_list)
167 for (auto const* actor : actors_list) {
168 XBT_DEBUG("Handling %p", actor);
169 auto* context = static_cast<ThreadContext*>(actor->context_.get());
175 // ParallelThreadContext
177 xbt::OsSemaphore* ParallelThreadContext::thread_sem_ = nullptr;
179 void ParallelThreadContext::initialize()
181 thread_sem_ = new xbt::OsSemaphore(get_nthreads());
184 void ParallelThreadContext::finalize()
187 thread_sem_ = nullptr;
190 void ParallelThreadContext::run_all(std::vector<actor::ActorImpl*> const& actors_list)
192 for (auto const* actor : actors_list)
193 static_cast<ThreadContext*>(actor->context_.get())->release();
195 for (auto const* actor : actors_list)
196 static_cast<ThreadContext*>(actor->context_.get())->wait();
199 void ParallelThreadContext::start_hook()
201 if (not is_maestro()) /* parallel run */
202 thread_sem_->acquire();
205 void ParallelThreadContext::yield_hook()
207 if (not is_maestro()) /* parallel run */
208 thread_sem_->release();
211 XBT_PRIVATE ContextFactory* thread_factory()
213 XBT_VERB("Activating thread context factory");
214 return new ThreadContextFactory();
216 } // namespace simgrid::kernel::context