Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4d303292268708a941a4c2464ccc846d615fdd29
[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 #else
72     THROWF(arg_error, 0, "No thread support for parallel context execution");
73 #endif
74   }else{
75     (*factory)->runall = smx_ctx_sysv_runall;
76   }    
77 }
78
79 int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory)
80
81 #ifdef CONTEXT_THREADS
82   if(parmap)
83     xbt_parmap_destroy(parmap);
84 #endif
85   return smx_ctx_base_factory_finalize(factory);
86 }
87
88 smx_context_t
89 smx_ctx_sysv_create_context_sized(size_t size, xbt_main_func_t code,
90                                   int argc, char **argv,
91                                   void_pfn_smxprocess_t cleanup_func,
92                                   void *data)
93 {
94   union u_ctx_addr ctx_addr;
95   smx_ctx_sysv_t context =
96       (smx_ctx_sysv_t) smx_ctx_base_factory_create_context_sized(size,
97                                                                  code,
98                                                                  argc,
99                                                                  argv,
100                                                                  cleanup_func,
101                                                                  data);
102
103   /* If the user provided a function for the process then use it
104      otherwise is the context for maestro */
105   if (code) {
106
107     int res;
108     res = getcontext(&(context->uc));
109     xbt_assert(res == 0,
110                 "Error in context saving: %d (%s)", errno,
111                 strerror(errno));
112
113     context->uc.uc_link = NULL;
114
115     context->uc.uc_stack.ss_sp =
116         pth_skaddr_makecontext(context->stack, smx_context_stack_size);
117
118     context->uc.uc_stack.ss_size =
119         pth_sksize_makecontext(context->stack, smx_context_stack_size);
120
121 #ifdef HAVE_VALGRIND_VALGRIND_H
122     context->valgrind_stack_id =
123         VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
124                                 ((char *) context->uc.uc_stack.ss_sp) +
125                                 context->uc.uc_stack.ss_size);
126 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
127     ctx_addr.addr = context;
128     makecontext(&context->uc, (void (*)())smx_ctx_sysv_wrapper,
129                 CTX_ADDR_LEN, CTX_ADDR_SPLIT(ctx_addr));
130   }else{
131     maestro_context = context;
132   }
133
134   return (smx_context_t) context;
135
136 }
137
138 static smx_context_t
139 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
140     void_pfn_smxprocess_t cleanup_func,
141     void *data)
142 {
143
144   return smx_ctx_sysv_create_context_sized(sizeof(s_smx_ctx_sysv_t) + smx_context_stack_size,
145                                            code, argc, argv, cleanup_func,
146                                            data);
147
148 }
149
150 void smx_ctx_sysv_free(smx_context_t context)
151 {
152
153   if (context) {
154
155 #ifdef HAVE_VALGRIND_VALGRIND_H
156     VALGRIND_STACK_DEREGISTER(((smx_ctx_sysv_t)
157                                context)->valgrind_stack_id);
158 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
159
160   }
161   smx_ctx_base_free(context);
162 }
163
164 void smx_ctx_sysv_stop(smx_context_t context)
165 {
166   smx_ctx_base_stop(context);
167   smx_ctx_sysv_suspend(context);
168 }
169
170 void smx_ctx_sysv_wrapper(int first, ...)
171
172   union u_ctx_addr ctx_addr;
173   smx_ctx_sysv_t context;
174
175   ctx_addr.intv[0] = first;
176   if (CTX_ADDR_LEN > 1) {
177     va_list ap;
178     int i;
179     va_start(ap, first);
180     for(i = 1; i < CTX_ADDR_LEN; i++)
181       ctx_addr.intv[i] = va_arg(ap, int);
182     va_end(ap);
183   }
184   context = ctx_addr.addr;
185   (context->super.code) (context->super.argc, context->super.argv);
186
187   smx_ctx_sysv_stop((smx_context_t) context);
188 }
189
190 void smx_ctx_sysv_suspend(smx_context_t context)
191 {
192   SIMIX_context_set_current((smx_context_t) maestro_context);
193   int rv;
194   rv = swapcontext(&((smx_ctx_sysv_t) context)->uc, &((smx_ctx_sysv_t)context)->old_uc);
195
196   xbt_assert((rv == 0), "Context swapping failure");
197 }
198
199 void smx_ctx_sysv_resume(smx_context_t context)
200 {
201   SIMIX_context_set_current(context);
202   int rv;
203   rv = swapcontext(&((smx_ctx_sysv_t)context)->old_uc, &((smx_ctx_sysv_t) context)->uc);
204
205   xbt_assert((rv == 0), "Context swapping failure");
206 }
207
208 void smx_ctx_sysv_runall(xbt_dynar_t processes)
209 {
210   smx_process_t process;
211   unsigned int cursor;
212
213   xbt_dynar_foreach(processes, cursor, process) {
214     XBT_DEBUG("Schedule item %u of %lu",cursor,xbt_dynar_length(processes));
215     smx_ctx_sysv_resume(process->context);
216   }
217 }
218
219 void smx_ctx_sysv_resume_parallel(smx_process_t process)
220 {
221   smx_context_t context = process->context;
222   SIMIX_context_set_current((smx_context_t) context);
223   int rv;
224   rv = swapcontext(&((smx_ctx_sysv_t)context)->old_uc, &((smx_ctx_sysv_t) context)->uc);
225   SIMIX_context_set_current((smx_context_t) maestro_context);
226
227   xbt_assert((rv == 0), "Context swapping failure");
228 }
229
230 void smx_ctx_sysv_runall_parallel(xbt_dynar_t processes)
231 {
232 #ifdef CONTEXT_THREADS
233   xbt_parmap_apply(parmap, (void_f_pvoid_t)smx_ctx_sysv_resume_parallel, processes);
234 #endif
235 }
236
237 smx_context_t smx_ctx_sysv_self_parallel(void)
238 {
239   /*smx_context_t self_context = (smx_context_t) xbt_os_thread_get_extra_data();
240   return self_context ? self_context : (smx_context_t) maestro_context;*/
241   return SIMIX_context_get_current();
242 }