Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Intermidiate step towards the new context mechanism [Cristian]
[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_resume(process);
146   /*smx_ctx_thread_swap(process);*/
147 }
148
149 /** 
150  * Calling this function makes the current context yield. The context
151  * that scheduled it returns from smx_context_schedule as if nothing
152  * had happened.
153  * 
154  * Only the processes can call this function, giving back the control
155  * to the maestro
156  */
157 static void smx_ctx_thread_yield(void)
158 {
159   smx_ctx_thread_suspend(simix_global->current_process);
160 }
161
162 static void smx_ctx_thread_start(smx_process_t process)
163 {
164   smx_ctx_thread_t ctx_thread = (smx_ctx_thread_t) process->context;
165
166   DEBUG1("Start context '%s'", process->name);
167   /* create and start the process */
168   ctx_thread->thread =
169     xbt_os_thread_create(process->name, smx_ctx_thread_wrapper,
170                          ctx_thread);
171
172   /* wait the starting of the newly created process */
173   xbt_os_sem_acquire(ctx_thread->end);
174 }
175
176 static void smx_ctx_thread_stop(int exit_code)
177 {
178   /* please no debug here: our procdata was already free'd */
179   if (simix_global->current_process->cleanup_func)
180     ((*simix_global->current_process->cleanup_func)) (simix_global->current_process->cleanup_arg);
181
182   /* signal to the maestro that it has finished */
183   xbt_os_sem_release(((smx_ctx_thread_t) simix_global->current_process->context)->end);
184
185   /* exit */
186   xbt_os_thread_exit(NULL);     /* We should provide return value in case other wants it */
187 }
188
189 /*FIXME: erase this function*/
190 static void smx_ctx_thread_swap(smx_process_t process)
191 {
192   return;
193 }
194
195 static void *smx_ctx_thread_wrapper(void *param)
196 {
197   smx_ctx_thread_t context = (smx_ctx_thread_t) param;
198
199   /* Tell the maestro we are starting, and wait for its green light */
200   xbt_os_sem_release(context->end);
201   xbt_os_sem_acquire(context->begin);
202
203   smx_ctx_thread_stop((context->code) (simix_global->current_process->argc, 
204                                        simix_global->current_process->argv));
205   return NULL;
206 }
207
208 static void smx_ctx_thread_suspend(smx_process_t process)
209 {
210   DEBUG1("Suspend context '%s'", process->name);
211   xbt_os_sem_release(((smx_ctx_thread_t) process->context)->end);
212   xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->begin);
213 }
214
215 static void smx_ctx_thread_resume(smx_process_t process)
216 {
217   DEBUG1("Resume context '%s'", process->name);
218   xbt_os_sem_release(((smx_ctx_thread_t) process->context)->begin);
219   xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->end);
220 }