Logo AND Algorithmique Numérique Distribuée

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