Logo AND Algorithmique Numérique Distribuée

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