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/simix/smx_private.hpp"
11 #include "src/xbt_modinter.h" /* prototype of os thread module's init/exit in XBT */
12 #include "xbt/function_types.h"
14 #include <boost/core/demangle.hpp>
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
24 // ThreadContextFactory
26 ThreadContextFactory::ThreadContextFactory() : ContextFactory()
28 if (smx_context_stack_size != 8 * 1024 * 1024)
29 XBT_INFO("Stack size modifications are ignored by thread factory.");
30 if (SIMIX_context_is_parallel())
31 ParallelThreadContext::initialize();
34 ThreadContextFactory::~ThreadContextFactory()
36 if (SIMIX_context_is_parallel())
37 ParallelThreadContext::finalize();
40 ThreadContext* ThreadContextFactory::create_context(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
42 if (SIMIX_context_is_parallel())
43 return this->new_context<ParallelThreadContext>(std::move(code), actor, maestro);
45 return this->new_context<SerialThreadContext>(std::move(code), actor, maestro);
48 void ThreadContextFactory::run_all()
50 if (SIMIX_context_is_parallel()) {
52 ParallelThreadContext::run_all();
55 SerialThreadContext::run_all();
61 ThreadContext::ThreadContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
62 : AttachContext(std::move(code), actor), is_maestro_(maestro)
64 /* If the user provided a function for the actor then use it */
66 /* create and start the actor */
67 this->thread_ = new std::thread(ThreadContext::wrapper, this);
68 /* wait the starting of the newly created actor */
72 /* Otherwise, we attach to the current thread */
74 Context::set_current(this);
78 ThreadContext::~ThreadContext()
80 if (this->thread_) { /* Maestro don't have any thread */
86 void ThreadContext::wrapper(ThreadContext* context)
88 Context::set_current(context);
91 install_sigsegv_stack(nullptr, true);
93 // Tell the caller (normally the maestro) we are starting, and wait for its green light
94 context->end_.release();
99 if (not context->is_maestro()) { // Just in case somebody detached maestro
100 context->Context::stop();
101 context->stop_hook();
103 } catch (ForcefulKillException const&) {
104 XBT_DEBUG("Caught a ForcefulKillException in Thread::wrapper");
105 xbt_assert(not context->is_maestro(), "Maestro shall not receive ForcefulKillExceptions, even when detached.");
106 } catch (simgrid::Exception const& e) {
107 XBT_INFO("Actor killed by an uncaught exception %s", boost::core::demangle(typeid(e).name()).c_str());
110 // Signal to the caller (normally the maestro) that we have finished:
114 install_sigsegv_stack(nullptr, false);
116 XBT_DEBUG("Terminating");
117 Context::set_current(nullptr);
120 void ThreadContext::release()
122 this->begin_.release();
125 void ThreadContext::wait()
127 this->end_.acquire();
130 void ThreadContext::start()
132 this->begin_.acquire();
136 void ThreadContext::yield()
139 this->end_.release();
142 void ThreadContext::stop()
146 throw ForcefulKillException();
149 void ThreadContext::suspend()
155 void ThreadContext::attach_start()
157 // We're breaking the layers here by depending on the upper layer:
158 auto* maestro = static_cast<ThreadContext*>(simix_global->maestro_->context_.get());
159 maestro->begin_.release();
160 xbt_assert(not this->is_maestro());
164 void ThreadContext::attach_stop()
166 xbt_assert(not this->is_maestro());
169 auto* maestro = static_cast<ThreadContext*>(simix_global->maestro_->context_.get());
170 maestro->end_.acquire();
172 Context::set_current(nullptr);
175 // SerialThreadContext
177 void SerialThreadContext::run_all()
179 for (smx_actor_t const& actor : simix_global->actors_to_run) {
180 XBT_DEBUG("Handling %p", actor);
181 auto* context = static_cast<ThreadContext*>(actor->context_.get());
187 // ParallelThreadContext
189 xbt::OsSemaphore* ParallelThreadContext::thread_sem_ = nullptr;
191 void ParallelThreadContext::initialize()
193 thread_sem_ = new xbt::OsSemaphore(SIMIX_context_get_nthreads());
196 void ParallelThreadContext::finalize()
199 thread_sem_ = nullptr;
202 void ParallelThreadContext::run_all()
204 for (smx_actor_t const& actor : simix_global->actors_to_run)
205 static_cast<ThreadContext*>(actor->context_.get())->release();
206 for (smx_actor_t const& actor : simix_global->actors_to_run)
207 static_cast<ThreadContext*>(actor->context_.get())->wait();
210 void ParallelThreadContext::start_hook()
212 if (not is_maestro()) /* parallel run */
213 thread_sem_->acquire();
216 void ParallelThreadContext::yield_hook()
218 if (not is_maestro()) /* parallel run */
219 thread_sem_->release();
222 XBT_PRIVATE ContextFactory* thread_factory()
224 XBT_VERB("Activating thread context factory");
225 return new ThreadContextFactory();
227 } // namespace context
228 } // namespace kernel
229 } // namespace simgrid