1 /* context_thread - implementation of context switching with native threads */
3 /* Copyright (c) 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "xbt/function_types.h"
11 #include "portable.h" /* loads context system definitions */
13 #include "xbt/xbt_os_thread.h"
14 #include "xbt_modinter.h" /* prototype of os thread module's init/exit in XBT */
15 #include "simix/context.h"
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
19 typedef struct s_smx_ctx_thread {
20 s_smx_ctx_base_t super; /* Fields of super implementation */
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 xbt_os_sem_t smx_ctx_thread_sem;
29 smx_ctx_thread_factory_create_context(xbt_main_func_t code, int argc,
31 void_pfn_smxprocess_t cleanup_func,
34 static void smx_ctx_thread_free(smx_context_t context);
35 static void smx_ctx_thread_stop(smx_context_t context);
36 static void smx_ctx_thread_suspend(smx_context_t context);
37 static void smx_ctx_thread_resume(smx_context_t new_context);
38 static void smx_ctx_thread_runall_serial(xbt_dynar_t processes);
39 static void smx_ctx_thread_runall_parallel(xbt_dynar_t processes);
40 static smx_context_t smx_ctx_thread_self(void);
42 static int smx_ctx_thread_factory_finalize(smx_context_factory_t *factory);
43 static void *smx_ctx_thread_wrapper(void *param);
45 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory)
47 smx_ctx_base_factory_init(factory);
48 XBT_VERB("Activating thread context factory");
50 (*factory)->finalize = smx_ctx_thread_factory_finalize;
51 (*factory)->create_context = smx_ctx_thread_factory_create_context;
52 /* Do not overload that method (*factory)->finalize */
53 (*factory)->free = smx_ctx_thread_free;
54 (*factory)->stop = smx_ctx_thread_stop;
55 (*factory)->suspend = smx_ctx_thread_suspend;
57 if (SIMIX_context_is_parallel())
58 (*factory)->runall = smx_ctx_thread_runall_parallel;
60 (*factory)->runall = smx_ctx_thread_runall_serial;
62 (*factory)->self = smx_ctx_thread_self;
63 (*factory)->name = "ctx_thread_factory";
65 if (SIMIX_context_is_parallel()) {
66 smx_ctx_thread_sem = xbt_os_sem_init(SIMIX_context_get_nthreads());
68 smx_ctx_thread_sem = NULL;
72 static int smx_ctx_thread_factory_finalize(smx_context_factory_t *factory)
74 if (smx_ctx_thread_sem) {
75 xbt_os_sem_destroy(smx_ctx_thread_sem);
76 smx_ctx_thread_sem = NULL;
78 return smx_ctx_base_factory_finalize(factory);
82 smx_ctx_thread_factory_create_context(xbt_main_func_t code, int argc,
84 void_pfn_smxprocess_t cleanup_func,
87 smx_ctx_thread_t context = (smx_ctx_thread_t)
88 smx_ctx_base_factory_create_context_sized(sizeof(s_smx_ctx_thread_t),
92 /* If the user provided a function for the process then use it
93 otherwise is the context for maestro */
95 context->begin = xbt_os_sem_init(0);
96 context->end = xbt_os_sem_init(0);
97 /* create and start the process */
98 /* NOTE: The first argument to xbt_os_thread_create used to be the process *
99 * name, but now the name is stored at SIMIX level, so we pass a null */
101 xbt_os_thread_create(NULL, smx_ctx_thread_wrapper, context, context);
103 /* wait the starting of the newly created process */
104 xbt_os_sem_acquire(context->end);
107 xbt_os_thread_set_extra_data(context);
110 return (smx_context_t) context;
113 static void smx_ctx_thread_free(smx_context_t pcontext)
115 smx_ctx_thread_t context = (smx_ctx_thread_t) pcontext;
117 /* check if this is the context of maestro (it doesn't has a real thread) */
118 if (context->thread) {
119 /* wait about the thread terminason */
120 xbt_os_thread_join(context->thread, NULL);
122 /* destroy the synchronisation objects */
123 xbt_os_sem_destroy(context->begin);
124 xbt_os_sem_destroy(context->end);
127 smx_ctx_base_free(pcontext);
130 static void smx_ctx_thread_stop(smx_context_t pcontext)
132 smx_ctx_thread_t context = (smx_ctx_thread_t) pcontext;
134 /* please no debug here: our procdata was already free'd */
135 smx_ctx_base_stop(pcontext);
137 if (smx_ctx_thread_sem) /* parallel run */
138 xbt_os_sem_release(smx_ctx_thread_sem);
140 /* signal to the maestro that it has finished */
141 xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
144 /* We should provide return value in case other wants it */
145 xbt_os_thread_exit(NULL);
148 static void *smx_ctx_thread_wrapper(void *param)
150 smx_ctx_thread_t context = (smx_ctx_thread_t) param;
152 /* Tell the maestro we are starting, and wait for its green light */
153 xbt_os_sem_release(context->end);
154 xbt_os_sem_acquire(context->begin);
155 if (smx_ctx_thread_sem) /* parallel run */
156 xbt_os_sem_acquire(smx_ctx_thread_sem);
158 (context->super.code) (context->super.argc, context->super.argv);
160 smx_ctx_thread_stop((smx_context_t) context);
164 static void smx_ctx_thread_suspend(smx_context_t context)
166 if (smx_ctx_thread_sem) /* parallel run */
167 xbt_os_sem_release(smx_ctx_thread_sem);
168 xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
169 xbt_os_sem_acquire(((smx_ctx_thread_t) context)->begin);
170 if (smx_ctx_thread_sem) /* parallel run */
171 xbt_os_sem_acquire(smx_ctx_thread_sem);
174 static void smx_ctx_thread_runall_serial(xbt_dynar_t processes)
176 smx_process_t process;
179 xbt_dynar_foreach(processes, cursor, process) {
180 xbt_os_sem_release(((smx_ctx_thread_t) process->context)->begin);
181 xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->end);
185 static void smx_ctx_thread_runall_parallel(xbt_dynar_t processes)
188 smx_process_t process;
190 xbt_dynar_foreach(processes, index, process)
191 xbt_os_sem_release(((smx_ctx_thread_t) process->context)->begin);
193 xbt_dynar_foreach(processes, index, process) {
194 xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->end);
198 static smx_context_t smx_ctx_thread_self(void)
200 return (smx_context_t) xbt_os_thread_get_extra_data();