Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5c30df546148cffb20d90333620111b7625e2d6f
[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 "smx_context_sysv_private.h"
10 #include "xbt/parmap.h"
11 #include "simix/private.h"
12
13 #ifdef HAVE_VALGRIND_VALGRIND_H
14 #  include <valgrind/valgrind.h>
15 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
16
17 #ifdef _XBT_WIN32
18 #include "win32_ucontext.h"
19 #include "win32_ucontext.c"
20 #else
21 #include "ucontext.h"
22 #endif
23
24 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
25
26 static xbt_parmap_t parmap;
27
28 static smx_context_t
29 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
30     void_pfn_smxprocess_t cleanup_func, void* data);
31
32 static void smx_ctx_sysv_wrapper(smx_ctx_sysv_t context);
33
34 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory)
35 {
36   smx_ctx_base_factory_init(factory);
37   VERB0("Activating SYSV context factory");
38
39   (*factory)->finalize = smx_ctx_sysv_factory_finalize;
40   (*factory)->create_context = smx_ctx_sysv_create_context;
41   /* Do not overload that method (*factory)->finalize */
42   (*factory)->free = smx_ctx_sysv_free;
43   (*factory)->stop = smx_ctx_sysv_stop;
44   (*factory)->suspend = smx_ctx_sysv_suspend;
45   (*factory)->name = "smx_sysv_context_factory";
46
47   if (smx_parallel_contexts) {
48 #ifdef CONTEXT_THREADS  /* To use parallel ucontexts a thread pool is needed */
49     parmap = xbt_parmap_new(2);
50     (*factory)->runall = smx_ctx_sysv_runall_parallel;
51     (*factory)->self = smx_ctx_sysv_self_parallel;
52 #else
53     THROW0(arg_error, 0, "No thread support for parallel context execution");
54 #endif
55   }else{
56     (*factory)->runall = smx_ctx_sysv_runall;
57   }    
58 }
59
60 int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory)
61
62   if(parmap)
63     xbt_parmap_destroy(parmap);
64   return smx_ctx_base_factory_finalize(factory);
65 }
66
67 smx_context_t
68 smx_ctx_sysv_create_context_sized(size_t size, xbt_main_func_t code,
69                                   int argc, char **argv,
70                                   void_pfn_smxprocess_t cleanup_func,
71                                   void *data)
72 {
73
74   smx_ctx_sysv_t context =
75       (smx_ctx_sysv_t) smx_ctx_base_factory_create_context_sized(size,
76                                                                  code,
77                                                                  argc,
78                                                                  argv,
79                                                                  cleanup_func,
80                                                                  data);
81
82   /* If the user provided a function for the process then use it
83      otherwise is the context for maestro */
84   if (code) {
85
86     xbt_assert2(getcontext(&(context->uc)) == 0,
87                 "Error in context saving: %d (%s)", errno,
88                 strerror(errno));
89
90     context->uc.uc_link = NULL;
91
92     context->uc.uc_stack.ss_sp =
93         pth_skaddr_makecontext(context->stack, CONTEXT_STACK_SIZE);
94
95     context->uc.uc_stack.ss_size =
96         pth_sksize_makecontext(context->stack, CONTEXT_STACK_SIZE);
97
98 #ifdef HAVE_VALGRIND_VALGRIND_H
99     context->valgrind_stack_id =
100         VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
101                                 ((char *) context->uc.uc_stack.ss_sp) +
102                                 context->uc.uc_stack.ss_size);
103 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
104
105     makecontext(&((smx_ctx_sysv_t) context)->uc, (void (*)())smx_ctx_sysv_wrapper,
106                 sizeof(void*)/sizeof(int), context);
107   }else{
108     maestro_context = context;
109   }
110
111   return (smx_context_t) context;
112
113 }
114
115 static smx_context_t
116 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
117     void_pfn_smxprocess_t cleanup_func,
118     void *data)
119 {
120
121   return smx_ctx_sysv_create_context_sized(sizeof(s_smx_ctx_sysv_t),
122                                            code, argc, argv, cleanup_func,
123                                            data);
124
125 }
126
127 void smx_ctx_sysv_free(smx_context_t context)
128 {
129
130   if (context) {
131
132 #ifdef HAVE_VALGRIND_VALGRIND_H
133     VALGRIND_STACK_DEREGISTER(((smx_ctx_sysv_t)
134                                context)->valgrind_stack_id);
135 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
136
137   }
138   smx_ctx_base_free(context);
139 }
140
141 void smx_ctx_sysv_stop(smx_context_t context)
142 {
143   smx_ctx_base_stop(context);
144   smx_ctx_sysv_suspend(context);
145 }
146
147 void smx_ctx_sysv_wrapper(smx_ctx_sysv_t context)
148
149   (context->super.code) (context->super.argc, context->super.argv);
150
151   smx_ctx_sysv_stop((smx_context_t) context);
152 }
153
154 void smx_ctx_sysv_suspend(smx_context_t context)
155 {
156   smx_current_context = (smx_context_t)maestro_context;
157   int rv = swapcontext(&((smx_ctx_sysv_t) context)->uc, &((smx_ctx_sysv_t)context)->old_uc);
158
159   xbt_assert0((rv == 0), "Context swapping failure");
160 }
161
162 void smx_ctx_sysv_resume(smx_context_t context)
163 {
164   smx_current_context = context; 
165   int rv = swapcontext(&((smx_ctx_sysv_t)context)->old_uc, &((smx_ctx_sysv_t) context)->uc);
166
167   xbt_assert0((rv == 0), "Context swapping failure");
168 }
169
170 void smx_ctx_sysv_runall(xbt_dynar_t processes)
171 {
172   smx_process_t process;
173   unsigned int cursor;
174
175   xbt_dynar_foreach(processes, cursor, process) {
176     DEBUG2("Schedule item %u of %lu",cursor,xbt_dynar_length(processes));
177     smx_ctx_sysv_resume(process->context);
178   }
179   xbt_dynar_reset(processes);
180 }
181
182 void smx_ctx_sysv_resume_parallel(smx_process_t process)
183 {
184   smx_context_t context = process->context;
185   xbt_os_thread_set_extra_data(context);
186   int rv = swapcontext(&((smx_ctx_sysv_t)context)->old_uc, &((smx_ctx_sysv_t) context)->uc);
187   xbt_os_thread_set_extra_data(NULL);
188
189   xbt_assert0((rv == 0), "Context swapping failure");
190 }
191
192 void smx_ctx_sysv_runall_parallel(xbt_dynar_t processes)
193 {
194   xbt_parmap_apply(parmap, (void_f_pvoid_t)smx_ctx_sysv_resume_parallel, processes);
195   xbt_dynar_reset(processes);
196 }
197
198 smx_context_t smx_ctx_sysv_self_parallel(void)
199 {
200   smx_context_t self_context = (smx_context_t) xbt_os_thread_get_extra_data();
201   return self_context ? self_context : (smx_context_t) maestro_context;
202 }