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_resume(process);
146 /*smx_ctx_thread_swap(process);*/
150 * Calling this function makes the current context yield. The context
151 * that scheduled it returns from smx_context_schedule as if nothing
154 * Only the processes can call this function, giving back the control
157 static void smx_ctx_thread_yield(void)
159 smx_ctx_thread_suspend(simix_global->current_process);
162 static void smx_ctx_thread_start(smx_process_t process)
164 smx_ctx_thread_t ctx_thread = (smx_ctx_thread_t) process->context;
166 DEBUG1("Start context '%s'", process->name);
167 /* create and start the process */
169 xbt_os_thread_create(process->name, smx_ctx_thread_wrapper,
172 /* wait the starting of the newly created process */
173 xbt_os_sem_acquire(ctx_thread->end);
176 static void smx_ctx_thread_stop(int exit_code)
178 /* please no debug here: our procdata was already free'd */
179 if (simix_global->current_process->cleanup_func)
180 ((*simix_global->current_process->cleanup_func)) (simix_global->current_process->cleanup_arg);
182 /* signal to the maestro that it has finished */
183 xbt_os_sem_release(((smx_ctx_thread_t) simix_global->current_process->context)->end);
186 xbt_os_thread_exit(NULL); /* We should provide return value in case other wants it */
189 /*FIXME: erase this function*/
190 static void smx_ctx_thread_swap(smx_process_t process)
195 static void *smx_ctx_thread_wrapper(void *param)
197 smx_ctx_thread_t context = (smx_ctx_thread_t) param;
199 /* Tell the maestro we are starting, and wait for its green light */
200 xbt_os_sem_release(context->end);
201 xbt_os_sem_acquire(context->begin);
203 smx_ctx_thread_stop((context->code) (simix_global->current_process->argc,
204 simix_global->current_process->argv));
208 static void smx_ctx_thread_suspend(smx_process_t process)
210 DEBUG1("Suspend context '%s'", process->name);
211 xbt_os_sem_release(((smx_ctx_thread_t) process->context)->end);
212 xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->begin);
215 static void smx_ctx_thread_resume(smx_process_t process)
217 DEBUG1("Resume context '%s'", process->name);
218 xbt_os_sem_release(((smx_ctx_thread_t) process->context)->begin);
219 xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->end);