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"
11 #include "xbt/xbt_context_private.h"
13 #include "portable.h" /* loads context system definitions */
15 #include "xbt/xbt_os_thread.h"
18 typedef struct s_xbt_ctx_thread {
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_xbt_ctx_thread_t, *xbt_ctx_thread_t;
26 xbt_ctx_thread_factory_create_context(const char *name, xbt_main_func_t code,
27 void_f_pvoid_t startup_func,
29 void_f_pvoid_t cleanup_func,
30 void *cleanup_arg, int argc,
35 xbt_ctx_thread_factory_create_master_context(xbt_context_t * maestro);
37 static int xbt_ctx_thread_factory_finalize(xbt_context_factory_t * factory);
39 static void xbt_ctx_thread_free(xbt_context_t context);
41 static void xbt_ctx_thread_kill(xbt_context_t context);
43 static void xbt_ctx_thread_schedule(xbt_context_t context);
45 static void xbt_ctx_thread_yield(void);
47 static void xbt_ctx_thread_start(xbt_context_t context);
49 static void xbt_ctx_thread_stop(int exit_code);
51 static void xbt_ctx_thread_swap(xbt_context_t context);
53 static void xbt_ctx_thread_schedule(xbt_context_t context);
55 static void xbt_ctx_thread_yield(void);
57 static void xbt_ctx_thread_suspend(xbt_context_t context);
59 static void xbt_ctx_thread_resume(xbt_context_t context);
61 static void *xbt_ctx_thread_wrapper(void *param);
63 void xbt_ctx_thread_factory_init(xbt_context_factory_t * factory)
65 *factory = xbt_new0(s_xbt_context_factory_t, 1);
67 (*factory)->create_context = xbt_ctx_thread_factory_create_context;
68 (*factory)->finalize = xbt_ctx_thread_factory_finalize;
69 (*factory)->create_maestro_context =
70 xbt_ctx_thread_factory_create_master_context;
71 (*factory)->name = "ctx_thread_factory";
75 xbt_ctx_thread_factory_create_master_context(xbt_context_t * maestro)
77 *maestro = (xbt_context_t) xbt_new0(s_xbt_ctx_thread_t, 1);
81 static int xbt_ctx_thread_factory_finalize(xbt_context_factory_t * factory)
89 xbt_ctx_thread_factory_create_context(const char *name, xbt_main_func_t code,
90 void_f_pvoid_t startup_func,
92 void_f_pvoid_t cleanup_func,
93 void *cleanup_arg, int argc,
96 xbt_ctx_thread_t context = xbt_new0(s_xbt_ctx_thread_t, 1);
99 context->name = xbt_strdup(name);
100 context->begin = xbt_os_sem_init(0);
101 context->end = xbt_os_sem_init(0);
102 context->iwannadie = 0; /* useless but makes valgrind happy */
103 context->argc = argc;
104 context->argv = argv;
105 context->startup_func = startup_func;
106 context->startup_arg = startup_arg;
107 context->cleanup_func = cleanup_func;
108 context->cleanup_arg = cleanup_arg;
110 context->free = xbt_ctx_thread_free;
111 context->kill = xbt_ctx_thread_kill;
112 context->schedule = xbt_ctx_thread_schedule;
113 context->yield = xbt_ctx_thread_yield;
114 context->start = xbt_ctx_thread_start;
115 context->stop = xbt_ctx_thread_stop;
117 return (xbt_context_t) context;
120 static void xbt_ctx_thread_free(xbt_context_t context)
123 xbt_ctx_thread_t ctx_thread = (xbt_ctx_thread_t) context;
125 free(ctx_thread->name);
127 if (ctx_thread->argv) {
130 for (i = 0; i < ctx_thread->argc; i++)
131 if (ctx_thread->argv[i])
132 free(ctx_thread->argv[i]);
134 free(ctx_thread->argv);
137 /* wait about the thread terminason */
138 xbt_os_thread_join(ctx_thread->thread, NULL);
140 /* destroy the synchronisation objects */
141 xbt_os_sem_destroy(ctx_thread->begin);
142 xbt_os_sem_destroy(ctx_thread->end);
144 /* finally destroy the context */
149 static void xbt_ctx_thread_kill(xbt_context_t context)
151 context->iwannadie = 1;
152 xbt_ctx_thread_swap(context);
156 * \param context the winner
158 * Calling this function blocks the current context and schedule \a context.
159 * When \a context will call xbt_context_yield, it will return
160 * to this function as if nothing had happened.
162 * Only the maestro can call this function to run a given process.
164 static void xbt_ctx_thread_schedule(xbt_context_t context)
166 xbt_assert0((current_context == maestro_context),
167 "You are not supposed to run this function here!");
168 xbt_ctx_thread_swap(context);
172 * Calling this function makes the current context yield. The context
173 * that scheduled it returns from xbt_context_schedule as if nothing
176 * Only the processes can call this function, giving back the control
179 static void xbt_ctx_thread_yield(void)
181 xbt_assert0((current_context != maestro_context),
182 "You are not supposed to run this function here!");
183 xbt_ctx_thread_swap(current_context);
186 static void xbt_ctx_thread_start(xbt_context_t context)
188 xbt_ctx_thread_t ctx_thread = (xbt_ctx_thread_t) context;
190 /* create and start the process */
192 xbt_os_thread_create(ctx_thread->name, xbt_ctx_thread_wrapper,
195 /* wait the starting of the newly created process */
196 xbt_os_sem_acquire(ctx_thread->end);
199 static void xbt_ctx_thread_stop(int exit_code)
201 if (current_context->cleanup_func)
202 ((*current_context->cleanup_func)) (current_context->cleanup_arg);
204 xbt_swag_remove(current_context, context_living);
205 xbt_swag_insert(current_context, context_to_destroy);
207 /* signal to the maestro that it has finished */
208 xbt_os_sem_release(((xbt_ctx_thread_t) current_context)->end);
211 xbt_os_thread_exit(NULL); /* We should provide return value in case other wants it */
214 static void xbt_ctx_thread_swap(xbt_context_t context)
216 if ((current_context != maestro_context) && !context->iwannadie) {
217 /* (0) it's not the scheduler and the process doesn't want to die, it just wants to yield */
219 /* yield itself, resume the maestro */
220 xbt_ctx_thread_suspend(context);
222 /* (1) the current process is the scheduler and the process doesn't want to die
223 * <-> the maestro wants to schedule the process
224 * -> the maestro schedules the process and waits
226 * (2) the current process is the scheduler and the process wants to die
227 * <-> the maestro wants to kill the process (has called the function xbt_context_kill())
228 * -> the maestro schedule the process and waits (xbt_os_sem_acquire(context->end))
229 * -> if the process stops (xbt_context_stop())
230 * -> the process resumes the maestro (xbt_os_sem_release(current_context->end)) and exit (xbt_os_thread_exit())
231 * -> else the process call xbt_context_yield()
234 * (3) the current process is not the scheduler and the process wants to die
235 * -> (3.1) if the current process is the process who wants to die
236 * -> (resume not need) goto (4)
237 * -> (3.2) else the current process is not the process who wants to die
238 * <-> the current process wants to kill an other process
239 * -> the current process resumes the process to die and waits
240 * -> if the process to kill stops
241 * -> it resumes the process who kill it and exit
242 * -> else if the process to kill calls to xbt_context_yield()
245 /* schedule the process associated with this context */
246 xbt_ctx_thread_resume(context);
250 /* (4) the current process wants to die */
251 if (current_context->iwannadie)
252 xbt_ctx_thread_stop(1);
255 static void *xbt_ctx_thread_wrapper(void *param)
257 xbt_ctx_thread_t context = (xbt_ctx_thread_t) param;
259 /* Tell the maestro we are starting, and wait for its green light */
260 xbt_os_sem_release(context->end);
261 xbt_os_sem_acquire(context->begin);
263 if (context->startup_func)
264 (*(context->startup_func)) (context->startup_arg);
267 xbt_ctx_thread_stop((context->code) (context->argc, context->argv));
271 static void xbt_ctx_thread_suspend(xbt_context_t context)
273 /* save the current context */
274 xbt_context_t self = current_context;
276 /* update the current context to this context */
277 current_context = context;
279 xbt_os_sem_release(((xbt_ctx_thread_t) context)->end);
280 xbt_os_sem_acquire(((xbt_ctx_thread_t) context)->begin);
282 /* restore the current context to the previously saved context */
283 current_context = self;
286 static void xbt_ctx_thread_resume(xbt_context_t context)
288 /* save the current context */
289 xbt_context_t self = current_context;
291 /* update the current context */
292 current_context = context;
294 xbt_os_sem_release(((xbt_ctx_thread_t) context)->begin);
295 xbt_os_sem_acquire(((xbt_ctx_thread_t) context)->end);
297 /* restore the current context to the previously saved context */
298 current_context = self;