static xbt_context_t init_context = NULL; /* the initial context */
static xbt_swag_t context_to_destroy = NULL; /* the list of the contexs to destroy */
static xbt_swag_t context_living = NULL; /* the list of the contexts in use */
-static xbt_os_mutex_t creation_mutex; /* For syncronization during process creation */
-static xbt_os_cond_t creation_cond; /* For syncronization during process creation */
-static xbt_os_mutex_t master_mutex; /* For syncronization during process scheduling*/
-static xbt_os_cond_t master_cond; /* For syncronization during process scheduling*/
-
static void
__xbt_process_schedule(xbt_context_t context);
__xbt_context_yield(xbt_context_t context) {
if(context) {
-
- /* save the current context */
xbt_context_t self = current_context;
-
- if(is_main_thread()) {
- /* the main thread has called this function
- * - update the current context
- * - signal the condition of the process to run
- * - wait on its condition
- * - restore thr current contex
- */
- xbt_os_mutex_lock(master_mutex);
- /* update the current context */
- current_context = context;
- __xbt_process_schedule(context);
- xbt_os_cond_wait(master_cond, master_mutex);
- xbt_os_mutex_unlock(master_mutex);
- /* retore the current context */
- current_context = self;
-
-
- } else {
- /* a java thread has called this function
- * - update the current context
- * - signal the condition of the main thread
- * - wait on its condition
- * - restore thr current contex
- */
- xbt_os_mutex_lock(master_mutex);
- /* update the current context */
current_context = context;
- xbt_os_cond_signal(master_cond);
- xbt_os_mutex_unlock(master_mutex);
- __xbt_process_unschedule(context);
- /* retore the current context */
+ __xbt_process_schedule(context);
+
current_context = self;
- }
}
-
if(current_context->iwannadie)
__context_exit(current_context, 1);
}
-
-
-
static void
xbt_context_free(xbt_context_t context) {
}
}
- /* destroy the mutex of the process */
- xbt_os_mutex_destroy(context->mutex);
-
- /* destroy the condition of the process */
- xbt_os_cond_destroy(context->cond);
-
- context->mutex = NULL;
- context->cond = NULL;
-
if(context->exception)
free(context->exception);
/*
* signal the condition of the java process
*/
-
- xbt_os_mutex_lock(master_mutex);
-
if (context->jprocess)
{
if (jprocess_is_alive(context->jprocess,get_current_thread_env()))
jprocess_exit(jprocess,get_current_thread_env());
}
}
-
-
}
/*
*/
void
jcontext_exit(xbt_context_t context ,int value,JNIEnv* env) {
+
jobject __jprocess = context->jprocess;
context->jprocess = NULL;
/* insert the context in the list of the contexts to destroy */
xbt_swag_insert(context, context_to_destroy);
- /*
- * signal the condition of the main thread.
- */
- xbt_os_mutex_lock(master_mutex);
- xbt_os_cond_signal(master_cond);
- xbt_os_mutex_unlock(master_mutex);
-
-
/* the global reference to the java process instance is deleted */
jprocess_delete_global_ref(__jprocess,get_current_thread_env());
}
/* append the current context in the list of context in use */
xbt_swag_insert(init_context, context_living);
-
- /* this mutex is used to synchronize the creation of the java process */
- creation_mutex = xbt_os_mutex_init();
- /* this mutex is used to synchronize the creation of the java process */
- creation_cond = xbt_os_cond_init();
-
- /* this mutex is used to synchronize the scheduling of the java process */
- master_mutex = xbt_os_mutex_init();
- /* this mutex is used to synchronize the scheduling of the java process */
- master_cond = xbt_os_cond_init();
__process_schedule = jprocess_schedule;
__process_unschedule = jprocess_unschedule;
DEBUG3("xbt_context_start of %p (jproc=%p, jenv=%p)",
context, context->jprocess, get_current_thread_env());
- /* the main thread locks the mutex used to create all the process */
- xbt_os_mutex_lock(creation_mutex);
-
/* the main thread starts the java process */
jprocess_start(context->jprocess,get_current_thread_env());
-
- /* the main thread waits the startup of the java process */
- xbt_os_cond_wait(creation_cond, creation_mutex);
-
- /* the java process is started, the main thread unlocks the mutex
- * used during the creation of the java process
- */
- xbt_os_mutex_unlock(creation_mutex);
}
context->code = code;
context->name = xbt_strdup(name);
- context->mutex = xbt_os_mutex_init();
- context->cond = xbt_os_cond_init();
-
-
context->argc = argc;
context->argv = argv;
context->startup_func = startup_func;
void xbt_context_yield(void) {
- __xbt_context_yield(current_context);
+ __xbt_process_unschedule(current_context);
+
+
}
/**
xbt_swag_free(context_to_destroy);
xbt_swag_free(context_living);
-
- xbt_os_mutex_destroy(creation_mutex);
- xbt_os_cond_destroy(creation_cond);
- xbt_os_mutex_destroy(master_mutex);
- xbt_os_cond_destroy(master_cond);
}
/**
__xbt_context_yield(context);
}
-xbt_os_cond_t
-xbt_creation_cond_get(void) {
- return creation_cond;
-}
-
-xbt_os_mutex_t
-xbt_creation_mutex_get(void) {
- return creation_mutex;
-}
-
void xbt_context_set_jprocess(xbt_context_t context, void *jp) {
context->jprocess = jp;
}
return context->jprocess;
}
-void xbt_context_set_jmutex(xbt_context_t context,void *jm) {
- context->mutex = jm;
-}
-void* xbt_context_get_jmutex(xbt_context_t context) {
- return context->mutex;
-}
-
-void xbt_context_set_jcond(xbt_context_t context,void *jc) {
- context->cond = jc;
-}
-void* xbt_context_get_jcond(xbt_context_t context) {
- return context->cond;
-}
-
void xbt_context_set_jenv(xbt_context_t context,void* je) {
context->jenv = je;
}