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 "xbt/function_types.h"
10 #include "src/simix/smx_private.h"
11 #include "src/internal_config.h" /* loads context system definitions */
13 #include "xbt/xbt_os_thread.h"
14 #include "src/xbt_modinter.h" /* prototype of os thread module's init/exit in XBT */
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) {
63 xbt_dynar_foreach(simix_global->process_to_run, cursor, process) {
64 XBT_DEBUG("Handling %p",process);
65 ThreadContext* context = static_cast<ThreadContext*>(process->context);
66 xbt_os_sem_release(context->begin_);
67 xbt_os_sem_acquire(context->end_);
73 xbt_dynar_foreach(simix_global->process_to_run, index, process)
74 xbt_os_sem_release(static_cast<ThreadContext*>(process->context)->begin_);
75 xbt_dynar_foreach(simix_global->process_to_run, index, process)
76 xbt_os_sem_acquire(static_cast<ThreadContext*>(process->context)->end_);
80 ThreadContext* ThreadContextFactory::self()
82 return static_cast<ThreadContext*>(xbt_os_thread_get_extra_data());
85 ThreadContext* ThreadContextFactory::attach(void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
87 return this->new_context<ThreadContext>(
88 std::function<void()>(), cleanup_func, process, false);
91 ThreadContext* ThreadContextFactory::create_maestro(std::function<void()> code, smx_actor_t process)
93 return this->new_context<ThreadContext>(std::move(code), nullptr, process, true);
96 ThreadContext::ThreadContext(std::function<void()> code,
97 void_pfn_smxprocess_t cleanup, smx_actor_t process, bool maestro)
98 : AttachContext(std::move(code), cleanup, process)
100 // We do not need the semaphores when maestro is in main,
101 // but creating them anyway simplifies things when maestro is externalized
102 this->begin_ = xbt_os_sem_init(0);
103 this->end_ = xbt_os_sem_init(0);
105 /* If the user provided a function for the process then use it */
107 if (smx_context_stack_size_was_set)
108 xbt_os_thread_setstacksize(smx_context_stack_size);
109 if (smx_context_guard_size_was_set)
110 xbt_os_thread_setguardsize(smx_context_guard_size);
112 /* create and start the process */
113 /* NOTE: The first argument to xbt_os_thread_create used to be the process *
114 * name, but now the name is stored at SIMIX level, so we pass a null */
116 xbt_os_thread_create(nullptr,
117 maestro ? &ThreadContext::maestro_wrapper : &ThreadContext::wrapper,
119 /* wait the starting of the newly created process */
120 xbt_os_sem_acquire(this->end_);
123 /* Otherwise, we attach to the current thread */
125 xbt_os_thread_set_extra_data(this);
129 ThreadContext::~ThreadContext()
131 if (this->thread_) /* If there is a thread (maestro don't have any), wait for its termination */
132 xbt_os_thread_join(this->thread_, nullptr);
134 /* destroy the synchronization objects */
135 xbt_os_sem_destroy(this->begin_);
136 xbt_os_sem_destroy(this->end_);
139 void *ThreadContext::wrapper(void *param)
141 ThreadContext* context = static_cast<ThreadContext*>(param);
144 /* Install alternate signal stack, for SIGSEGV handler. */
146 stack.ss_sp = sigsegv_stack;
147 stack.ss_size = sizeof sigsegv_stack;
149 sigaltstack(&stack, nullptr);
151 /* Tell the maestro we are starting, and wait for its green light */
152 xbt_os_sem_release(context->end_);
154 xbt_os_sem_acquire(context->begin_);
155 if (smx_ctx_thread_sem) /* parallel run */
156 xbt_os_sem_acquire(smx_ctx_thread_sem);
164 void *ThreadContext::maestro_wrapper(void *param)
166 ThreadContext* context = static_cast<ThreadContext*>(param);
169 /* Install alternate signal stack, for SIGSEGV handler. */
171 stack.ss_sp = sigsegv_stack;
172 stack.ss_size = sizeof sigsegv_stack;
174 sigaltstack(&stack, nullptr);
176 /* Tell the caller we are starting */
177 xbt_os_sem_release(context->end_);
179 // Wait for the caller to give control back to us:
180 xbt_os_sem_acquire(context->begin_);
183 // Tell main that we have finished:
184 xbt_os_sem_release(context->end_);
189 void ThreadContext::start()
191 xbt_os_sem_acquire(this->begin_);
192 if (smx_ctx_thread_sem) /* parallel run */
193 xbt_os_sem_acquire(smx_ctx_thread_sem);
196 void ThreadContext::stop()
199 if (smx_ctx_thread_sem)
200 xbt_os_sem_release(smx_ctx_thread_sem);
202 // Signal to the maestro that it has finished:
203 xbt_os_sem_release(this->end_);
205 xbt_os_thread_exit(nullptr);
208 void ThreadContext::suspend()
210 if (smx_ctx_thread_sem)
211 xbt_os_sem_release(smx_ctx_thread_sem);
212 xbt_os_sem_release(this->end_);
213 xbt_os_sem_acquire(this->begin_);
214 if (smx_ctx_thread_sem)
215 xbt_os_sem_acquire(smx_ctx_thread_sem);
218 void ThreadContext::attach_start()
220 // We're breaking the layers here by depending on the upper layer:
221 ThreadContext* maestro = (ThreadContext*) simix_global->maestro_process->context;
222 xbt_os_sem_release(maestro->begin_);
226 void ThreadContext::attach_stop()
228 if (smx_ctx_thread_sem)
229 xbt_os_sem_release(smx_ctx_thread_sem);
230 xbt_os_sem_release(this->end_);
232 ThreadContext* maestro = (ThreadContext*) simix_global->maestro_process->context;
233 xbt_os_sem_acquire(maestro->end_);
235 xbt_os_thread_set_extra_data(nullptr);