Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
reimplement SIMIX_process_kill() without SIMIX_process_schedule() so that the latter...
[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 "private.h"
11 #include "context_sysv_config.h"        /* loads context system definitions */
12 #include "portable.h"
13 #include <ucontext.h>           /* context relative declarations */
14
15 /* lower this if you want to reduce the memory consumption  */
16 #define STACK_SIZE 128*1024
17
18 #ifdef HAVE_VALGRIND_VALGRIND_H
19 #  include <valgrind/valgrind.h>
20 #endif /* HAVE_VALGRIND_VALGRIND_H */
21
22 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
23
24 typedef struct s_smx_ctx_sysv {
25   SMX_CTX_BASE_T;
26   ucontext_t uc;                /* the thread that execute the code */
27   char stack[STACK_SIZE];       /* the thread stack size */
28   struct s_smx_ctx_sysv *prev;           /* the previous process */
29 #ifdef HAVE_VALGRIND_VALGRIND_H
30   unsigned int valgrind_stack_id;       /* the valgrind stack id */
31 #endif                          
32 } s_smx_ctx_sysv_t, *smx_ctx_sysv_t;
33
34 static smx_context_t 
35 smx_ctx_sysv_factory_create_context(xbt_main_func_t code, int argc, char** argv, 
36     void_f_pvoid_t cleanup_func, void* cleanup_arg);
37
38 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory);
39
40 static void smx_ctx_sysv_free(smx_context_t context);
41
42 static void smx_ctx_sysv_stop(smx_context_t context);
43
44 static void smx_ctx_sysv_suspend(smx_context_t context);
45
46 static void 
47 smx_ctx_sysv_resume(smx_context_t old_context, smx_context_t new_context);
48
49 static void smx_ctx_sysv_wrapper(void);
50
51 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory)
52 {
53   *factory = xbt_new0(s_smx_context_factory_t, 1);
54
55   (*factory)->create_context = smx_ctx_sysv_factory_create_context;
56   (*factory)->finalize = smx_ctx_sysv_factory_finalize;
57   (*factory)->free = smx_ctx_sysv_free;
58   (*factory)->stop = smx_ctx_sysv_stop;
59   (*factory)->suspend = smx_ctx_sysv_suspend;
60   (*factory)->resume = smx_ctx_sysv_resume;
61   (*factory)->name = "smx_sysv_context_factory";
62 }
63
64 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t * factory)
65 {
66   free(*factory);
67   *factory = NULL;
68   return 0;
69 }
70
71 static smx_context_t 
72 smx_ctx_sysv_factory_create_context(xbt_main_func_t code, int argc, char** argv, 
73     void_f_pvoid_t cleanup_func, void* cleanup_arg)
74 {
75   smx_ctx_sysv_t context = xbt_new0(s_smx_ctx_sysv_t, 1);
76
77   /* If the user provided a function for the process then use it
78      otherwise is the context for maestro */
79   if(code){
80     context->code = code;
81
82     xbt_assert2(getcontext(&(context->uc)) == 0,
83         "Error in context saving: %d (%s)", errno, strerror(errno));
84
85     context->uc.uc_link = NULL;
86
87     context->uc.uc_stack.ss_sp =
88         pth_skaddr_makecontext(context->stack, STACK_SIZE);
89
90     context->uc.uc_stack.ss_size =
91         pth_sksize_makecontext(context->stack, STACK_SIZE);
92
93 #ifdef HAVE_VALGRIND_VALGRIND_H
94     context->valgrind_stack_id =
95         VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
96             ((char *) context->uc.uc_stack.ss_sp) +
97             context->uc.uc_stack.ss_size);
98 #endif /* HAVE_VALGRIND_VALGRIND_H */
99
100     context->argc = argc;
101     context->argv = argv;
102     context->cleanup_func = cleanup_func;
103     context->cleanup_arg = cleanup_arg;
104
105     makecontext(&((smx_ctx_sysv_t)context)->uc, smx_ctx_sysv_wrapper, 0);
106   }
107
108   return (smx_context_t)context;
109 }
110
111 static void smx_ctx_sysv_free(smx_context_t pcontext)
112 {
113   int i;
114   smx_ctx_sysv_t context = (smx_ctx_sysv_t)pcontext;   
115   if (context){
116
117 #ifdef HAVE_VALGRIND_VALGRIND_H
118     VALGRIND_STACK_DEREGISTER(((smx_ctx_sysv_t) context)->valgrind_stack_id);
119 #endif /* HAVE_VALGRIND_VALGRIND_H */
120
121     /* free argv */
122     if (context->argv) {
123       for (i = 0; i < context->argc; i++)
124         if (context->argv[i])
125           free(context->argv[i]);
126
127       free(context->argv);
128     }
129
130     /* destroy the context */
131     free(context);
132   }
133 }
134
135 static void smx_ctx_sysv_stop(smx_context_t pcontext)
136 {
137   smx_ctx_sysv_t context = (smx_ctx_sysv_t)pcontext;
138
139   if (context->cleanup_func)
140     (*context->cleanup_func) (context->cleanup_arg);
141
142   smx_ctx_sysv_suspend(pcontext);
143 }
144
145 static void smx_ctx_sysv_wrapper()
146 {
147   /*FIXME: I would like to avoid accessing simix_global to get the current
148     context by passing it as an argument of the wrapper function. The problem
149     is that this function is called from smx_ctx_sysv_start, and uses
150     makecontext for calling it, and the stupid posix specification states that
151     all the arguments of the function should be int(32 bits), making it useless
152     in 64-bit architectures where pointers are 64 bit long.
153    */
154   smx_ctx_sysv_t context = 
155       (smx_ctx_sysv_t)simix_global->current_process->context;
156
157   (context->code) (context->argc, context->argv);
158
159   smx_ctx_sysv_stop((smx_context_t)context);
160 }
161
162 static void smx_ctx_sysv_suspend(smx_context_t context)
163 {
164   int rv;
165
166   smx_ctx_sysv_t prev_context = ((smx_ctx_sysv_t) context)->prev;
167
168   ((smx_ctx_sysv_t) context)->prev = NULL;
169
170   rv = swapcontext(&((smx_ctx_sysv_t) context)->uc, &prev_context->uc);
171
172   xbt_assert0((rv == 0), "Context swapping failure");
173 }
174
175 static void 
176 smx_ctx_sysv_resume(smx_context_t old_context, smx_context_t new_context)
177 {
178   int rv;
179
180   ((smx_ctx_sysv_t) new_context)->prev = (smx_ctx_sysv_t)old_context;
181
182   rv = swapcontext(&((smx_ctx_sysv_t)old_context)->uc,
183       &((smx_ctx_sysv_t)new_context)->uc);
184
185   xbt_assert0((rv == 0), "Context swapping failure");
186 }