Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Inline abstract interface to factories' functions (smx_context_* func) [Cristian]
[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 "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 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t * factory)
118 {
119   /*FIXME free(maestro_context->exception);*/
120   free(*factory);
121   *factory = NULL;
122   return 0;
123 }
124
125 static int
126 smx_ctx_sysv_factory_create_context(smx_process_t *smx_process, xbt_main_func_t code)
127 {
128   VERB1("Create context %s", (*smx_process)->name);
129   smx_ctx_sysv_t context = xbt_new0(s_smx_ctx_sysv_t, 1);
130
131   context->code = code;
132
133   xbt_assert2(getcontext(&(context->uc)) == 0,
134               "Error in context saving: %d (%s)", errno, strerror(errno));
135   context->uc.uc_link = NULL;
136   context->uc.uc_stack.ss_sp =
137     pth_skaddr_makecontext(context->stack, STACK_SIZE);
138   context->uc.uc_stack.ss_size =
139     pth_sksize_makecontext(context->stack, STACK_SIZE);
140 #ifdef HAVE_VALGRIND_VALGRIND_H
141   context->valgrind_stack_id =
142     VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
143                             ((char *) context->uc.uc_stack.ss_sp) +
144                             context->uc.uc_stack.ss_size);
145 #endif /* HAVE_VALGRIND_VALGRIND_H */
146
147   context->exception = xbt_new(ex_ctx_t, 1);
148   XBT_CTX_INITIALIZE(context->exception);
149   (*smx_process)->context = (smx_context_t)context;
150   (*smx_process)->iwannadie = 0;
151
152   /* FIXME: Check what should return */
153   return 1;
154 }
155
156 static void smx_ctx_sysv_free(smx_process_t process)
157 {
158   smx_ctx_sysv_t context = (smx_ctx_sysv_t)process->context;   
159   if (context){
160
161     if (context->exception)
162       free(context->exception);
163
164 #ifdef HAVE_VALGRIND_VALGRIND_H
165     VALGRIND_STACK_DEREGISTER(((smx_ctx_sysv_t) context)->valgrind_stack_id);
166 #endif /* HAVE_VALGRIND_VALGRIND_H */
167
168     /* destroy the context */
169     free(context);
170   }
171 }
172
173 static void smx_ctx_sysv_kill(smx_process_t process)
174 {
175   DEBUG2("Kill process '%s' (from '%s')", process->name,
176          simix_global->current_process->name);
177   process->iwannadie = 1;
178   smx_ctx_sysv_swap(process);
179 }
180
181 /** 
182  * \param context the winner
183  *
184  * Calling this function blocks the current context and schedule \a context.  
185  * When \a context will call xbt_context_yield, it will return
186  * to this function as if nothing had happened.
187  * 
188  * Only the maestro can call this function to run a given process.
189  */
190 static void smx_ctx_sysv_schedule(smx_process_t process)
191 {
192   DEBUG1("Schedule process '%s'", process->name);
193   xbt_assert0((simix_global->current_process == simix_global->maestro_process),
194               "You are not supposed to run this function here!");
195   smx_ctx_sysv_swap(process);
196 }
197
198 /** 
199  * Calling this function makes the current context yield. The context
200  * that scheduled it returns from xbt_context_schedule as if nothing
201  * had happened.
202  * 
203  * Only the processes can call this function, giving back the control
204  * to the maestro
205  */
206 static void smx_ctx_sysv_yield(void)
207 {
208   DEBUG1("Yielding process '%s'", simix_global->current_process->name);
209   xbt_assert0((simix_global->current_process != simix_global->maestro_process),
210               "You are not supposed to run this function here!");
211   smx_ctx_sysv_swap(simix_global->current_process);
212 }
213
214 static void smx_ctx_sysv_start(smx_process_t process)
215 {
216   /*DEBUG1("Start context '%s'", context->name);*/
217   makecontext(&(((smx_ctx_sysv_t) process->context)->uc), smx_ctx_sysv_wrapper, 0);
218 }
219
220 static void smx_ctx_sysv_stop(int exit_code)
221 {
222   /* please no debug here: our procdata was already free'd */
223   if (simix_global->current_process->cleanup_func)
224     ((*simix_global->current_process->cleanup_func)) (simix_global->current_process->cleanup_arg);
225
226   smx_ctx_sysv_swap(simix_global->current_process);
227 }
228
229 static void smx_ctx_sysv_swap(smx_process_t process)
230 {
231   DEBUG2("Swap context: '%s' -> '%s'", simix_global->current_process->name, process->name);
232   xbt_assert0(simix_global->current_process, "You have to call context_init() first.");
233   xbt_assert0(process, "Invalid argument");
234
235   if (((smx_ctx_sysv_t) process->context)->prev == NULL)
236     smx_ctx_sysv_resume(process);
237   else
238     smx_ctx_sysv_suspend(process);
239
240   if (simix_global->current_process->iwannadie)
241     smx_ctx_sysv_stop(1);
242 }
243
244 static void smx_ctx_sysv_wrapper(void)
245 {
246   smx_ctx_sysv_stop((*(simix_global->current_process->context->code))
247                     (simix_global->current_process->argc, simix_global->current_process->argv));
248 }
249
250 static void smx_ctx_sysv_suspend(smx_process_t process)
251 {
252   int rv;
253
254   DEBUG1("Suspend context: '%s'", simix_global->current_process->name);
255   smx_process_t prev_process = ((smx_ctx_sysv_t) process->context)->prev;
256
257   simix_global->current_process = prev_process;
258
259   ((smx_ctx_sysv_t) process->context)->prev = NULL;
260
261   rv = swapcontext(&(((smx_ctx_sysv_t) process->context)->uc), &(((smx_ctx_sysv_t)prev_process->context)->uc));
262
263   xbt_assert0((rv == 0), "Context swapping failure");
264 }
265
266 static void smx_ctx_sysv_resume(smx_process_t process)
267 {
268   int rv;
269   smx_ctx_sysv_t new_context = (smx_ctx_sysv_t)process->context;
270   smx_ctx_sysv_t prev_context = (smx_ctx_sysv_t)simix_global->current_process->context;
271
272   DEBUG2("Resume context: '%s' (from '%s')", process->name,
273          simix_global->current_process->name);
274
275   ((smx_ctx_sysv_t) process->context)->prev = simix_global->current_process;
276
277   simix_global->current_process = process;
278
279   rv = swapcontext(&prev_context->uc, &new_context->uc);
280
281   xbt_assert0((rv == 0), "Context swapping failure");
282 }