3 /* context_java - implementation of context switching for java threads */
5 /* Copyright (c) 2007-2008 the SimGrid team. All right reserved */
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. */
11 #include "xbt/function_types.h"
12 #include "xbt/ex_interface.h"
14 #include "smx_context_java.h"
16 XBT_LOG_NEW_DEFAULT_CATEGORY(jmsg, "MSG for Java(TM)");
18 /* callback: context fetching */
19 static ex_ctx_t *xbt_ctx_java_ex_ctx(void);
21 /* callback: termination */
22 static void xbt_ctx_java_ex_terminate(xbt_ex_t * e);
25 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc, char** argv,
26 void_f_pvoid_t cleanup_func, void* cleanup_arg);
28 static int smx_ctx_java_factory_finalize(smx_context_factory_t * factory);
30 static void smx_ctx_java_free(smx_context_t context);
32 static void smx_ctx_java_start(smx_context_t context);
34 static void smx_ctx_java_stop(smx_context_t context);
36 static void smx_ctx_java_suspend(smx_context_t context);
39 smx_ctx_java_resume(smx_context_t old_context, smx_context_t new_context);
42 /* callback: context fetching */
43 static ex_ctx_t *xbt_ctx_java_ex_ctx(void)
45 return simix_global->current_process->context->exception;
48 /* callback: termination */
49 static void xbt_ctx_java_ex_terminate(xbt_ex_t * e)
55 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory)
57 /* context exception handlers */
58 __xbt_ex_ctx = xbt_ctx_java_ex_ctx;
59 __xbt_ex_terminate = xbt_ctx_java_ex_terminate;
61 /* instantiate the context factory */
62 *factory = xbt_new0(s_smx_context_factory_t, 1);
64 (*factory)->create_context = smx_ctx_java_factory_create_context;
65 (*factory)->finalize = smx_ctx_java_factory_finalize;
66 (*factory)->free = smx_ctx_java_free;
67 (*factory)->start = smx_ctx_java_start;
68 (*factory)->stop = smx_ctx_java_stop;
69 (*factory)->suspend = smx_ctx_java_suspend;
70 (*factory)->resume = smx_ctx_java_resume;
72 (*factory)->name = "ctx_java_factory";
75 static int smx_ctx_java_factory_finalize(smx_context_factory_t * factory)
77 /*FIXME: free(maestro_context->exception);*/
85 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc, char** argv,
86 void_f_pvoid_t cleanup_func, void* cleanup_arg)
88 smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
90 context->exception = xbt_new(ex_ctx_t, 1);
91 XBT_CTX_INITIALIZE(context->exception);
93 /* If the user provided a function for the process then use it
94 otherwise is the context for maestro */
96 context->cleanup_func = cleanup_func;
97 context->cleanup_arg = cleanup_arg;
98 context->jprocess = (jobject) code;
99 context->jenv = get_current_thread_env();
102 return (smx_context_t) context;
105 static void smx_ctx_java_free(smx_context_t context)
108 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
110 if (ctx_java->jprocess) {
111 jobject jprocess = ctx_java->jprocess;
113 ctx_java->jprocess = NULL;
115 /* if the java process is alive join it */
116 if (jprocess_is_alive(jprocess, get_current_thread_env()))
117 jprocess_join(jprocess, get_current_thread_env());
120 if (ctx_java->exception)
121 free(ctx_java->exception);
128 static void smx_ctx_java_start(smx_context_t context)
130 jprocess_start(((smx_ctx_java_t) context)->jprocess,
131 get_current_thread_env());
134 static void smx_ctx_java_stop(smx_context_t context)
136 jobject jprocess = NULL;
138 smx_ctx_java_t ctx_java;
140 if (context->cleanup_func)
141 (*(context->cleanup_func)) (context->cleanup_arg);
143 ctx_java = (smx_ctx_java_t) context;
145 /*FIXME: is this really necessary?*/
146 if (simix_global->current_process->iwannadie) {
147 /* The maestro call xbt_context_stop() with an exit code set to one */
148 if (ctx_java->jprocess) {
149 /* if the java process is alive schedule it */
150 if (jprocess_is_alive(ctx_java->jprocess, get_current_thread_env())) {
151 jprocess_schedule(simix_global->current_process->context);
152 jprocess = ctx_java->jprocess;
153 ctx_java->jprocess = NULL;
155 /* interrupt the java process */
156 jprocess_exit(jprocess, get_current_thread_env());
161 /* the java process exits */
162 jprocess = ctx_java->jprocess;
163 ctx_java->jprocess = NULL;
166 /* delete the global reference associated with the java process */
167 jprocess_delete_global_ref(jprocess, get_current_thread_env());
170 /*static void smx_ctx_java_swap(smx_context_t context)
173 smx_context_t self = current_context;
175 current_context = context;
177 jprocess_schedule(context);
179 current_context = self;
182 if (current_context->iwannadie)
183 smx_ctx_java_stop(1);
186 static void smx_ctx_java_suspend(smx_context_t context)
188 jprocess_unschedule(context);
192 smx_ctx_java_resume(smx_context_t old_context, smx_context_t new_context)
194 jprocess_schedule(new_context);