Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2bef17db166ebcaeed9f35babad6d06fbd60bc1d
[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
33 static void smx_ctx_sysv_wrapper(smx_ctx_sysv_t context);
34
35 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory)
36 {
37   smx_ctx_base_factory_init(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(_surf_parallel_contexts){
48     tpool = xbt_tpool_new(2, 10);
49     (*factory)->runall = smx_ctx_sysv_runall_parallel;
50   }else{
51     (*factory)->runall = smx_ctx_sysv_runall;
52   }    
53 }
54
55 int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory)
56
57   if(tpool)
58     xbt_tpool_destroy(tpool);
59   return smx_ctx_base_factory_finalize(factory);
60 }
61
62 smx_context_t
63 smx_ctx_sysv_create_context_sized(size_t size, xbt_main_func_t code,
64                                   int argc, char **argv,
65                                   void_pfn_smxprocess_t cleanup_func,
66                                   void *data)
67 {
68
69   smx_ctx_sysv_t context =
70       (smx_ctx_sysv_t) smx_ctx_base_factory_create_context_sized(size,
71                                                                  code,
72                                                                  argc,
73                                                                  argv,
74                                                                  cleanup_func,
75                                                                  data);
76
77   /* If the user provided a function for the process then use it
78      otherwise is the context for maestro */
79   if (code) {
80
81     xbt_assert2(getcontext(&(context->uc)) == 0,
82                 "Error in context saving: %d (%s)", errno,
83                 strerror(errno));
84
85     context->uc.uc_link = NULL;
86
87     context->uc.uc_stack.ss_sp =
88         pth_skaddr_makecontext(context->stack, CONTEXT_STACK_SIZE);
89
90     context->uc.uc_stack.ss_size =
91         pth_sksize_makecontext(context->stack, CONTEXT_STACK_SIZE);
92
93 #ifdef HAVE_VALGRIND_VALGRIND_H
94     context->valgrind_stack_id =
95         VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
96                                 ((char *) context->uc.uc_stack.ss_sp) +
97                                 context->uc.uc_stack.ss_size);
98 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
99
100     makecontext(&((smx_ctx_sysv_t) context)->uc, (void (*)())smx_ctx_sysv_wrapper,
101                 sizeof(void*)/sizeof(int), context);
102   }
103
104   return (smx_context_t) context;
105
106 }
107
108 static smx_context_t
109 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char **argv,
110     void_pfn_smxprocess_t cleanup_func,
111     void *data)
112 {
113
114   return smx_ctx_sysv_create_context_sized(sizeof(s_smx_ctx_sysv_t),
115                                            code, argc, argv, cleanup_func,
116                                            data);
117
118 }
119
120 void smx_ctx_sysv_free(smx_context_t context)
121 {
122
123   if (context) {
124
125 #ifdef HAVE_VALGRIND_VALGRIND_H
126     VALGRIND_STACK_DEREGISTER(((smx_ctx_sysv_t)
127                                context)->valgrind_stack_id);
128 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
129
130   }
131   smx_ctx_base_free(context);
132 }
133
134 void smx_ctx_sysv_stop(smx_context_t context)
135 {
136   smx_ctx_base_stop(context);
137
138   smx_ctx_sysv_suspend(context);
139 }
140
141 void smx_ctx_sysv_wrapper(smx_ctx_sysv_t context)
142
143   (context->super.code) (context->super.argc, context->super.argv);
144
145   smx_ctx_sysv_stop((smx_context_t) context);
146 }
147
148 void smx_ctx_sysv_suspend(smx_context_t context)
149 {
150   ucontext_t maestro_ctx =
151       ((smx_ctx_sysv_t) simix_global->maestro_process->context)->uc;
152
153   int rv = swapcontext(&((smx_ctx_sysv_t) context)->uc, &maestro_ctx);
154
155   xbt_assert0((rv == 0), "Context swapping failure");
156 }
157
158 void smx_ctx_sysv_resume(smx_context_t new_context)
159 {
160   smx_ctx_sysv_t maestro =
161       (smx_ctx_sysv_t) simix_global->maestro_process->context;
162
163   int rv =
164       swapcontext(&(maestro->uc), &((smx_ctx_sysv_t) new_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_context_t old_context;
172   smx_process_t process;
173   
174   while ((process = xbt_swag_extract(processes))) {
175     old_context = smx_current_context;
176     smx_current_context = process->context;
177     smx_ctx_sysv_resume(smx_current_context);
178     smx_current_context = old_context;
179   }
180 }
181
182 void smx_ctx_sysv_runall_parallel(xbt_swag_t processes)
183 {
184   smx_process_t process;
185   while((process = xbt_swag_extract(processes))){
186     xbt_tpool_queue_job(tpool, (void_f_pvoid_t)smx_ctx_sysv_resume, process->context);
187   }
188 }