Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New simplified API for the context factory [Cristian]
[simgrid.git] / src / simix / smx_context_sysv.c
1 /* $Id$ */
2
3 /* context_sysv - implementation of context switching with ucontextes from Sys V */
4
5 /* Copyright (c) 2004-2008 the SimGrid team. All right reserved */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "xbt/ex_interface.h"
11 #include "private.h"
12
13 #include "context_sysv_config.h"        /* loads context system definitions                             */
14 #include "portable.h"
15 #include <ucontext.h>           /* context relative declarations                                */
16 #define STACK_SIZE 128*1024     /* lower this if you want to reduce the memory consumption      */
17 #ifdef HAVE_VALGRIND_VALGRIND_H
18 #  include <valgrind/valgrind.h>
19 #endif /* HAVE_VALGRIND_VALGRIND_H */
20
21 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(smx_context);
22
23 typedef struct s_smx_ctx_sysv {
24   SMX_CTX_BASE_T;
25   ucontext_t uc;                /* the thread that execute the code */
26   char stack[STACK_SIZE];       /* the thread stack size */
27   struct s_smx_ctx_sysv *prev;           /* the previous process */
28 #ifdef HAVE_VALGRIND_VALGRIND_H
29   unsigned int valgrind_stack_id;       /* the valgrind stack id */
30 #endif                          
31 } s_smx_ctx_sysv_t, *smx_ctx_sysv_t;
32
33
34 /* callback: context fetching */
35 static ex_ctx_t *xbt_jcontext_ex_ctx(void);
36
37 /* callback: termination */
38 static void xbt_jcontext_ex_terminate(xbt_ex_t *e);
39
40 static smx_context_t smx_ctx_sysv_factory_create_context(xbt_main_func_t code);
41
42 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory);
43
44 static smx_context_t smx_ctx_sysv_factory_create_maestro_context(void);
45
46 static void smx_ctx_sysv_free(smx_context_t context);
47
48 static void smx_ctx_sysv_start(smx_context_t context);
49
50 static void smx_ctx_sysv_stop(int exit_code);
51
52 static void smx_ctx_sysv_suspend(smx_context_t context);
53
54 static void smx_ctx_sysv_resume(smx_context_t old_context,
55                                 smx_context_t new_context);
56
57 static void smx_ctx_sysv_wrapper(void);
58
59 /* callback: context fetching */
60 static ex_ctx_t *xbt_ctx_sysv_ex_ctx(void)
61 {
62   return simix_global->current_process->context->exception;
63 }
64
65 /* callback: termination */
66 static void xbt_ctx_sysv_ex_terminate(xbt_ex_t * e)
67 {
68   xbt_ex_display(e);
69   abort();
70 }
71
72 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory)
73 {
74   *factory = xbt_new0(s_smx_context_factory_t, 1);
75
76   (*factory)->create_context = smx_ctx_sysv_factory_create_context;
77   (*factory)->finalize = smx_ctx_sysv_factory_finalize;
78   (*factory)->create_maestro_context = smx_ctx_sysv_factory_create_maestro_context;
79   (*factory)->free = smx_ctx_sysv_free;
80   (*factory)->start = smx_ctx_sysv_start;
81   (*factory)->stop = smx_ctx_sysv_stop;
82   (*factory)->suspend = smx_ctx_sysv_suspend;
83   (*factory)->resume = smx_ctx_sysv_resume;
84   (*factory)->name = "smx_sysv_context_factory";
85
86   /* context exception handlers */
87   __xbt_ex_ctx = xbt_ctx_sysv_ex_ctx;
88   __xbt_ex_terminate = xbt_ctx_sysv_ex_terminate;
89 }
90
91 static smx_context_t smx_ctx_sysv_factory_create_maestro_context()
92 {
93   smx_ctx_sysv_t context = xbt_new0(s_smx_ctx_sysv_t, 1);
94
95   context->exception = xbt_new(ex_ctx_t, 1);  
96   XBT_CTX_INITIALIZE(context->exception);
97
98   return (smx_context_t)context;
99 }
100
101 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t * factory)
102 {
103   /*FIXME free(maestro_context->exception);*/
104   free(*factory);
105   *factory = NULL;
106   return 0;
107 }
108
109 static smx_context_t smx_ctx_sysv_factory_create_context(xbt_main_func_t code)
110 {
111   smx_ctx_sysv_t context = xbt_new0(s_smx_ctx_sysv_t, 1);
112
113   context->code = code;
114
115   xbt_assert2(getcontext(&(context->uc)) == 0,
116               "Error in context saving: %d (%s)", errno, strerror(errno));
117   context->uc.uc_link = NULL;
118   context->uc.uc_stack.ss_sp =
119     pth_skaddr_makecontext(context->stack, STACK_SIZE);
120   context->uc.uc_stack.ss_size =
121     pth_sksize_makecontext(context->stack, STACK_SIZE);
122 #ifdef HAVE_VALGRIND_VALGRIND_H
123   context->valgrind_stack_id =
124     VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
125                             ((char *) context->uc.uc_stack.ss_sp) +
126                             context->uc.uc_stack.ss_size);
127 #endif /* HAVE_VALGRIND_VALGRIND_H */
128
129   context->exception = xbt_new(ex_ctx_t, 1);
130   XBT_CTX_INITIALIZE(context->exception);
131   
132   return (smx_context_t)context;
133 }
134
135 static void smx_ctx_sysv_free(smx_context_t pcontext)
136 {
137   smx_ctx_sysv_t context = (smx_ctx_sysv_t)pcontext;   
138   if (context){
139
140     if (context->exception)
141       free(context->exception);
142
143 #ifdef HAVE_VALGRIND_VALGRIND_H
144     VALGRIND_STACK_DEREGISTER(((smx_ctx_sysv_t) context)->valgrind_stack_id);
145 #endif /* HAVE_VALGRIND_VALGRIND_H */
146
147     /* destroy the context */
148     free(context);
149   }
150 }
151
152 static void smx_ctx_sysv_start(smx_context_t context)
153 {
154   makecontext(&((smx_ctx_sysv_t)context)->uc, smx_ctx_sysv_wrapper, 0);
155 }
156
157 static void smx_ctx_sysv_stop(int exit_code)
158 {
159   if (simix_global->current_process->cleanup_func)
160     ((*simix_global->current_process->cleanup_func))
161       (simix_global->current_process->cleanup_arg);
162
163   smx_ctx_sysv_suspend(simix_global->current_process->context);
164 }
165
166 static void smx_ctx_sysv_wrapper(void)
167 {
168   smx_ctx_sysv_stop((*(simix_global->current_process->context->code))
169                         (simix_global->current_process->argc, 
170                          simix_global->current_process->argv));
171 }
172
173 static void smx_ctx_sysv_suspend(smx_context_t context)
174 {
175   int rv;
176
177   smx_ctx_sysv_t prev_context = ((smx_ctx_sysv_t) context)->prev;
178
179   ((smx_ctx_sysv_t) context)->prev = NULL;
180
181   rv = swapcontext(&((smx_ctx_sysv_t) context)->uc, &prev_context->uc);
182
183   xbt_assert0((rv == 0), "Context swapping failure");
184 }
185
186 static void smx_ctx_sysv_resume(smx_context_t old_context,
187                                 smx_context_t new_context)
188 {
189   int rv;
190
191   ((smx_ctx_sysv_t) new_context)->prev = (smx_ctx_sysv_t)old_context;
192
193   rv = swapcontext(&((smx_ctx_sysv_t)old_context)->uc,
194                    &((smx_ctx_sysv_t)new_context)->uc);
195
196   xbt_assert0((rv == 0), "Context swapping failure");
197 }