Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d17a0807a8e4ef7cae0d4115a4b4cd443aa29922
[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/threadpool.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_tpool_t tpool;
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
38   (*factory)->finalize = smx_ctx_sysv_factory_finalize;
39   (*factory)->create_context = smx_ctx_sysv_create_context;
40   /* Do not overload that method (*factory)->finalize */
41   (*factory)->free = smx_ctx_sysv_free;
42   (*factory)->stop = smx_ctx_sysv_stop;
43   (*factory)->suspend = smx_ctx_sysv_suspend;
44   (*factory)->name = "smx_sysv_context_factory";
45
46   if(_surf_parallel_contexts){
47 #ifdef CONTEXT_THREADS  /* To use parallel ucontexts a thread pool is needed */
48     tpool = xbt_tpool_new(2, 10);
49     (*factory)->runall = smx_ctx_sysv_runall_parallel;
50     (*factory)->self = smx_ctx_sysv_self_parallel;
51 #else
52     THROW0(arg_error, 0, "No thread support for parallel context execution");
53 #endif
54   }else{
55     (*factory)->runall = smx_ctx_sysv_runall;
56   }    
57 }
58
59 int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory)
60
61   if(tpool)
62     xbt_tpool_destroy(tpool);
63   return smx_ctx_base_factory_finalize(factory);
64 }
65
66 smx_context_t
67 smx_ctx_sysv_create_context_sized(size_t size, xbt_main_func_t code,
68                                   int argc, char **argv,
69                                   void_pfn_smxprocess_t cleanup_func,
70                                   void *data)
71 {
72
73   smx_ctx_sysv_t context =
74       (smx_ctx_sysv_t) smx_ctx_base_factory_create_context_sized(size,
75                                                                  code,
76                                                                  argc,
77                                                                  argv,
78                                                                  cleanup_func,
79                                                                  data);
80
81   /* If the user provided a function for the process then use it
82      otherwise is the context for maestro */
83   if (code) {
84
85     xbt_assert2(getcontext(&(context->uc)) == 0,
86                 "Error in context saving: %d (%s)", errno,
87                 strerror(errno));
88
89     context->uc.uc_link = NULL;
90
91     context->uc.uc_stack.ss_sp =
92         pth_skaddr_makecontext(context->stack, CONTEXT_STACK_SIZE);
93
94     context->uc.uc_stack.ss_size =
95         pth_sksize_makecontext(context->stack, CONTEXT_STACK_SIZE);
96
97 #ifdef HAVE_VALGRIND_VALGRIND_H
98     context->valgrind_stack_id =
99         VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
100                                 ((char *) context->uc.uc_stack.ss_sp) +
101                                 context->uc.uc_stack.ss_size);
102 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
103
104     makecontext(&((smx_ctx_sysv_t) context)->uc, (void (*)())smx_ctx_sysv_wrapper,
105                 sizeof(void*)/sizeof(int), context);
106   }else{
107     maestro_context = context;
108   }
109
110   return (smx_context_t) context;
111
112 }
113
114 static smx_context_t
115 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
116     void_pfn_smxprocess_t cleanup_func,
117     void *data)
118 {
119
120   return smx_ctx_sysv_create_context_sized(sizeof(s_smx_ctx_sysv_t),
121                                            code, argc, argv, cleanup_func,
122                                            data);
123
124 }
125
126 void smx_ctx_sysv_free(smx_context_t context)
127 {
128
129   if (context) {
130
131 #ifdef HAVE_VALGRIND_VALGRIND_H
132     VALGRIND_STACK_DEREGISTER(((smx_ctx_sysv_t)
133                                context)->valgrind_stack_id);
134 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
135
136   }
137   smx_ctx_base_free(context);
138 }
139
140 void smx_ctx_sysv_stop(smx_context_t context)
141 {
142   smx_ctx_base_stop(context);
143   smx_ctx_sysv_suspend(context);
144 }
145
146 void smx_ctx_sysv_wrapper(smx_ctx_sysv_t context)
147
148   (context->super.code) (context->super.argc, context->super.argv);
149
150   smx_ctx_sysv_stop((smx_context_t) context);
151 }
152
153 void smx_ctx_sysv_suspend(smx_context_t context)
154 {
155   smx_current_context = (smx_context_t)maestro_context;
156   int rv = swapcontext(&((smx_ctx_sysv_t) context)->uc, &((smx_ctx_sysv_t)context)->old_uc);
157
158   xbt_assert0((rv == 0), "Context swapping failure");
159 }
160
161 void smx_ctx_sysv_resume(smx_context_t context)
162 {
163   smx_current_context = context; 
164   int rv = swapcontext(&((smx_ctx_sysv_t)context)->old_uc, &((smx_ctx_sysv_t) context)->uc);
165
166   xbt_assert0((rv == 0), "Context swapping failure");
167 }
168
169 void smx_ctx_sysv_runall(xbt_swag_t processes)
170 {
171   smx_process_t process;
172   
173   while ((process = xbt_swag_extract(processes)))
174     smx_ctx_sysv_resume(process->context);
175 }
176
177 void smx_ctx_sysv_resume_parallel(smx_context_t context)
178 {
179   xbt_os_thread_set_extra_data(context);
180   int rv = swapcontext(&((smx_ctx_sysv_t)context)->old_uc, &((smx_ctx_sysv_t) context)->uc);
181   xbt_os_thread_set_extra_data(NULL);
182
183   xbt_assert0((rv == 0), "Context swapping failure");
184 }
185
186 void smx_ctx_sysv_runall_parallel(xbt_swag_t processes)
187 {
188   smx_process_t process;
189   while((process = xbt_swag_extract(processes))){
190     xbt_tpool_queue_job(tpool, (void_f_pvoid_t)smx_ctx_sysv_resume_parallel, process->context);
191   }
192   xbt_tpool_wait_all(tpool);
193 }
194
195 smx_context_t smx_ctx_sysv_self_parallel(void)
196 {
197   smx_context_t self_context = (smx_context_t) xbt_os_thread_get_extra_data();
198   return self_context ? self_context : (smx_context_t) maestro_context;
199 }