-/* $Id$ */
-
/* context_java - implementation of context switching for java threads */
-/* Copyright (c) 2007-2008 the SimGrid team. All right reserved */
+/* Copyright (c) 2009, 2010. The SimGrid Team.
+ * All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
#include "xbt/function_types.h"
-#include "xbt/ex_interface.h"
-#include "xbt/xbt_context_private.h"
-#include "xbt/xbt_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);
-
-/* callback: termination */
-static void xbt_ctx_java_ex_terminate(xbt_ex_t * e);
-
-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 int
-xbt_ctx_java_factory_create_maestro_context(xbt_context_t * maestro);
-
-static int xbt_ctx_java_factory_finalize(xbt_context_factory_t * factory);
-
-static void xbt_ctx_java_free(xbt_context_t context);
-
-static void xbt_ctx_java_kill(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_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);
+#include "smx_context_java.h"
-static void xbt_ctx_java_yield(void);
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(jmsg, bindings, "MSG for Java(TM)");
-static void xbt_ctx_java_suspend(xbt_context_t context);
+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);
-static void xbt_ctx_java_resume(xbt_context_t context);
+static void smx_ctx_java_free(smx_context_t context);
+static void smx_ctx_java_start(smx_context_t context);
+static void smx_ctx_java_stop(smx_context_t context);
+static void smx_ctx_java_suspend(smx_context_t context);
+static void smx_ctx_java_resume(smx_context_t new_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)
-{
- xbt_ex_display(e);
- abort();
-}
-
-void xbt_ctx_java_factory_init(xbt_context_factory_t * factory)
+void SIMIX_ctx_java_factory_init(smx_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";
-}
+ smx_ctx_base_factory_init(factory);
-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);
+ (*factory)->create_context = smx_ctx_java_factory_create_context;
+ /* Leave default behavior of (*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;
- context->exception = xbt_new(ex_ctx_t, 1);
- XBT_CTX_INITIALIZE(context->exception);
-
- *maestro = (xbt_context_t) context;
-
- return 0;
-}
-
-static int xbt_ctx_java_factory_finalize(xbt_context_factory_t * factory)
-{
- free(maestro_context->exception);
- free(*factory);
- *factory = NULL;
-
- return 0;
+ (*factory)->name = "ctx_java_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 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->super.cleanup_func = cleanup_func;
+ context->super.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;
if (jprocess_is_alive(jprocess, get_current_thread_env()))
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());
-}
+ smx_ctx_base_free(context);
+}
-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;
-
- if (current_context->cleanup_func)
- (*(current_context->cleanup_func)) (current_context->cleanup_arg);
+ smx_ctx_java_t ctx_java;
- xbt_swag_remove(current_context, context_living);
- xbt_swag_insert(current_context, context_to_destroy);
+ if (context->cleanup_func)
+ (*(context->cleanup_func)) (context->cleanup_arg);
- ctx_java = (xbt_ctx_java_t) current_context;
+ ctx_java = (smx_ctx_java_t) 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;
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;
}
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);
+}
+
+// FIXME: inline those functions
+static void
+smx_ctx_java_resume(smx_context_t new_context)
+{
+ jprocess_schedule(new_context);
}