Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c67ea319f2ef288792bb1ec1855ef9d426e991ae
[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
31 typedef struct s_smx_ctx_raw {
32   s_smx_ctx_base_t super;       /* Fields of super implementation */
33   char *malloced_stack; /* malloced area containing the stack */
34   raw_stack_t stack_top; /* pointer to stack top (within previous area) */
35 #ifdef HAVE_VALGRIND_VALGRIND_H
36   unsigned int valgrind_stack_id;       /* the valgrind stack id */
37 #endif
38 } s_smx_ctx_raw_t, *smx_ctx_raw_t;
39
40 smx_ctx_raw_t maestro_context;
41
42 static inline __attribute__((regparm(2))) void raw_swapcontext(raw_stack_t*old, raw_stack_t*new)  {
43   __asm__ __volatile__ (
44       "ret"
45       );
46 }
47 typedef void (*rawctx_entry_point_t)(void *);
48 static inline raw_stack_t raw_makecontext(char*malloced_stack, int stack_size,
49     rawctx_entry_point_t entry_point, void *arg) {
50   __asm__ __volatile__ (
51       "popl  %ebx"
52   );
53   return NULL;
54 }
55 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
56
57 static xbt_tpool_t tpool;
58
59 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
60
61
62 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory)
63
64   if(tpool)
65     xbt_tpool_destroy(tpool);
66   return smx_ctx_base_factory_finalize(factory);
67 }
68
69 static smx_context_t
70 smx_ctx_raw_create_context_sized(size_t size, xbt_main_func_t code,
71     int argc, char **argv,
72     void_pfn_smxprocess_t cleanup_func,
73     void *data)
74 {
75
76   smx_ctx_raw_t context =
77       (smx_ctx_raw_t) smx_ctx_base_factory_create_context_sized(size,
78           code,
79           argc,
80           argv,
81           cleanup_func,
82           data);
83
84   /* If the user provided a function for the process then use it
85      otherwise is the context for maestro */
86      if (code) {
87        context->malloced_stack = xbt_malloc0(CONTEXT_STACK_SIZE);
88        context->stack_top =
89            raw_makecontext(context->malloced_stack,CONTEXT_STACK_SIZE,
90                (void(*)(void*))smx_ctx_raw_wrapper,context);
91
92 #ifdef HAVE_VALGRIND_VALGRIND_H
93        context->valgrind_stack_id =
94            VALGRIND_STACK_REGISTER(context->malloced_stack,
95                context->malloced_stack + CONTEXT_STACK_SIZE);
96 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
97
98      }else{
99        maestro_context = context;
100      }
101
102      return (smx_context_t) context;
103
104 }
105
106 static smx_context_t
107 smx_ctx_raw_create_context(xbt_main_func_t code, int argc, char **argv,
108     void_pfn_smxprocess_t cleanup_func,
109     void *data)
110 {
111
112   return smx_ctx_raw_create_context_sized(sizeof(s_smx_ctx_raw_t),
113       code, argc, argv, cleanup_func,
114       data);
115
116 }
117
118 static void smx_ctx_raw_free(smx_context_t context)
119 {
120
121   if (context) {
122
123 #ifdef HAVE_VALGRIND_VALGRIND_H
124     VALGRIND_STACK_DEREGISTER(((smx_ctx_raw_t)
125         context)->valgrind_stack_id);
126 #endif                          /* HAVE_VALGRIND_VALGRIND_H */
127
128   }
129   smx_ctx_base_free(context);
130 }
131
132 static void smx_ctx_raw_suspend(smx_context_t context)
133 {
134   smx_current_context = (smx_context_t)maestro_context;
135   raw_swapcontext(&((smx_ctx_raw_t) context)->stack_top, &maestro_context->stack_top);
136 }
137
138 static void smx_ctx_raw_stop(smx_context_t context)
139 {
140   smx_ctx_base_stop(context);
141   smx_ctx_raw_suspend(context);
142 }
143
144 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context)
145
146   (context->super.code) (context->super.argc, context->super.argv);
147
148   smx_ctx_raw_stop((smx_context_t) context);
149 }
150
151 static void smx_ctx_raw_resume(smx_context_t context)
152 {
153   smx_current_context = context; 
154   raw_swapcontext(&maestro_context->stack_top, &((smx_ctx_raw_t) context)->stack_top);
155 }
156
157 static void smx_ctx_raw_runall(xbt_swag_t processes)
158 {
159   smx_process_t process;
160
161   while ((process = xbt_swag_extract(processes)))
162     smx_ctx_raw_resume(process->context);
163 }
164
165 static void smx_ctx_raw_resume_parallel(smx_context_t context)
166 {
167   xbt_os_thread_set_extra_data(context);
168   raw_swapcontext(&maestro_context->stack_top, &((smx_ctx_raw_t) context)->stack_top);
169 }
170
171 static void smx_ctx_raw_runall_parallel(xbt_swag_t processes)
172 {
173   smx_process_t process;
174   while((process = xbt_swag_extract(processes))){
175     xbt_tpool_queue_job(tpool, (void_f_pvoid_t)smx_ctx_raw_resume_parallel, process->context);
176   }
177   xbt_tpool_wait_all(tpool);
178 }
179
180 static smx_context_t smx_ctx_raw_self_parallel(void)
181 {
182   smx_context_t self_context = (smx_context_t) xbt_os_thread_get_extra_data();
183   return self_context ? self_context : (smx_context_t) maestro_context;
184 }
185
186 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory)
187 {
188   smx_ctx_base_factory_init(factory);
189
190   (*factory)->finalize  = smx_ctx_raw_factory_finalize;
191   (*factory)->create_context = smx_ctx_raw_create_context;
192   /* Do not overload that method (*factory)->finalize */
193   (*factory)->free = smx_ctx_raw_free;
194   (*factory)->stop = smx_ctx_raw_stop;
195   (*factory)->suspend = smx_ctx_raw_suspend;
196   (*factory)->name = "smx_raw_context_factory";
197
198   if(_surf_parallel_contexts){
199 #ifdef CONTEXT_THREADS  /* To use parallel ucontexts a thread pool is needed */
200     tpool = xbt_tpool_new(2, 10);
201     (*factory)->runall = smx_ctx_raw_runall_parallel;
202     (*factory)->self = smx_ctx_raw_self_parallel;
203 #else
204     THROW0(arg_error, 0, "No thread support for parallel context execution");
205 #endif
206   }else{
207     (*factory)->runall = smx_ctx_raw_runall;
208   }
209 }