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"
13 #include "xbt/xbt_os_thread.h"
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
24 // ThreadContextFactory
26 ThreadContextFactory::ThreadContextFactory()
27 : ContextFactory("ThreadContextFactory"), parallel_(SIMIX_context_is_parallel())
30 ParallelThreadContext::initialize();
33 ThreadContextFactory::~ThreadContextFactory()
36 ParallelThreadContext::finalize();
39 ThreadContext* ThreadContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup,
40 smx_actor_t process, bool maestro)
43 return this->new_context<ParallelThreadContext>(std::move(code), cleanup, process, maestro);
45 return this->new_context<SerialThreadContext>(std::move(code), cleanup, process, maestro);
48 void ThreadContextFactory::run_all()
52 ParallelThreadContext::run_all();
55 SerialThreadContext::run_all();
61 ThreadContext::ThreadContext(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t actor, bool maestro)
62 : AttachContext(std::move(code), cleanup, actor), is_maestro_(maestro)
64 /* If the user provided a function for the process then use it */
66 /* create and start the process */
67 this->thread_ = new std::thread(ThreadContext::wrapper, this);
68 /* wait the starting of the newly created process */
72 /* Otherwise, we attach to the current thread */
74 Context::set_current(this);
78 ThreadContext::~ThreadContext()
80 if (this->thread_) /* If there is a thread (maestro don't have any), wait for its termination */
85 void *ThreadContext::wrapper(void *param)
87 ThreadContext* context = static_cast<ThreadContext*>(param);
88 Context::set_current(context);
91 /* Install alternate signal stack, for SIGSEGV handler. */
93 stack.ss_sp = sigsegv_stack;
94 stack.ss_size = sizeof sigsegv_stack;
96 sigaltstack(&stack, nullptr);
98 // Tell the caller (normally the maestro) we are starting, and wait for its green light
99 context->end_.release();
104 } catch (StopRequest const&) {
105 XBT_DEBUG("Caught a StopRequest");
106 xbt_assert(not context->is_maestro(), "Maestro shall not receive StopRequests, even when detached.");
107 } catch (simgrid::Exception const& e) {
108 XBT_INFO("Actor killed by an uncatched exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
111 if (not context->is_maestro()) // Just in case somebody detached maestro
112 context->Context::stop();
114 // Signal to the caller (normally the maestro) that we have finished:
118 stack.ss_flags = SS_DISABLE;
119 sigaltstack(&stack, nullptr);
124 void ThreadContext::release()
126 this->begin_.release();
129 void ThreadContext::wait()
131 this->end_.acquire();
134 void ThreadContext::start()
136 this->begin_.acquire();
140 void ThreadContext::yield()
143 this->end_.release();
146 void ThreadContext::stop()
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 = (ThreadContext*)simix_global->maestro_process->context_;
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 = (ThreadContext*)simix_global->maestro_process->context_;
173 maestro->end_.acquire();
175 Context::set_current(nullptr);
178 // SerialThreadContext
180 void SerialThreadContext::run_all()
182 for (smx_actor_t const& process : simix_global->process_to_run) {
183 XBT_DEBUG("Handling %p", process);
184 ThreadContext* context = static_cast<ThreadContext*>(process->context_);
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& process : simix_global->process_to_run)
208 static_cast<ThreadContext*>(process->context_)->release();
209 for (smx_actor_t const& process : simix_global->process_to_run)
210 static_cast<ThreadContext*>(process->context_)->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();