Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fixed the pthread context implementation, now it compiles and runs
[simgrid.git] / src / simix / smx_context_thread.c
1 /* $Id$ */
2
3 /* context_thread - implementation of context switching with native threads */
4
5 /* Copyright (c) 2004-2008 the SimGrid team. All right reserved */
6
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. */
9
10 #include "xbt/function_types.h"
11 #include "private.h"
12
13 #include "portable.h"           /* loads context system definitions */
14 #include "xbt/swag.h"
15 #include "xbt/xbt_os_thread.h"
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(smx_context);
18
19 typedef struct s_smx_ctx_thread {
20   SMX_CTX_BASE_T;
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;
25
26 static int
27 smx_ctx_thread_factory_create_context(smx_process_t *smx_process, xbt_main_func_t code);
28
29 static int
30 smx_ctx_thread_factory_create_master_context(smx_process_t * maestro);
31
32 static int smx_ctx_thread_factory_finalize(smx_context_factory_t * factory);
33
34 static void smx_ctx_thread_free(smx_process_t process);
35
36 static void smx_ctx_thread_kill(smx_process_t process);
37
38 static void smx_ctx_thread_schedule(smx_process_t process);
39
40 static void smx_ctx_thread_yield(void);
41
42 static void smx_ctx_thread_start(smx_process_t process);
43
44 static void smx_ctx_thread_stop(int exit_code);
45
46 static void smx_ctx_thread_swap(smx_process_t process);
47
48 static void smx_ctx_thread_schedule(smx_process_t process);
49
50 static void smx_ctx_thread_yield(void);
51
52 static void smx_ctx_thread_suspend(smx_process_t process);
53
54 static void smx_ctx_thread_resume(smx_process_t process);
55
56 static void *smx_ctx_thread_wrapper(void *param);
57
58 void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory)
59 {
60   *factory = xbt_new0(s_smx_context_factory_t, 1);
61
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";
72 }
73
74 static int
75 smx_ctx_thread_factory_create_master_context(smx_process_t * maestro)
76 {
77   (*maestro)->context = (smx_context_t) xbt_new0(s_smx_ctx_thread_t, 1);
78   return 0;
79 }
80
81 static int smx_ctx_thread_factory_finalize(smx_context_factory_t * factory)
82 {
83   free(*factory);
84   *factory = NULL;
85   return 0;
86 }
87
88 static int
89 smx_ctx_thread_factory_create_context(smx_process_t *smx_process, xbt_main_func_t code)
90 {
91   smx_ctx_thread_t context = xbt_new0(s_smx_ctx_thread_t, 1);
92
93   VERB1("Create context %s", (*smx_process)->name);
94   context->code = code;
95   context->begin = xbt_os_sem_init(0);
96   context->end = xbt_os_sem_init(0);
97
98   (*smx_process)->context = (smx_context_t)context;
99   (*smx_process)->iwannadie = 0;
100
101   /* FIXME: Check what should return */
102   return 1;
103 }
104
105 static void smx_ctx_thread_free(smx_process_t process)
106 {
107   smx_ctx_thread_t context = (smx_ctx_thread_t)process->context;
108
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);
114     
115     /* destroy the synchronisation objects */
116     xbt_os_sem_destroy(context->begin);
117     xbt_os_sem_destroy(context->end);
118   }
119     
120   /* finally destroy the context */
121   free(context);
122 }
123
124 static void smx_ctx_thread_kill(smx_process_t process)
125 {
126   DEBUG1("Kill process '%s'", process->name);
127   process->iwannadie = 1;
128   smx_ctx_thread_swap(process);
129 }
130
131 /** 
132  * \param context the winner
133  *
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.
137  * 
138  * Only the maestro can call this function to run a given process.
139  */
140 static void smx_ctx_thread_schedule(smx_process_t process)
141 {
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_swap(process);
146 }
147
148 /** 
149  * Calling this function makes the current context yield. The context
150  * that scheduled it returns from smx_context_schedule as if nothing
151  * had happened.
152  * 
153  * Only the processes can call this function, giving back the control
154  * to the maestro
155  */
156 static void smx_ctx_thread_yield(void)
157 {
158   DEBUG1("Yield process '%s'", simix_global->current_process->name);
159   xbt_assert0((simix_global->current_process != simix_global->maestro_process),
160               "You are not supposed to run this function here!");
161   smx_ctx_thread_swap(simix_global->current_process);
162 }
163
164 static void smx_ctx_thread_start(smx_process_t process)
165 {
166   smx_ctx_thread_t ctx_thread = (smx_ctx_thread_t) process->context;
167
168   DEBUG1("Start context '%s'", process->name);
169   /* create and start the process */
170   ctx_thread->thread =
171     xbt_os_thread_create(process->name, smx_ctx_thread_wrapper,
172                          ctx_thread);
173
174   /* wait the starting of the newly created process */
175   xbt_os_sem_acquire(ctx_thread->end);
176 }
177
178 static void smx_ctx_thread_stop(int exit_code)
179 {
180   /* please no debug here: our procdata was already free'd */
181   if (simix_global->current_process->cleanup_func)
182     ((*simix_global->current_process->cleanup_func)) (simix_global->current_process->cleanup_arg);
183
184   /* signal to the maestro that it has finished */
185   xbt_os_sem_release(((smx_ctx_thread_t) simix_global->current_process->context)->end);
186
187   /* exit */
188   xbt_os_thread_exit(NULL);     /* We should provide return value in case other wants it */
189 }
190
191 static void smx_ctx_thread_swap(smx_process_t process)
192 {
193   
194   DEBUG2("Swap context: '%s' -> '%s'", simix_global->current_process->name, process->name);
195   if ((simix_global->current_process != simix_global->maestro_process) && !process->iwannadie) {
196     /* (0) it's not the scheduler and the process doesn't want to die, it just wants to yield */
197
198     /* yield itself, resume the maestro */
199     smx_ctx_thread_suspend(process);
200   } else {
201     /* (1) the current process is the scheduler and the process doesn't want to die
202      *      <-> the maestro wants to schedule the process
203      *              -> the maestro schedules the process and waits
204      *
205      * (2) the current process is the scheduler and the process wants to die
206      *      <-> the maestro wants to kill the process (has called the function smx_context_kill())
207      *              -> the maestro schedule the process and waits (xbt_os_sem_acquire(context->end))
208      *              -> if the process stops (smx_context_stop())
209      *                      -> the process resumes the maestro (xbt_os_sem_release(current_context->end)) and exit (xbt_os_thread_exit())
210      *              -> else the process call smx_context_yield()
211      *                      -> goto (3.1)
212      *
213      * (3) the current process is not the scheduler and the process wants to die
214      *              -> (3.1) if the current process is the process who wants to die
215      *                      -> (resume not need) goto (4)
216      *              -> (3.2) else the current process is not the process who wants to die
217      *                      <-> the current process wants to kill an other process
218      *                              -> the current process resumes the process to die and waits
219      *                              -> if the process to kill stops
220      *                                      -> it resumes the process who kill it and exit
221      *                              -> else if the process to kill calls to smx_context_yield()
222      *                                      -> goto (3.1)
223      */
224     /* schedule the process associated with this context */
225     smx_ctx_thread_resume(process);
226
227   }
228
229   /* (4) the current process wants to die */
230   if (simix_global->current_process->iwannadie)
231     smx_ctx_thread_stop(1);
232 }
233
234 static void *smx_ctx_thread_wrapper(void *param)
235 {
236   smx_ctx_thread_t context = (smx_ctx_thread_t) param;
237
238   /* Tell the maestro we are starting, and wait for its green light */
239   xbt_os_sem_release(context->end);
240   xbt_os_sem_acquire(context->begin);
241
242   smx_ctx_thread_stop((context->code) (simix_global->current_process->argc, simix_global->current_process->argv));
243   return NULL;
244 }
245
246 static void smx_ctx_thread_suspend(smx_process_t process)
247 {
248   /* save the current process */
249   smx_process_t self = simix_global->current_process;
250
251   DEBUG1("Suspend context '%s'", process->name);
252
253   /* update the current process to this process */
254   simix_global->current_process = process;
255
256   xbt_os_sem_release(((smx_ctx_thread_t) process->context)->end);
257   xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->begin);
258
259   /* restore the current process to the previously saved process */
260   simix_global->current_process = self;
261 }
262
263 static void smx_ctx_thread_resume(smx_process_t process)
264 {
265   /* save the current process */
266   smx_process_t self = simix_global->current_process;
267
268   DEBUG1("Resume context '%s'", process->name);
269
270   /* update the current process */
271   simix_global->current_process = process;
272
273   xbt_os_sem_release(((smx_ctx_thread_t) process->context)->begin);
274   xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->end);
275
276   /* restore the current process to the previously saved process */
277   simix_global->current_process = self;
278 }