1 /* Copyright (c) 2009-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "xbt/function_types.h"
11 #include "src/simix/smx_private.h"
12 #include "src/internal_config.h" /* loads context system definitions */
14 #include "xbt/xbt_os_thread.h"
15 #include "src/xbt_modinter.h" /* prototype of os thread module's init/exit in XBT */
17 #include "src/kernel/context/ContextThread.hpp"
19 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
21 static xbt_os_sem_t smx_ctx_thread_sem = nullptr;
27 XBT_PRIVATE ContextFactory* thread_factory()
29 XBT_VERB("Activating thread context factory");
30 return new ThreadContextFactory();
33 ThreadContextFactory::ThreadContextFactory()
34 : ContextFactory("ThreadContextFactory")
36 if (SIMIX_context_is_parallel()) {
37 smx_ctx_thread_sem = xbt_os_sem_init(SIMIX_context_get_nthreads());
39 smx_ctx_thread_sem = nullptr;
43 ThreadContextFactory::~ThreadContextFactory()
45 if (smx_ctx_thread_sem) {
46 xbt_os_sem_destroy(smx_ctx_thread_sem);
47 smx_ctx_thread_sem = nullptr;
51 ThreadContext* ThreadContextFactory::create_context(
52 std::function<void()> code,
53 void_pfn_smxprocess_t cleanup, smx_actor_t process)
55 return this->new_context<ThreadContext>(std::move(code), cleanup, process, not code);
58 void ThreadContextFactory::run_all()
60 if (smx_ctx_thread_sem == nullptr) {
64 xbt_dynar_foreach(simix_global->process_to_run, cursor, process) {
65 XBT_DEBUG("Handling %p",process);
66 ThreadContext* context = static_cast<ThreadContext*>(process->context);
67 xbt_os_sem_release(context->begin_);
68 xbt_os_sem_acquire(context->end_);
74 xbt_dynar_foreach(simix_global->process_to_run, index, process)
75 xbt_os_sem_release(static_cast<ThreadContext*>(process->context)->begin_);
76 xbt_dynar_foreach(simix_global->process_to_run, index, process)
77 xbt_os_sem_acquire(static_cast<ThreadContext*>(process->context)->end_);
81 ThreadContext* ThreadContextFactory::self()
83 return static_cast<ThreadContext*>(xbt_os_thread_get_extra_data());
86 ThreadContext* ThreadContextFactory::attach(void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
88 return this->new_context<ThreadContext>(
89 std::function<void()>(), cleanup_func, process, false);
92 ThreadContext* ThreadContextFactory::create_maestro(std::function<void()> code, smx_actor_t process)
94 return this->new_context<ThreadContext>(std::move(code), nullptr, process, true);
97 ThreadContext::ThreadContext(std::function<void()> code,
98 void_pfn_smxprocess_t cleanup, smx_actor_t process, bool maestro)
99 : AttachContext(std::move(code), cleanup, process)
101 // We do not need the semaphores when maestro is in main,
102 // but creating them anyway simplifies things when maestro is externalized
103 this->begin_ = xbt_os_sem_init(0);
104 this->end_ = xbt_os_sem_init(0);
106 /* If the user provided a function for the process then use it */
108 if (smx_context_stack_size_was_set)
109 xbt_os_thread_setstacksize(smx_context_stack_size);
110 if (smx_context_guard_size_was_set)
111 xbt_os_thread_setguardsize(smx_context_guard_size);
113 /* create and start the process */
114 /* NOTE: The first argument to xbt_os_thread_create used to be the process *
115 * name, but now the name is stored at SIMIX level, so we pass a null */
117 xbt_os_thread_create(nullptr,
118 maestro ? ThreadContext::maestro_wrapper : ThreadContext::wrapper,
120 /* wait the starting of the newly created process */
121 xbt_os_sem_acquire(this->end_);
124 /* Otherwise, we attach to the current thread */
126 xbt_os_thread_set_extra_data(this);
130 ThreadContext::~ThreadContext()
132 if (this->thread_) /* If there is a thread (maestro don't have any), wait for its termination */
133 xbt_os_thread_join(this->thread_, nullptr);
135 /* destroy the synchronization objects */
136 xbt_os_sem_destroy(this->begin_);
137 xbt_os_sem_destroy(this->end_);
140 void *ThreadContext::wrapper(void *param)
142 ThreadContext* context = static_cast<ThreadContext*>(param);
145 /* Install alternate signal stack, for SIGSEGV handler. */
147 stack.ss_sp = sigsegv_stack;
148 stack.ss_size = sizeof sigsegv_stack;
150 sigaltstack(&stack, nullptr);
152 /* Tell the maestro we are starting, and wait for its green light */
153 xbt_os_sem_release(context->end_);
155 xbt_os_sem_acquire(context->begin_);
156 if (smx_ctx_thread_sem) /* parallel run */
157 xbt_os_sem_acquire(smx_ctx_thread_sem);
165 void *ThreadContext::maestro_wrapper(void *param)
167 ThreadContext* context = static_cast<ThreadContext*>(param);
170 /* Install alternate signal stack, for SIGSEGV handler. */
172 stack.ss_sp = sigsegv_stack;
173 stack.ss_size = sizeof sigsegv_stack;
175 sigaltstack(&stack, nullptr);
177 /* Tell the caller we are starting */
178 xbt_os_sem_release(context->end_);
180 // Wait for the caller to give control back to us:
181 xbt_os_sem_acquire(context->begin_);
184 // Tell main that we have finished:
185 xbt_os_sem_release(context->end_);
190 void ThreadContext::start()
192 xbt_os_sem_acquire(this->begin_);
193 if (smx_ctx_thread_sem) /* parallel run */
194 xbt_os_sem_acquire(smx_ctx_thread_sem);
197 void ThreadContext::stop()
200 if (smx_ctx_thread_sem)
201 xbt_os_sem_release(smx_ctx_thread_sem);
203 // Signal to the maestro that it has finished:
204 xbt_os_sem_release(this->end_);
206 xbt_os_thread_exit(nullptr);
209 void ThreadContext::suspend()
211 if (smx_ctx_thread_sem)
212 xbt_os_sem_release(smx_ctx_thread_sem);
213 xbt_os_sem_release(this->end_);
214 xbt_os_sem_acquire(this->begin_);
215 if (smx_ctx_thread_sem)
216 xbt_os_sem_acquire(smx_ctx_thread_sem);
219 void ThreadContext::attach_start()
221 // We're breaking the layers here by depending on the upper layer:
222 ThreadContext* maestro = (ThreadContext*) simix_global->maestro_process->context;
223 xbt_os_sem_release(maestro->begin_);
227 void ThreadContext::attach_stop()
229 if (smx_ctx_thread_sem)
230 xbt_os_sem_release(smx_ctx_thread_sem);
231 xbt_os_sem_release(this->end_);
233 ThreadContext* maestro = (ThreadContext*) simix_global->maestro_process->context;
234 xbt_os_sem_acquire(maestro->end_);
236 xbt_os_thread_set_extra_data(nullptr);