1 /* Copyright (c) 2009-2019. 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(), parallel_(SIMIX_context_is_parallel())
28 ParallelThreadContext::initialize();
31 ThreadContextFactory::~ThreadContextFactory()
34 ParallelThreadContext::finalize();
37 ThreadContext* ThreadContextFactory::create_context(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
40 return this->new_context<ParallelThreadContext>(std::move(code), actor, maestro);
42 return this->new_context<SerialThreadContext>(std::move(code), actor, maestro);
45 void ThreadContextFactory::run_all()
49 ParallelThreadContext::run_all();
52 SerialThreadContext::run_all();
58 ThreadContext::ThreadContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
59 : AttachContext(std::move(code), actor), is_maestro_(maestro)
61 /* If the user provided a function for the actor then use it */
63 /* create and start the actor */
64 this->thread_ = new std::thread(ThreadContext::wrapper, this);
65 /* wait the starting of the newly created actor */
69 /* Otherwise, we attach to the current thread */
71 Context::set_current(this);
75 ThreadContext::~ThreadContext()
77 if (this->thread_) { /* Maestro don't have any thread */
83 void ThreadContext::wrapper(ThreadContext* context)
85 Context::set_current(context);
88 /* Install alternate signal stack, for SIGSEGV handler. */
90 stack.ss_sp = sigsegv_stack;
91 stack.ss_size = sizeof sigsegv_stack;
93 sigaltstack(&stack, nullptr);
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", simgrid::xbt::demangle(typeid(e).name()).get());
112 // Signal to the caller (normally the maestro) that we have finished:
116 stack.ss_flags = SS_DISABLE;
117 sigaltstack(&stack, nullptr);
119 XBT_DEBUG("Terminating");
120 Context::set_current(nullptr);
123 void ThreadContext::release()
125 this->begin_.release();
128 void ThreadContext::wait()
130 this->end_.acquire();
133 void ThreadContext::start()
135 this->begin_.acquire();
139 void ThreadContext::yield()
142 this->end_.release();
145 void ThreadContext::stop()
149 throw ForcefulKillException();
152 void ThreadContext::suspend()
158 void ThreadContext::attach_start()
160 // We're breaking the layers here by depending on the upper layer:
161 ThreadContext* maestro = static_cast<ThreadContext*>(simix_global->maestro_->context_.get());
162 maestro->begin_.release();
163 xbt_assert(not this->is_maestro());
167 void ThreadContext::attach_stop()
169 xbt_assert(not this->is_maestro());
172 ThreadContext* maestro = static_cast<ThreadContext*>(simix_global->maestro_->context_.get());
173 maestro->end_.acquire();
175 Context::set_current(nullptr);
178 // SerialThreadContext
180 void SerialThreadContext::run_all()
182 for (smx_actor_t const& actor : simix_global->actors_to_run) {
183 XBT_DEBUG("Handling %p", actor);
184 ThreadContext* 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 for (smx_actor_t const& actor : simix_global->actors_to_run)
208 static_cast<ThreadContext*>(actor->context_.get())->release();
209 for (smx_actor_t const& actor : simix_global->actors_to_run)
210 static_cast<ThreadContext*>(actor->context_.get())->wait();
213 void ParallelThreadContext::start_hook()
215 if (not is_maestro()) /* parallel run */
216 thread_sem_->acquire();
219 void ParallelThreadContext::yield_hook()
221 if (not is_maestro()) /* parallel run */
222 thread_sem_->release();
225 XBT_PRIVATE ContextFactory* thread_factory()
227 XBT_VERB("Activating thread context factory");
228 return new ThreadContextFactory();
230 } // namespace context
231 } // namespace kernel
232 } // namespace simgrid