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. */
7 #include "xbt/function_types.h"
8 #include "smx_private.h"
9 #include "src/portable.h" /* loads context system definitions */
11 #include "xbt/xbt_os_thread.h"
12 #include "src/xbt_modinter.h" /* prototype of os thread module's init/exit in XBT */
14 #include "src/simix/ThreadContext.hpp"
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
18 static xbt_os_sem_t smx_ctx_thread_sem = nullptr;
23 XBT_PRIVATE ContextFactory* thread_factory()
25 XBT_VERB("Activating thread context factory");
26 return new ThreadContextFactory();
29 ThreadContextFactory::ThreadContextFactory()
30 : ContextFactory("ThreadContextFactory")
32 if (SIMIX_context_is_parallel()) {
33 smx_ctx_thread_sem = xbt_os_sem_init(SIMIX_context_get_nthreads());
35 smx_ctx_thread_sem = nullptr;
39 ThreadContextFactory::~ThreadContextFactory()
41 if (smx_ctx_thread_sem) {
42 xbt_os_sem_destroy(smx_ctx_thread_sem);
43 smx_ctx_thread_sem = nullptr;
47 ThreadContext* ThreadContextFactory::create_context(
48 std::function<void()> code,
49 void_pfn_smxprocess_t cleanup, smx_process_t process)
51 return this->new_context<ThreadContext>(std::move(code), cleanup, process);
54 ThreadContext::ThreadContext(std::function<void()> code,
55 void_pfn_smxprocess_t cleanup, smx_process_t process)
56 : Context(std::move(code), cleanup, process)
58 /* If the user provided a function for the process then use it
59 otherwise is the context for maestro */
61 this->begin_ = xbt_os_sem_init(0);
62 this->end_ = xbt_os_sem_init(0);
63 if (smx_context_stack_size_was_set)
64 xbt_os_thread_setstacksize(smx_context_stack_size);
65 if (smx_context_guard_size_was_set)
66 xbt_os_thread_setguardsize(smx_context_guard_size);
68 /* create and start the process */
69 /* NOTE: The first argument to xbt_os_thread_create used to be the process *
70 * name, but now the name is stored at SIMIX level, so we pass a null */
72 xbt_os_thread_create(NULL, ThreadContext::wrapper, this, this);
74 /* wait the starting of the newly created process */
75 xbt_os_sem_acquire(this->end_);
78 xbt_os_thread_set_extra_data(this);
82 void ThreadContextFactory::run_all()
84 if (smx_ctx_thread_sem == nullptr) {
86 smx_process_t process;
88 xbt_dynar_foreach(simix_global->process_to_run, cursor, process) {
89 XBT_DEBUG("Handling %p",process);
90 ThreadContext* context = static_cast<ThreadContext*>(process->context);
91 xbt_os_sem_release(context->begin_);
92 xbt_os_sem_acquire(context->end_);
97 smx_process_t process;
98 xbt_dynar_foreach(simix_global->process_to_run, index, process)
99 xbt_os_sem_release(static_cast<ThreadContext*>(process->context)->begin_);
100 xbt_dynar_foreach(simix_global->process_to_run, index, process)
101 xbt_os_sem_acquire(static_cast<ThreadContext*>(process->context)->end_);
105 ThreadContext* ThreadContextFactory::self()
107 return static_cast<ThreadContext*>(xbt_os_thread_get_extra_data());
110 ThreadContext::~ThreadContext()
112 /* check if this is the context of maestro (it doesn't have a real thread) */
114 /* wait about the thread terminason */
115 xbt_os_thread_join(this->thread_, nullptr);
117 /* destroy the synchronisation objects */
118 xbt_os_sem_destroy(this->begin_);
119 xbt_os_sem_destroy(this->end_);
123 void *ThreadContext::wrapper(void *param)
125 ThreadContext* context = static_cast<ThreadContext*>(param);
128 /* Install alternate signal stack, for SIGSEGV handler. */
130 stack.ss_sp = sigsegv_stack;
131 stack.ss_size = sizeof sigsegv_stack;
133 sigaltstack(&stack, nullptr);
135 /* Tell the maestro we are starting, and wait for its green light */
136 xbt_os_sem_release(context->end_);
137 xbt_os_sem_acquire(context->begin_);
138 if (smx_ctx_thread_sem) /* parallel run */
139 xbt_os_sem_acquire(smx_ctx_thread_sem);
146 void ThreadContext::stop()
149 if (smx_ctx_thread_sem)
150 xbt_os_sem_release(smx_ctx_thread_sem);
152 // Signal to the maestro that it has finished:
153 xbt_os_sem_release(this->end_);
155 xbt_os_thread_exit(NULL);
158 void ThreadContext::suspend()
160 if (smx_ctx_thread_sem)
161 xbt_os_sem_release(smx_ctx_thread_sem);
162 xbt_os_sem_release(this->end_);
163 xbt_os_sem_acquire(this->begin_);
164 if (smx_ctx_thread_sem)
165 xbt_os_sem_acquire(smx_ctx_thread_sem);