1 /* context_thread - implementation of context switching with native threads */
3 /* Copyright (c) 2009-2015. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "xbt/function_types.h"
10 #include "smx_private.h"
11 #include "src/portable.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 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
18 typedef struct s_smx_ctx_thread {
19 s_smx_ctx_base_t super; /* Fields of super implementation */
20 xbt_os_thread_t thread; /* a plain dumb thread (portable to posix or windows) */
21 xbt_os_sem_t begin; /* this semaphore is used to schedule/yield the process */
22 xbt_os_sem_t end; /* this semaphore is used to schedule/unschedule the process */
23 } s_smx_ctx_thread_t, *smx_ctx_thread_t;
25 static xbt_os_sem_t smx_ctx_thread_sem;
28 smx_ctx_thread_factory_create_context(xbt_main_func_t code,
29 int argc, char **argv,
30 void_pfn_smxprocess_t cleanup_func,
31 smx_process_t process);
33 static void smx_ctx_thread_free(smx_context_t context);
34 static void smx_ctx_thread_stop(smx_context_t context);
35 static void smx_ctx_thread_suspend(smx_context_t context);
36 static void smx_ctx_thread_runall_serial(void);
37 static void smx_ctx_thread_runall_parallel(void);
38 static smx_context_t smx_ctx_thread_self(void);
40 static int smx_ctx_thread_factory_finalize(smx_context_factory_t *factory);
41 static void *smx_ctx_thread_wrapper(void *param);
43 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory)
45 smx_ctx_base_factory_init(factory);
46 XBT_VERB("Activating thread context factory");
48 (*factory)->finalize = smx_ctx_thread_factory_finalize;
49 (*factory)->create_context = smx_ctx_thread_factory_create_context;
50 /* Do not overload that method (*factory)->finalize */
51 (*factory)->free = smx_ctx_thread_free;
52 (*factory)->stop = smx_ctx_thread_stop;
53 (*factory)->suspend = smx_ctx_thread_suspend;
55 if (SIMIX_context_is_parallel())
56 (*factory)->runall = smx_ctx_thread_runall_parallel;
58 (*factory)->runall = smx_ctx_thread_runall_serial;
60 (*factory)->self = smx_ctx_thread_self;
61 (*factory)->name = "ctx_thread_factory";
63 if (SIMIX_context_is_parallel()) {
64 smx_ctx_thread_sem = xbt_os_sem_init(SIMIX_context_get_nthreads());
66 smx_ctx_thread_sem = NULL;
70 static int smx_ctx_thread_factory_finalize(smx_context_factory_t *factory)
72 if (smx_ctx_thread_sem) {
73 xbt_os_sem_destroy(smx_ctx_thread_sem);
74 smx_ctx_thread_sem = NULL;
76 return smx_ctx_base_factory_finalize(factory);
80 smx_ctx_thread_factory_create_context(xbt_main_func_t code, int argc,
82 void_pfn_smxprocess_t cleanup_func,
83 smx_process_t process)
85 smx_ctx_thread_t context = (smx_ctx_thread_t)
86 smx_ctx_base_factory_create_context_sized(sizeof(s_smx_ctx_thread_t),
88 cleanup_func, process);
90 /* If the user provided a function for the process then use it
91 otherwise is the context for maestro */
93 context->begin = xbt_os_sem_init(0);
94 context->end = xbt_os_sem_init(0);
95 if (smx_context_stack_size_was_set)
96 xbt_os_thread_setstacksize(smx_context_stack_size);
97 if (smx_context_guard_size_was_set)
98 xbt_os_thread_setguardsize(smx_context_guard_size);
100 /* create and start the process */
101 /* NOTE: The first argument to xbt_os_thread_create used to be the process *
102 * name, but now the name is stored at SIMIX level, so we pass a null */
104 xbt_os_thread_create(NULL, smx_ctx_thread_wrapper, context, context);
107 /* wait the starting of the newly created process */
108 xbt_os_sem_acquire(context->end);
111 xbt_os_thread_set_extra_data(context);
114 return (smx_context_t) context;
117 static void smx_ctx_thread_free(smx_context_t pcontext)
119 smx_ctx_thread_t context = (smx_ctx_thread_t) pcontext;
121 /* check if this is the context of maestro (it doesn't have a real thread) */
122 if (context->thread) {
123 /* wait about the thread terminason */
124 xbt_os_thread_join(context->thread, NULL);
126 /* destroy the synchronisation objects */
127 xbt_os_sem_destroy(context->begin);
128 xbt_os_sem_destroy(context->end);
131 smx_ctx_base_free(pcontext);
134 static void smx_ctx_thread_stop(smx_context_t pcontext)
136 smx_ctx_thread_t context = (smx_ctx_thread_t) pcontext;
138 /* please no debug here: our procdata was already free'd */
139 smx_ctx_base_stop(pcontext);
141 if (smx_ctx_thread_sem) /* parallel run */
142 xbt_os_sem_release(smx_ctx_thread_sem);
144 /* signal to the maestro that it has finished */
145 xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
148 /* We should provide return value in case other wants it */
149 xbt_os_thread_exit(NULL);
152 static void *smx_ctx_thread_wrapper(void *param)
154 smx_ctx_thread_t context = (smx_ctx_thread_t) param;
156 /* Install alternate signal stack, for SIGSEGV handler. */
158 stack.ss_sp = sigsegv_stack;
159 stack.ss_size = sizeof sigsegv_stack;
161 sigaltstack(&stack, NULL);
163 /* Tell the maestro we are starting, and wait for its green light */
164 xbt_os_sem_release(context->end);
165 xbt_os_sem_acquire(context->begin);
166 if (smx_ctx_thread_sem) /* parallel run */
167 xbt_os_sem_acquire(smx_ctx_thread_sem);
169 (context->super.code) (context->super.argc, context->super.argv);
171 smx_ctx_thread_stop((smx_context_t) context);
175 static void smx_ctx_thread_suspend(smx_context_t context)
177 if (smx_ctx_thread_sem) /* parallel run */
178 xbt_os_sem_release(smx_ctx_thread_sem);
179 xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
180 xbt_os_sem_acquire(((smx_ctx_thread_t) context)->begin);
181 if (smx_ctx_thread_sem) /* parallel run */
182 xbt_os_sem_acquire(smx_ctx_thread_sem);
185 static void smx_ctx_thread_runall_serial(void)
187 smx_process_t process;
190 xbt_dynar_foreach(simix_global->process_to_run, cursor, process) {
191 XBT_DEBUG("Handling %p",process);
192 xbt_os_sem_release(((smx_ctx_thread_t) process->context)->begin);
193 xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->end);
197 static void smx_ctx_thread_runall_parallel(void)
200 smx_process_t process;
202 xbt_dynar_foreach(simix_global->process_to_run, index, process)
203 xbt_os_sem_release(((smx_ctx_thread_t) process->context)->begin);
205 xbt_dynar_foreach(simix_global->process_to_run, index, process) {
206 xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->end);
210 static smx_context_t smx_ctx_thread_self(void)
212 return (smx_context_t) xbt_os_thread_get_extra_data();