Logo AND Algorithmique Numérique Distribuée

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