1 /* Copyright (c) 2009-2021. 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 "src/simix/smx_private.hpp"
12 #include "xbt/function_types.h"
13 #include "xbt/xbt_modinter.h" /* prototype of os thread module's init/exit in XBT */
15 #include <boost/core/demangle.hpp>
20 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
26 // ThreadContextFactory
28 ThreadContextFactory::ThreadContextFactory() : ContextFactory()
30 if (smx_context_stack_size != 8 * 1024 * 1024)
31 XBT_INFO("Stack size modifications are ignored by thread factory.");
32 if (SIMIX_context_is_parallel())
33 ParallelThreadContext::initialize();
36 ThreadContextFactory::~ThreadContextFactory()
38 if (SIMIX_context_is_parallel())
39 ParallelThreadContext::finalize();
42 ThreadContext* ThreadContextFactory::create_context(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
44 if (SIMIX_context_is_parallel())
45 return this->new_context<ParallelThreadContext>(std::move(code), actor, maestro);
47 return this->new_context<SerialThreadContext>(std::move(code), actor, maestro);
50 void ThreadContextFactory::run_all()
52 if (SIMIX_context_is_parallel()) {
54 ParallelThreadContext::run_all();
57 SerialThreadContext::run_all();
63 ThreadContext::ThreadContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
64 : AttachContext(std::move(code), actor), is_maestro_(maestro)
66 /* If the user provided a function for the actor then use it */
68 /* create and start the actor */
69 this->thread_ = new std::thread(ThreadContext::wrapper, this);
70 /* wait the starting of the newly created actor */
74 /* Otherwise, we attach to the current thread */
76 Context::set_current(this);
80 ThreadContext::~ThreadContext()
82 if (this->thread_) { /* Maestro don't have any thread */
88 void ThreadContext::wrapper(ThreadContext* context)
90 Context::set_current(context);
93 install_sigsegv_stack(nullptr, true);
95 // Tell the caller (normally the maestro) we are starting, and wait for its green light
96 context->end_.release();
101 if (not context->is_maestro()) { // Just in case somebody detached maestro
102 context->Context::stop();
103 context->stop_hook();
105 } catch (ForcefulKillException const&) {
106 XBT_DEBUG("Caught a ForcefulKillException in Thread::wrapper");
107 xbt_assert(not context->is_maestro(), "Maestro shall not receive ForcefulKillExceptions, even when detached.");
108 } catch (simgrid::Exception const& e) {
109 XBT_INFO("Actor killed by an uncaught exception %s", boost::core::demangle(typeid(e).name()).c_str());
112 // Signal to the caller (normally the maestro) that we have finished:
116 install_sigsegv_stack(nullptr, false);
118 XBT_DEBUG("Terminating");
119 Context::set_current(nullptr);
122 void ThreadContext::release()
124 this->begin_.release();
127 void ThreadContext::wait()
129 this->end_.acquire();
132 void ThreadContext::start()
134 this->begin_.acquire();
138 void ThreadContext::yield()
141 this->end_.release();
144 void ThreadContext::stop()
148 throw ForcefulKillException();
151 void ThreadContext::suspend()
157 void ThreadContext::attach_start()
159 // We're breaking the layers here by depending on the upper layer:
160 auto* maestro = static_cast<ThreadContext*>(simix_global->maestro_->context_.get());
161 maestro->begin_.release();
162 xbt_assert(not this->is_maestro());
166 void ThreadContext::attach_stop()
168 xbt_assert(not this->is_maestro());
171 auto* maestro = static_cast<ThreadContext*>(simix_global->maestro_->context_.get());
172 maestro->end_.acquire();
174 Context::set_current(nullptr);
177 // SerialThreadContext
179 void SerialThreadContext::run_all()
181 const auto& to_run = EngineImpl::get_instance()->get_actors_to_run();
182 for (smx_actor_t const& actor : to_run) {
183 XBT_DEBUG("Handling %p", actor);
184 auto* context = static_cast<ThreadContext*>(actor->context_.get());
190 // ParallelThreadContext
192 xbt::OsSemaphore* ParallelThreadContext::thread_sem_ = nullptr;
194 void ParallelThreadContext::initialize()
196 thread_sem_ = new xbt::OsSemaphore(SIMIX_context_get_nthreads());
199 void ParallelThreadContext::finalize()
202 thread_sem_ = nullptr;
205 void ParallelThreadContext::run_all()
207 const auto& to_release = EngineImpl::get_instance()->get_actors_to_run();
208 for (smx_actor_t const& actor : to_release)
209 static_cast<ThreadContext*>(actor->context_.get())->release();
210 const auto& to_wait = EngineImpl::get_instance()->get_actors_to_run();
211 for (smx_actor_t const& actor : to_wait)
212 static_cast<ThreadContext*>(actor->context_.get())->wait();
215 void ParallelThreadContext::start_hook()
217 if (not is_maestro()) /* parallel run */
218 thread_sem_->acquire();
221 void ParallelThreadContext::yield_hook()
223 if (not is_maestro()) /* parallel run */
224 thread_sem_->release();
227 XBT_PRIVATE ContextFactory* thread_factory()
229 XBT_VERB("Activating thread context factory");
230 return new ThreadContextFactory();
232 } // namespace context
233 } // namespace kernel
234 } // namespace simgrid