Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
I probably had been drinking too much the day I have written this command... Sorry...
[simgrid.git] / src / xbt / context.c
1 /*      $Id$     */
2
3 /* a fast and simple context switching library                              */
4
5 /* Copyright (c) 2004 Arnaud Legrand.                                       */
6 /* Copyright (c) 2004 Martin Quinson.                                       */
7 /* All rights reserved.                                                     */
8
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. */
11
12 #include "portable.h"
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");
18
19 /* #define WARNING(format, ...) (fprintf(stderr, "[%s , %s : %d] ", __FILE__, __FUNCTION__, __LINE__),\ */
20 /*                               fprintf(stderr, format, ## __VA_ARGS__), \ */
21 /*                               fprintf(stderr, "\n")) */
22 /* #define VOIRP(expr) WARNING("  {" #expr " = %p }", expr) */
23
24 #ifndef HAVE_UCONTEXT_H
25 /* don't want to play with conditional compilation in automake tonight, sorry.
26    include directly the c file from here when needed. */
27 # include "context_win32.c" 
28 #endif
29
30 static xbt_context_t current_context = NULL;
31 static xbt_context_t init_context = NULL;
32 static xbt_swag_t context_to_destroy = NULL;
33 static xbt_swag_t context_living = NULL;
34
35 static void __xbt_context_yield(xbt_context_t context)
36 {
37   int return_value = 0;
38
39   xbt_assert0(current_context,"You have to call context_init() first.");
40   
41 /*   WARNING("--------- current_context (%p) is yielding to context(%p) ---------",current_context,context); */
42 /*   VOIRP(current_context); */
43 /*   if(current_context) VOIRP(current_context->save); */
44 /*   VOIRP(context); */
45 /*   if(context) VOIRP(context->save); */
46
47   if (context) {
48     if(context->save==NULL) {
49 /*       WARNING("**** Yielding to somebody else ****"); */
50 /*       WARNING("Saving current_context value (%p) to context(%p)->save",current_context,context); */
51       context->save = current_context ;
52 /*       WARNING("current_context becomes  context(%p) ",context); */
53       current_context = context ;
54 /*       WARNING("Current position memorized (context->save). Jumping to context (%p)",context); */
55       return_value = swapcontext (&(context->save->uc), &(context->uc));
56       xbt_assert0((return_value==0),"Context swapping failure");
57 /*       WARNING("I am (%p). Coming back\n",context); */
58     } else {
59       xbt_context_t old_context = context->save ;
60 /*       WARNING("**** Back ! ****"); */
61 /*       WARNING("Setting current_context (%p) to context(%p)->save",current_context,context); */
62       current_context = context->save ;
63 /*       WARNING("Setting context(%p)->save to NULL",context); */
64       context->save = NULL ;
65 /*       WARNING("Current position memorized (%p). Jumping to context (%p)",context,old_context); */
66       return_value = swapcontext (&(context->uc), &(old_context->uc));
67       xbt_assert0((return_value==0),"Context swapping failure");
68 /*       WARNING("I am (%p). Coming back\n",context); */
69     }
70   }
71
72   return;
73 }
74
75 static void xbt_context_destroy(xbt_context_t context)
76 {
77   xbt_free(context);
78
79   return;
80 }
81
82 void xbt_context_init(void)
83 {
84   if(!current_context) {
85     current_context = init_context = xbt_new0(s_xbt_context_t,1);
86     context_to_destroy = xbt_swag_new(xbt_swag_offset(*current_context,hookup));
87     context_living = xbt_swag_new(xbt_swag_offset(*current_context,hookup));
88     xbt_swag_insert(init_context, context_living);
89   }
90 }
91
92 void xbt_context_empty_trash(void)
93 {
94   xbt_context_t context=NULL;
95
96   while((context=xbt_swag_extract(context_to_destroy)))
97     xbt_context_destroy(context);
98 }
99
100 static void *__context_wrapper(void *c)
101 {
102   xbt_context_t context = c;
103   int i;
104
105 /*   msg_global->current_process = process; */
106
107   if(context->startup_func)
108     context->startup_func(context->startup_arg);
109
110   /*  WARNING("Calling the main function"); */
111   /*   xbt_context_yield(context); */
112   (context->code) (context->argc,context->argv);
113
114   for(i=0;i<context->argc; i++) 
115     if(context->argv[i]) xbt_free(context->argv[i]);
116   if(context->argv) xbt_free(context->argv);
117
118   if(context->cleanup_func)
119     context->cleanup_func(context->cleanup_arg);
120
121   xbt_swag_remove(context, context_living);
122   xbt_swag_insert(context, context_to_destroy);
123
124   __xbt_context_yield(context);
125   xbt_assert0(0,"You're cannot be here!");
126   return NULL;
127 }
128
129 void xbt_context_start(xbt_context_t context) 
130 {
131 /*   xbt_fifo_insert(msg_global->process, process); */
132 /*   xbt_fifo_insert(msg_global->process_to_run, process); */
133
134   makecontext (&(context->uc), (void (*) (void)) __context_wrapper,
135                1, context);
136   return;
137 }
138
139 xbt_context_t xbt_context_new(xbt_context_function_t code, 
140                               void_f_pvoid_t startup_func, void *startup_arg,
141                               void_f_pvoid_t cleanup_func, void *cleanup_arg,
142                               int argc, char *argv[])
143 {
144   xbt_context_t res = NULL;
145
146   res = xbt_new0(s_xbt_context_t,1);
147
148   xbt_assert0(getcontext(&(res->uc))==0,"Error in context saving.");
149
150   res->code = code;
151   res->uc.uc_link = NULL;
152   res->argc = argc;
153   res->argv = argv;
154 /*   res->uc.uc_link = &(current_context->uc); */
155   /* WARNING : when this context is over, the current_context (i.e. the 
156      father), is awaken... Theorically, the wrapper should prevent using 
157      this feature. */
158   res->uc.uc_stack.ss_sp = res->stack;
159   res->uc.uc_stack.ss_size = STACK_SIZE;
160   res->startup_func = startup_func;
161   res->startup_arg = startup_arg;
162   res->cleanup_func = cleanup_func;
163   res->cleanup_arg = cleanup_arg;
164
165   xbt_swag_insert(res, context_living);
166
167   return res;
168 }
169
170
171 void xbt_context_yield(void)
172 {
173   __xbt_context_yield(current_context);
174 }
175
176 void xbt_context_schedule(xbt_context_t context)
177 {
178   xbt_assert0((current_context==init_context),
179               "You are not supposed to run this function here!");
180   __xbt_context_yield(context);
181 }
182
183 void xbt_context_exit(void) {
184   xbt_context_t context=NULL;
185
186   xbt_context_empty_trash();
187   xbt_swag_free(context_to_destroy);
188
189   while((context=xbt_swag_extract(context_living)))
190     xbt_context_free(context);
191
192   xbt_swag_free(context_living);
193
194   init_context = current_context = NULL ;
195 }
196
197 void xbt_context_free(xbt_context_t context)
198 {
199   int i ;
200
201   xbt_swag_remove(context, context_living);  
202   for(i=0;i<context->argc; i++) 
203     if(context->argv[i]) xbt_free(context->argv[i]);
204   if(context->argv) xbt_free(context->argv);
205   
206   if(context->cleanup_func)
207     context->cleanup_func(context->cleanup_arg);
208   xbt_context_destroy(context);
209
210   return;
211 }