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,
18 void_pfn_smxprocess_t cleanup_func,
21 static void smx_ctx_java_free(smx_context_t context);
22 static void smx_ctx_java_start(smx_context_t context);
23 static void smx_ctx_java_stop(smx_context_t context);
24 static void smx_ctx_java_suspend(smx_context_t context);
25 static void smx_ctx_java_resume(smx_context_t new_context);
26 static void smx_ctx_java_runall(xbt_swag_t processes);
28 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory)
30 /* instantiate the context factory */
31 smx_ctx_base_factory_init(factory);
33 (*factory)->create_context = smx_ctx_java_factory_create_context;
34 /* Leave default behavior of (*factory)->finalize */
35 (*factory)->free = smx_ctx_java_free;
36 (*factory)->stop = smx_ctx_java_stop;
37 (*factory)->suspend = smx_ctx_java_suspend;
38 (*factory)->runall = smx_ctx_java_runall;
39 (*factory)->name = "ctx_java_factory";
43 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc,
45 void_pfn_smxprocess_t cleanup_func,
48 smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
50 /* If the user provided a function for the process then use it
51 otherwise is the context for maestro */
53 context->super.cleanup_func = cleanup_func;
54 context->jprocess = (jobject) code;
55 context->jenv = get_current_thread_env();
56 jprocess_start(((smx_ctx_java_t) context)->jprocess,
57 get_current_thread_env());
59 smx_current_context = (smx_context_t)context;
61 context->super.data = data;
63 return (smx_context_t) context;
66 static void smx_ctx_java_free(smx_context_t context)
69 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
71 if (ctx_java->jprocess) {
72 jobject jprocess = ctx_java->jprocess;
74 ctx_java->jprocess = NULL;
76 /* if the java process is alive join it */
77 if (jprocess_is_alive(jprocess, get_current_thread_env()))
78 jprocess_join(jprocess, get_current_thread_env());
82 smx_ctx_base_free(context);
85 static void smx_ctx_java_stop(smx_context_t context)
87 jobject jprocess = NULL;
89 smx_ctx_java_t ctx_java;
91 if (context->cleanup_func)
92 (*(context->cleanup_func)) (context->data);
94 ctx_java = (smx_ctx_java_t) context;
96 /*FIXME: is this really necessary? */
97 if (((smx_process_t)smx_current_context->data)->iwannadie) {
98 /* The maestro call xbt_context_stop() with an exit code set to one */
99 if (ctx_java->jprocess) {
100 /* if the java process is alive schedule it */
101 if (jprocess_is_alive(ctx_java->jprocess, get_current_thread_env())) {
102 jprocess_schedule(smx_current_context);
103 jprocess = ctx_java->jprocess;
104 ctx_java->jprocess = NULL;
106 /* interrupt the java process */
107 jprocess_exit(jprocess, get_current_thread_env());
112 /* the java process exits */
113 jprocess = ctx_java->jprocess;
114 ctx_java->jprocess = NULL;
117 /* delete the global reference associated with the java process */
118 jprocess_delete_global_ref(jprocess, get_current_thread_env());
121 /*static void smx_ctx_java_swap(smx_context_t context)
124 smx_context_t self = current_context;
126 current_context = context;
128 jprocess_schedule(context);
130 current_context = self;
133 if (current_context->iwannadie)
134 smx_ctx_java_stop(1);
137 static void smx_ctx_java_suspend(smx_context_t context)
139 jprocess_unschedule(context);
142 // FIXME: inline those functions
143 static void smx_ctx_java_resume(smx_context_t new_context)
145 jprocess_schedule(new_context);
148 static void smx_ctx_java_runall(xbt_swag_t processes)
150 smx_process_t process;
151 smx_context_t old_context;
153 while((process = xbt_swag_extract(processes))){
154 old_context = smx_current_context;
155 smx_current_context = process->context;
156 smx_ctx_java_resume(smx_current_context);
157 smx_current_context = old_context;