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. */
10 #include "xbt/ex_interface.h"
13 #include "context_sysv_config.h" /* loads context system definitions */
15 #include <ucontext.h> /* context relative declarations */
16 #define STACK_SIZE 128*1024 /* lower this if you want to reduce the memory consumption */
17 #ifdef HAVE_VALGRIND_VALGRIND_H
18 # include <valgrind/valgrind.h>
19 #endif /* HAVE_VALGRIND_VALGRIND_H */
21 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(smx_context);
23 typedef struct s_smx_ctx_sysv {
25 ucontext_t uc; /* the thread that execute the code */
26 char stack[STACK_SIZE]; /* the thread stack size */
27 struct s_smx_ctx_sysv *prev; /* the previous process */
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 /* callback: context fetching */
35 static ex_ctx_t *xbt_jcontext_ex_ctx(void);
37 /* callback: termination */
38 static void xbt_jcontext_ex_terminate(xbt_ex_t *e);
40 static smx_context_t smx_ctx_sysv_factory_create_context(xbt_main_func_t code);
42 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory);
44 static smx_context_t smx_ctx_sysv_factory_create_maestro_context(void);
46 static void smx_ctx_sysv_free(smx_context_t context);
48 static void smx_ctx_sysv_start(smx_context_t context);
50 static void smx_ctx_sysv_stop(int exit_code);
52 static void smx_ctx_sysv_suspend(smx_context_t context);
54 static void smx_ctx_sysv_resume(smx_context_t old_context,
55 smx_context_t new_context);
57 static void smx_ctx_sysv_wrapper(void);
59 /* callback: context fetching */
60 static ex_ctx_t *xbt_ctx_sysv_ex_ctx(void)
62 return simix_global->current_process->context->exception;
65 /* callback: termination */
66 static void xbt_ctx_sysv_ex_terminate(xbt_ex_t * e)
72 void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory)
74 *factory = xbt_new0(s_smx_context_factory_t, 1);
76 (*factory)->create_context = smx_ctx_sysv_factory_create_context;
77 (*factory)->finalize = smx_ctx_sysv_factory_finalize;
78 (*factory)->create_maestro_context = smx_ctx_sysv_factory_create_maestro_context;
79 (*factory)->free = smx_ctx_sysv_free;
80 (*factory)->start = smx_ctx_sysv_start;
81 (*factory)->stop = smx_ctx_sysv_stop;
82 (*factory)->suspend = smx_ctx_sysv_suspend;
83 (*factory)->resume = smx_ctx_sysv_resume;
84 (*factory)->name = "smx_sysv_context_factory";
86 /* context exception handlers */
87 __xbt_ex_ctx = xbt_ctx_sysv_ex_ctx;
88 __xbt_ex_terminate = xbt_ctx_sysv_ex_terminate;
91 static smx_context_t smx_ctx_sysv_factory_create_maestro_context()
93 smx_ctx_sysv_t context = xbt_new0(s_smx_ctx_sysv_t, 1);
95 context->exception = xbt_new(ex_ctx_t, 1);
96 XBT_CTX_INITIALIZE(context->exception);
98 return (smx_context_t)context;
101 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t * factory)
103 /*FIXME free(maestro_context->exception);*/
109 static smx_context_t smx_ctx_sysv_factory_create_context(xbt_main_func_t code)
111 smx_ctx_sysv_t context = xbt_new0(s_smx_ctx_sysv_t, 1);
113 context->code = code;
115 xbt_assert2(getcontext(&(context->uc)) == 0,
116 "Error in context saving: %d (%s)", errno, strerror(errno));
117 context->uc.uc_link = NULL;
118 context->uc.uc_stack.ss_sp =
119 pth_skaddr_makecontext(context->stack, STACK_SIZE);
120 context->uc.uc_stack.ss_size =
121 pth_sksize_makecontext(context->stack, STACK_SIZE);
122 #ifdef HAVE_VALGRIND_VALGRIND_H
123 context->valgrind_stack_id =
124 VALGRIND_STACK_REGISTER(context->uc.uc_stack.ss_sp,
125 ((char *) context->uc.uc_stack.ss_sp) +
126 context->uc.uc_stack.ss_size);
127 #endif /* HAVE_VALGRIND_VALGRIND_H */
129 context->exception = xbt_new(ex_ctx_t, 1);
130 XBT_CTX_INITIALIZE(context->exception);
132 return (smx_context_t)context;
135 static void smx_ctx_sysv_free(smx_context_t pcontext)
137 smx_ctx_sysv_t context = (smx_ctx_sysv_t)pcontext;
140 if (context->exception)
141 free(context->exception);
143 #ifdef HAVE_VALGRIND_VALGRIND_H
144 VALGRIND_STACK_DEREGISTER(((smx_ctx_sysv_t) context)->valgrind_stack_id);
145 #endif /* HAVE_VALGRIND_VALGRIND_H */
147 /* destroy the context */
152 static void smx_ctx_sysv_start(smx_context_t context)
154 makecontext(&((smx_ctx_sysv_t)context)->uc, smx_ctx_sysv_wrapper, 0);
157 static void smx_ctx_sysv_stop(int exit_code)
159 if (simix_global->current_process->cleanup_func)
160 ((*simix_global->current_process->cleanup_func))
161 (simix_global->current_process->cleanup_arg);
163 smx_ctx_sysv_suspend(simix_global->current_process->context);
166 static void smx_ctx_sysv_wrapper(void)
168 smx_ctx_sysv_stop((*(simix_global->current_process->context->code))
169 (simix_global->current_process->argc,
170 simix_global->current_process->argv));
173 static void smx_ctx_sysv_suspend(smx_context_t context)
177 smx_ctx_sysv_t prev_context = ((smx_ctx_sysv_t) context)->prev;
179 ((smx_ctx_sysv_t) context)->prev = NULL;
181 rv = swapcontext(&((smx_ctx_sysv_t) context)->uc, &prev_context->uc);
183 xbt_assert0((rv == 0), "Context swapping failure");
186 static void smx_ctx_sysv_resume(smx_context_t old_context,
187 smx_context_t new_context)
191 ((smx_ctx_sysv_t) new_context)->prev = (smx_ctx_sysv_t)old_context;
193 rv = swapcontext(&((smx_ctx_sysv_t)old_context)->uc,
194 &((smx_ctx_sysv_t)new_context)->uc);
196 xbt_assert0((rv == 0), "Context swapping failure");