Logo AND Algorithmique Numérique Distribuée

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