3 /* context_sysv - implementation of context switching with ucontextes from Sys V */
5 /* Copyright (c) 2004-2008 the SimGrid team. All right reserved */
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. */
11 #include "context_sysv_config.h" /* loads context system definitions */
13 #include <ucontext.h> /* context relative declarations */
15 /* lower this if you want to reduce the memory consumption */
16 #define STACK_SIZE 128*1024
18 #ifdef HAVE_VALGRIND_VALGRIND_H
19 # include <valgrind/valgrind.h>
20 #endif /* HAVE_VALGRIND_VALGRIND_H */
22 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
24 typedef struct s_smx_ctx_sysv {
25 s_smx_ctx_base_t super; /* Fields of super implementation */
26 ucontext_t uc; /* the thread that execute the code */
27 char stack[STACK_SIZE]; /* the thread stack size */
28 #ifdef HAVE_VALGRIND_VALGRIND_H
29 unsigned int valgrind_stack_id; /* the valgrind stack id */
31 } s_smx_ctx_sysv_t, *smx_ctx_sysv_t;
34 smx_ctx_sysv_factory_create_context(xbt_main_func_t code, int argc, char** argv,
35 void_f_pvoid_t cleanup_func, void* cleanup_arg);
37 static void smx_ctx_sysv_free(smx_context_t context);
38 static void smx_ctx_sysv_stop(smx_context_t context);
39 static void smx_ctx_sysv_suspend(smx_context_t context);
40 static void smx_ctx_sysv_resume(smx_context_t new_context);
42 static void smx_ctx_sysv_wrapper(void);
44 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory) {
46 smx_ctx_base_factory_init(factory);
48 (*factory)->create_context = smx_ctx_sysv_factory_create_context;
49 /* Do not overload that method (*factory)->finalize */
50 (*factory)->free = smx_ctx_sysv_free;
51 (*factory)->stop = smx_ctx_sysv_stop;
52 (*factory)->suspend = smx_ctx_sysv_suspend;
53 (*factory)->resume = smx_ctx_sysv_resume;
54 (*factory)->name = "smx_sysv_context_factory";
58 smx_ctx_sysv_factory_create_context(xbt_main_func_t code, int argc, char** argv,
59 void_f_pvoid_t cleanup_func, void* cleanup_arg)
61 smx_ctx_sysv_t context = (smx_ctx_sysv_t)smx_ctx_base_factory_create_context_sized
62 (sizeof(s_smx_ctx_sysv_t), code,argc,argv,cleanup_func,cleanup_arg);
64 /* If the user provided a function for the process then use it
65 otherwise is the context for maestro */
68 xbt_assert2(getcontext(&(context->uc)) == 0,
69 "Error in context saving: %d (%s)", errno, strerror(errno));
71 context->uc.uc_link = NULL;
73 context->uc.uc_stack.ss_sp =
74 pth_skaddr_makecontext(context->stack, STACK_SIZE);
76 context->uc.uc_stack.ss_size =
77 pth_sksize_makecontext(context->stack, STACK_SIZE);
79 #ifdef HAVE_VALGRIND_VALGRIND_H
80 context->valgrind_stack_id =
81 VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
82 ((char *) context->uc.uc_stack.ss_sp) +
83 context->uc.uc_stack.ss_size);
84 #endif /* HAVE_VALGRIND_VALGRIND_H */
86 makecontext(&((smx_ctx_sysv_t)context)->uc, smx_ctx_sysv_wrapper, 0);
89 return (smx_context_t)context;
92 static void smx_ctx_sysv_free(smx_context_t context) {
96 #ifdef HAVE_VALGRIND_VALGRIND_H
97 VALGRIND_STACK_DEREGISTER(((smx_ctx_sysv_t) context)->valgrind_stack_id);
98 #endif /* HAVE_VALGRIND_VALGRIND_H */
101 smx_ctx_base_free(context);
104 static void smx_ctx_sysv_stop(smx_context_t context) {
105 smx_ctx_base_stop(context);
107 smx_ctx_sysv_suspend(context);
110 static void smx_ctx_sysv_wrapper()
112 /*FIXME: I would like to avoid accessing simix_global to get the current
113 context by passing it as an argument of the wrapper function. The problem
114 is that this function is called from smx_ctx_sysv_start, and uses
115 makecontext for calling it, and the stupid posix specification states that
116 all the arguments of the function should be int(32 bits), making it useless
117 in 64-bit architectures where pointers are 64 bit long.
119 smx_ctx_sysv_t context =
120 (smx_ctx_sysv_t)simix_global->current_process->context;
122 (context->super.code) (context->super.argc, context->super.argv);
124 smx_ctx_sysv_stop((smx_context_t)context);
127 static void smx_ctx_sysv_suspend(smx_context_t context) {
128 ucontext_t maestro_ctx = ((smx_ctx_sysv_t)simix_global->maestro_process->context)->uc;
130 int rv = swapcontext(&((smx_ctx_sysv_t) context)->uc, &maestro_ctx);
132 xbt_assert0((rv == 0), "Context swapping failure");
136 smx_ctx_sysv_resume(smx_context_t new_context) {
137 smx_ctx_sysv_t maestro = (smx_ctx_sysv_t)simix_global->maestro_process->context;
139 int rv = swapcontext(&(maestro->uc), &((smx_ctx_sysv_t)new_context)->uc);
141 xbt_assert0((rv == 0), "Context swapping failure");