4 * Copyright 2006,2007 Arnaud Legrand,Martin Quinson,Malek Cherier
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.
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"
20 #define JAVA_SIMGRID /* get the right definition of the xbt_ctx with all we need here */
21 #include "xbt/context_private.h"
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_ctx, xbt, "Context");
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*/
37 __xbt_context_yield(xbt_context_t context);
41 __xbt_context_yield(xbt_context_t context) {
43 /* save the current context */
44 xbt_context_t self = current_context;
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
54 xbt_os_mutex_lock(master_mutex);
55 xbt_os_mutex_lock(context->mutex);
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;
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
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;
87 if(current_context->iwannadie) {
88 __context_exit(current_context, 1);
95 xbt_context_free(xbt_context_t context) {
97 if(context->jprocess) {
98 jobject jprocess = context->jprocess;
99 context->jprocess = NULL;
101 /* if the java process is alive join it */
102 if(jprocess_is_alive(jprocess,context->jenv)) {
103 jprocess_join(jprocess,context->jenv);
107 /* destroy the mutex of the process */
108 xbt_os_mutex_destroy(context->mutex);
110 /* destroy the condition of the process */
111 xbt_os_cond_destroy(context->cond);
113 context->mutex = NULL;
114 context->cond = NULL;
116 if(context->exception)
117 free(context->exception);
124 * This function is only called by the main thread.
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);
132 /* remove the context from the list of the contexts in use. */
133 xbt_swag_remove(context, context_living);
135 /* insert the context in the list of contexts to destroy. */
136 xbt_swag_insert(context, context_to_destroy);
139 * signal the condition of the java process
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);
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);
157 * This function is called by a java process (only).
160 jcontext_exit(xbt_context_t context ,int value,JNIEnv* env) {
161 jobject __jprocess = context->jprocess;
162 context->jprocess = NULL;
164 if(context->cleanup_func)
165 context->cleanup_func(context->cleanup_arg);
167 /* remove the context of the list of living contexts */
168 xbt_swag_remove(context, context_living);
170 /* insert the context in the list of the contexts to destroy */
171 xbt_swag_insert(context, context_to_destroy);
174 * signal the condition of the main thread.
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);
182 /* the global reference to the java process instance is deleted */
183 jprocess_delete_global_ref(__jprocess,env);
186 /* callback: context fetching */
188 __context_ex_ctx(void) {
189 return current_context->exception;
192 /* callback: termination */
194 __context_ex_terminate(xbt_ex_t *e) {
201 * This function initialize the xbt module context which contains
202 * all functions of the context API .
204 * @remark This function must be called before use all other function
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);
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;
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));
224 /* append the current context in the list of context in use */
225 xbt_swag_insert(init_context, context_living);
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();
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();
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.
244 void xbt_context_empty_trash(void) {
245 xbt_context_t context=NULL;
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);
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);
257 /* the main thread locks the mutex used to create all the process */
258 xbt_os_mutex_lock(creation_mutex);
260 /* the main thread starts the java process */
261 jprocess_start(context->jprocess,context->jenv);
263 /* the main thread waits the startup of the java process */
264 xbt_os_cond_wait(creation_cond, creation_mutex);
266 /* the java process is started, the main thread unlocks the mutex
267 * used during the creation of the java process
269 xbt_os_mutex_unlock(creation_mutex);
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);
280 context->code = code;
282 context->mutex = xbt_os_mutex_init();
283 context->cond = xbt_os_cond_init();
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);
295 xbt_swag_insert(context, context_living);
301 void xbt_context_yield(void) {
302 __xbt_context_yield(current_context);
306 * \param context the winner
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.
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);
318 * This function kill all existing context and free all the memory
319 * that has been allocated in this module.
321 void xbt_context_exit(void) {
323 xbt_context_t context=NULL;
325 xbt_context_empty_trash();
327 while((context=xbt_swag_extract(context_living))) {
328 if(context!=init_context)
329 xbt_context_kill(context);
332 free(init_context->exception);
335 init_context = current_context = NULL ;
337 xbt_context_empty_trash();
339 xbt_swag_free(context_to_destroy);
340 xbt_swag_free(context_living);
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);
349 * \param context poor victim
351 * This function simply kills \a context... scarry isn't it ?
353 void xbt_context_kill(xbt_context_t context) {
354 context->iwannadie=1;
355 __xbt_context_yield(context);
359 xbt_creation_cond_get(void) {
360 return creation_cond;
364 xbt_creation_mutex_get(void) {
365 return creation_mutex;
368 void xbt_context_set_jprocess(xbt_context_t context, void *jp) {
369 context->jprocess = jp;
371 void* xbt_context_get_jprocess(xbt_context_t context) {
372 return context->jprocess;
375 void xbt_context_set_jmutex(xbt_context_t context,void *jm) {
378 void* xbt_context_get_jmutex(xbt_context_t context) {
379 return context->mutex;
382 void xbt_context_set_jcond(xbt_context_t context,void *jc) {
385 void* xbt_context_get_jcond(xbt_context_t context) {
386 return context->cond;
389 void xbt_context_set_jenv(xbt_context_t context,void* je) {
392 void* xbt_context_get_jenv(xbt_context_t context) {
393 return context->jenv;