1 /* context_java - implementation of context switching for java threads */
3 /* Copyright (c) 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "xbt/function_types.h"
11 #include "smx_context_java.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(jmsg, bindings, "MSG for Java(TM)");
16 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc, char** argv,
17 void_f_pvoid_t cleanup_func, void* cleanup_arg);
19 static void smx_ctx_java_free(smx_context_t context);
20 static void smx_ctx_java_start(smx_context_t context);
21 static void smx_ctx_java_stop(smx_context_t context);
22 static void smx_ctx_java_suspend(smx_context_t context);
23 static void smx_ctx_java_resume(smx_context_t new_context);
25 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory)
27 /* instantiate the context factory */
28 smx_ctx_base_factory_init(factory);
30 (*factory)->create_context = smx_ctx_java_factory_create_context;
31 /* Leave default behavior of (*factory)->finalize */
32 (*factory)->free = smx_ctx_java_free;
33 (*factory)->stop = smx_ctx_java_stop;
34 (*factory)->suspend = smx_ctx_java_suspend;
35 (*factory)->resume = smx_ctx_java_resume;
37 (*factory)->name = "ctx_java_factory";
41 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc, char** argv,
42 void_f_pvoid_t cleanup_func, void* cleanup_arg)
44 smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
46 /* If the user provided a function for the process then use it
47 otherwise is the context for maestro */
49 context->super.cleanup_func = cleanup_func;
50 context->super.cleanup_arg = cleanup_arg;
51 context->jprocess = (jobject) code;
52 context->jenv = get_current_thread_env();
53 jprocess_start(((smx_ctx_java_t) context)->jprocess,
54 get_current_thread_env());
57 return (smx_context_t) context;
60 static void smx_ctx_java_free(smx_context_t context)
63 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
65 if (ctx_java->jprocess) {
66 jobject jprocess = ctx_java->jprocess;
68 ctx_java->jprocess = NULL;
70 /* if the java process is alive join it */
71 if (jprocess_is_alive(jprocess, get_current_thread_env()))
72 jprocess_join(jprocess, get_current_thread_env());
76 smx_ctx_base_free(context);
79 static void smx_ctx_java_stop(smx_context_t context)
81 jobject jprocess = NULL;
83 smx_ctx_java_t ctx_java;
85 if (context->cleanup_func)
86 (*(context->cleanup_func)) (context->cleanup_arg);
88 ctx_java = (smx_ctx_java_t) context;
90 /*FIXME: is this really necessary?*/
91 if (simix_global->current_process->iwannadie) {
92 /* The maestro call xbt_context_stop() with an exit code set to one */
93 if (ctx_java->jprocess) {
94 /* if the java process is alive schedule it */
95 if (jprocess_is_alive(ctx_java->jprocess, get_current_thread_env())) {
96 jprocess_schedule(simix_global->current_process->context);
97 jprocess = ctx_java->jprocess;
98 ctx_java->jprocess = NULL;
100 /* interrupt the java process */
101 jprocess_exit(jprocess, get_current_thread_env());
106 /* the java process exits */
107 jprocess = ctx_java->jprocess;
108 ctx_java->jprocess = NULL;
111 /* delete the global reference associated with the java process */
112 jprocess_delete_global_ref(jprocess, get_current_thread_env());
115 /*static void smx_ctx_java_swap(smx_context_t context)
118 smx_context_t self = current_context;
120 current_context = context;
122 jprocess_schedule(context);
124 current_context = self;
127 if (current_context->iwannadie)
128 smx_ctx_java_stop(1);
131 static void smx_ctx_java_suspend(smx_context_t context)
133 jprocess_unschedule(context);
136 // FIXME: inline those functions
138 smx_ctx_java_resume(smx_context_t new_context)
140 jprocess_schedule(new_context);