1 /* Copyright (c) 2009-2022. 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);
25 // ThreadContextFactory
27 ThreadContextFactory::ThreadContextFactory() : ContextFactory()
29 if (stack_size != 8 * 1024 * 1024)
30 XBT_INFO("Stack size modifications are ignored by thread factory.");
32 ParallelThreadContext::initialize();
35 ThreadContextFactory::~ThreadContextFactory()
38 ParallelThreadContext::finalize();
41 ThreadContext* ThreadContextFactory::create_context(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
44 return this->new_context<ParallelThreadContext>(std::move(code), actor, maestro);
46 return this->new_context<SerialThreadContext>(std::move(code), actor, maestro);
49 void ThreadContextFactory::run_all()
53 ParallelThreadContext::run_all();
56 SerialThreadContext::run_all();
62 ThreadContext::ThreadContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
63 : AttachContext(std::move(code), actor, maestro)
65 /* If the user provided a function for the actor then use it */
67 /* create and start the actor */
68 this->thread_ = new std::thread(ThreadContext::wrapper, this);
69 /* wait the start of the newly created actor */
73 /* Otherwise, we attach to the current thread */
75 Context::set_current(this);
79 ThreadContext::~ThreadContext()
81 if (this->thread_) { /* Maestro don't have any thread */
87 void ThreadContext::wrapper(ThreadContext* context)
89 Context::set_current(context);
92 install_sigsegv_stack(nullptr, true);
94 // Tell the caller (normally the maestro) we are starting, and wait for its green light
95 context->end_.release();
100 if (not context->is_maestro()) // Just in case somebody detached maestro
102 } catch (ForcefulKillException const&) {
103 XBT_DEBUG("Caught a ForcefulKillException in Thread::wrapper");
104 xbt_assert(not context->is_maestro(), "Maestro shall not receive ForcefulKillExceptions, even when detached.");
105 } catch (simgrid::Exception const& e) {
106 XBT_INFO("Actor killed by an uncaught exception %s", boost::core::demangle(typeid(e).name()).c_str());
109 // Signal to the caller (normally the maestro) that we have finished:
113 install_sigsegv_stack(nullptr, false);
115 XBT_DEBUG("Terminating");
116 Context::set_current(nullptr);
119 void ThreadContext::release()
121 this->begin_.release();
124 void ThreadContext::wait()
126 this->end_.acquire();
129 void ThreadContext::start()
131 this->begin_.acquire();
135 void ThreadContext::yield()
138 this->end_.release();
141 void ThreadContext::suspend()
147 void ThreadContext::attach_start()
149 // We're breaking the layers here by depending on the upper layer:
150 auto* maestro = static_cast<ThreadContext*>(EngineImpl::get_instance()->get_maestro()->context_.get());
151 maestro->begin_.release();
152 xbt_assert(not this->is_maestro());
156 void ThreadContext::attach_stop()
158 xbt_assert(not this->is_maestro());
161 auto* maestro = static_cast<ThreadContext*>(EngineImpl::get_instance()->get_maestro()->context_.get());
162 maestro->end_.acquire();
164 Context::set_current(nullptr);
167 // SerialThreadContext
169 void SerialThreadContext::run_all()
171 const auto& to_run = EngineImpl::get_instance()->get_actors_to_run();
172 for (smx_actor_t const& actor : to_run) {
173 XBT_DEBUG("Handling %p", actor);
174 auto* context = static_cast<ThreadContext*>(actor->context_.get());
180 // ParallelThreadContext
182 xbt::OsSemaphore* ParallelThreadContext::thread_sem_ = nullptr;
184 void ParallelThreadContext::initialize()
186 thread_sem_ = new xbt::OsSemaphore(get_nthreads());
189 void ParallelThreadContext::finalize()
192 thread_sem_ = nullptr;
195 void ParallelThreadContext::run_all()
197 const auto& to_release = EngineImpl::get_instance()->get_actors_to_run();
198 for (smx_actor_t const& actor : to_release)
199 static_cast<ThreadContext*>(actor->context_.get())->release();
200 const auto& to_wait = EngineImpl::get_instance()->get_actors_to_run();
201 for (smx_actor_t const& actor : to_wait)
202 static_cast<ThreadContext*>(actor->context_.get())->wait();
205 void ParallelThreadContext::start_hook()
207 if (not is_maestro()) /* parallel run */
208 thread_sem_->acquire();
211 void ParallelThreadContext::yield_hook()
213 if (not is_maestro()) /* parallel run */
214 thread_sem_->release();
217 XBT_PRIVATE ContextFactory* thread_factory()
219 XBT_VERB("Activating thread context factory");
220 return new ThreadContextFactory();
222 } // namespace context
223 } // namespace kernel
224 } // namespace simgrid