2 #include "xbt/function_types.h"
\r
4 static xbt_context_t
\r
5 xbt_thread_context_factory_create_context(const char* name, xbt_main_func_t code, void_f_pvoid_t startup_func, void* startup_arg, void_f_pvoid_t cleanup_func, void* cleanup_arg, int argc, char** argv);
\r
9 xbt_thread_context_factory_create_master_context(xbt_context_t* maestro);
\r
12 xbt_thread_context_factory_finalize(xbt_context_factory_t* factory);
\r
15 xbt_thread_context_free(xbt_context_t context);
\r
18 xbt_thread_context_kill(xbt_context_t context);
\r
21 xbt_thread_context_schedule(xbt_context_t context);
\r
24 xbt_thread_context_yield(void);
\r
27 xbt_thread_context_start(xbt_context_t context);
\r
30 xbt_thread_context_stop(int exit_code);
\r
33 xbt_thread_context_swap(xbt_context_t context);
\r
36 xbt_thread_context_schedule(xbt_context_t context);
\r
39 xbt_thread_context_yield(void);
\r
42 xbt_thread_context_suspend(xbt_context_t context);
\r
45 xbt_thread_context_resume(xbt_context_t context);
\r
48 xbt_thread_context_wrapper(void* param);
\r
51 xbt_thread_context_factory_init(xbt_context_factory_t* factory)
\r
53 *factory = xbt_new0(s_xbt_context_factory_t,1);
\r
55 (*factory)->create_context = xbt_thread_context_factory_create_context;
\r
56 (*factory)->finalize = xbt_thread_context_factory_finalize;
\r
57 (*factory)->create_maestro_context = xbt_thread_context_factory_create_master_context;
\r
58 (*factory)->name = "thread_context_factory";
\r
64 xbt_thread_context_factory_create_master_context(xbt_context_t* maestro)
\r
66 *maestro = (xbt_context_t)xbt_new0(s_xbt_thread_context_t, 1);
\r
71 xbt_thread_context_factory_finalize(xbt_context_factory_t* factory)
\r
78 static xbt_context_t
\r
79 xbt_thread_context_factory_create_context(const char* name, xbt_main_func_t code, void_f_pvoid_t startup_func, void* startup_arg, void_f_pvoid_t cleanup_func, void* cleanup_arg, int argc, char** argv)
\r
81 xbt_thread_context_t context = xbt_new0(s_xbt_thread_context_t, 1);
\r
83 context->code = code;
\r
84 context->name = xbt_strdup(name);
\r
85 context->begin = xbt_os_sem_init(0);
\r
86 context->end = xbt_os_sem_init(0);
\r
87 context->iwannadie = 0; /* useless but makes valgrind happy */
\r
88 context->argc = argc;
\r
89 context->argv = argv;
\r
90 context->startup_func = startup_func;
\r
91 context->startup_arg = startup_arg;
\r
92 context->cleanup_func = cleanup_func;
\r
93 context->cleanup_arg = cleanup_arg;
\r
95 context->free = xbt_thread_context_free;
\r
96 context->kill = xbt_thread_context_kill;
\r
97 context->schedule = xbt_thread_context_schedule;
\r
98 context->yield = xbt_thread_context_yield;
\r
99 context->start = xbt_thread_context_start;
\r
100 context->stop = xbt_thread_context_stop;
\r
102 return (xbt_context_t)context;
\r
106 xbt_thread_context_free(xbt_context_t context)
\r
110 xbt_thread_context_t thread_context = (xbt_thread_context_t)context;
\r
112 free(thread_context->name);
\r
114 if(thread_context->argv)
\r
118 for(i = 0; i < thread_context->argc; i++)
\r
119 if(thread_context->argv[i])
\r
120 free(thread_context->argv[i]);
\r
122 free(thread_context->argv);
\r
125 /* wait about the thread terminason */
\r
126 xbt_os_thread_join(thread_context->thread, NULL);
\r
128 /* destroy the synchronisation objects */
\r
129 xbt_os_sem_destroy(thread_context->begin);
\r
130 xbt_os_sem_destroy(thread_context->end);
\r
132 /* finally destroy the context */
\r
138 xbt_thread_context_kill(xbt_context_t context)
\r
140 context->iwannadie = 1;
\r
141 xbt_thread_context_swap(context);
\r
145 * \param context the winner
\r
147 * Calling this function blocks the current context and schedule \a context.
\r
148 * When \a context will call xbt_context_yield, it will return
\r
149 * to this function as if nothing had happened.
\r
151 * Only the maestro can call this function to run a given process.
\r
154 xbt_thread_context_schedule(xbt_context_t context)
\r
156 xbt_assert0((current_context == maestro_context),"You are not supposed to run this function here!");
\r
157 xbt_thread_context_swap(context);
\r
161 * Calling this function makes the current context yield. The context
\r
162 * that scheduled it returns from xbt_context_schedule as if nothing
\r
165 * Only the processes can call this function, giving back the control
\r
169 xbt_thread_context_yield(void)
\r
171 xbt_assert0((current_context != maestro_context),"You are not supposed to run this function here!");
\r
172 xbt_thread_context_swap(current_context);
\r
176 xbt_thread_context_start(xbt_context_t context)
\r
178 xbt_thread_context_t thread_context = (xbt_thread_context_t)context;
\r
180 /* create and start the process */
\r
181 thread_context->thread = xbt_os_thread_create(thread_context->name,xbt_thread_context_wrapper,thread_context);
\r
183 /* wait the starting of the newly created process */
\r
184 xbt_os_sem_acquire(thread_context->end);
\r
188 xbt_thread_context_stop(int exit_code)
\r
190 if(current_context->cleanup_func)
\r
191 ((*current_context->cleanup_func))(current_context->cleanup_arg);
\r
193 xbt_swag_remove(current_context, context_living);
\r
194 xbt_swag_insert(current_context, context_to_destroy);
\r
196 /* signal to the maestro that it has finished */
\r
197 xbt_os_sem_release(((xbt_thread_context_t)current_context)->end);
\r
200 xbt_os_thread_exit(NULL); /* We should provide return value in case other wants it */
\r
204 xbt_thread_context_swap(xbt_context_t context)
\r
206 if((current_context != maestro_context) && !context->iwannadie)
\r
208 /* (0) it's not the scheduler and the process doesn't want to die, it just wants to yield */
\r
210 /* yield itself, resume the maestro */
\r
211 xbt_thread_context_suspend(context);
\r
215 /* (1) the current process is the scheduler and the process doesn't want to die
\r
216 * <-> the maestro wants to schedule the process
\r
217 * -> the maestro schedules the process and waits
\r
219 * (2) the current process is the scheduler and the process wants to die
\r
220 * <-> the maestro wants to kill the process (has called the function xbt_context_kill())
\r
221 * -> the maestro schedule the process and waits (xbt_os_sem_acquire(context->end))
\r
222 * -> if the process stops (xbt_context_stop())
\r
223 * -> the process resumes the maestro (xbt_os_sem_release(current_context->end)) and exit (xbt_os_thread_exit())
\r
224 * -> else the process call xbt_context_yield()
\r
227 * (3) the current process is not the scheduler and the process wants to die
\r
228 * -> (3.1) if the current process is the process who wants to die
\r
229 * -> (resume not need) goto (4)
\r
230 * -> (3.2) else the current process is not the process who wants to die
\r
231 * <-> the current process wants to kill an other process
\r
232 * -> the current process resumes the process to die and waits
\r
233 * -> if the process to kill stops
\r
234 * -> it resumes the process who kill it and exit
\r
235 * -> else if the process to kill calls to xbt_context_yield()
\r
238 /* schedule the process associated with this context */
\r
239 xbt_thread_context_resume(context);
\r
243 /* (4) the current process wants to die */
\r
244 if(current_context->iwannadie)
\r
245 xbt_thread_context_stop(1);
\r
249 xbt_thread_context_wrapper(void* param)
\r
251 xbt_thread_context_t context = (xbt_thread_context_t)param;
\r
253 /* signal its starting to the maestro and wait to start its job*/
\r
254 xbt_os_sem_release(context->end);
\r
255 xbt_os_sem_acquire(context->begin);
\r
257 if (context->startup_func)
\r
258 (*(context->startup_func))(context->startup_arg);
\r
261 xbt_thread_context_stop((context->code) (context->argc, context->argv));
\r
266 xbt_thread_context_suspend(xbt_context_t context)
\r
268 /* save the current context */
\r
269 xbt_context_t self = current_context;
\r
271 /* update the current context to this context */
\r
272 current_context = context;
\r
274 xbt_os_sem_release(((xbt_thread_context_t)context)->end);
\r
275 xbt_os_sem_acquire(((xbt_thread_context_t)context)->begin);
\r
277 /* restore the current context to the previously saved context */
\r
278 current_context = self;
\r
282 xbt_thread_context_resume(xbt_context_t context)
\r
284 /* save the current context */
\r
285 xbt_context_t self = current_context;
\r
287 /* update the current context */
\r
288 current_context = context;
\r
290 xbt_os_sem_release(((xbt_thread_context_t)context)->begin);
\r
291 xbt_os_sem_acquire(((xbt_thread_context_t)context)->end);
\r
293 /* restore the current context to the previously saved context */
\r
294 current_context = self;
\r