1 /* Copyright (c) 2009-2020. 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"
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
23 // ThreadContextFactory
25 ThreadContextFactory::ThreadContextFactory() : ContextFactory()
27 if (smx_context_stack_size != 8 * 1024 * 1024)
28 XBT_INFO("Stack size modifications are ignored by thread factory.");
29 if (SIMIX_context_is_parallel())
30 ParallelThreadContext::initialize();
33 ThreadContextFactory::~ThreadContextFactory()
35 if (SIMIX_context_is_parallel())
36 ParallelThreadContext::finalize();
39 ThreadContext* ThreadContextFactory::create_context(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
41 if (SIMIX_context_is_parallel())
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()
49 if (SIMIX_context_is_parallel()) {
51 ParallelThreadContext::run_all();
54 SerialThreadContext::run_all();
60 ThreadContext::ThreadContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
61 : AttachContext(std::move(code), actor), is_maestro_(maestro)
63 /* If the user provided a function for the actor then use it */
65 /* create and start the actor */
66 this->thread_ = new std::thread(ThreadContext::wrapper, this);
67 /* wait the starting of the newly created actor */
71 /* Otherwise, we attach to the current thread */
73 Context::set_current(this);
77 ThreadContext::~ThreadContext()
79 if (this->thread_) { /* Maestro don't have any thread */
85 void ThreadContext::wrapper(ThreadContext* context)
87 Context::set_current(context);
90 /* Install alternate signal stack, for SIGSEGV handler. */
92 stack.ss_sp = sigsegv_stack;
93 stack.ss_size = sizeof sigsegv_stack;
95 sigaltstack(&stack, nullptr);
97 // Tell the caller (normally the maestro) we are starting, and wait for its green light
98 context->end_.release();
103 if (not context->is_maestro()) { // Just in case somebody detached maestro
104 context->Context::stop();
105 context->stop_hook();
107 } catch (ForcefulKillException const&) {
108 XBT_DEBUG("Caught a ForcefulKillException in Thread::wrapper");
109 xbt_assert(not context->is_maestro(), "Maestro shall not receive ForcefulKillExceptions, even when detached.");
110 } catch (simgrid::Exception const& e) {
111 XBT_INFO("Actor killed by an uncaught exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
114 // Signal to the caller (normally the maestro) that we have finished:
118 stack.ss_flags = SS_DISABLE;
119 sigaltstack(&stack, nullptr);
121 XBT_DEBUG("Terminating");
122 Context::set_current(nullptr);
125 void ThreadContext::release()
127 this->begin_.release();
130 void ThreadContext::wait()
132 this->end_.acquire();
135 void ThreadContext::start()
137 this->begin_.acquire();
141 void ThreadContext::yield()
144 this->end_.release();
147 void ThreadContext::stop()
151 throw ForcefulKillException();
154 void ThreadContext::suspend()
160 void ThreadContext::attach_start()
162 // We're breaking the layers here by depending on the upper layer:
163 auto* maestro = static_cast<ThreadContext*>(simix_global->maestro_->context_.get());
164 maestro->begin_.release();
165 xbt_assert(not this->is_maestro());
169 void ThreadContext::attach_stop()
171 xbt_assert(not this->is_maestro());
174 auto* maestro = static_cast<ThreadContext*>(simix_global->maestro_->context_.get());
175 maestro->end_.acquire();
177 Context::set_current(nullptr);
180 // SerialThreadContext
182 void SerialThreadContext::run_all()
184 for (smx_actor_t const& actor : simix_global->actors_to_run) {
185 XBT_DEBUG("Handling %p", actor);
186 auto* context = static_cast<ThreadContext*>(actor->context_.get());
192 // ParallelThreadContext
194 xbt::OsSemaphore* ParallelThreadContext::thread_sem_ = nullptr;
196 void ParallelThreadContext::initialize()
198 thread_sem_ = new xbt::OsSemaphore(SIMIX_context_get_nthreads());
201 void ParallelThreadContext::finalize()
204 thread_sem_ = nullptr;
207 void ParallelThreadContext::run_all()
209 for (smx_actor_t const& actor : simix_global->actors_to_run)
210 static_cast<ThreadContext*>(actor->context_.get())->release();
211 for (smx_actor_t const& actor : simix_global->actors_to_run)
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