Logo AND Algorithmique Numérique Distribuée

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