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 <simgrid/simix.h>
13 #include "smx_context_java.h"
14 #include "jxbt_utilities.h"
15 #include "xbt/dynar.h"
16 JavaVM *get_current_vm(void);
17 JavaVM *get_current_vm(void)
20 JNI_GetCreatedJavaVMs(&jvm,1,NULL);
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(jmsg, bindings, "MSG for Java(TM)");
28 static smx_context_t my_current_context = NULL;
31 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc,
33 void_pfn_smxprocess_t cleanup_func,
36 static void smx_ctx_java_free(smx_context_t context);
37 static void smx_ctx_java_start(smx_context_t context);
38 static void smx_ctx_java_suspend(smx_context_t context);
39 static void smx_ctx_java_resume(smx_context_t new_context);
40 static void smx_ctx_java_runall(void);
41 static void* smx_ctx_java_thread_run(void *data);
42 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory)
44 /* instantiate the context factory */
45 smx_ctx_base_factory_init(factory);
47 (*factory)->create_context = smx_ctx_java_factory_create_context;
48 /* Leave default behavior of (*factory)->finalize */
49 (*factory)->free = smx_ctx_java_free;
50 (*factory)->stop = smx_ctx_java_stop;
51 (*factory)->suspend = smx_ctx_java_suspend;
52 (*factory)->runall = smx_ctx_java_runall;
53 (*factory)->name = "ctx_java_factory";
54 //(*factory)->finalize = smx_ctx_base_factory_finalize;
55 (*factory)->self = smx_ctx_java_self;
56 (*factory)->get_data = smx_ctx_base_get_data;
58 smx_context_t smx_ctx_java_self(void)
60 return my_current_context;
64 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc,
66 void_pfn_smxprocess_t cleanup_func,
69 smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
70 /* If the user provided a function for the process then use it
71 otherwise is the context for maestro */
74 context->jprocess = (jobject) code;
77 context->jprocess = NULL;
79 context->super.cleanup_func = cleanup_func;
80 context->begin = xbt_os_sem_init(0);
81 context->end = xbt_os_sem_init(0);
83 context->super.argc = argc;
84 context->super.argv = argv;
85 context->super.code = code;
87 context->thread = xbt_os_thread_create(NULL,smx_ctx_java_thread_run,context,NULL);
90 context->thread = NULL;
91 my_current_context = (smx_context_t)context;
93 context->super.data = data;
95 return (smx_context_t) context;
98 static void* smx_ctx_java_thread_run(void *data) {
99 smx_ctx_java_t context = (smx_ctx_java_t)data;
100 //Attach the thread to the JVM
102 JavaVM *jvm = get_current_vm();
103 jint error = (*jvm)->AttachCurrentThread(jvm, (void **) &env, NULL);
104 xbt_assert((error == JNI_OK), "The thread could not be attached to the JVM");
105 context->jenv = get_current_thread_env();
106 //Wait for the first scheduling round to happen.
107 xbt_os_sem_acquire(context->begin);
108 //Create the "Process" object if needed.
109 if (context->super.argc > 0) {
110 (*(context->super.code))(context->super.argc, context->super.argv);
112 xbt_assert((context->jprocess != NULL), "Process not created...");
113 //wait for the process to be able to begin
115 jfieldID jprocess_field_Process_startTime = jxbt_get_sfield(env, "org/simgrid/msg/Process", "startTime", "D");
116 jdouble startTime = (*env)->GetDoubleField(env, context->jprocess, jprocess_field_Process_startTime);
117 if (startTime > MSG_get_clock()) {
118 MSG_process_sleep(startTime - MSG_get_clock());
120 //Execution of the "run" method.
121 jmethodID id = jxbt_get_smethod(env, "org/simgrid/msg/Process", "run", "()V");
122 xbt_assert( (id != NULL), "Method not found...");
123 (*env)->CallVoidMethod(env, context->jprocess, id);
124 smx_ctx_java_stop((smx_context_t)context);
129 static void smx_ctx_java_free(smx_context_t context)
132 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
133 if (ctx_java->thread) { /* We are not in maestro context */
134 xbt_os_thread_join(ctx_java->thread, NULL);
135 xbt_os_sem_destroy(ctx_java->begin);
136 xbt_os_sem_destroy(ctx_java->end);
139 smx_ctx_base_free(context);
143 void smx_ctx_java_stop(smx_context_t context)
145 smx_ctx_java_t ctx_java = (smx_ctx_java_t)context;
146 xbt_assert(context == my_current_context,
147 "The context to stop must be the current one");
148 /* I am the current process and I am dying */
149 if (context->iwannadie == -1) {
150 context->iwannadie = 0;
151 JNIEnv *env = get_current_thread_env();
152 jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", bprintf("Process killed :)"));
153 THROWF(cancel_error, 0, "process cancelled");
156 smx_ctx_base_stop(context);
157 /* detach the thread and kills it */
158 JNIEnv *env = ctx_java->jenv;
159 (*env)->DeleteGlobalRef(env,ctx_java->jprocess);
160 JavaVM *jvm = get_current_vm();
161 jint error = (*jvm)->DetachCurrentThread(jvm);
162 xbt_assert((error == JNI_OK), "The thread couldn't be detached.");
163 xbt_os_sem_release(((smx_ctx_java_t)context)->end);
164 xbt_os_thread_exit(NULL);
169 static void smx_ctx_java_suspend(smx_context_t context)
171 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
172 xbt_os_sem_release(ctx_java->end);
173 xbt_os_sem_acquire(ctx_java->begin);
176 // FIXME: inline those functions
177 static void smx_ctx_java_resume(smx_context_t new_context)
179 XBT_DEBUG("XXXX Context Resume\n");
180 smx_ctx_java_t ctx_java = (smx_ctx_java_t) new_context;
181 xbt_os_sem_release(ctx_java->begin);
182 xbt_os_sem_acquire(ctx_java->end);
185 static void smx_ctx_java_runall(void)
187 xbt_dynar_t processes = SIMIX_process_get_runnable();
188 XBT_DEBUG("XXXX Run all\n");
189 smx_process_t process;
190 smx_context_t old_context;
192 xbt_dynar_foreach(processes, cursor, process) {
193 old_context = my_current_context;
194 my_current_context = SIMIX_process_get_context(process);
195 smx_ctx_java_resume(my_current_context);
196 my_current_context = old_context;
199 XBT_DEBUG("XXXX End of run all\n");