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"
13 #include <boost/core/demangle.hpp>
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ker_context);
20 namespace simgrid::kernel::context {
22 // ThreadContextFactory
24 ThreadContextFactory::ThreadContextFactory() : ContextFactory()
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();
32 ThreadContextFactory::~ThreadContextFactory()
34 if (Context::is_parallel())
35 ParallelThreadContext::finalize();
38 ThreadContext* ThreadContextFactory::create_context(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
40 if (Context::is_parallel())
41 return this->new_context<ParallelThreadContext>(std::move(code), actor, maestro);
43 return this->new_context<SerialThreadContext>(std::move(code), actor, maestro);
46 void ThreadContextFactory::run_all(std::vector<actor::ActorImpl*> const& actors_list)
48 if (Context::is_parallel())
49 ParallelThreadContext::run_all(actors_list);
52 SerialThreadContext::run_all(actors_list);
57 ThreadContext::ThreadContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
58 : AttachContext(std::move(code), actor, maestro)
60 /* If the user provided a function for the actor then use it */
62 /* create and start the actor */
63 this->thread_ = new std::thread(ThreadContext::wrapper, this);
64 /* wait the start of the newly created actor */
68 /* Otherwise, we attach to the current thread */
70 Context::set_current(this);
74 ThreadContext::~ThreadContext()
76 if (this->thread_) { /* Maestro don't have any thread */
82 void ThreadContext::wrapper(ThreadContext* context)
84 Context::set_current(context);
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();
93 if (not context->is_maestro()) // Just in case somebody detached maestro
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());
102 // Signal to the caller (normally the maestro) that we have finished:
105 install_sigsegv_stack(false);
106 XBT_DEBUG("Terminating");
107 Context::set_current(nullptr);
110 void ThreadContext::release()
112 this->begin_.release();
115 void ThreadContext::wait()
117 this->end_.acquire();
120 void ThreadContext::start()
122 this->begin_.acquire();
126 void ThreadContext::yield()
129 this->end_.release();
132 void ThreadContext::suspend()
138 void ThreadContext::attach_start()
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());
147 void ThreadContext::attach_stop()
149 xbt_assert(not this->is_maestro());
152 auto* maestro = static_cast<ThreadContext*>(EngineImpl::get_instance()->get_maestro()->context_.get());
153 maestro->end_.acquire();
155 Context::set_current(nullptr);
158 // SerialThreadContext
160 void SerialThreadContext::run_all(std::vector<actor::ActorImpl*> const& actors_list)
162 for (auto const* actor : actors_list) {
163 XBT_DEBUG("Handling %p", actor);
164 auto* context = static_cast<ThreadContext*>(actor->context_.get());
170 // ParallelThreadContext
172 xbt::OsSemaphore* ParallelThreadContext::thread_sem_ = nullptr;
174 void ParallelThreadContext::initialize()
176 thread_sem_ = new xbt::OsSemaphore(get_nthreads());
179 void ParallelThreadContext::finalize()
182 thread_sem_ = nullptr;
185 void ParallelThreadContext::run_all(std::vector<actor::ActorImpl*> const& actors_list)
187 for (auto const* actor : actors_list)
188 static_cast<ThreadContext*>(actor->context_.get())->release();
190 for (auto const* actor : actors_list)
191 static_cast<ThreadContext*>(actor->context_.get())->wait();
194 void ParallelThreadContext::start_hook()
196 if (not is_maestro()) /* parallel run */
197 thread_sem_->acquire();
200 void ParallelThreadContext::yield_hook()
202 if (not is_maestro()) /* parallel run */
203 thread_sem_->release();
206 XBT_PRIVATE ContextFactory* thread_factory()
208 XBT_VERB("Activating thread context factory");
209 return new ThreadContextFactory();
211 } // namespace simgrid::kernel::context