Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics : create -> new
[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 #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" 
23 #endif
24
25 static xbt_context_t current_context = NULL;
26 static xbt_dynar_t context_to_destroy = NULL;
27
28 void xbt_context_init(void)
29 {
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);
33   }
34 }
35
36 void xbt_context_empty_trash(void)
37 {
38   xbt_dynar_reset(context_to_destroy);
39 }
40
41 static void *__context_wrapper(void *c)
42 {
43   xbt_context_t context = c;
44   int i;
45
46 /*   msg_global->current_process = process; */
47
48   /*  WARNING("Calling the main function"); */
49   (context->code) (context->argc,context->argv);
50
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);
54
55   xbt_dynar_push(context_to_destroy, &context);
56
57   xbt_context_yield(context);
58
59   return NULL;
60 }
61
62 void xbt_context_start(xbt_context_t context) 
63 {
64
65 /*   TBX_FIFO_insert(msg_global->process, process); */
66 /*   TBX_FIFO_insert(msg_global->process_to_run, process); */
67
68   /*  WARNING("Assigning __MSG_process_launcher to context (%p)",context); */
69   makecontext (&(context->uc), (void (*) (void)) __context_wrapper,
70                1, context);
71
72   return;
73 }
74
75 xbt_context_t xbt_context_new(xbt_context_function_t code,
76                          int argc, char *argv[])
77 {
78   xbt_context_t res = NULL;
79
80   res = xbt_new0(s_xbt_context_t,1);
81
82   /*  WARNING("Initializing context (%p)",res); */
83
84   xbt_assert0(getcontext(&(res->uc))==0,"Error in context saving.");
85
86   /*  VOIRP(res->uc); */
87   res->code = code;
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;
93   return res;
94 }
95
96 static void xbt_context_destroy(xbt_context_t context)
97 {
98   xbt_free(context);
99
100   return;
101 }
102
103 void xbt_context_yield(xbt_context_t context)
104 {
105
106   xbt_assert0(current_context,"You have to call context_init() first.");
107
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); */
115
116   if (context) {
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); */
129     } else {
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); */
140     }
141 /*     msg_global->current_process = self; */
142   }
143
144   return;
145 }