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);
71 /* If the user provided a function for the process then use it
72 otherwise is the context for maestro */
74 context->super.cleanup_func = cleanup_func;
75 context->jprocess = (jobject) code;
76 context->begin = xbt_os_sem_init(0);
77 context->end = xbt_os_sem_init(0);
78 context->thread = xbt_os_thread_create(NULL,smx_ctx_java_thread_run,context,NULL);
81 context->thread = NULL;
82 my_current_context = (smx_context_t)context;
84 context->super.data = data;
86 return (smx_context_t) context;
89 static void* smx_ctx_java_thread_run(void *data) {
90 smx_ctx_java_t context = (smx_ctx_java_t)data;
91 //Attach the thread to the JVM
93 JavaVM *jvm = get_current_vm();
94 jint error = (*jvm)->AttachCurrentThread(jvm, (void **) &env, NULL);
95 xbt_assert((error == JNI_OK), "The thread could not be attached to the JVM");
96 context->jenv = get_current_thread_env();
97 //Wait for the first scheduling round to happen.
98 xbt_os_sem_acquire(context->begin);
99 //wait for the process to be able to begin
101 jfieldID jprocess_field_Process_startTime = jxbt_get_sfield(env, "org/simgrid/msg/Process", "startTime", "D");
102 jdouble startTime = (*env)->GetDoubleField(env, context->jprocess, jprocess_field_Process_startTime);
103 if (startTime > MSG_get_clock()) {
104 MSG_process_sleep(startTime - MSG_get_clock());
106 //Execution of the "run" method.
107 jmethodID id = jxbt_get_smethod(env, "org/simgrid/msg/Process", "run", "()V");
108 xbt_assert( (id != NULL), "Method not found...");
109 (*env)->CallVoidMethod(env, context->jprocess, id);
110 smx_ctx_java_stop((smx_context_t)context);
115 static void smx_ctx_java_free(smx_context_t context)
118 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
119 if (ctx_java->thread) { /* We are not in maestro context */
120 xbt_os_thread_join(ctx_java->thread, NULL);
121 xbt_os_sem_destroy(ctx_java->begin);
122 xbt_os_sem_destroy(ctx_java->end);
125 smx_ctx_base_free(context);
129 void smx_ctx_java_stop(smx_context_t context)
131 smx_ctx_java_t ctx_java = (smx_ctx_java_t)context;
132 xbt_assert(context == my_current_context,
133 "The context to stop must be the current one");
134 /* I am the current process and I am dying */
135 if (context->iwannadie == -1) {
136 context->iwannadie = 0;
137 JNIEnv *env = get_current_thread_env();
138 jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", bprintf("Process killed :)"));
139 THROWF(cancel_error, 0, "process cancelled");
142 smx_ctx_base_stop(context);
143 /* detach the thread and kills it */
144 JNIEnv *env = ctx_java->jenv;
145 (*env)->DeleteGlobalRef(env,ctx_java->jprocess);
146 JavaVM *jvm = get_current_vm();
147 jint error = (*jvm)->DetachCurrentThread(jvm);
148 xbt_assert((error == JNI_OK), "The thread couldn't be detached.");
149 xbt_os_sem_release(((smx_ctx_java_t)context)->end);
150 xbt_os_thread_exit(NULL);
155 static void smx_ctx_java_suspend(smx_context_t context)
157 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
158 xbt_os_sem_release(ctx_java->end);
159 xbt_os_sem_acquire(ctx_java->begin);
162 // FIXME: inline those functions
163 static void smx_ctx_java_resume(smx_context_t new_context)
165 XBT_DEBUG("XXXX Context Resume\n");
166 smx_ctx_java_t ctx_java = (smx_ctx_java_t) new_context;
167 xbt_os_sem_release(ctx_java->begin);
168 xbt_os_sem_acquire(ctx_java->end);
171 static void smx_ctx_java_runall(void)
173 xbt_dynar_t processes = SIMIX_process_get_runnable();
174 XBT_DEBUG("XXXX Run all\n");
175 smx_process_t process;
176 smx_context_t old_context;
178 xbt_dynar_foreach(processes, cursor, process) {
179 old_context = my_current_context;
180 my_current_context = SIMIX_process_get_context(process);
181 smx_ctx_java_resume(my_current_context);
182 my_current_context = old_context;
185 XBT_DEBUG("XXXX End of run all\n");