Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b1636d7927fca89d8404e1a5e1b9b7b658a973d3
[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 "smx_context_private.h"
11
12 #ifdef HAVE_VALGRIND_VALGRIND_H
13 #  include <valgrind/valgrind.h>
14 #endif /* HAVE_VALGRIND_VALGRIND_H */
15
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
17
18 static smx_context_t 
19 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char** argv,
20     void_f_pvoid_t cleanup_func, void* cleanup_arg);
21
22
23 static void smx_ctx_sysv_wrapper(void);
24
25 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory) {
26
27   smx_ctx_base_factory_init(factory);
28
29   (*factory)->create_context = smx_ctx_sysv_create_context;
30   /* Do not overload that method (*factory)->finalize */
31   (*factory)->free = smx_ctx_sysv_free;
32   (*factory)->stop = smx_ctx_sysv_stop;
33   (*factory)->suspend = smx_ctx_sysv_suspend;
34   (*factory)->resume = smx_ctx_sysv_resume;
35   (*factory)->name = "smx_sysv_context_factory";
36 }
37
38 smx_context_t
39 smx_ctx_sysv_create_context_sized(size_t size, xbt_main_func_t code, int argc, char** argv,
40     void_f_pvoid_t cleanup_func, void* cleanup_arg) {
41
42   smx_ctx_sysv_t context = (smx_ctx_sysv_t)smx_ctx_base_factory_create_context_sized
43       (size, code,argc,argv,cleanup_func,cleanup_arg);
44
45   /* If the user provided a function for the process then use it
46      otherwise is the context for maestro */
47   if(code){
48
49     xbt_assert2(getcontext(&(context->uc)) == 0,
50         "Error in context saving: %d (%s)", errno, strerror(errno));
51
52     context->uc.uc_link = NULL;
53
54     context->uc.uc_stack.ss_sp =
55         pth_skaddr_makecontext(context->stack, CONTEXT_STACK_SIZE);
56
57     context->uc.uc_stack.ss_size =
58         pth_sksize_makecontext(context->stack, CONTEXT_STACK_SIZE);
59
60 #ifdef HAVE_VALGRIND_VALGRIND_H
61     context->valgrind_stack_id =
62         VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
63             ((char *) context->uc.uc_stack.ss_sp) +
64             context->uc.uc_stack.ss_size);
65 #endif /* HAVE_VALGRIND_VALGRIND_H */
66
67     makecontext(&((smx_ctx_sysv_t)context)->uc, smx_ctx_sysv_wrapper, 0);
68   }
69
70   return (smx_context_t)context;
71
72 }
73
74 static smx_context_t
75 smx_ctx_sysv_create_context(xbt_main_func_t code, int argc, char** argv,
76     void_f_pvoid_t cleanup_func, void* cleanup_arg) {
77
78   return smx_ctx_sysv_create_context_sized(sizeof(s_smx_ctx_sysv_t),
79       code,argc,argv,cleanup_func,cleanup_arg);
80
81 }
82
83 void smx_ctx_sysv_free(smx_context_t context) {
84
85   if (context){
86
87 #ifdef HAVE_VALGRIND_VALGRIND_H
88     VALGRIND_STACK_DEREGISTER(((smx_ctx_sysv_t) context)->valgrind_stack_id);
89 #endif /* HAVE_VALGRIND_VALGRIND_H */
90
91   }
92   smx_ctx_base_free(context);
93 }
94
95 void smx_ctx_sysv_stop(smx_context_t context) {
96   smx_ctx_base_stop(context);
97
98   smx_ctx_sysv_suspend(context);
99 }
100
101 void smx_ctx_sysv_wrapper() {
102   /*FIXME: I would like to avoid accessing simix_global to get the current
103     context by passing it as an argument of the wrapper function. The problem
104     is that this function is called from smx_ctx_sysv_start, and uses
105     makecontext for calling it, and the stupid posix specification states that
106     all the arguments of the function should be int(32 bits), making it useless
107     in 64-bit architectures where pointers are 64 bit long.
108    */
109   smx_ctx_sysv_t context = 
110       (smx_ctx_sysv_t)simix_global->current_process->context;
111
112   (context->super.code) (context->super.argc, context->super.argv);
113
114   smx_ctx_sysv_stop((smx_context_t)context);
115 }
116
117 void smx_ctx_sysv_suspend(smx_context_t context) {
118   ucontext_t maestro_ctx = ((smx_ctx_sysv_t)simix_global->maestro_process->context)->uc;
119
120   int rv = swapcontext(&((smx_ctx_sysv_t) context)->uc, &maestro_ctx);
121
122   xbt_assert0((rv == 0), "Context swapping failure");
123 }
124
125 void smx_ctx_sysv_resume(smx_context_t new_context) {
126   smx_ctx_sysv_t maestro = (smx_ctx_sysv_t)simix_global->maestro_process->context;
127
128   int rv = swapcontext(&(maestro->uc), &((smx_ctx_sysv_t)new_context)->uc);
129
130   xbt_assert0((rv == 0), "Context swapping failure");
131 }