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);
79 context->thread = xbt_os_thread_create(NULL,smx_ctx_java_thread_run,context,NULL);
82 context->thread = NULL;
83 my_current_context = (smx_context_t)context;
85 context->super.data = data;
87 return (smx_context_t) context;
90 static void* smx_ctx_java_thread_run(void *data) {
91 smx_ctx_java_t context = (smx_ctx_java_t)data;
92 //Attach the thread to the JVM
94 JavaVM *jvm = get_current_vm();
95 jint error = (*jvm)->AttachCurrentThread(jvm, (void **) &env, NULL);
96 xbt_assert((error == JNI_OK), "The thread could not be attached to the JVM");
97 context->jenv = get_current_thread_env();
98 //Wait for the first scheduling round to happen.
99 xbt_os_sem_acquire(context->begin);
100 //Execution of the "run" method.
101 jmethodID id = jxbt_get_smethod(env, "org/simgrid/msg/Process", "run", "()V");
102 xbt_assert( (id != NULL), "Method not found...");
103 (*env)->CallVoidMethod(env, context->jprocess, id);
104 smx_ctx_java_stop((smx_context_t)context);
109 static void smx_ctx_java_free(smx_context_t context)
112 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
113 if (ctx_java->thread) { /* We are not in maestro context */
114 xbt_os_thread_join(ctx_java->thread, NULL);
115 xbt_os_sem_destroy(ctx_java->begin);
116 xbt_os_sem_destroy(ctx_java->end);
119 smx_ctx_base_free(context);
123 void smx_ctx_java_stop(smx_context_t context)
125 smx_ctx_java_t ctx_java = (smx_ctx_java_t)context;
126 xbt_assert(context == my_current_context,
127 "The context to stop must be the current one");
128 /* I am the current process and I am dying */
129 if (ctx_java->killed == 1) {
130 ctx_java->killed = 0;
131 JNIEnv *env = get_current_thread_env();
132 jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledException", bprintf("Process killed :)"));
133 THROWF(cancel_error, 0, "process cancelled");
136 smx_ctx_base_stop(context);
137 /* detach the thread and kills it */
138 JNIEnv *env = ctx_java->jenv;
139 (*env)->DeleteGlobalRef(env,ctx_java->jprocess);
140 JavaVM *jvm = get_current_vm();
141 jint error = (*jvm)->DetachCurrentThread(jvm);
142 xbt_assert((error == JNI_OK), "The thread couldn't be detached.");
143 xbt_os_sem_release(((smx_ctx_java_t)context)->end);
144 xbt_os_thread_exit(NULL);
149 static void smx_ctx_java_suspend(smx_context_t context)
151 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
152 xbt_os_sem_release(ctx_java->end);
153 xbt_os_sem_acquire(ctx_java->begin);
156 // FIXME: inline those functions
157 static void smx_ctx_java_resume(smx_context_t new_context)
159 XBT_DEBUG("XXXX Context Resume\n");
160 smx_ctx_java_t ctx_java = (smx_ctx_java_t) new_context;
161 xbt_os_sem_release(ctx_java->begin);
162 xbt_os_sem_acquire(ctx_java->end);
165 static void smx_ctx_java_runall(void)
167 xbt_dynar_t processes = SIMIX_process_get_runnable();
168 XBT_DEBUG("XXXX Run all\n");
169 smx_process_t process;
170 smx_context_t old_context;
172 xbt_dynar_foreach(processes, cursor, process) {
173 old_context = my_current_context;
174 my_current_context = SIMIX_process_get_context(process);
175 smx_ctx_java_resume(my_current_context);
176 my_current_context = old_context;
179 XBT_DEBUG("XXXX End of run all\n");