Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove usage of xbt_assert[0-9].
[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 "simix/private.h"
14 #include "gras_config.h"
15
16 #ifdef HAVE_VALGRIND_VALGRIND_H
17 #  include <valgrind/valgrind.h>
18 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
19
20 #ifdef _XBT_WIN32
21 #include "win32_ucontext.h"
22 #else
23 #include "ucontext.h"
24 #endif
25
26 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
27
28 #ifdef CONTEXT_THREADS
29 static xbt_parmap_t parmap;
30 #endif
31
32 static smx_context_t
33 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
34     void_pfn_smxprocess_t cleanup_func, void* data);
35
36 static void smx_ctx_sysv_wrapper(int count, ...);
37
38 /* This is a bit paranoid about SIZEOF_VOIDP not being a multiple of SIZEOF_INT,
39  * but it doesn't harm. */
40 #define CTX_ADDR_LEN (SIZEOF_VOIDP / SIZEOF_INT + !!(SIZEOF_VOIDP % SIZEOF_INT))
41 union u_ctx_addr {
42   void *addr;
43   int intv[CTX_ADDR_LEN];
44 };
45 #if (CTX_ADDR_LEN == 1)
46 #  define CTX_ADDR_SPLIT(u) (u).intv[0]
47 #elif (CTX_ADDR_LEN == 2)
48 #  define CTX_ADDR_SPLIT(u) (u).intv[0], (u).intv[1]
49 #else
50 #  error Your architecture is not supported yet
51 #endif
52
53 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory)
54 {
55   smx_ctx_base_factory_init(factory);
56   XBT_VERB("Activating SYSV context factory");
57
58   (*factory)->finalize = smx_ctx_sysv_factory_finalize;
59   (*factory)->create_context = smx_ctx_sysv_create_context;
60   /* Do not overload that method (*factory)->finalize */
61   (*factory)->free = smx_ctx_sysv_free;
62   (*factory)->stop = smx_ctx_sysv_stop;
63   (*factory)->suspend = smx_ctx_sysv_suspend;
64   (*factory)->name = "smx_sysv_context_factory";
65
66   if (SIMIX_context_is_parallel()) {
67 #ifdef CONTEXT_THREADS  /* To use parallel ucontexts a thread pool is needed */
68     parmap = xbt_parmap_new(2);
69     (*factory)->runall = smx_ctx_sysv_runall_parallel;
70     (*factory)->self = smx_ctx_sysv_self_parallel;
71     (*factory)->get_thread_id = smx_ctx_sysv_get_thread_id;
72 #else
73     THROWF(arg_error, 0, "No thread support for parallel context execution");
74 #endif
75   }else{
76     (*factory)->runall = smx_ctx_sysv_runall;
77   }    
78 }
79
80 int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory)
81
82 #ifdef CONTEXT_THREADS
83   if(parmap)
84     xbt_parmap_destroy(parmap);
85 #endif
86   return smx_ctx_base_factory_finalize(factory);
87 }
88
89 smx_context_t
90 smx_ctx_sysv_create_context_sized(size_t size, xbt_main_func_t code,
91                                   int argc, char **argv,
92                                   void_pfn_smxprocess_t cleanup_func,
93                                   void *data)
94 {
95   union u_ctx_addr ctx_addr;
96   smx_ctx_sysv_t context =
97       (smx_ctx_sysv_t) smx_ctx_base_factory_create_context_sized(size,
98                                                                  code,
99                                                                  argc,
100                                                                  argv,
101                                                                  cleanup_func,
102                                                                  data);
103
104   /* If the user provided a function for the process then use it
105      otherwise is the context for maestro */
106   if (code) {
107
108     int res;
109     res = getcontext(&(context->uc));
110     xbt_assert(res == 0,
111                 "Error in context saving: %d (%s)", errno,
112                 strerror(errno));
113
114     context->uc.uc_link = NULL;
115
116     context->uc.uc_stack.ss_sp =
117         pth_skaddr_makecontext(context->stack, smx_context_stack_size);
118
119     context->uc.uc_stack.ss_size =
120         pth_sksize_makecontext(context->stack, smx_context_stack_size);
121
122 #ifdef HAVE_VALGRIND_VALGRIND_H
123     context->valgrind_stack_id =
124         VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
125                                 ((char *) context->uc.uc_stack.ss_sp) +
126                                 context->uc.uc_stack.ss_size);
127 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
128     ctx_addr.addr = context;
129     makecontext(&context->uc, (void (*)())smx_ctx_sysv_wrapper,
130                 CTX_ADDR_LEN, CTX_ADDR_SPLIT(ctx_addr));
131   }else{
132     maestro_context = context;
133   }
134
135   return (smx_context_t) context;
136
137 }
138
139 static smx_context_t
140 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
141     void_pfn_smxprocess_t cleanup_func,
142     void *data)
143 {
144
145   return smx_ctx_sysv_create_context_sized(sizeof(s_smx_ctx_sysv_t) + smx_context_stack_size,
146                                            code, argc, argv, cleanup_func,
147                                            data);
148
149 }
150
151 void smx_ctx_sysv_free(smx_context_t context)
152 {
153
154   if (context) {
155
156 #ifdef HAVE_VALGRIND_VALGRIND_H
157     VALGRIND_STACK_DEREGISTER(((smx_ctx_sysv_t)
158                                context)->valgrind_stack_id);
159 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
160
161   }
162   smx_ctx_base_free(context);
163 }
164
165 void smx_ctx_sysv_stop(smx_context_t context)
166 {
167   smx_ctx_base_stop(context);
168   smx_ctx_sysv_suspend(context);
169 }
170
171 void smx_ctx_sysv_wrapper(int first, ...)
172
173   union u_ctx_addr ctx_addr;
174   smx_ctx_sysv_t context;
175
176   ctx_addr.intv[0] = first;
177   if (CTX_ADDR_LEN > 1) {
178     va_list ap;
179     int i;
180     va_start(ap, first);
181     for(i = 1; i < CTX_ADDR_LEN; i++)
182       ctx_addr.intv[i] = va_arg(ap, int);
183     va_end(ap);
184   }
185   context = ctx_addr.addr;
186   (context->super.code) (context->super.argc, context->super.argv);
187
188   smx_ctx_sysv_stop((smx_context_t) context);
189 }
190
191 void smx_ctx_sysv_suspend(smx_context_t context)
192 {
193   smx_current_context = (smx_context_t)maestro_context;
194   int rv;
195   rv = swapcontext(&((smx_ctx_sysv_t) context)->uc, &((smx_ctx_sysv_t)context)->old_uc);
196
197   xbt_assert((rv == 0), "Context swapping failure");
198 }
199
200 void smx_ctx_sysv_resume(smx_context_t context)
201 {
202   smx_current_context = context; 
203   int rv;
204   rv = swapcontext(&((smx_ctx_sysv_t)context)->old_uc, &((smx_ctx_sysv_t) context)->uc);
205
206   xbt_assert((rv == 0), "Context swapping failure");
207 }
208
209 void smx_ctx_sysv_runall(xbt_dynar_t processes)
210 {
211   smx_process_t process;
212   unsigned int cursor;
213
214   xbt_dynar_foreach(processes, cursor, process) {
215     XBT_DEBUG("Schedule item %u of %lu",cursor,xbt_dynar_length(processes));
216     smx_ctx_sysv_resume(process->context);
217   }
218   xbt_dynar_reset(processes);
219 }
220
221 void smx_ctx_sysv_resume_parallel(smx_process_t process)
222 {
223   smx_context_t context = process->context;
224   smx_current_context = (smx_context_t)context;
225   int rv;
226   rv = swapcontext(&((smx_ctx_sysv_t)context)->old_uc, &((smx_ctx_sysv_t) context)->uc);
227   smx_current_context = (smx_context_t)maestro_context;
228
229   xbt_assert((rv == 0), "Context swapping failure");
230 }
231
232 void smx_ctx_sysv_runall_parallel(xbt_dynar_t processes)
233 {
234 #ifdef CONTEXT_THREADS
235   xbt_parmap_apply(parmap, (void_f_pvoid_t)smx_ctx_sysv_resume_parallel, processes);
236 #endif
237   xbt_dynar_reset(processes);
238 }
239
240 smx_context_t smx_ctx_sysv_self_parallel(void)
241 {
242   /*smx_context_t self_context = (smx_context_t) xbt_os_thread_get_extra_data();
243   return self_context ? self_context : (smx_context_t) maestro_context;*/
244   return smx_current_context;
245 }
246
247 int smx_ctx_sysv_get_thread_id(void)
248 {
249   return (int)(unsigned long)xbt_os_thread_get_extra_data();
250 }