1 /* Copyright (c) 2009-2017. 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. */
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"
14 #include "xbt/xbt_os_thread.h"
16 #include "src/kernel/context/ContextThread.hpp"
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
20 static xbt_os_sem_t smx_ctx_thread_sem = nullptr;
26 XBT_PRIVATE ContextFactory* thread_factory()
28 XBT_VERB("Activating thread context factory");
29 return new ThreadContextFactory();
32 ThreadContextFactory::ThreadContextFactory()
33 : ContextFactory("ThreadContextFactory")
35 if (SIMIX_context_is_parallel()) {
36 smx_ctx_thread_sem = xbt_os_sem_init(SIMIX_context_get_nthreads());
38 smx_ctx_thread_sem = nullptr;
42 ThreadContextFactory::~ThreadContextFactory()
44 if (smx_ctx_thread_sem) {
45 xbt_os_sem_destroy(smx_ctx_thread_sem);
46 smx_ctx_thread_sem = nullptr;
50 ThreadContext* ThreadContextFactory::create_context(
51 std::function<void()> code,
52 void_pfn_smxprocess_t cleanup, smx_actor_t process)
54 return this->new_context<ThreadContext>(std::move(code), cleanup, process, not code);
57 void ThreadContextFactory::run_all()
59 if (smx_ctx_thread_sem == nullptr) {
61 for (smx_actor_t const& process : simix_global->process_to_run) {
62 XBT_DEBUG("Handling %p",process);
63 ThreadContext* context = static_cast<ThreadContext*>(process->context);
64 xbt_os_sem_release(context->begin_);
65 xbt_os_sem_acquire(context->end_);
69 for (smx_actor_t const& process : simix_global->process_to_run)
70 xbt_os_sem_release(static_cast<ThreadContext*>(process->context)->begin_);
71 for (smx_actor_t const& process : simix_global->process_to_run)
72 xbt_os_sem_acquire(static_cast<ThreadContext*>(process->context)->end_);
76 ThreadContext* ThreadContextFactory::self()
78 return static_cast<ThreadContext*>(xbt_os_thread_get_extra_data());
81 ThreadContext* ThreadContextFactory::attach(void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
83 return this->new_context<ThreadContext>(
84 std::function<void()>(), cleanup_func, process, false);
87 ThreadContext* ThreadContextFactory::create_maestro(std::function<void()> code, smx_actor_t process)
89 return this->new_context<ThreadContext>(std::move(code), nullptr, process, true);
92 ThreadContext::ThreadContext(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process,
94 : AttachContext(std::move(code), cleanup, process), is_maestro_(maestro)
96 // We do not need the semaphores when maestro is in main,
97 // but creating them anyway simplifies things when maestro is externalized
98 this->begin_ = xbt_os_sem_init(0);
99 this->end_ = xbt_os_sem_init(0);
101 /* If the user provided a function for the process then use it */
103 if (smx_context_stack_size_was_set)
104 xbt_os_thread_setstacksize(smx_context_stack_size);
105 if (smx_context_guard_size_was_set)
106 xbt_os_thread_setguardsize(smx_context_guard_size);
108 /* create and start the process */
109 /* NOTE: The first argument to xbt_os_thread_create used to be the process *
110 * name, but now the name is stored at SIMIX level, so we pass a null */
111 this->thread_ = xbt_os_thread_create(nullptr, ThreadContext::wrapper, this, this);
112 /* wait the starting of the newly created process */
113 xbt_os_sem_acquire(this->end_);
116 /* Otherwise, we attach to the current thread */
118 xbt_os_thread_set_extra_data(this);
122 ThreadContext::~ThreadContext()
124 if (this->thread_) /* If there is a thread (maestro don't have any), wait for its termination */
125 xbt_os_thread_join(this->thread_, nullptr);
127 /* destroy the synchronization objects */
128 xbt_os_sem_destroy(this->begin_);
129 xbt_os_sem_destroy(this->end_);
132 void *ThreadContext::wrapper(void *param)
134 ThreadContext* context = static_cast<ThreadContext*>(param);
137 /* Install alternate signal stack, for SIGSEGV handler. */
139 stack.ss_sp = sigsegv_stack;
140 stack.ss_size = sizeof sigsegv_stack;
142 sigaltstack(&stack, nullptr);
144 // Tell the caller (normally the maestro) we are starting, and wait for its green light
145 xbt_os_sem_release(context->end_);
150 if (not context->is_maestro_) // really?
151 context->Context::stop();
152 } catch (StopRequest const&) {
153 XBT_DEBUG("Caught a StopRequest");
154 xbt_assert(not context->is_maestro_, "I'm not supposed to be maestro here.");
157 // Signal to the caller (normally the maestro) that we have finished:
161 stack.ss_flags = SS_DISABLE;
162 sigaltstack(&stack, nullptr);
167 void ThreadContext::start()
169 xbt_os_sem_acquire(this->begin_);
170 if (not is_maestro_ && smx_ctx_thread_sem) /* parallel run */
171 xbt_os_sem_acquire(smx_ctx_thread_sem);
174 void ThreadContext::yield()
176 if (not is_maestro_ && smx_ctx_thread_sem) /* parallel run */
177 xbt_os_sem_release(smx_ctx_thread_sem);
178 xbt_os_sem_release(this->end_);
181 void ThreadContext::stop()
187 void ThreadContext::suspend()
193 void ThreadContext::attach_start()
195 // We're breaking the layers here by depending on the upper layer:
196 ThreadContext* maestro = (ThreadContext*) simix_global->maestro_process->context;
197 xbt_os_sem_release(maestro->begin_);
198 xbt_assert(not this->is_maestro_);
202 void ThreadContext::attach_stop()
204 xbt_assert(not this->is_maestro_);
207 ThreadContext* maestro = (ThreadContext*) simix_global->maestro_process->context;
208 xbt_os_sem_acquire(maestro->end_);
210 xbt_os_thread_set_extra_data(nullptr);