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"
16 #include "xbt_modinter.h" /* prototype of os thread module's init/exit in XBT */
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
20 typedef struct s_smx_ctx_thread {
22 xbt_os_thread_t thread; /* a plain dumb thread (portable to posix or windows) */
23 xbt_os_sem_t begin; /* this semaphore is used to schedule/yield the process */
24 xbt_os_sem_t end; /* this semaphore is used to schedule/unschedule the process */
25 } s_smx_ctx_thread_t, *smx_ctx_thread_t;
28 smx_ctx_thread_factory_create_context(xbt_main_func_t code, int argc, char** argv,
29 void_f_pvoid_t cleanup_func, void* cleanup_arg);
31 static int smx_ctx_thread_factory_finalize(smx_context_factory_t * factory);
33 static void smx_ctx_thread_free(smx_context_t context);
35 static void smx_ctx_thread_start(smx_context_t context);
37 static void smx_ctx_thread_stop(smx_context_t context);
39 static void smx_ctx_thread_suspend(smx_context_t context);
42 smx_ctx_thread_resume(smx_context_t old_context, smx_context_t new_context);
44 static void *smx_ctx_thread_wrapper(void *param);
46 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory)
48 *factory = xbt_new0(s_smx_context_factory_t, 1);
50 (*factory)->create_context = smx_ctx_thread_factory_create_context;
51 (*factory)->finalize = smx_ctx_thread_factory_finalize;
52 (*factory)->free = smx_ctx_thread_free;
53 (*factory)->start = smx_ctx_thread_start;
54 (*factory)->stop = smx_ctx_thread_stop;
55 (*factory)->suspend = smx_ctx_thread_suspend;
56 (*factory)->resume = smx_ctx_thread_resume;
57 (*factory)->name = "ctx_thread_factory";
60 static int smx_ctx_thread_factory_finalize(smx_context_factory_t * factory)
68 smx_ctx_thread_factory_create_context(xbt_main_func_t code, int argc, char** argv,
69 void_f_pvoid_t cleanup_func, void* cleanup_arg)
71 smx_ctx_thread_t context = xbt_new0(s_smx_ctx_thread_t, 1);
73 /* If the user provided a function for the process then use it
74 otherwise is the context for maestro */
79 context->cleanup_func = cleanup_func;
80 context->cleanup_arg = cleanup_arg;
81 context->begin = xbt_os_sem_init(0);
82 context->end = xbt_os_sem_init(0);
85 return (smx_context_t)context;
88 static void smx_ctx_thread_free(smx_context_t pcontext)
91 smx_ctx_thread_t context = (smx_ctx_thread_t)pcontext;
93 /* check if this is the context of maestro (it doesn't has a real thread) */
94 if (context->thread) {
95 /* wait about the thread terminason */
96 xbt_os_thread_join(context->thread, NULL);
98 /* destroy the synchronisation objects */
99 xbt_os_sem_destroy(context->begin);
100 xbt_os_sem_destroy(context->end);
105 for (i = 0; i < context->argc; i++)
106 if (context->argv[i])
107 free(context->argv[i]);
112 /* finally destroy the context */
116 static void smx_ctx_thread_start(smx_context_t context)
118 smx_ctx_thread_t ctx_thread = (smx_ctx_thread_t)context;
120 /* create and start the process */
121 /* NOTE: The first argument to xbt_os_thread_create used to be the process *
122 * name, but now the name is stored at SIMIX level, so we pass a null */
124 xbt_os_thread_create(NULL, smx_ctx_thread_wrapper, ctx_thread);
126 /* wait the starting of the newly created process */
127 xbt_os_sem_acquire(ctx_thread->end);
130 static void smx_ctx_thread_stop(smx_context_t pcontext)
133 smx_ctx_thread_t context = (smx_ctx_thread_t)pcontext;
135 /* please no debug here: our procdata was already free'd */
136 if (context->cleanup_func)
137 (*context->cleanup_func) (context->cleanup_arg);
139 /* signal to the maestro that it has finished */
140 xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
143 /* We should provide return value in case other wants it */
144 xbt_os_thread_exit(NULL);
147 static void *smx_ctx_thread_wrapper(void *param)
149 smx_ctx_thread_t context = (smx_ctx_thread_t) param;
151 /* Tell the maestro we are starting, and wait for its green light */
152 xbt_os_sem_release(context->end);
153 xbt_os_sem_acquire(context->begin);
155 (context->code) (context->argc, context->argv);
157 smx_ctx_thread_stop((smx_context_t)context);
161 static void smx_ctx_thread_suspend(smx_context_t context)
163 xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
164 xbt_os_sem_acquire(((smx_ctx_thread_t) context)->begin);
167 static void smx_ctx_thread_resume(smx_context_t not_used,
168 smx_context_t new_context)
170 xbt_os_sem_release(((smx_ctx_thread_t) new_context)->begin);
171 xbt_os_sem_acquire(((smx_ctx_thread_t) new_context)->end);