3 /* a fast and simple context switching library */
5 /* Copyright (c) 2004 Arnaud Legrand. */
6 /* Copyright (c) 2004 Martin Quinson. */
7 /* All rights reserved. */
9 /* This program is free software; you can redistribute it and/or modify it
10 * under the terms of the license (GNU LGPL) which comes with this package. */
13 #include "context_private.h"
14 #include "xbt/error.h"
15 #include "xbt/dynar.h"
16 #include "gras_config.h"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(context, xbt, "Context");
19 #ifndef HAVE_UCONTEXT_H
20 /* don't want to play with conditional compilation in automake tonight, sorry.
21 include directly the c file from here when needed. */
22 # include "context_win32.c"
25 static xbt_context_t current_context = NULL;
26 static xbt_dynar_t context_to_destroy = NULL;
28 void xbt_context_init(void)
30 if(!current_context) {
31 current_context = xbt_new0(s_xbt_context_t,1);
32 context_to_destroy = xbt_dynar_new(sizeof(xbt_context_t),xbt_free);
36 void xbt_context_empty_trash(void)
38 xbt_dynar_reset(context_to_destroy);
41 static void *__context_wrapper(void *c)
43 xbt_context_t context = c;
46 /* msg_global->current_process = process; */
48 /* WARNING("Calling the main function"); */
49 (context->code) (context->argc,context->argv);
51 for(i=0;i<context->argc; i++)
52 if(context->argv[i]) xbt_free(context->argv[i]);
53 if(context->argv) xbt_free(context->argv);
55 xbt_dynar_push(context_to_destroy, &context);
57 xbt_context_yield(context);
62 void xbt_context_start(xbt_context_t context)
65 /* TBX_FIFO_insert(msg_global->process, process); */
66 /* TBX_FIFO_insert(msg_global->process_to_run, process); */
68 /* WARNING("Assigning __MSG_process_launcher to context (%p)",context); */
69 makecontext (&(context->uc), (void (*) (void)) __context_wrapper,
75 xbt_context_t xbt_context_new(xbt_context_function_t code,
76 int argc, char *argv[])
78 xbt_context_t res = NULL;
80 res = xbt_new0(s_xbt_context_t,1);
82 /* WARNING("Initializing context (%p)",res); */
84 xbt_assert0(getcontext(&(res->uc))==0,"Error in context saving.");
88 res->uc.uc_link = &(current_context->uc); /* FIXME LATER */
89 /* WARNING : when this context is over, the current_context (i.e. the
90 father), is awaken... May result in bugs later.*/
91 res->uc.uc_stack.ss_sp = res->stack;
92 res->uc.uc_stack.ss_size = STACK_SIZE;
96 static void xbt_context_destroy(xbt_context_t context)
103 void xbt_context_yield(xbt_context_t context)
106 xbt_assert0(current_context,"You have to call context_init() first.");
108 /* __MSG_context_init(); */
109 /* fprintf(stderr,"\n"); */
110 /* WARNING("--------- current_context (%p) is yielding to context(%p) ---------",current_context,context); */
111 /* VOIRP(current_context); */
112 /* if(current_context) VOIRP(current_context->save); */
113 /* VOIRP(context); */
114 /* if(context) VOIRP(context->save); */
117 /* m_process_t self = msg_global->current_process; */
118 if(context->save==NULL) {
119 /* WARNING("**** Yielding to somebody else ****"); */
120 /* WARNING("Saving current_context value (%p) to context(%p)->save",current_context,context); */
121 context->save = current_context ;
122 context->uc.uc_link = &(current_context->uc);
123 /* WARNING("current_context becomes context(%p) ",context); */
124 current_context = context ;
125 /* WARNING("Current position memorized (context->save). Jumping to context (%p)",context); */
126 if(!swapcontext (&(context->save->uc), &(context->uc)))
127 xbt_assert0(0,"Context swapping failure");
128 /* WARNING("I am (%p). Coming back\n",context); */
130 xbt_context_t old_context = context->save ;
131 /* WARNING("**** Back ! ****"); */
132 /* WARNING("Setting current_context (%p) to context(%p)->save",current_context,context); */
133 current_context = context->save ;
134 /* WARNING("Setting context(%p)->save to NULL",current_context,context); */
135 context->save = NULL ;
136 /* WARNING("Current position memorized (%p). Jumping to context (%p)",context,old_context); */
137 if(!swapcontext (&(context->uc), &(old_context->uc)) )
138 xbt_assert0(0,"Context swapping failure");
139 /* WARNING("I am (%p). Coming back\n",context); */
141 /* msg_global->current_process = self; */