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 smx_context_t smx_ctx_java_factory_create_maestro_context(void);
30 static int smx_ctx_java_factory_finalize(smx_context_factory_t * factory);
32 static void smx_ctx_java_free(smx_context_t context);
34 static void smx_ctx_java_start(smx_context_t context);
36 static void smx_ctx_java_stop(smx_context_t context);
38 static void smx_ctx_java_suspend(smx_context_t context);
41 smx_ctx_java_resume(smx_context_t old_context, smx_context_t new_context);
44 /* callback: context fetching */
45 static ex_ctx_t *xbt_ctx_java_ex_ctx(void)
47 return simix_global->current_process->context->exception;
50 /* callback: termination */
51 static void xbt_ctx_java_ex_terminate(xbt_ex_t * e)
57 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory)
59 /* context exception handlers */
60 __xbt_ex_ctx = xbt_ctx_java_ex_ctx;
61 __xbt_ex_terminate = xbt_ctx_java_ex_terminate;
63 /* instantiate the context factory */
64 *factory = xbt_new0(s_smx_context_factory_t, 1);
66 (*factory)->create_context = smx_ctx_java_factory_create_context;
67 (*factory)->finalize = smx_ctx_java_factory_finalize;
68 (*factory)->create_maestro_context = smx_ctx_java_factory_create_maestro_context;
69 (*factory)->free = smx_ctx_java_free;
70 (*factory)->start = smx_ctx_java_start;
71 (*factory)->stop = smx_ctx_java_stop;
72 (*factory)->suspend = smx_ctx_java_suspend;
73 (*factory)->resume = smx_ctx_java_resume;
75 (*factory)->name = "ctx_java_factory";
78 static smx_context_t smx_ctx_java_factory_create_maestro_context(void)
80 smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
82 context->exception = xbt_new(ex_ctx_t, 1);
83 XBT_CTX_INITIALIZE(context->exception);
85 return (smx_context_t) context;
88 static int smx_ctx_java_factory_finalize(smx_context_factory_t * factory)
90 /*FIXME: free(maestro_context->exception);*/
98 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc, char** argv,
99 void_f_pvoid_t cleanup_func, void* cleanup_arg)
101 smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
103 context->cleanup_func = cleanup_func;
104 context->cleanup_arg = cleanup_arg;
105 context->exception = xbt_new(ex_ctx_t, 1);
106 XBT_CTX_INITIALIZE(context->exception);
107 context->jprocess = (jobject) code;
108 context->jenv = get_current_thread_env();
110 return (smx_context_t) context;
113 static void smx_ctx_java_free(smx_context_t context)
116 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
118 if (ctx_java->jprocess) {
119 jobject jprocess = ctx_java->jprocess;
121 ctx_java->jprocess = NULL;
123 /* if the java process is alive join it */
124 if (jprocess_is_alive(jprocess, get_current_thread_env()))
125 jprocess_join(jprocess, get_current_thread_env());
128 if (ctx_java->exception)
129 free(ctx_java->exception);
136 static void smx_ctx_java_start(smx_context_t context)
138 jprocess_start(((smx_ctx_java_t) context)->jprocess,
139 get_current_thread_env());
142 static void smx_ctx_java_stop(smx_context_t context)
144 jobject jprocess = NULL;
146 smx_ctx_java_t ctx_java;
148 if (context->cleanup_func)
149 (*(context->cleanup_func)) (context->cleanup_arg);
151 ctx_java = (smx_ctx_java_t) context;
153 /*FIXME: is this really necessary?*/
154 if (simix_global->current_process->iwannadie) {
155 /* The maestro call xbt_context_stop() with an exit code set to one */
156 if (ctx_java->jprocess) {
157 /* if the java process is alive schedule it */
158 if (jprocess_is_alive(ctx_java->jprocess, get_current_thread_env())) {
159 jprocess_schedule(simix_global->current_process->context);
160 jprocess = ctx_java->jprocess;
161 ctx_java->jprocess = NULL;
163 /* interrupt the java process */
164 jprocess_exit(jprocess, get_current_thread_env());
169 /* the java process exits */
170 jprocess = ctx_java->jprocess;
171 ctx_java->jprocess = NULL;
174 /* delete the global reference associated with the java process */
175 jprocess_delete_global_ref(jprocess, get_current_thread_env());
178 /*static void smx_ctx_java_swap(smx_context_t context)
181 smx_context_t self = current_context;
183 current_context = context;
185 jprocess_schedule(context);
187 current_context = self;
190 if (current_context->iwannadie)
191 smx_ctx_java_stop(1);
194 static void smx_ctx_java_suspend(smx_context_t context)
196 jprocess_unschedule(context);
200 smx_ctx_java_resume(smx_context_t old_context, smx_context_t new_context)
202 jprocess_schedule(new_context);