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, void_pfn_smxprocess_t cleanup,
38 smx_actor_t actor, bool maestro)
41 return this->new_context<ParallelThreadContext>(std::move(code), cleanup, actor, maestro);
43 return this->new_context<SerialThreadContext>(std::move(code), cleanup, actor, maestro);
46 void ThreadContextFactory::run_all()
50 ParallelThreadContext::run_all();
53 SerialThreadContext::run_all();
59 ThreadContext::ThreadContext(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t actor, bool maestro)
60 : AttachContext(std::move(code), cleanup, actor), is_maestro_(maestro)
62 /* If the user provided a function for the actor then use it */
64 /* create and start the actor */
65 this->thread_ = new std::thread(ThreadContext::wrapper, this);
66 /* wait the starting of the newly created actor */
70 /* Otherwise, we attach to the current thread */
72 Context::set_current(this);
76 ThreadContext::~ThreadContext()
78 if (this->thread_) { /* Maestro don't have any thread */
84 void *ThreadContext::wrapper(void *param)
86 ThreadContext* context = static_cast<ThreadContext*>(param);
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 } catch (StopRequest const&) {
106 XBT_DEBUG("Caught a StopRequest");
107 xbt_assert(not context->is_maestro(), "Maestro shall not receive StopRequests, even when detached.");
108 } catch (simgrid::Exception const& e) {
109 XBT_INFO("Actor killed by an uncatched 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);
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()
150 void ThreadContext::suspend()
156 void ThreadContext::attach_start()
158 // We're breaking the layers here by depending on the upper layer:
159 ThreadContext* maestro = (ThreadContext*)simix_global->maestro_process->context_;
160 maestro->begin_.release();
161 xbt_assert(not this->is_maestro());
165 void ThreadContext::attach_stop()
167 xbt_assert(not this->is_maestro());
170 ThreadContext* maestro = (ThreadContext*)simix_global->maestro_process->context_;
171 maestro->end_.acquire();
173 Context::set_current(nullptr);
176 // SerialThreadContext
178 void SerialThreadContext::run_all()
180 for (smx_actor_t const& actor : simix_global->process_to_run) {
181 XBT_DEBUG("Handling %p", actor);
182 ThreadContext* context = static_cast<ThreadContext*>(actor->context_);
188 // ParallelThreadContext
190 xbt::OsSemaphore* ParallelThreadContext::thread_sem_ = nullptr;
192 void ParallelThreadContext::initialize()
194 thread_sem_ = new xbt::OsSemaphore(SIMIX_context_get_nthreads());
197 void ParallelThreadContext::finalize()
200 thread_sem_ = nullptr;
203 void ParallelThreadContext::run_all()
205 for (smx_actor_t const& actor : simix_global->process_to_run)
206 static_cast<ThreadContext*>(actor->context_)->release();
207 for (smx_actor_t const& actor : simix_global->process_to_run)
208 static_cast<ThreadContext*>(actor->context_)->wait();
211 void ParallelThreadContext::start_hook()
213 if (not is_maestro()) /* parallel run */
214 thread_sem_->acquire();
217 void ParallelThreadContext::yield_hook()
219 if (not is_maestro()) /* parallel run */
220 thread_sem_->release();
223 XBT_PRIVATE ContextFactory* thread_factory()
225 XBT_VERB("Activating thread context factory");
226 return new ThreadContextFactory();