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"
13 #include "xbt/xbt_context_private.h"
14 #include "xbt/xbt_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 xbt_ctx_java_factory_create_context(const char *name, xbt_main_func_t code,
26 void_f_pvoid_t startup_func,
28 void_f_pvoid_t cleanup_func,
29 void *cleanup_arg, int argc, char **argv);
32 xbt_ctx_java_factory_create_maestro_context(xbt_context_t * maestro);
34 static int xbt_ctx_java_factory_finalize(xbt_context_factory_t * factory);
36 static void xbt_ctx_java_free(xbt_context_t context);
38 static void xbt_ctx_java_kill(xbt_context_t context);
40 static void xbt_ctx_java_schedule(xbt_context_t context);
42 static void xbt_ctx_java_yield(void);
44 static void xbt_ctx_java_start(xbt_context_t context);
46 static void xbt_ctx_java_stop(int exit_code);
48 static void xbt_ctx_java_swap(xbt_context_t context);
50 static void xbt_ctx_java_schedule(xbt_context_t context);
52 static void xbt_ctx_java_yield(void);
54 static void xbt_ctx_java_suspend(xbt_context_t context);
56 static void xbt_ctx_java_resume(xbt_context_t context);
59 /* callback: context fetching */
60 static ex_ctx_t *xbt_ctx_java_ex_ctx(void)
62 return current_context->exception;
65 /* callback: termination */
66 static void xbt_ctx_java_ex_terminate(xbt_ex_t * e)
72 void xbt_ctx_java_factory_init(xbt_context_factory_t * factory)
74 /* context exception handlers */
75 __xbt_ex_ctx = xbt_ctx_java_ex_ctx;
76 __xbt_ex_terminate = xbt_ctx_java_ex_terminate;
78 /* instantiate the context factory */
79 *factory = xbt_new0(s_xbt_context_factory_t, 1);
81 (*factory)->create_context = xbt_ctx_java_factory_create_context;
82 (*factory)->finalize = xbt_ctx_java_factory_finalize;
83 (*factory)->create_maestro_context = xbt_ctx_java_factory_create_maestro_context;
84 (*factory)->free = xbt_ctx_java_free;
85 (*factory)->kill = xbt_ctx_java_kill;
86 (*factory)->schedule = xbt_ctx_java_schedule;
87 (*factory)->yield = xbt_ctx_java_yield;
88 (*factory)->start = xbt_ctx_java_start;
89 (*factory)->stop = xbt_ctx_java_stop;
90 (*factory)->name = "ctx_java_factory";
94 xbt_ctx_java_factory_create_maestro_context(xbt_context_t * maestro)
96 xbt_ctx_java_t context = xbt_new0(s_xbt_ctx_java_t, 1);
98 context->exception = xbt_new(ex_ctx_t, 1);
99 XBT_CTX_INITIALIZE(context->exception);
101 *maestro = (xbt_context_t) context;
106 static int xbt_ctx_java_factory_finalize(xbt_context_factory_t * factory)
108 free(maestro_context->exception);
116 xbt_ctx_java_factory_create_context(const char *name, xbt_main_func_t code,
117 void_f_pvoid_t startup_func,
119 void_f_pvoid_t cleanup_func,
120 void *cleanup_arg, int argc, char **argv)
122 xbt_ctx_java_t context = xbt_new0(s_xbt_ctx_java_t, 1);
124 context->name = xbt_strdup(name);
125 context->cleanup_func = cleanup_func;
126 context->cleanup_arg = cleanup_arg;
127 context->exception = xbt_new(ex_ctx_t, 1);
128 XBT_CTX_INITIALIZE(context->exception);
129 context->jprocess = (jobject) startup_arg;
130 context->jenv = get_current_thread_env();
132 return (xbt_context_t) context;
135 static void xbt_ctx_java_free(xbt_context_t context)
138 xbt_ctx_java_t ctx_java = (xbt_ctx_java_t) context;
140 free(ctx_java->name);
142 if (ctx_java->jprocess) {
143 jobject jprocess = ctx_java->jprocess;
145 ctx_java->jprocess = NULL;
147 /* if the java process is alive join it */
148 if (jprocess_is_alive(jprocess, get_current_thread_env()))
149 jprocess_join(jprocess, get_current_thread_env());
152 if (ctx_java->exception)
153 free(ctx_java->exception);
160 static void xbt_ctx_java_kill(xbt_context_t context)
162 context->iwannadie = 1;
163 xbt_ctx_java_swap(context);
167 * \param context the winner
169 * Calling this function blocks the current context and schedule \a context.
170 * When \a context will call xbt_context_yield, it will return
171 * to this function as if nothing had happened.
173 * Only the maestro can call this function to run a given process.
175 static void xbt_ctx_java_schedule(xbt_context_t context)
177 xbt_assert0((current_context == maestro_context),
178 "You are not supposed to run this function here!");
179 xbt_ctx_java_swap(context);
183 * Calling this function makes the current context yield. The context
184 * that scheduled it returns from xbt_context_schedule as if nothing
187 * Only the processes can call this function, giving back the control
190 static void xbt_ctx_java_yield(void)
192 xbt_assert0((current_context != maestro_context),
193 "You are not supposed to run this function here!");
194 jprocess_unschedule(current_context);
197 static void xbt_ctx_java_start(xbt_context_t context)
199 jprocess_start(((xbt_ctx_java_t) context)->jprocess,
200 get_current_thread_env());
203 static void xbt_ctx_java_stop(int exit_code)
205 jobject jprocess = NULL;
207 xbt_ctx_java_t ctx_java;
209 if (current_context->cleanup_func)
210 (*(current_context->cleanup_func)) (current_context->cleanup_arg);
212 xbt_swag_remove(current_context, context_living);
213 xbt_swag_insert(current_context, context_to_destroy);
215 ctx_java = (xbt_ctx_java_t) current_context;
217 if (ctx_java->iwannadie) {
218 /* The maestro call xbt_context_stop() with an exit code set to one */
219 if (ctx_java->jprocess) {
220 /* if the java process is alive schedule it */
221 if (jprocess_is_alive(ctx_java->jprocess, get_current_thread_env())) {
222 jprocess_schedule(current_context);
223 jprocess = ctx_java->jprocess;
224 ctx_java->jprocess = NULL;
226 /* interrupt the java process */
227 jprocess_exit(jprocess, get_current_thread_env());
232 /* the java process exits */
233 jprocess = ctx_java->jprocess;
234 ctx_java->jprocess = NULL;
237 /* delete the global reference associated with the java process */
238 jprocess_delete_global_ref(jprocess, get_current_thread_env());
241 static void xbt_ctx_java_swap(xbt_context_t context)
244 xbt_context_t self = current_context;
246 current_context = context;
248 jprocess_schedule(context);
250 current_context = self;
253 if (current_context->iwannadie)
254 xbt_ctx_java_stop(1);