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 */
17 #include "simix/smx_context_private.h"
19 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
21 typedef struct s_smx_ctx_thread {
22 s_smx_ctx_base_t super; /* Fields of super implementation */
23 xbt_os_thread_t thread; /* a plain dumb thread (portable to posix or windows) */
24 xbt_os_sem_t begin; /* this semaphore is used to schedule/yield the process */
25 xbt_os_sem_t end; /* this semaphore is used to schedule/unschedule the process */
26 } s_smx_ctx_thread_t, *smx_ctx_thread_t;
29 smx_ctx_thread_factory_create_context(xbt_main_func_t code, int argc, char** argv,
30 void_f_pvoid_t cleanup_func, void* cleanup_arg);
32 static void smx_ctx_thread_free(smx_context_t context);
33 static void smx_ctx_thread_stop(smx_context_t context);
34 static void smx_ctx_thread_suspend(smx_context_t context);
35 static void smx_ctx_thread_resume(smx_context_t new_context);
37 static void *smx_ctx_thread_wrapper(void *param);
39 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory) {
41 smx_ctx_base_factory_init(factory);
43 (*factory)->create_context = smx_ctx_thread_factory_create_context;
44 /* Do not overload that method (*factory)->finalize */
45 (*factory)->free = smx_ctx_thread_free;
46 (*factory)->stop = smx_ctx_thread_stop;
47 (*factory)->suspend = smx_ctx_thread_suspend;
48 (*factory)->resume = smx_ctx_thread_resume;
49 (*factory)->name = "ctx_thread_factory";
53 smx_ctx_thread_factory_create_context(xbt_main_func_t code, int argc, char** argv,
54 void_f_pvoid_t cleanup_func, void* cleanup_arg)
56 smx_ctx_thread_t context = (smx_ctx_thread_t)smx_ctx_base_factory_create_context_sized
57 (sizeof(s_smx_ctx_thread_t), code,argc,argv,cleanup_func,cleanup_arg);
59 /* If the user provided a function for the process then use it
60 otherwise is the context for maestro */
62 context->begin = xbt_os_sem_init(0);
63 context->end = xbt_os_sem_init(0);
66 /* create and start the process */
67 /* NOTE: The first argument to xbt_os_thread_create used to be the process *
68 * name, but now the name is stored at SIMIX level, so we pass a null */
70 xbt_os_thread_create(NULL, smx_ctx_thread_wrapper, context);
72 /* wait the starting of the newly created process */
73 xbt_os_sem_acquire(context->end);
76 return (smx_context_t)context;
79 static void smx_ctx_thread_free(smx_context_t pcontext)
81 smx_ctx_thread_t context = (smx_ctx_thread_t)pcontext;
83 /* check if this is the context of maestro (it doesn't has a real thread) */
84 if (context->thread) {
85 /* wait about the thread terminason */
86 xbt_os_thread_join(context->thread, NULL);
88 /* destroy the synchronisation objects */
89 xbt_os_sem_destroy(context->begin);
90 xbt_os_sem_destroy(context->end);
93 smx_ctx_base_free(pcontext);
96 static void smx_ctx_thread_stop(smx_context_t pcontext)
99 smx_ctx_thread_t context = (smx_ctx_thread_t)pcontext;
101 /* please no debug here: our procdata was already free'd */
102 smx_ctx_base_stop(pcontext);
104 /* signal to the maestro that it has finished */
105 xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
108 /* We should provide return value in case other wants it */
109 xbt_os_thread_exit(NULL);
112 static void *smx_ctx_thread_wrapper(void *param)
114 smx_ctx_thread_t context = (smx_ctx_thread_t) param;
116 /* Tell the maestro we are starting, and wait for its green light */
117 xbt_os_sem_release(context->end);
118 xbt_os_sem_acquire(context->begin);
120 (context->super.code) (context->super.argc, context->super.argv);
122 smx_ctx_thread_stop((smx_context_t)context);
126 static void smx_ctx_thread_suspend(smx_context_t context) {
127 xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
128 xbt_os_sem_acquire(((smx_ctx_thread_t) context)->begin);
131 static void smx_ctx_thread_resume(smx_context_t new_context) {
132 xbt_os_sem_release(((smx_ctx_thread_t) new_context)->begin);
133 xbt_os_sem_acquire(((smx_ctx_thread_t) new_context)->end);