1 /* Copyright (c) 2009-2018. 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 process,
63 : AttachContext(std::move(code), cleanup, process), is_maestro_(maestro)
65 // We do not need the semaphores when maestro is in main,
66 // but creating them anyway simplifies things when maestro is externalized
67 this->begin_ = xbt_os_sem_init(0);
68 this->end_ = xbt_os_sem_init(0);
70 /* If the user provided a function for the process then use it */
72 if (smx_context_stack_size_was_set)
73 xbt_os_thread_setstacksize(smx_context_stack_size);
74 if (smx_context_guard_size_was_set)
75 xbt_os_thread_setguardsize(smx_context_guard_size);
77 /* create and start the process */
78 /* NOTE: The first argument to xbt_os_thread_create used to be the process *
79 * name, but now the name is stored at SIMIX level, so we pass a null */
80 this->thread_ = xbt_os_thread_create(nullptr, ThreadContext::wrapper, this, this);
81 /* wait the starting of the newly created process */
82 xbt_os_sem_acquire(this->end_);
85 /* Otherwise, we attach to the current thread */
87 xbt_os_thread_set_extra_data(this);
91 ThreadContext::~ThreadContext()
93 if (this->thread_) /* If there is a thread (maestro don't have any), wait for its termination */
94 xbt_os_thread_join(this->thread_, nullptr);
96 /* destroy the synchronization objects */
97 xbt_os_sem_destroy(this->begin_);
98 xbt_os_sem_destroy(this->end_);
101 void *ThreadContext::wrapper(void *param)
103 ThreadContext* context = static_cast<ThreadContext*>(param);
106 /* Install alternate signal stack, for SIGSEGV handler. */
108 stack.ss_sp = sigsegv_stack;
109 stack.ss_size = sizeof sigsegv_stack;
111 sigaltstack(&stack, nullptr);
113 // Tell the caller (normally the maestro) we are starting, and wait for its green light
114 xbt_os_sem_release(context->end_);
119 if (not context->is_maestro()) // really?
120 context->Context::stop();
121 } catch (StopRequest const&) {
122 XBT_DEBUG("Caught a StopRequest");
123 xbt_assert(not context->is_maestro(), "I'm not supposed to be maestro here.");
124 } catch (simgrid::HostFailureException const&) {
125 XBT_DEBUG("Caught an HostFailureException");
127 if (not context->is_maestro()) // really?
128 context->Context::stop();
130 // Signal to the caller (normally the maestro) that we have finished:
134 stack.ss_flags = SS_DISABLE;
135 sigaltstack(&stack, nullptr);
140 void ThreadContext::release()
142 xbt_os_sem_release(this->begin_);
145 void ThreadContext::wait()
147 xbt_os_sem_acquire(this->end_);
150 void ThreadContext::start()
152 xbt_os_sem_acquire(this->begin_);
156 void ThreadContext::yield()
159 xbt_os_sem_release(this->end_);
162 void ThreadContext::stop()
168 void ThreadContext::suspend()
174 void ThreadContext::attach_start()
176 // We're breaking the layers here by depending on the upper layer:
177 ThreadContext* maestro = (ThreadContext*)simix_global->maestro_process->context_;
178 xbt_os_sem_release(maestro->begin_);
179 xbt_assert(not this->is_maestro());
183 void ThreadContext::attach_stop()
185 xbt_assert(not this->is_maestro());
188 ThreadContext* maestro = (ThreadContext*)simix_global->maestro_process->context_;
189 xbt_os_sem_acquire(maestro->end_);
191 xbt_os_thread_set_extra_data(nullptr);
194 // SerialThreadContext
196 void SerialThreadContext::run_all()
198 for (smx_actor_t const& process : simix_global->process_to_run) {
199 XBT_DEBUG("Handling %p", process);
200 ThreadContext* context = static_cast<ThreadContext*>(process->context_);
206 // ParallelThreadContext
208 xbt_os_sem_t ParallelThreadContext::thread_sem_ = nullptr;
210 void ParallelThreadContext::initialize()
212 thread_sem_ = xbt_os_sem_init(SIMIX_context_get_nthreads());
215 void ParallelThreadContext::finalize()
217 xbt_os_sem_destroy(thread_sem_);
218 thread_sem_ = nullptr;
221 void ParallelThreadContext::run_all()
223 for (smx_actor_t const& process : simix_global->process_to_run)
224 static_cast<ThreadContext*>(process->context_)->release();
225 for (smx_actor_t const& process : simix_global->process_to_run)
226 static_cast<ThreadContext*>(process->context_)->wait();
229 void ParallelThreadContext::start_hook()
231 if (not is_maestro()) /* parallel run */
232 xbt_os_sem_acquire(thread_sem_);
235 void ParallelThreadContext::yield_hook()
237 if (not is_maestro()) /* parallel run */
238 xbt_os_sem_release(thread_sem_);
241 XBT_PRIVATE ContextFactory* thread_factory()
243 XBT_VERB("Activating thread context factory");
244 return new ThreadContextFactory();