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()
26 : ContextFactory("ThreadContextFactory"), parallel_(SIMIX_context_is_parallel())
29 ParallelThreadContext::initialize();
32 ThreadContextFactory::~ThreadContextFactory()
35 ParallelThreadContext::finalize();
38 ThreadContext* ThreadContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup,
39 smx_actor_t process, bool maestro)
42 return this->new_context<ParallelThreadContext>(std::move(code), cleanup, process, maestro);
44 return this->new_context<SerialThreadContext>(std::move(code), cleanup, process, maestro);
47 void ThreadContextFactory::run_all()
51 ParallelThreadContext::run_all();
54 SerialThreadContext::run_all();
60 ThreadContext::ThreadContext(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t actor, bool maestro)
61 : AttachContext(std::move(code), cleanup, actor), is_maestro_(maestro)
63 /* If the user provided a function for the process then use it */
65 /* create and start the process */
66 this->thread_ = new std::thread(ThreadContext::wrapper, this);
67 /* wait the starting of the newly created process */
71 /* Otherwise, we attach to the current thread */
73 Context::set_current(this);
77 ThreadContext::~ThreadContext()
79 if (this->thread_) /* If there is a thread (maestro don't have any), wait for its termination */
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 } catch (StopRequest const&) {
104 XBT_DEBUG("Caught a StopRequest");
105 xbt_assert(not context->is_maestro(), "Maestro shall not receive StopRequests, even when detached.");
106 } catch (simgrid::Exception const& e) {
107 XBT_INFO("Actor killed by an uncatched exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
110 if (not context->is_maestro()) // Just in case somebody detached maestro
111 context->Context::stop();
113 // Signal to the caller (normally the maestro) that we have finished:
117 stack.ss_flags = SS_DISABLE;
118 sigaltstack(&stack, 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()
151 void ThreadContext::suspend()
157 void ThreadContext::attach_start()
159 // We're breaking the layers here by depending on the upper layer:
160 ThreadContext* maestro = (ThreadContext*)simix_global->maestro_process->context_;
161 maestro->begin_.release();
162 xbt_assert(not this->is_maestro());
166 void ThreadContext::attach_stop()
168 xbt_assert(not this->is_maestro());
171 ThreadContext* maestro = (ThreadContext*)simix_global->maestro_process->context_;
172 maestro->end_.acquire();
174 Context::set_current(nullptr);
177 // SerialThreadContext
179 void SerialThreadContext::run_all()
181 for (smx_actor_t const& process : simix_global->process_to_run) {
182 XBT_DEBUG("Handling %p", process);
183 ThreadContext* context = static_cast<ThreadContext*>(process->context_);
189 // ParallelThreadContext
191 xbt::OsSemaphore* ParallelThreadContext::thread_sem_ = nullptr;
193 void ParallelThreadContext::initialize()
195 thread_sem_ = new xbt::OsSemaphore(SIMIX_context_get_nthreads());
198 void ParallelThreadContext::finalize()
201 thread_sem_ = nullptr;
204 void ParallelThreadContext::run_all()
206 for (smx_actor_t const& process : simix_global->process_to_run)
207 static_cast<ThreadContext*>(process->context_)->release();
208 for (smx_actor_t const& process : simix_global->process_to_run)
209 static_cast<ThreadContext*>(process->context_)->wait();
212 void ParallelThreadContext::start_hook()
214 if (not is_maestro()) /* parallel run */
215 thread_sem_->acquire();
218 void ParallelThreadContext::yield_hook()
220 if (not is_maestro()) /* parallel run */
221 thread_sem_->release();
224 XBT_PRIVATE ContextFactory* thread_factory()
226 XBT_VERB("Activating thread context factory");
227 return new ThreadContextFactory();