Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not require doxygen in maintainer mode
[simgrid.git] / src / xbt / xbt_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 "xbt/xbt_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(xbt_context);
22
23 typedef struct s_xbt_ctx_sysv {
24   XBT_CTX_BASE_T;
25   ucontext_t uc;                /* the thread that execute the code                             */
26   char stack[STACK_SIZE];       /* the thread stack size                                        */
27   struct s_xbt_ctx_sysv *prev;  /* the previous thread                                          */
28 #ifdef HAVE_VALGRIND_VALGRIND_H
29   unsigned int valgrind_stack_id;       /* the valgrind stack id.       */
30 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
31 } s_xbt_ctx_sysv_t, *xbt_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 xbt_context_t
41 xbt_ctx_sysv_factory_create_context(const char *name, xbt_main_func_t code,
42                                     void_f_pvoid_t startup_func,
43                                     void *startup_arg,
44                                     void_f_pvoid_t cleanup_func,
45                                     void *cleanup_arg, int argc, char **argv);
46
47 static int xbt_ctx_sysv_factory_finalize(xbt_context_factory_t * factory);
48
49 static int
50 xbt_ctx_sysv_factory_create_maestro_context(xbt_context_t * maestro);
51
52 static void xbt_ctx_sysv_free(xbt_context_t context);
53
54 static void xbt_ctx_sysv_kill(xbt_context_t context);
55
56 static void xbt_ctx_sysv_schedule(xbt_context_t context);
57
58 static void xbt_ctx_sysv_yield(void);
59
60 static void xbt_ctx_sysv_start(xbt_context_t context);
61
62 static void xbt_ctx_sysv_stop(int exit_code);
63
64 static void xbt_ctx_sysv_swap(xbt_context_t context);
65
66 static void xbt_ctx_sysv_schedule(xbt_context_t context);
67
68 static void xbt_ctx_sysv_yield(void);
69
70 static void xbt_ctx_sysv_suspend(xbt_context_t context);
71
72 static void xbt_ctx_sysv_resume(xbt_context_t context);
73
74 static void xbt_ctx_sysv_wrapper(void);
75
76 /* callback: context fetching */
77 static ex_ctx_t *xbt_ctx_sysv_ex_ctx(void)
78 {
79   return current_context->exception;
80 }
81
82 /* callback: termination */
83 static void xbt_ctx_sysv_ex_terminate(xbt_ex_t * e)
84 {
85   xbt_ex_display(e);
86   abort();
87 }
88
89
90 void xbt_ctx_sysv_factory_init(xbt_context_factory_t * factory)
91 {
92   /* context exception */
93   *factory = xbt_new0(s_xbt_context_factory_t, 1);
94
95   (*factory)->create_context = xbt_ctx_sysv_factory_create_context;
96   (*factory)->finalize = xbt_ctx_sysv_factory_finalize;
97   (*factory)->create_maestro_context =
98     xbt_ctx_sysv_factory_create_maestro_context;
99   (*factory)->name = "ctx_sysv_context_factory";
100
101   /* context exception handlers */
102   __xbt_ex_ctx = xbt_ctx_sysv_ex_ctx;
103   __xbt_ex_terminate = xbt_ctx_sysv_ex_terminate;
104 }
105
106 static int
107 xbt_ctx_sysv_factory_create_maestro_context(xbt_context_t * maestro)
108 {
109
110   xbt_ctx_sysv_t context = xbt_new0(s_xbt_ctx_sysv_t, 1);
111
112   context->name = (char *) "maestro";
113
114   context->exception = xbt_new(ex_ctx_t, 1);
115   XBT_CTX_INITIALIZE(context->exception);
116
117   *maestro = (xbt_context_t) context;
118
119   return 0;
120
121 }
122
123
124 static int xbt_ctx_sysv_factory_finalize(xbt_context_factory_t * factory)
125 {
126   free(maestro_context->exception);
127   free(*factory);
128   *factory = NULL;
129   return 0;
130 }
131
132 static xbt_context_t
133 xbt_ctx_sysv_factory_create_context(const char *name, xbt_main_func_t code,
134                                     void_f_pvoid_t startup_func,
135                                     void *startup_arg,
136                                     void_f_pvoid_t cleanup_func,
137                                     void *cleanup_arg, int argc, char **argv)
138 {
139   VERB1("Create context %s", name);
140   xbt_ctx_sysv_t context = xbt_new0(s_xbt_ctx_sysv_t, 1);
141
142   context->code = code;
143   context->name = xbt_strdup(name);
144
145   xbt_assert2(getcontext(&(context->uc)) == 0,
146               "Error in context saving: %d (%s)", errno, strerror(errno));
147   context->uc.uc_link = NULL;
148   context->uc.uc_stack.ss_sp =
149     pth_skaddr_makecontext(context->stack, STACK_SIZE);
150   context->uc.uc_stack.ss_size =
151     pth_sksize_makecontext(context->stack, STACK_SIZE);
152 #ifdef HAVE_VALGRIND_VALGRIND_H
153   context->valgrind_stack_id =
154     VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
155                             ((char *) context->uc.uc_stack.ss_sp) +
156                             context->uc.uc_stack.ss_size);
157 #endif /* HAVE_VALGRIND_VALGRIND_H */
158
159   context->exception = xbt_new(ex_ctx_t, 1);
160   XBT_CTX_INITIALIZE(context->exception);
161   context->iwannadie = 0;       /* useless but makes valgrind happy */
162   context->argc = argc;
163   context->argv = argv;
164   context->startup_func = startup_func;
165   context->startup_arg = startup_arg;
166   context->cleanup_func = cleanup_func;
167   context->cleanup_arg = cleanup_arg;
168
169
170   context->free = xbt_ctx_sysv_free;
171   context->kill = xbt_ctx_sysv_kill;
172   context->schedule = xbt_ctx_sysv_schedule;
173   context->yield = xbt_ctx_sysv_yield;
174   context->start = xbt_ctx_sysv_start;
175   context->stop = xbt_ctx_sysv_stop;
176
177   return (xbt_context_t) context;
178 }
179
180 static void xbt_ctx_sysv_free(xbt_context_t context)
181 {
182   if (context) {
183     free(context->name);
184
185     if (context->argv) {
186       int i;
187
188       for (i = 0; i < context->argc; i++)
189         if (context->argv[i])
190           free(context->argv[i]);
191
192       free(context->argv);
193     }
194
195     if (context->exception)
196       free(context->exception);
197
198 #ifdef HAVE_VALGRIND_VALGRIND_H
199     VALGRIND_STACK_DEREGISTER(((xbt_ctx_sysv_t) context)->valgrind_stack_id);
200 #endif /* HAVE_VALGRIND_VALGRIND_H */
201
202     /* finally destroy the context */
203     free(context);
204   }
205 }
206
207 static void xbt_ctx_sysv_kill(xbt_context_t context)
208 {
209   DEBUG2("Kill context '%s' (from '%s')", context->name,
210          current_context->name);
211   context->iwannadie = 1;
212   xbt_ctx_sysv_swap(context);
213 }
214
215 /** 
216  * \param context the winner
217  *
218  * Calling this function blocks the current context and schedule \a context.  
219  * When \a context will call xbt_context_yield, it will return
220  * to this function as if nothing had happened.
221  * 
222  * Only the maestro can call this function to run a given process.
223  */
224 static void xbt_ctx_sysv_schedule(xbt_context_t context)
225 {
226   DEBUG1("Schedule context '%s'", context->name);
227   xbt_assert0((current_context == maestro_context),
228               "You are not supposed to run this function here!");
229   xbt_ctx_sysv_swap(context);
230 }
231
232 /** 
233  * Calling this function makes the current context yield. The context
234  * that scheduled it returns from xbt_context_schedule as if nothing
235  * had happened.
236  * 
237  * Only the processes can call this function, giving back the control
238  * to the maestro
239  */
240 static void xbt_ctx_sysv_yield(void)
241 {
242   DEBUG1("Yielding context '%s'", current_context->name);
243   xbt_assert0((current_context != maestro_context),
244               "You are not supposed to run this function here!");
245   xbt_ctx_sysv_swap(current_context);
246 }
247
248 static void xbt_ctx_sysv_start(xbt_context_t context)
249 {
250   DEBUG1("Start context '%s'", context->name);
251   makecontext(&(((xbt_ctx_sysv_t) context)->uc), xbt_ctx_sysv_wrapper, 0);
252 }
253
254 static void xbt_ctx_sysv_stop(int exit_code)
255 {
256   /* please no debug here: our procdata was already free'd */
257   if (current_context->cleanup_func)
258     ((*current_context->cleanup_func)) (current_context->cleanup_arg);
259
260   xbt_swag_remove(current_context, context_living);
261   xbt_swag_insert(current_context, context_to_destroy);
262
263   xbt_ctx_sysv_swap(current_context);
264 }
265
266 static void xbt_ctx_sysv_swap(xbt_context_t context)
267 {
268   DEBUG2("Swap context: '%s' -> '%s'", current_context->name, context->name);
269   xbt_assert0(current_context, "You have to call context_init() first.");
270   xbt_assert0(context, "Invalid argument");
271
272   if (((xbt_ctx_sysv_t) context)->prev == NULL)
273     xbt_ctx_sysv_resume(context);
274   else
275     xbt_ctx_sysv_suspend(context);
276
277   if (current_context->iwannadie)
278     xbt_ctx_sysv_stop(1);
279 }
280
281 static void xbt_ctx_sysv_wrapper(void)
282 {
283   if (current_context->startup_func)
284     (*current_context->startup_func) (current_context->startup_arg);
285
286   xbt_ctx_sysv_stop((*(current_context->code))
287                     (current_context->argc, current_context->argv));
288 }
289
290 static void xbt_ctx_sysv_suspend(xbt_context_t context)
291 {
292   int rv;
293
294   DEBUG1("Suspend context: '%s'", current_context->name);
295   xbt_ctx_sysv_t prev_context = ((xbt_ctx_sysv_t) context)->prev;
296
297   current_context = (xbt_context_t) (((xbt_ctx_sysv_t) context)->prev);
298
299   ((xbt_ctx_sysv_t) context)->prev = NULL;
300
301   rv = swapcontext(&(((xbt_ctx_sysv_t) context)->uc), &(prev_context->uc));
302
303   xbt_assert0((rv == 0), "Context swapping failure");
304 }
305
306 static void xbt_ctx_sysv_resume(xbt_context_t context)
307 {
308   int rv;
309
310   DEBUG2("Resume context: '%s' (from '%s')", context->name,
311          current_context->name);
312   ((xbt_ctx_sysv_t) context)->prev = (xbt_ctx_sysv_t) current_context;
313
314   current_context = context;
315
316   rv =
317     swapcontext(&(((xbt_ctx_sysv_t) context)->prev->uc),
318                 &(((xbt_ctx_sysv_t) context)->uc));
319
320   xbt_assert0((rv == 0), "Context swapping failure");
321 }