Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Adding a context library to xbt.
[simgrid.git] / src / xbt / context.c
1 /*      $Id$     */
2
3 /* a fast and simple context switching library                              */
4
5 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
6
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. */
9
10 #include "context_private.h"
11 #include "xbt/error.h"
12 #include "xbt/dynar.h"
13 #include "gras_config.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(context, xbt, "Context");
15
16
17 /************************ GNU LIBC CONTEXTS **************************/
18 #if USE_CONTEXT==1
19
20 static context_t current_context = NULL;
21 static xbt_dynar_t context_to_destroy = NULL;
22
23 void context_init(void)
24 {
25   if(!current_context) {
26     current_context = xbt_new0(s_context_t,1);
27     context_to_destroy = xbt_dynar_new(sizeof(context_t),xbt_free);
28   }
29 }
30
31 void context_empty_trash(void)
32 {
33   xbt_dynar_reset(context_to_destroy);
34 }
35
36 static void *__context_wrapper(void *c)
37 {
38   context_t context = c;
39   int i;
40
41 /*   msg_global->current_process = process; */
42
43   /*  WARNING("Calling the main function"); */
44   (context->code) (context->argc,context->argv);
45
46   for(i=0;i<context->argc; i++) 
47     if(context->argv[i]) xbt_free(context->argv[i]);
48   if(context->argv) xbt_free(context->argv);
49
50   xbt_dynar_push(context_to_destroy, &context);
51
52   context_yield(context);
53
54   return NULL;
55 }
56
57 void context_start(context_t context) 
58 {
59
60 /*   TBX_FIFO_insert(msg_global->process, process); */
61 /*   TBX_FIFO_insert(msg_global->process_to_run, process); */
62
63   /*  WARNING("Assigning __MSG_process_launcher to context (%p)",context); */
64   makecontext (&(context->uc), (void (*) (void)) __context_wrapper,
65                1, context);
66
67   return;
68 }
69
70 context_t context_create(context_function_t code,
71                          int argc, char *argv[])
72 {
73   context_t res = NULL;
74
75   res = xbt_new0(s_context_t,1);
76
77   /*  WARNING("Initializing context (%p)",res); */
78
79   xbt_assert0(getcontext(&(res->uc))==0,"Error in context saving.");
80
81   /*  VOIRP(res->uc); */
82   res->code = code;
83   res->uc.uc_link = &(current_context->uc); /* FIXME LATER */
84   /* WARNING : when this context is over, the current_context (i.e. the 
85      father), is awaken... May result in bugs later.*/
86   res->uc.uc_stack.ss_sp = res->stack;
87   res->uc.uc_stack.ss_size = STACK_SIZE;
88   return res;
89 }
90
91 static void context_destroy(context_t context)
92 {
93   xbt_free(context);
94
95   return;
96 }
97
98 void context_yield(context_t context)
99 {
100
101   xbt_assert0(current_context,"You have to call context_init() first.");
102
103   /*   __MSG_context_init(); */
104   /*  fprintf(stderr,"\n"); */
105   /*  WARNING("--------- current_context (%p) is yielding to context(%p) ---------",current_context,context); */
106   /*  VOIRP(current_context); */
107   /*  if(current_context) VOIRP(current_context->save); */
108   /*  VOIRP(context); */
109   /*  if(context) VOIRP(context->save); */
110
111   if (context) {
112 /*     m_process_t self = msg_global->current_process; */
113     if(context->save==NULL) {
114       /*      WARNING("**** Yielding to somebody else ****"); */
115       /*      WARNING("Saving current_context value (%p) to context(%p)->save",current_context,context); */
116       context->save = current_context ;
117       context->uc.uc_link = &(current_context->uc);
118       /*      WARNING("current_context becomes  context(%p) ",context); */
119       current_context = context ;
120       /*      WARNING("Current position memorized (context->save). Jumping to context (%p)",context); */
121       if(!swapcontext (&(context->save->uc), &(context->uc))) 
122         xbt_assert0(0,"Context swapping failure");
123       /*      WARNING("I am (%p). Coming back\n",context); */
124     } else {
125       context_t old_context = context->save ;
126       /*      WARNING("**** Back ! ****"); */
127       /*      WARNING("Setting current_context (%p) to context(%p)->save",current_context,context); */
128       current_context = context->save ;
129       /*      WARNING("Setting context(%p)->save to NULL",current_context,context); */
130       context->save = NULL ;
131       /*      WARNING("Current position memorized (%p). Jumping to context (%p)",context,old_context); */
132       if(!swapcontext (&(context->uc), &(old_context->uc)) ) 
133         xbt_assert0(0,"Context swapping failure");
134       /*      WARNING("I am (%p). Coming back\n",context); */
135     }
136 /*     msg_global->current_process = self; */
137   }
138
139   return;
140 }
141 #else 
142
143 /****************************** PTHREADS ***************************/
144 /* #if HAVE_LIBPTHREAD==1 */
145 #ifdef HAVE_LIBPTHREAD  /* Nevermind, let's use pthreads... */
146
147 static void *__MSG_process_launcher(void *p)
148 {
149   m_process_t process = (m_process_t) p;
150   sim_data_process_t sim_data = process->simdata;
151   context_t context = NULL;
152   int i;
153   msg_global->current_process = process;
154   TBX_FIFO_insert(msg_global->process, process);
155
156   TBX_FIFO_insert(msg_global->process_to_run, process);
157 /*   pthread_mutex_lock(&(sim_data->context->mutex)); */
158   __MSG_context_yield(sim_data->context);
159
160   (*sim_data->context->code) (sim_data->argc,sim_data->argv);
161
162   TBX_FIFO_remove(msg_global->process, process);
163   TBX_FIFO_remove(msg_global->process_to_run, process);
164
165   /* Free blocked process if any */
166 /*   pthread_mutex_lock(&(sim_data->context->mutex)); */
167 /*   pthread_cond_signal(&(sim_data->context->cond)); */
168 /*   pthread_mutex_unlock(&(sim_data->context->mutex)); */
169
170   context = sim_data->context;
171
172   for(i=0;i<sim_data->argc; i++) 
173     if(sim_data->argv[i]) FREE(sim_data->argv[i]);
174   if(sim_data->argv) FREE(sim_data->argv);
175   if(process->name) FREE(process->name);
176   FREE(sim_data);
177   FREE(process);
178
179   TBX_FIFO_insert(msg_global->context_to_destroy,context);
180   __MSG_context_yield(context);
181 /*   __MSG_context_destroy(&context); */
182
183   return NULL;
184 }
185
186 MSG_error_t __MSG_context_start(m_process_t process) 
187 {
188   sim_data_process_t sim_data = NULL;
189
190   sim_data = process->simdata;
191
192   pthread_mutex_lock(&(sim_data->context->mutex));
193
194   /* Launch the thread */
195   if (pthread_create(sim_data->context->thread, NULL, __MSG_process_launcher,
196                      process) != 0) {
197     WARNING("Unable to create a thread.");
198     return MSG_FATAL;
199   }
200   pthread_cond_wait(&(sim_data->context->cond), &(sim_data->context->mutex));
201   pthread_mutex_unlock(&(sim_data->context->mutex));
202 /*   __MSG_context_yield(sim_data->context); */
203
204   return MSG_OK;
205 }
206
207 context_t __MSG_context_create(m_process_code_t code)
208 {
209   context_t res = NULL;
210
211   res = CALLOC(1, sizeof(struct s_context));
212
213   res->code = code;
214   res->thread = CALLOC(1, sizeof(pthread_t));
215   if (pthread_mutex_init(&(res->mutex), NULL) != 0)
216     FAILURE("Mutex initialization error");
217   if (pthread_cond_init(&(res->cond), NULL) != 0)
218     FAILURE("Condition initialization error");
219
220   return res;
221 }
222
223 MSG_error_t __MSG_context_destroy(context_t * context)
224 {
225   ASSERT(((context != NULL) && (*context != NULL)), "Invalid parameters");
226
227   FREE((*context)->thread);
228   pthread_mutex_destroy(&((*context)->mutex));
229   pthread_cond_destroy(&((*context)->cond));
230
231   FREE(*context);
232   *context = NULL;
233
234   return MSG_OK;
235 }
236
237 MSG_error_t __MSG_context_yield(context_t context)
238 {
239   if (context) {
240     m_process_t self = msg_global->current_process;
241     pthread_mutex_lock(&(context->mutex));
242     pthread_cond_signal(&context->cond);
243     pthread_cond_wait(&context->cond, &context->mutex);
244     pthread_mutex_unlock(&(context->mutex));
245     msg_global->current_process = self;
246   }
247   return MSG_OK;
248 }
249 #endif /* HAVE_LIBPTHREAD */
250 #endif /* USE_CONTEXT */