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"
13 #include "smx_context_java.h"
15 XBT_LOG_NEW_DEFAULT_CATEGORY(jmsg, "MSG for Java(TM)");
18 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc, char** argv,
19 void_f_pvoid_t cleanup_func, void* cleanup_arg);
21 static int smx_ctx_java_factory_finalize(smx_context_factory_t * factory);
23 static void smx_ctx_java_free(smx_context_t context);
25 static void smx_ctx_java_start(smx_context_t context);
27 static void smx_ctx_java_stop(smx_context_t context);
29 static void smx_ctx_java_suspend(smx_context_t context);
32 smx_ctx_java_resume(smx_context_t old_context, smx_context_t new_context);
34 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory)
36 /* instantiate the context factory */
37 *factory = xbt_new0(s_smx_context_factory_t, 1);
39 (*factory)->create_context = smx_ctx_java_factory_create_context;
40 (*factory)->finalize = smx_ctx_java_factory_finalize;
41 (*factory)->free = smx_ctx_java_free;
42 (*factory)->stop = smx_ctx_java_stop;
43 (*factory)->suspend = smx_ctx_java_suspend;
44 (*factory)->resume = smx_ctx_java_resume;
46 (*factory)->name = "ctx_java_factory";
49 static int smx_ctx_java_factory_finalize(smx_context_factory_t * factory)
57 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc, char** argv,
58 void_f_pvoid_t cleanup_func, void* cleanup_arg)
60 smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
62 /* If the user provided a function for the process then use it
63 otherwise is the context for maestro */
65 context->cleanup_func = cleanup_func;
66 context->cleanup_arg = cleanup_arg;
67 context->jprocess = (jobject) code;
68 context->jenv = get_current_thread_env();
69 jprocess_start(((smx_ctx_java_t) context)->jprocess,
70 get_current_thread_env());
73 return (smx_context_t) context;
76 static void smx_ctx_java_free(smx_context_t context)
79 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
81 if (ctx_java->jprocess) {
82 jobject jprocess = ctx_java->jprocess;
84 ctx_java->jprocess = NULL;
86 /* if the java process is alive join it */
87 if (jprocess_is_alive(jprocess, get_current_thread_env()))
88 jprocess_join(jprocess, get_current_thread_env());
96 static void smx_ctx_java_stop(smx_context_t context)
98 jobject jprocess = NULL;
100 smx_ctx_java_t ctx_java;
102 if (context->cleanup_func)
103 (*(context->cleanup_func)) (context->cleanup_arg);
105 ctx_java = (smx_ctx_java_t) context;
107 /*FIXME: is this really necessary?*/
108 if (simix_global->current_process->iwannadie) {
109 /* The maestro call xbt_context_stop() with an exit code set to one */
110 if (ctx_java->jprocess) {
111 /* if the java process is alive schedule it */
112 if (jprocess_is_alive(ctx_java->jprocess, get_current_thread_env())) {
113 jprocess_schedule(simix_global->current_process->context);
114 jprocess = ctx_java->jprocess;
115 ctx_java->jprocess = NULL;
117 /* interrupt the java process */
118 jprocess_exit(jprocess, get_current_thread_env());
123 /* the java process exits */
124 jprocess = ctx_java->jprocess;
125 ctx_java->jprocess = NULL;
128 /* delete the global reference associated with the java process */
129 jprocess_delete_global_ref(jprocess, get_current_thread_env());
132 /*static void smx_ctx_java_swap(smx_context_t context)
135 smx_context_t self = current_context;
137 current_context = context;
139 jprocess_schedule(context);
141 current_context = self;
144 if (current_context->iwannadie)
145 smx_ctx_java_stop(1);
148 static void smx_ctx_java_suspend(smx_context_t context)
150 jprocess_unschedule(context);
154 smx_ctx_java_resume(smx_context_t old_context, smx_context_t new_context)
156 jprocess_schedule(new_context);