Logo AND Algorithmique Numérique Distribuée

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