Logo AND Algorithmique Numérique Distribuée

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