Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Keep up with last SIMIX modifications.
[simgrid.git] / src / java / jxbt_context.c
1 /*
2  * $Id$
3  *
4  * Copyright 2006,2007 Arnaud Legrand,Martin Quinson,Malek Cherier         
5  * All right 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
11 #include "portable.h"
12 #include "xbt/log.h"
13 #include "xbt/dynar.h"
14 #include "xbt/xbt_os_thread.h"
15 #include "xbt/ex_interface.h"
16 #include "java/jxbt_context.h"
17 #include "java/jmsg.h"
18 #include "java/jmsg_process.h"
19
20 #define JAVA_SIMGRID /* get the right definition of the xbt_ctx with all we need here */
21 #include "xbt/context_private.h"
22
23
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_ctx, xbt, "Context");
25
26
27 static xbt_context_t current_context = NULL;    /* the current context                  */
28 static xbt_context_t init_context = NULL;       /* the initial context                  */
29 static xbt_swag_t context_to_destroy = NULL;    /* the list of the contexs to destroy   */
30 static xbt_swag_t context_living = NULL;        /* the list of the contexts in use      */
31 static xbt_os_mutex_t creation_mutex;              /* For syncronization during process creation */
32 static xbt_os_cond_t creation_cond;              /* For syncronization during process creation */
33 static xbt_os_mutex_t master_mutex;             /* For syncronization during process scheduling*/
34 static xbt_os_cond_t master_cond;               /* For syncronization during process scheduling*/
35
36 static void 
37 __xbt_context_yield(xbt_context_t context);
38
39
40 static void 
41 __xbt_context_yield(xbt_context_t context) {
42   if(context) {
43     /* save the current context */
44     xbt_context_t self = current_context;
45     
46     if(is_main_thread()) {
47       /* the main thread has called this function
48        * - update the current context
49        * - signal the condition of the process to run
50        * - wait on its condition
51        * - restore thr current contex
52        */
53       
54       xbt_os_mutex_lock(master_mutex);
55       xbt_os_mutex_lock(context->mutex);
56                         
57       /* update the current context */
58       current_context = context;
59       xbt_os_cond_signal(context->cond);
60       xbt_os_mutex_unlock(context->mutex);
61       xbt_os_cond_wait(master_cond, master_mutex);
62       xbt_os_mutex_unlock(master_mutex);
63       /* retore the current context */
64       current_context = self;
65                         
66     } else {
67         /* a java thread has called this function
68          * - update the current context
69          * - signal the condition of the main thread
70          * - wait on its condition
71          * - restore thr current contex
72          */
73
74         xbt_os_mutex_lock(master_mutex);
75         xbt_os_mutex_lock(context->mutex);
76         /* update the current context */
77         current_context = context;
78         xbt_os_cond_signal(master_cond);
79         xbt_os_mutex_unlock(master_mutex);
80         xbt_os_cond_wait(context->cond, context->mutex);
81         xbt_os_mutex_unlock(context->mutex);
82         /* retore the current context */
83         current_context = self;
84       }
85   }
86         
87   if(current_context->iwannadie) {
88     __context_exit(current_context, 1);
89   }
90 }
91
92
93
94 static void 
95 xbt_context_free(xbt_context_t context) {
96   if(context) {
97     if(context->jprocess) {
98       jobject jprocess = context->jprocess;
99       context->jprocess = NULL;
100                         
101       /* if the java process is alive join it */
102       if(jprocess_is_alive(jprocess,context->jenv)) {
103         jprocess_join(jprocess,context->jenv);
104       }
105     }
106         
107     /* destroy the mutex of the process */
108     xbt_os_mutex_destroy(context->mutex);
109
110     /* destroy the condition of the process */
111     xbt_os_cond_destroy(context->cond);
112
113     context->mutex = NULL;
114     context->cond = NULL;
115     
116     if(context->exception) 
117       free(context->exception);
118     
119     free(context);
120     context = NULL;
121   }
122 }
123 /*
124  * This function is only called by the main thread.
125  */
126 void 
127 __context_exit(xbt_context_t context ,int value) {
128   /* call the cleanup function of the context */
129   if(context->cleanup_func)
130     context->cleanup_func(context->cleanup_arg);
131         
132   /* remove the context from the list of the contexts in use. */
133   xbt_swag_remove(context, context_living);
134         
135   /* insert the context in the list of contexts to destroy. */ 
136   xbt_swag_insert(context, context_to_destroy);
137         
138   /*
139    * signal the condition of the java process 
140    */
141   xbt_os_mutex_lock(master_mutex);
142   xbt_os_mutex_lock(context->mutex);
143   xbt_os_cond_signal(context->cond);
144   xbt_os_mutex_unlock(context->mutex);
145         
146   if (context->jprocess) {
147     /* if the java process is alive, stop it */
148     if (jprocess_is_alive(context->jprocess,context->jenv)) {
149       jobject jprocess = context->jprocess;
150       context->jprocess = NULL;
151       jprocess_exit(jprocess,context->jenv);
152     }
153   }
154 }
155
156 /*
157  * This function is called by a java process (only).
158  */
159 void 
160 jcontext_exit(xbt_context_t context ,int value,JNIEnv* env) {
161   jobject __jprocess = context->jprocess;
162   context->jprocess = NULL;
163         
164   if(context->cleanup_func)
165     context->cleanup_func(context->cleanup_arg);
166
167   /* remove the context of the list of living contexts */
168   xbt_swag_remove(context, context_living);
169
170   /* insert the context in the list of the contexts to destroy */
171   xbt_swag_insert(context, context_to_destroy);
172         
173   /*
174    * signal the condition of the main thread.
175    */
176   xbt_os_mutex_lock(master_mutex);
177   xbt_os_mutex_lock(context->mutex);
178   xbt_os_cond_signal(master_cond);
179   xbt_os_mutex_unlock(master_mutex);
180         
181         
182   /* the global reference to the java process instance is deleted */
183   jprocess_delete_global_ref(__jprocess,env);
184 }
185
186 /* callback: context fetching */
187 static ex_ctx_t *
188 __context_ex_ctx(void) {
189   return current_context->exception;
190 }
191
192 /* callback: termination */
193 static void 
194 __context_ex_terminate(xbt_ex_t *e) {
195   xbt_ex_display(e);
196
197   abort();
198 }
199
200 /**
201  * This function initialize the xbt module context which contains 
202  * all functions of the context API .
203  *
204  * @remark This function must be called before use all other function 
205  * of this API.
206  */
207 void 
208 xbt_context_init(void) {
209   if(!current_context) {
210     /* allocate the current context. */
211     current_context = init_context = xbt_new0(s_xbt_context_t,1);
212                 
213     /* context exception */
214     init_context->exception = xbt_new(ex_ctx_t,1);
215     XBT_CTX_INITIALIZE(init_context->exception);
216     __xbt_ex_ctx       = __context_ex_ctx;
217     __xbt_ex_terminate = __context_ex_terminate;
218     
219     /* this list contains the context to destroy */
220     context_to_destroy = xbt_swag_new(xbt_swag_offset(*current_context,hookup));
221     /* this list contains the context in use */
222     context_living = xbt_swag_new(xbt_swag_offset(*current_context,hookup));
223     
224     /* append the current context in the list of context in use */
225     xbt_swag_insert(init_context, context_living);
226            
227     /* this mutex is used to synchronize the creation of the java process */
228     creation_mutex = xbt_os_mutex_init();
229     /* this mutex is used to synchronize the creation of the java process */
230     creation_cond = xbt_os_cond_init();
231
232     /* this mutex is used to synchronize the scheduling of the java process */
233     master_mutex = xbt_os_mutex_init();
234     /* this mutex is used to synchronize the scheduling of the java process */
235     master_cond = xbt_os_cond_init();
236   }
237 }
238
239 /** 
240  * This function is used as a garbage collection
241  * Should be called some time to time to free the memory allocated for contexts
242  * that have finished executing their main functions.
243  */
244 void xbt_context_empty_trash(void) {
245   xbt_context_t context=NULL;
246         
247   /* destroy all the context contained in the list of context to destroy */
248   while((context=xbt_swag_extract(context_to_destroy)))
249     xbt_context_free(context);
250 }
251
252 void 
253 xbt_context_start(xbt_context_t context)  {
254   DEBUG3("xbt_context_start of %p (jproc=%p, jenv=%p)",
255          context, context->jprocess, context->jenv);
256
257   /* the main thread locks the mutex used to create all the process     */
258   xbt_os_mutex_lock(creation_mutex);
259
260   /* the main thread starts the java process                            */
261   jprocess_start(context->jprocess,context->jenv);
262
263   /* the main thread waits the startup of the java process              */
264   xbt_os_cond_wait(creation_cond, creation_mutex);
265         
266   /* the java process is started, the main thread unlocks the mutex
267    * used during the creation of the java process
268    */
269   xbt_os_mutex_unlock(creation_mutex);
270 }
271
272
273 xbt_context_t 
274 xbt_context_new(xbt_main_func_t code, 
275                 void_f_pvoid_t startup_func, void *startup_arg,
276                 void_f_pvoid_t cleanup_func, void *cleanup_arg,
277                 int argc, char *argv[]) {
278   xbt_context_t context = xbt_new0(s_xbt_context_t,1);
279
280   context->code = code;
281         
282   context->mutex = xbt_os_mutex_init();
283   context->cond = xbt_os_cond_init();
284         
285         
286   context->argc = argc;
287   context->argv = argv;
288   context->startup_func = startup_func;
289   context->startup_arg = startup_arg;
290   context->cleanup_func = cleanup_func;
291   context->cleanup_arg = cleanup_arg;
292   context->exception = xbt_new(ex_ctx_t,1);
293   XBT_CTX_INITIALIZE(context->exception);
294         
295   xbt_swag_insert(context, context_living);
296         
297   return context;
298 }
299
300
301 void xbt_context_yield(void) {
302   __xbt_context_yield(current_context);
303 }
304
305 /** 
306  * \param context the winner
307  *
308  * Calling this function blocks the current context and schedule \a context.  
309  * When \a context will call xbt_context_yield, it will return
310  * to this function as if nothing had happened.
311  */
312 void xbt_context_schedule(xbt_context_t context) {
313   xbt_assert0((current_context==init_context),"You are not supposed to run this function here!");
314   __xbt_context_yield(context);
315 }
316
317 /** 
318  * This function kill all existing context and free all the memory
319  * that has been allocated in this module.
320  */
321 void xbt_context_exit(void)  {
322
323   xbt_context_t context=NULL;
324         
325   xbt_context_empty_trash();
326         
327   while((context=xbt_swag_extract(context_living))) {
328     if(context!=init_context) 
329       xbt_context_kill(context);
330   }
331
332   free(init_context->exception); 
333   free(init_context);   
334
335   init_context = current_context = NULL ;
336
337   xbt_context_empty_trash();
338
339   xbt_swag_free(context_to_destroy);
340   xbt_swag_free(context_living);
341         
342   xbt_os_mutex_destroy(creation_mutex);
343   xbt_os_cond_destroy(creation_cond);
344   xbt_os_mutex_destroy(master_mutex);
345   xbt_os_cond_destroy(master_cond);
346 }
347
348 /** 
349  * \param context poor victim
350  *
351  * This function simply kills \a context... scarry isn't it ?
352  */
353 void xbt_context_kill(xbt_context_t context) {
354   context->iwannadie=1;
355   __xbt_context_yield(context);
356 }
357
358 xbt_os_cond_t
359 xbt_creation_cond_get(void) {
360   return creation_cond;
361 }
362
363 xbt_os_mutex_t
364 xbt_creation_mutex_get(void) {
365   return creation_mutex;
366 }
367
368 void  xbt_context_set_jprocess(xbt_context_t context, void *jp) {
369   context->jprocess = jp;
370 }
371 void* xbt_context_get_jprocess(xbt_context_t context)           { 
372    return context->jprocess;
373 }
374
375 void  xbt_context_set_jmutex(xbt_context_t context,void *jm)    {
376    context->mutex = jm;
377 }
378 void* xbt_context_get_jmutex(xbt_context_t context)             { 
379    return context->mutex; 
380 }
381
382 void  xbt_context_set_jcond(xbt_context_t context,void *jc)     {
383    context->cond = jc;
384 }
385 void* xbt_context_get_jcond(xbt_context_t context)              { 
386    return context->cond;
387 }
388
389 void  xbt_context_set_jenv(xbt_context_t context,void* je)      {
390    context->jenv = je;
391 }
392 void* xbt_context_get_jenv(xbt_context_t context)               { 
393    return context->jenv;
394 }
395
396
397 /* @} */