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;
26 static smx_context_t smx_ctx_thread_factory_create_context(xbt_main_func_t code);
28 static smx_context_t smx_ctx_thread_factory_create_master_context(void);
30 static int smx_ctx_thread_factory_finalize(smx_context_factory_t * factory);
32 static void smx_ctx_thread_free(smx_context_t context);
34 static void smx_ctx_thread_start(smx_context_t context);
36 static void smx_ctx_thread_stop(int exit_code);
38 static void smx_ctx_thread_suspend(smx_context_t context);
40 static void smx_ctx_thread_resume(smx_context_t old_context,
41 smx_context_t new_context);
43 static void *smx_ctx_thread_wrapper(void *param);
45 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory)
47 *factory = xbt_new0(s_smx_context_factory_t, 1);
49 (*factory)->create_context = smx_ctx_thread_factory_create_context;
50 (*factory)->finalize = smx_ctx_thread_factory_finalize;
51 (*factory)->create_maestro_context = smx_ctx_thread_factory_create_master_context;
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 smx_context_t smx_ctx_thread_factory_create_master_context(void)
62 return (smx_context_t) xbt_new0(s_smx_ctx_thread_t, 1);
65 static int smx_ctx_thread_factory_finalize(smx_context_factory_t * factory)
72 static smx_context_t smx_ctx_thread_factory_create_context(xbt_main_func_t code)
74 smx_ctx_thread_t context = xbt_new0(s_smx_ctx_thread_t, 1);
77 context->begin = xbt_os_sem_init(0);
78 context->end = xbt_os_sem_init(0);
80 return (smx_context_t)context;
83 static void smx_ctx_thread_free(smx_context_t pcontext)
85 smx_ctx_thread_t context = (smx_ctx_thread_t)pcontext;
87 /* Check if this is the context of maestro (it doesn't has a real thread) */
88 if (context->thread) {
89 /* wait about the thread terminason */
90 xbt_os_thread_join(context->thread, NULL);
91 free(context->thread);
93 /* destroy the synchronisation objects */
94 xbt_os_sem_destroy(context->begin);
95 xbt_os_sem_destroy(context->end);
98 /* finally destroy the context */
102 static void smx_ctx_thread_start(smx_context_t context)
104 smx_ctx_thread_t ctx_thread = (smx_ctx_thread_t)context;
106 /* create and start the process */
107 /* NOTE: The first argument to xbt_os_thread_create used to be the process *
108 * name, but now the name is stored at simix level, so we pass a null */
110 xbt_os_thread_create(NULL, smx_ctx_thread_wrapper, ctx_thread);
112 /* wait the starting of the newly created process */
113 xbt_os_sem_acquire(ctx_thread->end);
116 static void smx_ctx_thread_stop(int exit_code)
118 /* please no debug here: our procdata was already free'd */
119 if (simix_global->current_process->cleanup_func)
120 (*simix_global->current_process->cleanup_func) (simix_global->current_process->cleanup_arg);
122 /* signal to the maestro that it has finished */
123 xbt_os_sem_release(((smx_ctx_thread_t) simix_global->current_process->context)->end);
126 xbt_os_thread_exit(NULL); /* We should provide return value in case other wants it */
129 static void *smx_ctx_thread_wrapper(void *param)
131 smx_ctx_thread_t context = (smx_ctx_thread_t) param;
133 /* Tell the maestro we are starting, and wait for its green light */
134 xbt_os_sem_release(context->end);
135 xbt_os_sem_acquire(context->begin);
137 smx_ctx_thread_stop((context->code) (simix_global->current_process->argc,
138 simix_global->current_process->argv));
142 static void smx_ctx_thread_suspend(smx_context_t context)
144 xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
145 xbt_os_sem_acquire(((smx_ctx_thread_t) context)->begin);
148 static void smx_ctx_thread_resume(smx_context_t not_used,
149 smx_context_t new_context)
151 xbt_os_sem_release(((smx_ctx_thread_t) new_context)->begin);
152 xbt_os_sem_acquire(((smx_ctx_thread_t) new_context)->end);