Logo AND Algorithmique Numérique Distribuée

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