Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
984be819d780fe772d549ca9c72fd5e41ba5c702
[simgrid.git] / src / simix / smx_context_sysv.c
1 /* context_sysv - context switching with ucontextes from System V           */
2
3 /* Copyright (c) 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6  /* This program is free software; you can redistribute it and/or modify it
7   * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include <stdarg.h>
10
11 #include "smx_context_sysv_private.h"
12 #include "xbt/parmap.h"
13 #include "xbt/parmap_private.h"
14 #include "simix/private.h"
15 #include "gras_config.h"
16
17 #ifdef HAVE_VALGRIND_VALGRIND_H
18 #  include <valgrind/valgrind.h>
19 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
20
21 #ifdef _XBT_WIN32
22 #include "win32_ucontext.h"
23 #else
24 #include "ucontext.h"
25 #endif
26
27 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
28
29 #ifdef CONTEXT_THREADS
30 static xbt_parmap_t parmap;
31 #endif
32 static unsigned long smx_ctx_sysv_process_index = 0; /* index of the next process to run in the
33                                          * list of runnable processes */
34 static smx_ctx_sysv_t maestro_context;
35
36 static smx_context_t
37 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
38     void_pfn_smxprocess_t cleanup_func, void* data);
39
40 static void smx_ctx_sysv_wrapper(int count, ...);
41
42 static void smx_ctx_sysv_stop_serial(smx_context_t context);
43 static void smx_ctx_sysv_suspend_serial(smx_context_t context);
44 static void smx_ctx_sysv_runall_serial(void);
45
46 static void smx_ctx_sysv_stop_parallel(smx_context_t context);
47 static void smx_ctx_sysv_suspend_parallel(smx_context_t context);
48 static void smx_ctx_sysv_runall_parallel(void);
49
50 static void smx_ctx_sysv_resume(smx_process_t first_process);
51
52 /* This is a bit paranoid about SIZEOF_VOIDP not being a multiple of SIZEOF_INT,
53  * but it doesn't harm. */
54 #define CTX_ADDR_LEN (SIZEOF_VOIDP / SIZEOF_INT + !!(SIZEOF_VOIDP % SIZEOF_INT))
55 union u_ctx_addr {
56   void *addr;
57   int intv[CTX_ADDR_LEN];
58 };
59 #if (CTX_ADDR_LEN == 1)
60 #  define CTX_ADDR_SPLIT(u) (u).intv[0]
61 #elif (CTX_ADDR_LEN == 2)
62 #  define CTX_ADDR_SPLIT(u) (u).intv[0], (u).intv[1]
63 #else
64 #  error Your architecture is not supported yet
65 #endif
66
67 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory)
68 {
69   smx_ctx_base_factory_init(factory);
70   XBT_VERB("Activating SYSV context factory");
71
72   (*factory)->finalize = smx_ctx_sysv_factory_finalize;
73   (*factory)->create_context = smx_ctx_sysv_create_context;
74   /* Do not overload that method (*factory)->finalize */
75   (*factory)->free = smx_ctx_sysv_free;
76   (*factory)->name = "smx_sysv_context_factory";
77
78   if (SIMIX_context_is_parallel()) {
79 #ifdef CONTEXT_THREADS  /* To use parallel ucontexts a thread pool is needed */
80     parmap = xbt_parmap_new(2);
81     (*factory)->stop = smx_ctx_sysv_stop_parallel;
82     (*factory)->suspend = smx_ctx_sysv_suspend_parallel;
83     (*factory)->runall = smx_ctx_sysv_runall_parallel;
84     (*factory)->self = smx_ctx_sysv_self_parallel;
85 #else
86     THROWF(arg_error, 0, "No thread support for parallel context execution");
87 #endif
88   } else {
89     (*factory)->stop = smx_ctx_sysv_stop_serial;
90     (*factory)->suspend = smx_ctx_sysv_suspend_serial;
91     (*factory)->runall = smx_ctx_sysv_runall_serial;
92   }    
93 }
94
95 int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory)
96
97 #ifdef CONTEXT_THREADS
98   if (parmap)
99     xbt_parmap_destroy(parmap);
100 #endif
101   return smx_ctx_base_factory_finalize(factory);
102 }
103
104 smx_context_t
105 smx_ctx_sysv_create_context_sized(size_t size, xbt_main_func_t code,
106                                   int argc, char **argv,
107                                   void_pfn_smxprocess_t cleanup_func,
108                                   void *data)
109 {
110   union u_ctx_addr ctx_addr;
111   smx_ctx_sysv_t context =
112       (smx_ctx_sysv_t) smx_ctx_base_factory_create_context_sized(size,
113                                                                  code,
114                                                                  argc,
115                                                                  argv,
116                                                                  cleanup_func,
117                                                                  data);
118
119   /* if the user provided a function for the process then use it,
120      otherwise it is the context for maestro */
121   if (code) {
122
123     int res = getcontext(&(context->uc));
124     xbt_assert(res == 0,
125         "Error in context saving: %d (%s)", errno,
126         strerror(errno));
127
128     context->uc.uc_link = NULL;
129
130     context->uc.uc_stack.ss_sp =
131         pth_skaddr_makecontext(context->stack, smx_context_stack_size);
132
133     context->uc.uc_stack.ss_size =
134         pth_sksize_makecontext(context->stack, smx_context_stack_size);
135
136 #ifdef HAVE_VALGRIND_VALGRIND_H
137     context->valgrind_stack_id =
138         VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
139                                 ((char *) context->uc.uc_stack.ss_sp) +
140                                 context->uc.uc_stack.ss_size);
141 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
142     ctx_addr.addr = context;
143     makecontext(&context->uc, (void (*)())smx_ctx_sysv_wrapper,
144                 CTX_ADDR_LEN, CTX_ADDR_SPLIT(ctx_addr));
145   } else {
146     maestro_context = context;
147   }
148
149   return (smx_context_t) context;
150 }
151
152 static smx_context_t
153 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
154     void_pfn_smxprocess_t cleanup_func,
155     void *data)
156 {
157
158   return smx_ctx_sysv_create_context_sized(sizeof(s_smx_ctx_sysv_t) + smx_context_stack_size,
159                                            code, argc, argv, cleanup_func,
160                                            data);
161
162 }
163
164 void smx_ctx_sysv_free(smx_context_t context)
165 {
166
167   if (context) {
168
169 #ifdef HAVE_VALGRIND_VALGRIND_H
170     VALGRIND_STACK_DEREGISTER(((smx_ctx_sysv_t)
171                                context)->valgrind_stack_id);
172 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
173
174   }
175   smx_ctx_base_free(context);
176 }
177
178 void smx_ctx_sysv_wrapper(int first, ...)
179
180   union u_ctx_addr ctx_addr;
181   smx_ctx_sysv_t context;
182
183   ctx_addr.intv[0] = first;
184   if (CTX_ADDR_LEN > 1) {
185     va_list ap;
186     int i;
187     va_start(ap, first);
188     for (i = 1; i < CTX_ADDR_LEN; i++)
189       ctx_addr.intv[i] = va_arg(ap, int);
190     va_end(ap);
191   }
192   context = ctx_addr.addr;
193   (context->super.code) (context->super.argc, context->super.argv);
194
195   simix_global->context_factory->stop((smx_context_t) context);
196 }
197
198 static void smx_ctx_sysv_stop_serial(smx_context_t context)
199 {
200   smx_ctx_base_stop(context);
201   smx_ctx_sysv_suspend_serial(context);
202 }
203
204 static void smx_ctx_sysv_suspend_serial(smx_context_t context)
205 {
206   /* determine the next context */
207   smx_context_t next_context;
208   unsigned long int i = smx_ctx_sysv_process_index++;
209
210   if (i < xbt_dynar_length(simix_global->process_to_run)) {
211     XBT_DEBUG("Run next process");
212
213     /* execute the next process */
214     next_context = xbt_dynar_get_as(
215         simix_global->process_to_run,i, smx_process_t)->context;
216   }
217   else {
218     /* all processes were run, return to maestro */
219     XBT_DEBUG("No more process to run");
220
221     next_context = (smx_context_t) maestro_context;
222   }
223   SIMIX_context_set_current(next_context);
224   swapcontext(&((smx_ctx_sysv_t) context)->uc,
225       &((smx_ctx_sysv_t) next_context)->uc);
226 }
227
228 static void smx_ctx_sysv_resume(smx_process_t first_process)
229 {
230   smx_context_t context = first_process->context;
231   SIMIX_context_set_current(context);
232   swapcontext(&maestro_context->uc,
233       &((smx_ctx_sysv_t) context)->uc);
234 }
235
236 static void smx_ctx_sysv_runall_serial(void)
237 {
238   if (xbt_dynar_length(simix_global->process_to_run) > 0) {
239     smx_process_t first_process =
240         xbt_dynar_get_as(simix_global->process_to_run, 0, smx_process_t);
241     smx_ctx_sysv_process_index = 1;
242
243     /* execute the first process */
244     smx_ctx_sysv_resume(first_process);
245   }
246 }
247
248 static void smx_ctx_sysv_stop_parallel(smx_context_t context)
249 {
250   smx_ctx_base_stop(context);
251   smx_ctx_sysv_suspend_parallel(context);
252 }
253
254 static void smx_ctx_sysv_suspend_parallel(smx_context_t context)
255 {
256 #ifdef CONTEXT_THREADS
257   /* determine the next context */
258   smx_context_t next_context;
259   unsigned long int i = __sync_fetch_and_add(&parmap->index, 1);
260
261   if (i < xbt_dynar_length(simix_global->process_to_run)) {
262     /* execute the next process */
263     XBT_DEBUG("Run next process");
264     next_context = xbt_dynar_get_as(simix_global->process_to_run, i, smx_process_t)->context;
265   }
266   else {
267     /* all processes were run, go to the barrier */
268     XBT_DEBUG("No more processes to run");
269     next_context = (smx_context_t) maestro_context;
270   }
271
272   SIMIX_context_set_current(next_context);
273   swapcontext(&((smx_ctx_sysv_t) context)->uc,
274       &((smx_ctx_sysv_t) next_context)->uc);
275 #endif
276 }
277
278 static void smx_ctx_sysv_runall_parallel(void)
279 {
280 #ifdef CONTEXT_THREADS
281   xbt_parmap_apply(parmap, (void_f_pvoid_t) smx_ctx_sysv_resume,
282       simix_global->process_to_run);
283 #endif
284 }
285
286 smx_context_t smx_ctx_sysv_self_parallel(void)
287 {
288   /*smx_context_t self_context = (smx_context_t) xbt_os_thread_get_extra_data();
289   return self_context ? self_context : (smx_context_t) maestro_context;*/
290   return SIMIX_context_get_current();
291 }