Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Lots of renamings from xbt_ to smx_
[simgrid.git] / src / simix / smx_context_sysv.c
1 /* $Id$ */
2
3 /* context_sysv - implementation of context switching with ucontextes from Sys V */
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/ex_interface.h"
11 #include "smx_context_private.h"
12
13 #include "context_sysv_config.h"        /* loads context system definitions                             */
14 #include "portable.h"
15 #include <ucontext.h>           /* context relative declarations                                */
16 #define STACK_SIZE 128*1024     /* lower this if you want to reduce the memory consumption      */
17 #ifdef HAVE_VALGRIND_VALGRIND_H
18 #  include <valgrind/valgrind.h>
19 #endif /* HAVE_VALGRIND_VALGRIND_H */
20
21 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(smx_context);
22
23 typedef struct s_smx_ctx_sysv {
24   SMX_CTX_BASE_T;
25   ucontext_t uc;                /* the thread that execute the code */
26   char stack[STACK_SIZE];       /* the thread stack size */
27   smx_process_t prev;           /* the previous process */
28 #ifdef HAVE_VALGRIND_VALGRIND_H
29   unsigned int valgrind_stack_id;       /* the valgrind stack id */
30 #endif                          
31 } s_smx_ctx_sysv_t, *smx_ctx_sysv_t;
32
33
34 /* callback: context fetching */
35 static ex_ctx_t *xbt_jcontext_ex_ctx(void);
36
37 /* callback: termination */
38 static void xbt_jcontext_ex_terminate(xbt_ex_t * e);
39
40 static int
41 smx_ctx_sysv_factory_create_context(smx_process_t *smx_process, xbt_main_func_t code);
42
43 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t * factory);
44
45 static int smx_ctx_sysv_factory_create_maestro_context(smx_process_t * maestro);
46
47 static void smx_ctx_sysv_free(smx_process_t process);
48
49 static void smx_ctx_sysv_kill(smx_process_t process);
50
51 static void smx_ctx_sysv_schedule(smx_process_t process);
52
53 static void smx_ctx_sysv_yield(void);
54
55 static void smx_ctx_sysv_start(smx_process_t process);
56
57 static void smx_ctx_sysv_stop(int exit_code);
58
59 static void smx_ctx_sysv_swap(smx_process_t process);
60
61 static void smx_ctx_sysv_schedule(smx_process_t process);
62
63 static void smx_ctx_sysv_yield(void);
64
65 static void smx_ctx_sysv_suspend(smx_process_t process);
66
67 static void smx_ctx_sysv_resume(smx_process_t process);
68
69 static void smx_ctx_sysv_wrapper(void);
70
71 /* callback: context fetching */
72 static ex_ctx_t *xbt_ctx_sysv_ex_ctx(void)
73 {
74   return simix_global->current_process->context->exception;
75 }
76
77 /* callback: termination */
78 static void xbt_ctx_sysv_ex_terminate(xbt_ex_t * e)
79 {
80   xbt_ex_display(e);
81   abort();
82 }
83
84 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t * factory)
85 {
86   *factory = xbt_new0(s_smx_context_factory_t, 1);
87
88   (*factory)->create_context = smx_ctx_sysv_factory_create_context;
89   (*factory)->finalize = smx_ctx_sysv_factory_finalize;
90   (*factory)->create_maestro_context = smx_ctx_sysv_factory_create_maestro_context;
91   (*factory)->free = smx_ctx_sysv_free;
92   (*factory)->kill = smx_ctx_sysv_kill;
93   (*factory)->schedule = smx_ctx_sysv_schedule;
94   (*factory)->yield = smx_ctx_sysv_yield;
95   (*factory)->start = smx_ctx_sysv_start;
96   (*factory)->stop = smx_ctx_sysv_stop;
97   (*factory)->name = "smx_sysv_context_factory";
98
99   /* context exception handlers */
100   __xbt_ex_ctx = xbt_ctx_sysv_ex_ctx;
101   __xbt_ex_terminate = xbt_ctx_sysv_ex_terminate;
102 }
103
104 static int smx_ctx_sysv_factory_create_maestro_context(smx_process_t *maestro)
105 {
106   smx_ctx_sysv_t context = xbt_new0(s_smx_ctx_sysv_t, 1);
107
108   context->exception = xbt_new(ex_ctx_t, 1);
109   XBT_CTX_INITIALIZE(context->exception);
110
111   (*maestro)->context = (smx_context_t) context;
112
113   return 0;
114
115 }
116
117
118 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t * factory)
119 {
120   /*FIXME free(maestro_context->exception);*/
121   free(*factory);
122   *factory = NULL;
123   return 0;
124 }
125
126 static int
127 smx_ctx_sysv_factory_create_context(smx_process_t *smx_process, xbt_main_func_t code)
128 {
129   VERB1("Create context %s", (*smx_process)->name);
130   smx_ctx_sysv_t context = xbt_new0(s_smx_ctx_sysv_t, 1);
131
132   context->code = code;
133
134   xbt_assert2(getcontext(&(context->uc)) == 0,
135               "Error in context saving: %d (%s)", errno, strerror(errno));
136   context->uc.uc_link = NULL;
137   context->uc.uc_stack.ss_sp =
138     pth_skaddr_makecontext(context->stack, STACK_SIZE);
139   context->uc.uc_stack.ss_size =
140     pth_sksize_makecontext(context->stack, STACK_SIZE);
141 #ifdef HAVE_VALGRIND_VALGRIND_H
142   context->valgrind_stack_id =
143     VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
144                             ((char *) context->uc.uc_stack.ss_sp) +
145                             context->uc.uc_stack.ss_size);
146 #endif /* HAVE_VALGRIND_VALGRIND_H */
147
148   context->exception = xbt_new(ex_ctx_t, 1);
149   XBT_CTX_INITIALIZE(context->exception);
150   (*smx_process)->context = (smx_context_t)context;
151   (*smx_process)->iwannadie = 0;
152
153   /* FIXME: Check what should return */
154   return 1;
155 }
156
157 static void smx_ctx_sysv_free(smx_process_t process)
158 {
159   smx_ctx_sysv_t context = (smx_ctx_sysv_t)process->context;   
160   if (context){
161
162     if (context->exception)
163       free(context->exception);
164
165 #ifdef HAVE_VALGRIND_VALGRIND_H
166     VALGRIND_STACK_DEREGISTER(((smx_ctx_sysv_t) context)->valgrind_stack_id);
167 #endif /* HAVE_VALGRIND_VALGRIND_H */
168
169     /* destroy the context */
170     free(context);
171   }
172 }
173
174 static void smx_ctx_sysv_kill(smx_process_t process)
175 {
176   DEBUG2("Kill process '%s' (from '%s')", process->name,
177          simix_global->current_process->name);
178   process->iwannadie = 1;
179   smx_ctx_sysv_swap(process);
180 }
181
182 /** 
183  * \param context the winner
184  *
185  * Calling this function blocks the current context and schedule \a context.  
186  * When \a context will call xbt_context_yield, it will return
187  * to this function as if nothing had happened.
188  * 
189  * Only the maestro can call this function to run a given process.
190  */
191 static void smx_ctx_sysv_schedule(smx_process_t process)
192 {
193   DEBUG1("Schedule process '%s'", process->name);
194   xbt_assert0((simix_global->current_process == simix_global->maestro_process),
195               "You are not supposed to run this function here!");
196   smx_ctx_sysv_swap(process);
197 }
198
199 /** 
200  * Calling this function makes the current context yield. The context
201  * that scheduled it returns from xbt_context_schedule as if nothing
202  * had happened.
203  * 
204  * Only the processes can call this function, giving back the control
205  * to the maestro
206  */
207 static void smx_ctx_sysv_yield(void)
208 {
209   DEBUG1("Yielding context '%s'", simix_global->current_process->name);
210   xbt_assert0((simix_global->current_process != simix_global->maestro_process),
211               "You are not supposed to run this function here!");
212   smx_ctx_sysv_swap(simix_global->current_process);
213 }
214
215 static void smx_ctx_sysv_start(smx_process_t process)
216 {
217   /*DEBUG1("Start context '%s'", context->name);*/
218   makecontext(&(((smx_ctx_sysv_t) process->context)->uc), smx_ctx_sysv_wrapper, 0);
219 }
220
221 static void smx_ctx_sysv_stop(int exit_code)
222 {
223   /* please no debug here: our procdata was already free'd */
224   if (simix_global->current_process->cleanup_func)
225     ((*simix_global->current_process->cleanup_func)) (simix_global->current_process->cleanup_arg);
226
227   smx_ctx_sysv_swap(simix_global->current_process);
228 }
229
230 static void smx_ctx_sysv_swap(smx_process_t process)
231 {
232   DEBUG2("Swap context: '%s' -> '%s'", simix_global->current_process->name, process->name);
233   xbt_assert0(simix_global->current_process, "You have to call context_init() first.");
234   xbt_assert0(process, "Invalid argument");
235
236   if (((smx_ctx_sysv_t) process->context)->prev == NULL)
237     smx_ctx_sysv_resume(process);
238   else
239     smx_ctx_sysv_suspend(process);
240
241   if (simix_global->current_process->iwannadie)
242     smx_ctx_sysv_stop(1);
243 }
244
245 static void smx_ctx_sysv_wrapper(void)
246 {
247   smx_ctx_sysv_stop((*(simix_global->current_process->context->code))
248                     (simix_global->current_process->argc, simix_global->current_process->argv));
249 }
250
251 static void smx_ctx_sysv_suspend(smx_process_t process)
252 {
253   int rv;
254
255   DEBUG1("Suspend context: '%s'", simix_global->current_process->name);
256   smx_process_t prev_process = ((smx_ctx_sysv_t) process->context)->prev;
257
258   simix_global->current_process = prev_process;
259
260   ((smx_ctx_sysv_t) process->context)->prev = NULL;
261
262   rv = swapcontext(&(((smx_ctx_sysv_t) process->context)->uc), &(((smx_ctx_sysv_t)prev_process->context)->uc));
263
264   xbt_assert0((rv == 0), "Context swapping failure");
265 }
266
267 static void smx_ctx_sysv_resume(smx_process_t process)
268 {
269   int rv;
270   smx_ctx_sysv_t new_context = (smx_ctx_sysv_t)process->context;
271   smx_ctx_sysv_t prev_context = (smx_ctx_sysv_t)simix_global->current_process->context;
272
273   DEBUG2("Resume context: '%s' (from '%s')", process->name,
274          simix_global->current_process->name);
275
276   ((smx_ctx_sysv_t) process->context)->prev = simix_global->current_process;
277
278   simix_global->current_process = process;
279
280   rv = swapcontext(&prev_context->uc, &new_context->uc);
281
282   xbt_assert0((rv == 0), "Context swapping failure");
283 }