1 /* context_java - implementation of context switching for java threads */
3 /* Copyright (c) 2007-2008 the SimGrid team. All right reserved */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "xbt/function_types.h"
10 #include "smx_context_java.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(jmsg, bindings, "MSG for Java(TM)");
15 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc, char** argv,
16 void_f_pvoid_t cleanup_func, void* cleanup_arg);
18 static void smx_ctx_java_free(smx_context_t context);
19 static void smx_ctx_java_start(smx_context_t context);
20 static void smx_ctx_java_stop(smx_context_t context);
21 static void smx_ctx_java_suspend(smx_context_t context);
22 static void smx_ctx_java_resume(smx_context_t new_context);
24 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory)
26 /* instantiate the context factory */
27 smx_ctx_base_factory_init(factory);
29 (*factory)->create_context = smx_ctx_java_factory_create_context;
30 /* Leave default behavior of (*factory)->finalize */
31 (*factory)->free = smx_ctx_java_free;
32 (*factory)->stop = smx_ctx_java_stop;
33 (*factory)->suspend = smx_ctx_java_suspend;
34 (*factory)->resume = smx_ctx_java_resume;
36 (*factory)->name = "ctx_java_factory";
40 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc, char** argv,
41 void_f_pvoid_t cleanup_func, void* cleanup_arg)
43 smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
45 /* If the user provided a function for the process then use it
46 otherwise is the context for maestro */
48 context->super.cleanup_func = cleanup_func;
49 context->super.cleanup_arg = cleanup_arg;
50 context->jprocess = (jobject) code;
51 context->jenv = get_current_thread_env();
52 jprocess_start(((smx_ctx_java_t) context)->jprocess,
53 get_current_thread_env());
56 return (smx_context_t) context;
59 static void smx_ctx_java_free(smx_context_t context)
62 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
64 if (ctx_java->jprocess) {
65 jobject jprocess = ctx_java->jprocess;
67 ctx_java->jprocess = NULL;
69 /* if the java process is alive join it */
70 if (jprocess_is_alive(jprocess, get_current_thread_env()))
71 jprocess_join(jprocess, get_current_thread_env());
75 smx_ctx_base_free(context);
78 static void smx_ctx_java_stop(smx_context_t context)
80 jobject jprocess = NULL;
82 smx_ctx_java_t ctx_java;
84 if (context->cleanup_func)
85 (*(context->cleanup_func)) (context->cleanup_arg);
87 ctx_java = (smx_ctx_java_t) context;
89 /*FIXME: is this really necessary?*/
90 if (simix_global->current_process->iwannadie) {
91 /* The maestro call xbt_context_stop() with an exit code set to one */
92 if (ctx_java->jprocess) {
93 /* if the java process is alive schedule it */
94 if (jprocess_is_alive(ctx_java->jprocess, get_current_thread_env())) {
95 jprocess_schedule(simix_global->current_process->context);
96 jprocess = ctx_java->jprocess;
97 ctx_java->jprocess = NULL;
99 /* interrupt the java process */
100 jprocess_exit(jprocess, get_current_thread_env());
105 /* the java process exits */
106 jprocess = ctx_java->jprocess;
107 ctx_java->jprocess = NULL;
110 /* delete the global reference associated with the java process */
111 jprocess_delete_global_ref(jprocess, get_current_thread_env());
114 /*static void smx_ctx_java_swap(smx_context_t context)
117 smx_context_t self = current_context;
119 current_context = context;
121 jprocess_schedule(context);
123 current_context = self;
126 if (current_context->iwannadie)
127 smx_ctx_java_stop(1);
130 static void smx_ctx_java_suspend(smx_context_t context)
132 jprocess_unschedule(context);
135 // FIXME: inline those functions
137 smx_ctx_java_resume(smx_context_t new_context)
139 jprocess_schedule(new_context);