3 /* context_thread - implementation of context switching with native threads */
5 /* Copyright (c) 2004-2008 the SimGrid team. All right reserved */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "xbt/function_types.h"
13 #include "portable.h" /* loads context system definitions */
15 #include "xbt/xbt_os_thread.h"
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(smx_context);
19 typedef struct s_smx_ctx_thread {
21 xbt_os_thread_t thread; /* a plain dumb thread (portable to posix or windows) */
22 xbt_os_sem_t begin; /* this semaphore is used to schedule/yield the process */
23 xbt_os_sem_t end; /* this semaphore is used to schedule/unschedule the process */
24 } s_smx_ctx_thread_t, *smx_ctx_thread_t;
27 smx_ctx_thread_factory_create_context(smx_process_t *smx_process, xbt_main_func_t code);
30 smx_ctx_thread_factory_create_master_context(smx_process_t * maestro);
32 static int smx_ctx_thread_factory_finalize(smx_context_factory_t * factory);
34 static void smx_ctx_thread_free(smx_process_t process);
36 static void smx_ctx_thread_kill(smx_process_t process);
38 static void smx_ctx_thread_schedule(smx_process_t process);
40 static void smx_ctx_thread_yield(void);
42 static void smx_ctx_thread_start(smx_process_t process);
44 static void smx_ctx_thread_stop(int exit_code);
46 static void smx_ctx_thread_swap(smx_process_t process);
48 static void smx_ctx_thread_schedule(smx_process_t process);
50 static void smx_ctx_thread_yield(void);
52 static void smx_ctx_thread_suspend(smx_process_t process);
54 static void smx_ctx_thread_resume(smx_process_t process);
56 static void *smx_ctx_thread_wrapper(void *param);
58 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory)
60 *factory = xbt_new0(s_smx_context_factory_t, 1);
62 (*factory)->create_context = smx_ctx_thread_factory_create_context;
63 (*factory)->finalize = smx_ctx_thread_factory_finalize;
64 (*factory)->create_maestro_context = smx_ctx_thread_factory_create_master_context;
65 (*factory)->free = smx_ctx_thread_free;
66 (*factory)->kill = smx_ctx_thread_kill;
67 (*factory)->schedule = smx_ctx_thread_schedule;
68 (*factory)->yield = smx_ctx_thread_yield;
69 (*factory)->start = smx_ctx_thread_start;
70 (*factory)->stop = smx_ctx_thread_stop;
71 (*factory)->name = "ctx_thread_factory";
75 smx_ctx_thread_factory_create_master_context(smx_process_t * maestro)
77 (*maestro)->context = (smx_context_t) xbt_new0(s_smx_ctx_thread_t, 1);
81 static int smx_ctx_thread_factory_finalize(smx_context_factory_t * factory)
89 smx_ctx_thread_factory_create_context(smx_process_t *smx_process, xbt_main_func_t code)
91 smx_ctx_thread_t context = xbt_new0(s_smx_ctx_thread_t, 1);
93 VERB1("Create context %s", (*smx_process)->name);
95 context->begin = xbt_os_sem_init(0);
96 context->end = xbt_os_sem_init(0);
98 (*smx_process)->context = (smx_context_t)context;
99 (*smx_process)->iwannadie = 0;
101 /* FIXME: Check what should return */
105 static void smx_ctx_thread_free(smx_process_t process)
107 smx_ctx_thread_t context = (smx_ctx_thread_t)process->context;
109 /* Check if this is the context of maestro (it doesn't has a real thread) */
110 if (context->thread) {
111 /* wait about the thread terminason */
112 xbt_os_thread_join(context->thread, NULL);
113 free(context->thread);
115 /* destroy the synchronisation objects */
116 xbt_os_sem_destroy(context->begin);
117 xbt_os_sem_destroy(context->end);
120 /* finally destroy the context */
124 static void smx_ctx_thread_kill(smx_process_t process)
126 DEBUG1("Kill process '%s'", process->name);
127 process->iwannadie = 1;
128 smx_ctx_thread_swap(process);
132 * \param context the winner
134 * Calling this function blocks the current context and schedule \a context.
135 * When \a context will call smx_context_yield, it will return
136 * to this function as if nothing had happened.
138 * Only the maestro can call this function to run a given process.
140 static void smx_ctx_thread_schedule(smx_process_t process)
142 DEBUG1("Schedule process '%s'", process->name);
143 xbt_assert0((simix_global->current_process == simix_global->maestro_process),
144 "You are not supposed to run this function here!");
145 smx_ctx_thread_swap(process);
149 * Calling this function makes the current context yield. The context
150 * that scheduled it returns from smx_context_schedule as if nothing
153 * Only the processes can call this function, giving back the control
156 static void smx_ctx_thread_yield(void)
158 DEBUG1("Yield process '%s'", simix_global->current_process->name);
159 xbt_assert0((simix_global->current_process != simix_global->maestro_process),
160 "You are not supposed to run this function here!");
161 smx_ctx_thread_swap(simix_global->current_process);
164 static void smx_ctx_thread_start(smx_process_t process)
166 smx_ctx_thread_t ctx_thread = (smx_ctx_thread_t) process->context;
168 DEBUG1("Start context '%s'", process->name);
169 /* create and start the process */
171 xbt_os_thread_create(process->name, smx_ctx_thread_wrapper,
174 /* wait the starting of the newly created process */
175 xbt_os_sem_acquire(ctx_thread->end);
178 static void smx_ctx_thread_stop(int exit_code)
180 /* please no debug here: our procdata was already free'd */
181 if (simix_global->current_process->cleanup_func)
182 ((*simix_global->current_process->cleanup_func)) (simix_global->current_process->cleanup_arg);
184 /* signal to the maestro that it has finished */
185 xbt_os_sem_release(((smx_ctx_thread_t) simix_global->current_process->context)->end);
188 xbt_os_thread_exit(NULL); /* We should provide return value in case other wants it */
191 static void smx_ctx_thread_swap(smx_process_t process)
194 DEBUG2("Swap context: '%s' -> '%s'", simix_global->current_process->name, process->name);
195 if ((simix_global->current_process != simix_global->maestro_process) && !process->iwannadie) {
196 /* (0) it's not the scheduler and the process doesn't want to die, it just wants to yield */
198 /* yield itself, resume the maestro */
199 smx_ctx_thread_suspend(process);
201 /* (1) the current process is the scheduler and the process doesn't want to die
202 * <-> the maestro wants to schedule the process
203 * -> the maestro schedules the process and waits
205 * (2) the current process is the scheduler and the process wants to die
206 * <-> the maestro wants to kill the process (has called the function smx_context_kill())
207 * -> the maestro schedule the process and waits (xbt_os_sem_acquire(context->end))
208 * -> if the process stops (smx_context_stop())
209 * -> the process resumes the maestro (xbt_os_sem_release(current_context->end)) and exit (xbt_os_thread_exit())
210 * -> else the process call smx_context_yield()
213 * (3) the current process is not the scheduler and the process wants to die
214 * -> (3.1) if the current process is the process who wants to die
215 * -> (resume not need) goto (4)
216 * -> (3.2) else the current process is not the process who wants to die
217 * <-> the current process wants to kill an other process
218 * -> the current process resumes the process to die and waits
219 * -> if the process to kill stops
220 * -> it resumes the process who kill it and exit
221 * -> else if the process to kill calls to smx_context_yield()
224 /* schedule the process associated with this context */
225 smx_ctx_thread_resume(process);
229 /* (4) the current process wants to die */
230 if (simix_global->current_process->iwannadie)
231 smx_ctx_thread_stop(1);
234 static void *smx_ctx_thread_wrapper(void *param)
236 smx_ctx_thread_t context = (smx_ctx_thread_t) param;
238 /* Tell the maestro we are starting, and wait for its green light */
239 xbt_os_sem_release(context->end);
240 xbt_os_sem_acquire(context->begin);
242 smx_ctx_thread_stop((context->code) (simix_global->current_process->argc, simix_global->current_process->argv));
246 static void smx_ctx_thread_suspend(smx_process_t process)
248 /* save the current process */
249 smx_process_t self = simix_global->current_process;
251 DEBUG1("Suspend context '%s'", process->name);
253 /* update the current process to this process */
254 simix_global->current_process = process;
256 xbt_os_sem_release(((smx_ctx_thread_t) process->context)->end);
257 xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->begin);
259 /* restore the current process to the previously saved process */
260 simix_global->current_process = self;
263 static void smx_ctx_thread_resume(smx_process_t process)
265 /* save the current process */
266 smx_process_t self = simix_global->current_process;
268 DEBUG1("Resume context '%s'", process->name);
270 /* update the current process */
271 simix_global->current_process = process;
273 xbt_os_sem_release(((smx_ctx_thread_t) process->context)->begin);
274 xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->end);
276 /* restore the current process to the previously saved process */
277 simix_global->current_process = self;