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 =
84 xbt_ctx_java_factory_create_maestro_context;
85 (*factory)->name = "ctx_java_factory";
89 xbt_ctx_java_factory_create_maestro_context(xbt_context_t * maestro)
91 xbt_ctx_java_t context = xbt_new0(s_xbt_ctx_java_t, 1);
93 context->exception = xbt_new(ex_ctx_t, 1);
94 XBT_CTX_INITIALIZE(context->exception);
96 *maestro = (xbt_context_t) context;
101 static int xbt_ctx_java_factory_finalize(xbt_context_factory_t * factory)
103 free(maestro_context->exception);
111 xbt_ctx_java_factory_create_context(const char *name, xbt_main_func_t code,
112 void_f_pvoid_t startup_func,
114 void_f_pvoid_t cleanup_func,
115 void *cleanup_arg, int argc, char **argv)
117 xbt_ctx_java_t context = xbt_new0(s_xbt_ctx_java_t, 1);
119 context->name = xbt_strdup(name);
121 context->cleanup_func = cleanup_func;
122 context->cleanup_arg = cleanup_arg;
124 context->exception = xbt_new(ex_ctx_t, 1);
125 XBT_CTX_INITIALIZE(context->exception);
127 context->free = xbt_ctx_java_free;
128 context->kill = xbt_ctx_java_kill;
129 context->schedule = xbt_ctx_java_schedule;
130 context->yield = xbt_ctx_java_yield;
131 context->start = xbt_ctx_java_start;
132 context->stop = xbt_ctx_java_stop;
133 context->jprocess = (jobject) startup_arg;
134 context->jenv = get_current_thread_env();
136 return (xbt_context_t) context;
139 static void xbt_ctx_java_free(xbt_context_t context)
142 xbt_ctx_java_t ctx_java = (xbt_ctx_java_t) context;
144 free(ctx_java->name);
146 if (ctx_java->jprocess) {
147 jobject jprocess = ctx_java->jprocess;
149 ctx_java->jprocess = NULL;
151 /* if the java process is alive join it */
152 if (jprocess_is_alive(jprocess, get_current_thread_env()))
153 jprocess_join(jprocess, get_current_thread_env());
156 if (ctx_java->exception)
157 free(ctx_java->exception);
164 static void xbt_ctx_java_kill(xbt_context_t context)
166 context->iwannadie = 1;
167 xbt_ctx_java_swap(context);
171 * \param context the winner
173 * Calling this function blocks the current context and schedule \a context.
174 * When \a context will call xbt_context_yield, it will return
175 * to this function as if nothing had happened.
177 * Only the maestro can call this function to run a given process.
179 static void xbt_ctx_java_schedule(xbt_context_t context)
181 xbt_assert0((current_context == maestro_context),
182 "You are not supposed to run this function here!");
183 xbt_ctx_java_swap(context);
187 * Calling this function makes the current context yield. The context
188 * that scheduled it returns from xbt_context_schedule as if nothing
191 * Only the processes can call this function, giving back the control
194 static void xbt_ctx_java_yield(void)
196 xbt_assert0((current_context != maestro_context),
197 "You are not supposed to run this function here!");
198 jprocess_unschedule(current_context);
201 static void xbt_ctx_java_start(xbt_context_t context)
203 jprocess_start(((xbt_ctx_java_t) context)->jprocess,
204 get_current_thread_env());
207 static void xbt_ctx_java_stop(int exit_code)
209 jobject jprocess = NULL;
211 xbt_ctx_java_t ctx_java;
213 if (current_context->cleanup_func)
214 (*(current_context->cleanup_func)) (current_context->cleanup_arg);
216 xbt_swag_remove(current_context, context_living);
217 xbt_swag_insert(current_context, context_to_destroy);
219 ctx_java = (xbt_ctx_java_t) current_context;
221 if (ctx_java->iwannadie) {
222 /* The maestro call xbt_context_stop() with an exit code set to one */
223 if (ctx_java->jprocess) {
224 /* if the java process is alive schedule it */
225 if (jprocess_is_alive(ctx_java->jprocess, get_current_thread_env())) {
226 jprocess_schedule(current_context);
227 jprocess = ctx_java->jprocess;
228 ctx_java->jprocess = NULL;
230 /* interrupt the java process */
231 jprocess_exit(jprocess, get_current_thread_env());
236 /* the java process exits */
237 jprocess = ctx_java->jprocess;
238 ctx_java->jprocess = NULL;
241 /* delete the global reference associated with the java process */
242 jprocess_delete_global_ref(jprocess, get_current_thread_env());
245 static void xbt_ctx_java_swap(xbt_context_t context)
248 xbt_context_t self = current_context;
250 current_context = context;
252 jprocess_schedule(context);
254 current_context = self;
257 if (current_context->iwannadie)
258 xbt_ctx_java_stop(1);