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)
80 /* Restore the default exception setup */
81 __xbt_ex_ctx = &__xbt_ex_ctx_default;
82 __xbt_ex_terminate = &__xbt_ex_terminate_default;
88 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc, char** argv,
89 void_f_pvoid_t cleanup_func, void* cleanup_arg)
91 smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
93 context->exception = xbt_new(ex_ctx_t, 1);
94 XBT_CTX_INITIALIZE(context->exception);
96 /* If the user provided a function for the process then use it
97 otherwise is the context for maestro */
99 context->cleanup_func = cleanup_func;
100 context->cleanup_arg = cleanup_arg;
101 context->jprocess = (jobject) code;
102 context->jenv = get_current_thread_env();
105 return (smx_context_t) context;
108 static void smx_ctx_java_free(smx_context_t context)
111 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
113 if (ctx_java->jprocess) {
114 jobject jprocess = ctx_java->jprocess;
116 ctx_java->jprocess = NULL;
118 /* if the java process is alive join it */
119 if (jprocess_is_alive(jprocess, get_current_thread_env()))
120 jprocess_join(jprocess, get_current_thread_env());
123 if (ctx_java->exception)
124 free(ctx_java->exception);
131 static void smx_ctx_java_start(smx_context_t context)
133 jprocess_start(((smx_ctx_java_t) context)->jprocess,
134 get_current_thread_env());
137 static void smx_ctx_java_stop(smx_context_t context)
139 jobject jprocess = NULL;
141 smx_ctx_java_t ctx_java;
143 if (context->cleanup_func)
144 (*(context->cleanup_func)) (context->cleanup_arg);
146 ctx_java = (smx_ctx_java_t) context;
148 /*FIXME: is this really necessary?*/
149 if (simix_global->current_process->iwannadie) {
150 /* The maestro call xbt_context_stop() with an exit code set to one */
151 if (ctx_java->jprocess) {
152 /* if the java process is alive schedule it */
153 if (jprocess_is_alive(ctx_java->jprocess, get_current_thread_env())) {
154 jprocess_schedule(simix_global->current_process->context);
155 jprocess = ctx_java->jprocess;
156 ctx_java->jprocess = NULL;
158 /* interrupt the java process */
159 jprocess_exit(jprocess, get_current_thread_env());
164 /* the java process exits */
165 jprocess = ctx_java->jprocess;
166 ctx_java->jprocess = NULL;
169 /* delete the global reference associated with the java process */
170 jprocess_delete_global_ref(jprocess, get_current_thread_env());
173 /*static void smx_ctx_java_swap(smx_context_t context)
176 smx_context_t self = current_context;
178 current_context = context;
180 jprocess_schedule(context);
182 current_context = self;
185 if (current_context->iwannadie)
186 smx_ctx_java_stop(1);
189 static void smx_ctx_java_suspend(smx_context_t context)
191 jprocess_unschedule(context);
195 smx_ctx_java_resume(smx_context_t old_context, smx_context_t new_context)
197 jprocess_schedule(new_context);