X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/42ad1e118f005d56af6f9e87d7ecec75bb311108..7e71d73eb6728d8d498d010976f038789049c131:/src/simix/smx_context_java.c diff --git a/src/simix/smx_context_java.c b/src/simix/smx_context_java.c index b40d5e5060..8a64130bd7 100644 --- a/src/simix/smx_context_java.c +++ b/src/simix/smx_context_java.c @@ -9,135 +9,74 @@ #include "xbt/function_types.h" -#include "xbt/ex_interface.h" -#include "xbt/xbt_context_private.h" -#include "xbt/xbt_context_java.h" +#include "private.h" +#include "smx_context_java.h" XBT_LOG_NEW_DEFAULT_CATEGORY(jmsg, "MSG for Java(TM)"); -/* callback: context fetching */ -static ex_ctx_t *xbt_ctx_java_ex_ctx(void); +static smx_context_t +smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc, char** argv, + void_f_pvoid_t cleanup_func, void* cleanup_arg); -/* callback: termination */ -static void xbt_ctx_java_ex_terminate(xbt_ex_t * e); +static int smx_ctx_java_factory_finalize(smx_context_factory_t * factory); -static xbt_context_t -xbt_ctx_java_factory_create_context(const char *name, xbt_main_func_t code, - void_f_pvoid_t startup_func, - void *startup_arg, - void_f_pvoid_t cleanup_func, - void *cleanup_arg, int argc, char **argv); +static void smx_ctx_java_free(smx_context_t context); -static int -xbt_ctx_java_factory_create_maestro_context(xbt_context_t * maestro); +static void smx_ctx_java_start(smx_context_t context); -static int xbt_ctx_java_factory_finalize(xbt_context_factory_t * factory); +static void smx_ctx_java_stop(smx_context_t context); -static void xbt_ctx_java_free(xbt_context_t context); +static void smx_ctx_java_suspend(smx_context_t context); -static void xbt_ctx_java_kill(xbt_context_t context); +static void + smx_ctx_java_resume(smx_context_t old_context, smx_context_t new_context); -static void xbt_ctx_java_schedule(xbt_context_t context); - -static void xbt_ctx_java_yield(void); - -static void xbt_ctx_java_start(xbt_context_t context); - -static void xbt_ctx_java_stop(int exit_code); - -static void xbt_ctx_java_swap(xbt_context_t context); - -static void xbt_ctx_java_schedule(xbt_context_t context); - -static void xbt_ctx_java_yield(void); - -static void xbt_ctx_java_suspend(xbt_context_t context); - -static void xbt_ctx_java_resume(xbt_context_t context); - - -/* callback: context fetching */ -static ex_ctx_t *xbt_ctx_java_ex_ctx(void) -{ - return current_context->exception; -} - -/* callback: termination */ -static void xbt_ctx_java_ex_terminate(xbt_ex_t * e) +void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory) { - xbt_ex_display(e); - abort(); -} - -void xbt_ctx_java_factory_init(xbt_context_factory_t * factory) -{ - /* context exception handlers */ - __xbt_ex_ctx = xbt_ctx_java_ex_ctx; - __xbt_ex_terminate = xbt_ctx_java_ex_terminate; - /* instantiate the context factory */ - *factory = xbt_new0(s_xbt_context_factory_t, 1); - - (*factory)->create_context = xbt_ctx_java_factory_create_context; - (*factory)->finalize = xbt_ctx_java_factory_finalize; - (*factory)->create_maestro_context = xbt_ctx_java_factory_create_maestro_context; - (*factory)->free = xbt_ctx_java_free; - (*factory)->kill = xbt_ctx_java_kill; - (*factory)->schedule = xbt_ctx_java_schedule; - (*factory)->yield = xbt_ctx_java_yield; - (*factory)->start = xbt_ctx_java_start; - (*factory)->stop = xbt_ctx_java_stop; - (*factory)->name = "ctx_java_factory"; -} + *factory = xbt_new0(s_smx_context_factory_t, 1); -static int -xbt_ctx_java_factory_create_maestro_context(xbt_context_t * maestro) -{ - xbt_ctx_java_t context = xbt_new0(s_xbt_ctx_java_t, 1); - - context->exception = xbt_new(ex_ctx_t, 1); - XBT_CTX_INITIALIZE(context->exception); - - *maestro = (xbt_context_t) context; + (*factory)->create_context = smx_ctx_java_factory_create_context; + (*factory)->finalize = smx_ctx_java_factory_finalize; + (*factory)->free = smx_ctx_java_free; + (*factory)->stop = smx_ctx_java_stop; + (*factory)->suspend = smx_ctx_java_suspend; + (*factory)->resume = smx_ctx_java_resume; - return 0; + (*factory)->name = "ctx_java_factory"; } -static int xbt_ctx_java_factory_finalize(xbt_context_factory_t * factory) +static int smx_ctx_java_factory_finalize(smx_context_factory_t * factory) { - free(maestro_context->exception); free(*factory); *factory = NULL; - return 0; } -static xbt_context_t -xbt_ctx_java_factory_create_context(const char *name, xbt_main_func_t code, - void_f_pvoid_t startup_func, - void *startup_arg, - void_f_pvoid_t cleanup_func, - void *cleanup_arg, int argc, char **argv) +static smx_context_t +smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc, char** argv, + void_f_pvoid_t cleanup_func, void* cleanup_arg) { - xbt_ctx_java_t context = xbt_new0(s_xbt_ctx_java_t, 1); - - context->name = xbt_strdup(name); - context->cleanup_func = cleanup_func; - context->cleanup_arg = cleanup_arg; - context->exception = xbt_new(ex_ctx_t, 1); - XBT_CTX_INITIALIZE(context->exception); - context->jprocess = (jobject) startup_arg; - context->jenv = get_current_thread_env(); - - return (xbt_context_t) context; + smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1); + + /* If the user provided a function for the process then use it + otherwise is the context for maestro */ + if(code){ + context->cleanup_func = cleanup_func; + context->cleanup_arg = cleanup_arg; + context->jprocess = (jobject) code; + context->jenv = get_current_thread_env(); + jprocess_start(((smx_ctx_java_t) context)->jprocess, + get_current_thread_env()); + } + + return (smx_context_t) context; } -static void xbt_ctx_java_free(xbt_context_t context) +static void smx_ctx_java_free(smx_context_t context) { if (context) { - xbt_ctx_java_t ctx_java = (xbt_ctx_java_t) context; - - free(ctx_java->name); + smx_ctx_java_t ctx_java = (smx_ctx_java_t) context; if (ctx_java->jprocess) { jobject jprocess = ctx_java->jprocess; @@ -149,77 +88,29 @@ static void xbt_ctx_java_free(xbt_context_t context) jprocess_join(jprocess, get_current_thread_env()); } - if (ctx_java->exception) - free(ctx_java->exception); - free(context); context = NULL; } -} - -static void xbt_ctx_java_kill(xbt_context_t context) -{ - context->iwannadie = 1; - xbt_ctx_java_swap(context); -} - -/** - * \param context the winner - * - * Calling this function blocks the current context and schedule \a context. - * When \a context will call xbt_context_yield, it will return - * to this function as if nothing had happened. - * - * Only the maestro can call this function to run a given process. - */ -static void xbt_ctx_java_schedule(xbt_context_t context) -{ - xbt_assert0((current_context == maestro_context), - "You are not supposed to run this function here!"); - xbt_ctx_java_swap(context); -} +} -/** - * Calling this function makes the current context yield. The context - * that scheduled it returns from xbt_context_schedule as if nothing - * had happened. - * - * Only the processes can call this function, giving back the control - * to the maestro - */ -static void xbt_ctx_java_yield(void) -{ - xbt_assert0((current_context != maestro_context), - "You are not supposed to run this function here!"); - jprocess_unschedule(current_context); -} - -static void xbt_ctx_java_start(xbt_context_t context) -{ - jprocess_start(((xbt_ctx_java_t) context)->jprocess, - get_current_thread_env()); -} - -static void xbt_ctx_java_stop(int exit_code) +static void smx_ctx_java_stop(smx_context_t context) { jobject jprocess = NULL; - xbt_ctx_java_t ctx_java; + smx_ctx_java_t ctx_java; - if (current_context->cleanup_func) - (*(current_context->cleanup_func)) (current_context->cleanup_arg); + if (context->cleanup_func) + (*(context->cleanup_func)) (context->cleanup_arg); - xbt_swag_remove(current_context, context_living); - xbt_swag_insert(current_context, context_to_destroy); + ctx_java = (smx_ctx_java_t) context; - ctx_java = (xbt_ctx_java_t) current_context; - - if (ctx_java->iwannadie) { + /*FIXME: is this really necessary?*/ + if (simix_global->current_process->iwannadie) { /* The maestro call xbt_context_stop() with an exit code set to one */ if (ctx_java->jprocess) { /* if the java process is alive schedule it */ if (jprocess_is_alive(ctx_java->jprocess, get_current_thread_env())) { - jprocess_schedule(current_context); + jprocess_schedule(simix_global->current_process->context); jprocess = ctx_java->jprocess; ctx_java->jprocess = NULL; @@ -238,10 +129,10 @@ static void xbt_ctx_java_stop(int exit_code) jprocess_delete_global_ref(jprocess, get_current_thread_env()); } -static void xbt_ctx_java_swap(xbt_context_t context) +/*static void smx_ctx_java_swap(smx_context_t context) { if (context) { - xbt_context_t self = current_context; + smx_context_t self = current_context; current_context = context; @@ -251,5 +142,16 @@ static void xbt_ctx_java_swap(xbt_context_t context) } if (current_context->iwannadie) - xbt_ctx_java_stop(1); + smx_ctx_java_stop(1); +}*/ + +static void smx_ctx_java_suspend(smx_context_t context) +{ + jprocess_unschedule(context); +} + +static void +smx_ctx_java_resume(smx_context_t old_context, smx_context_t new_context) +{ + jprocess_schedule(new_context); }