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_sigsegv_stack(nullptr, true);
92 // Tell the caller (normally the maestro) we are starting, and wait for its green light
93 context->end_.release();
98 if (not context->is_maestro()) { // Just in case somebody detached maestro
99 context->Context::stop();
100 context->stop_hook();
102 } catch (ForcefulKillException const&) {
103 XBT_DEBUG("Caught a ForcefulKillException in Thread::wrapper");
104 xbt_assert(not context->is_maestro(), "Maestro shall not receive ForcefulKillExceptions, even when detached.");
105 } catch (simgrid::Exception const& e) {
106 XBT_INFO("Actor killed by an uncaught exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
109 // Signal to the caller (normally the maestro) that we have finished:
113 install_sigsegv_stack(nullptr, false);
115 XBT_DEBUG("Terminating");
116 Context::set_current(nullptr);
119 void ThreadContext::release()
121 this->begin_.release();
124 void ThreadContext::wait()
126 this->end_.acquire();
129 void ThreadContext::start()
131 this->begin_.acquire();
135 void ThreadContext::yield()
138 this->end_.release();
141 void ThreadContext::stop()
145 throw ForcefulKillException();
148 void ThreadContext::suspend()
154 void ThreadContext::attach_start()
156 // We're breaking the layers here by depending on the upper layer:
157 auto* maestro = static_cast<ThreadContext*>(simix_global->maestro_->context_.get());
158 maestro->begin_.release();
159 xbt_assert(not this->is_maestro());
163 void ThreadContext::attach_stop()
165 xbt_assert(not this->is_maestro());
168 auto* maestro = static_cast<ThreadContext*>(simix_global->maestro_->context_.get());
169 maestro->end_.acquire();
171 Context::set_current(nullptr);
174 // SerialThreadContext
176 void SerialThreadContext::run_all()
178 for (smx_actor_t const& actor : simix_global->actors_to_run) {
179 XBT_DEBUG("Handling %p", actor);
180 auto* context = static_cast<ThreadContext*>(actor->context_.get());
186 // ParallelThreadContext
188 xbt::OsSemaphore* ParallelThreadContext::thread_sem_ = nullptr;
190 void ParallelThreadContext::initialize()
192 thread_sem_ = new xbt::OsSemaphore(SIMIX_context_get_nthreads());
195 void ParallelThreadContext::finalize()
198 thread_sem_ = nullptr;
201 void ParallelThreadContext::run_all()
203 for (smx_actor_t const& actor : simix_global->actors_to_run)
204 static_cast<ThreadContext*>(actor->context_.get())->release();
205 for (smx_actor_t const& actor : simix_global->actors_to_run)
206 static_cast<ThreadContext*>(actor->context_.get())->wait();
209 void ParallelThreadContext::start_hook()
211 if (not is_maestro()) /* parallel run */
212 thread_sem_->acquire();
215 void ParallelThreadContext::yield_hook()
217 if (not is_maestro()) /* parallel run */
218 thread_sem_->release();
221 XBT_PRIVATE ContextFactory* thread_factory()
223 XBT_VERB("Activating thread context factory");
224 return new ThreadContextFactory();
226 } // namespace context
227 } // namespace kernel
228 } // namespace simgrid