Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Throw an error at runtime instead of breaking compilation.
[simgrid.git] / src / simix / smx_context_raw.c
1 /* context_raw - 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 "xbt/threadpool.h"
10 #include "simix/private.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 /* lower this if you want to reduce the memory consumption  */
24 #ifndef CONTEXT_STACK_SIZE      /* allow lua to override this */
25 #define CONTEXT_STACK_SIZE 128*1024
26 #endif                          /*CONTEXT_STACK_SIZE */
27
28
29 typedef char * raw_stack_t;
30 typedef void (*rawctx_entry_point_t)(void *);
31
32 typedef struct s_smx_ctx_raw {
33   s_smx_ctx_base_t super;       /* Fields of super implementation */
34   char *malloced_stack; /* malloced area containing the stack */
35   raw_stack_t stack_top; /* pointer to stack top (within previous area) */
36 #ifdef HAVE_VALGRIND_VALGRIND_H
37   unsigned int valgrind_stack_id;       /* the valgrind stack id */
38 #endif
39 } s_smx_ctx_raw_t, *smx_ctx_raw_t;
40
41 smx_ctx_raw_t maestro_context;
42
43 extern raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
44                                    rawctx_entry_point_t entry_point, void* arg);
45 extern void raw_swapcontext(raw_stack_t* old, raw_stack_t* new);
46
47 #ifdef PROCESSOR_i686
48 __asm__ (
49 ".globl raw_makecontext\n"
50 "raw_makecontext:\n"
51 "   movl 4(%esp),%eax\n"  /* stack */
52 "   addl 8(%esp),%eax\n"  /* size  */
53 "   movl 12(%esp),%ecx\n" /* func  */
54 "   movl 16(%esp),%edx\n" /* arg   */
55 "   movl %edx, -4(%eax)\n"
56 "   movl $0,   -8(%eax)\n"
57 "   movl %ecx,-12(%eax)\n"
58 "   movl $0,  -16(%eax)\n"
59 "   movl $0,  -20(%eax)\n"
60 "   movl $0,  -24(%eax)\n"
61 "   movl $0,  -28(%eax)\n"
62 "   subl $28,%eax\n"
63 "   ret\n"
64 );
65
66 __asm__ (
67    ".globl raw_swapcontext\n"
68    "raw_swapcontext:\n"
69    "   movl 4(%esp),%eax\n" /* old */
70    "   movl 8(%esp),%edx\n" /* new */
71    "   pushl %ebp\n"
72    "   pushl %ebx\n"
73    "   pushl %esi\n"
74    "   pushl %edi\n"
75    "   movl %esp,(%eax)\n"
76    "   movl (%edx),%esp\n"
77    "   popl %edi\n"
78    "   popl %esi\n"
79    "   popl %ebx\n"
80    "   popl %ebp\n"
81    "   ret\n"
82 );
83 #else
84 raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
85                             rawctx_entry_point_t entry_point, void* arg) {
86    THROW_UNIMPLEMENTED;
87 }
88
89 void raw_swapcontext(raw_stack_t* old, raw_stack_t* new) {
90    THROW_UNIMPLEMENTED;
91 }
92
93 #endif
94
95 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
96
97 static xbt_tpool_t tpool;
98
99 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
100
101
102 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory)
103
104   if(tpool)
105     xbt_tpool_destroy(tpool);
106   return smx_ctx_base_factory_finalize(factory);
107 }
108
109 static smx_context_t
110 smx_ctx_raw_create_context_sized(size_t size, xbt_main_func_t code,
111     int argc, char **argv,
112     void_pfn_smxprocess_t cleanup_func,
113     void *data)
114 {
115
116   smx_ctx_raw_t context =
117       (smx_ctx_raw_t) smx_ctx_base_factory_create_context_sized(size,
118           code,
119           argc,
120           argv,
121           cleanup_func,
122           data);
123
124   /* If the user provided a function for the process then use it
125      otherwise is the context for maestro */
126      if (code) {
127        context->malloced_stack = xbt_malloc0(CONTEXT_STACK_SIZE);
128        context->stack_top =
129            raw_makecontext(context->malloced_stack,CONTEXT_STACK_SIZE,
130                (void(*)(void*))smx_ctx_raw_wrapper,context);
131
132 #ifdef HAVE_VALGRIND_VALGRIND_H
133        context->valgrind_stack_id =
134            VALGRIND_STACK_REGISTER(context->malloced_stack,
135                context->malloced_stack + CONTEXT_STACK_SIZE);
136 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
137
138      }else{
139        maestro_context = context;
140      }
141
142      return (smx_context_t) context;
143
144 }
145
146 static smx_context_t
147 smx_ctx_raw_create_context(xbt_main_func_t code, int argc, char **argv,
148     void_pfn_smxprocess_t cleanup_func,
149     void *data)
150 {
151
152   return smx_ctx_raw_create_context_sized(sizeof(s_smx_ctx_raw_t),
153       code, argc, argv, cleanup_func,
154       data);
155
156 }
157
158 static void smx_ctx_raw_free(smx_context_t context)
159 {
160
161   if (context) {
162
163 #ifdef HAVE_VALGRIND_VALGRIND_H
164     VALGRIND_STACK_DEREGISTER(((smx_ctx_raw_t)
165         context)->valgrind_stack_id);
166 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
167
168   }
169   smx_ctx_base_free(context);
170 }
171
172 static void smx_ctx_raw_suspend(smx_context_t context)
173 {
174   smx_current_context = (smx_context_t)maestro_context;
175   raw_swapcontext(&((smx_ctx_raw_t) context)->stack_top, &maestro_context->stack_top);
176 }
177
178 static void smx_ctx_raw_stop(smx_context_t context)
179 {
180   smx_ctx_base_stop(context);
181   smx_ctx_raw_suspend(context);
182 }
183
184 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context)
185
186   (context->super.code) (context->super.argc, context->super.argv);
187
188   smx_ctx_raw_stop((smx_context_t) context);
189 }
190
191 static void smx_ctx_raw_resume(smx_context_t context)
192 {
193   smx_current_context = context; 
194   raw_swapcontext(&maestro_context->stack_top, &((smx_ctx_raw_t) context)->stack_top);
195 }
196
197 static void smx_ctx_raw_runall(xbt_swag_t processes)
198 {
199   smx_process_t process;
200
201   while ((process = xbt_swag_extract(processes)))
202     smx_ctx_raw_resume(process->context);
203 }
204
205 static void smx_ctx_raw_resume_parallel(smx_context_t context)
206 {
207   xbt_os_thread_set_extra_data(context);
208   raw_swapcontext(&maestro_context->stack_top, &((smx_ctx_raw_t) context)->stack_top);
209 }
210
211 static void smx_ctx_raw_runall_parallel(xbt_swag_t processes)
212 {
213   smx_process_t process;
214   while((process = xbt_swag_extract(processes))){
215     xbt_tpool_queue_job(tpool, (void_f_pvoid_t)smx_ctx_raw_resume_parallel, process->context);
216   }
217   xbt_tpool_wait_all(tpool);
218 }
219
220 static smx_context_t smx_ctx_raw_self_parallel(void)
221 {
222   smx_context_t self_context = (smx_context_t) xbt_os_thread_get_extra_data();
223   return self_context ? self_context : (smx_context_t) maestro_context;
224 }
225
226 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory)
227 {
228   smx_ctx_base_factory_init(factory);
229
230   (*factory)->finalize  = smx_ctx_raw_factory_finalize;
231   (*factory)->create_context = smx_ctx_raw_create_context;
232   /* Do not overload that method (*factory)->finalize */
233   (*factory)->free = smx_ctx_raw_free;
234   (*factory)->stop = smx_ctx_raw_stop;
235   (*factory)->suspend = smx_ctx_raw_suspend;
236   (*factory)->name = "smx_raw_context_factory";
237
238   if(_surf_parallel_contexts){
239 #ifdef CONTEXT_THREADS  /* To use parallel ucontexts a thread pool is needed */
240     tpool = xbt_tpool_new(2, 10);
241     (*factory)->runall = smx_ctx_raw_runall_parallel;
242     (*factory)->self = smx_ctx_raw_self_parallel;
243 #else
244     THROW0(arg_error, 0, "No thread support for parallel context execution");
245 #endif
246   }else{
247     (*factory)->runall = smx_ctx_raw_runall;
248   }
249 }