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 <simix/simix.h>
12 #include "smx_context_java.h"
13 #include "simix/process_private.h"
14 #include "xbt/dynar.h"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(jmsg, bindings, "MSG for Java(TM)");
18 static smx_context_t my_current_context = NULL;
20 static smx_context_t smx_ctx_java_self(void);
22 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc,
24 void_pfn_smxprocess_t cleanup_func,
27 static void smx_ctx_java_free(smx_context_t context);
28 static void smx_ctx_java_start(smx_context_t context);
29 static void smx_ctx_java_stop(smx_context_t context);
30 static void smx_ctx_java_suspend(smx_context_t context);
31 static void smx_ctx_java_resume(smx_context_t new_context);
32 static void smx_ctx_java_runall(xbt_dynar_t processes);
33 static void* smx_ctx_java_get_data(smx_context_t context);
35 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory)
37 /* instantiate the context factory */
38 smx_ctx_base_factory_init(factory);
40 (*factory)->create_context = smx_ctx_java_factory_create_context;
41 /* Leave default behavior of (*factory)->finalize */
42 (*factory)->free = smx_ctx_java_free;
43 (*factory)->stop = smx_ctx_java_stop;
44 (*factory)->suspend = smx_ctx_java_suspend;
45 (*factory)->runall = smx_ctx_java_runall;
46 (*factory)->name = "ctx_java_factory";
47 //(*factory)->finalize = smx_ctx_base_factory_finalize;
48 (*factory)->self = smx_ctx_java_self;
49 (*factory)->get_data = smx_ctx_base_get_data;
50 (*factory)->get_thread_id = smx_ctx_base_get_thread_id;
53 static smx_context_t smx_ctx_java_self(void)
55 return my_current_context;
58 static void* smx_ctx_java_get_data(smx_context_t context)
64 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc,
66 void_pfn_smxprocess_t cleanup_func,
69 fprintf(stderr,"XXXX Create Context\n");
70 smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
72 /* If the user provided a function for the process then use it
73 otherwise is the context for maestro */
75 context->super.cleanup_func = cleanup_func;
76 context->jprocess = (jobject) code;
77 context->jenv = get_current_thread_env();
78 jprocess_start(((smx_ctx_java_t) context)->jprocess,
79 get_current_thread_env());
81 my_current_context = (smx_context_t)context;
83 context->super.data = data;
85 return (smx_context_t) context;
88 static void smx_ctx_java_free(smx_context_t context)
91 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
93 if (ctx_java->jprocess) {
94 jobject jprocess = ctx_java->jprocess;
96 ctx_java->jprocess = NULL;
98 /* if the java process is alive join it */
99 if (jprocess_is_alive(jprocess, get_current_thread_env()))
100 jprocess_join(jprocess, get_current_thread_env());
104 smx_ctx_base_free(context);
107 static void smx_ctx_java_stop(smx_context_t context)
109 jobject jprocess = NULL;
110 fprintf(stderr,"XXXX Context Stop\n");
112 smx_ctx_java_t ctx_java;
114 if (context->cleanup_func)
115 (*(context->cleanup_func)) (context->data);
117 ctx_java = (smx_ctx_java_t) context;
119 /*FIXME: is this really necessary? Seems to. */
120 if (my_current_context->iwannadie) {
122 /* The maestro call xbt_context_stop() with an exit code set to one */
123 if (ctx_java->jprocess) {
124 /* if the java process is alive schedule it */
125 if (jprocess_is_alive(ctx_java->jprocess, get_current_thread_env())) {
126 jprocess_schedule(my_current_context);
127 jprocess = ctx_java->jprocess;
128 ctx_java->jprocess = NULL;
130 /* interrupt the java process */
131 jprocess_exit(jprocess, get_current_thread_env());
135 /* the java process exits */
136 jprocess = ctx_java->jprocess;
137 ctx_java->jprocess = NULL;
140 /* delete the global reference associated with the java process */
141 jprocess_delete_global_ref(jprocess, get_current_thread_env());
144 static void smx_ctx_java_suspend(smx_context_t context)
146 jprocess_unschedule(context);
149 // FIXME: inline those functions
150 static void smx_ctx_java_resume(smx_context_t new_context)
152 fprintf(stderr,"XXXX Context Resume\n");
153 jprocess_schedule(new_context);
156 static void smx_ctx_java_runall(xbt_dynar_t processes)
158 fprintf(stderr,"XXXX Run all\n");
159 smx_process_t process;
160 smx_context_t old_context;
163 xbt_dynar_foreach(processes, cursor, process) {
164 old_context = my_current_context;
165 my_current_context = process->context;
166 smx_ctx_java_resume(my_current_context);
167 my_current_context = old_context;
169 xbt_dynar_reset(processes);
171 fprintf(stderr,"XXXX End of run all\n");