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>
12 #include "smx_context_java.h"
13 #include "jxbt_utilities.h"
14 #include "xbt/dynar.h"
15 JavaVM *get_current_vm(void);
16 JavaVM *get_current_vm(void)
19 JNI_GetCreatedJavaVMs(&jvm,1,NULL);
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(jmsg, bindings, "MSG for Java(TM)");
27 static smx_context_t my_current_context = NULL;
30 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc,
32 void_pfn_smxprocess_t cleanup_func,
35 static void smx_ctx_java_free(smx_context_t context);
36 static void smx_ctx_java_start(smx_context_t context);
37 static void smx_ctx_java_suspend(smx_context_t context);
38 static void smx_ctx_java_resume(smx_context_t new_context);
39 static void smx_ctx_java_runall(void);
40 static void* smx_ctx_java_thread_run(void *data);
41 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory)
43 /* instantiate the context factory */
44 smx_ctx_base_factory_init(factory);
46 (*factory)->create_context = smx_ctx_java_factory_create_context;
47 /* Leave default behavior of (*factory)->finalize */
48 (*factory)->free = smx_ctx_java_free;
49 (*factory)->stop = smx_ctx_java_stop;
50 (*factory)->suspend = smx_ctx_java_suspend;
51 (*factory)->runall = smx_ctx_java_runall;
52 (*factory)->name = "ctx_java_factory";
53 //(*factory)->finalize = smx_ctx_base_factory_finalize;
54 (*factory)->self = smx_ctx_java_self;
55 (*factory)->get_data = smx_ctx_base_get_data;
57 smx_context_t smx_ctx_java_self(void)
59 return my_current_context;
63 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc,
65 void_pfn_smxprocess_t cleanup_func,
68 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 */
73 context->super.cleanup_func = cleanup_func;
74 context->jprocess = (jobject) code;
75 context->begin = xbt_os_sem_init(0);
76 context->end = xbt_os_sem_init(0);
77 context->thread = xbt_os_thread_create(NULL,smx_ctx_java_thread_run,context,NULL);
80 context->thread = NULL;
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_thread_run(void *data) {
89 smx_ctx_java_t context = (smx_ctx_java_t)data;
90 //Attach the thread to the JVM
92 JavaVM *jvm = get_current_vm();
93 jint error = (*jvm)->AttachCurrentThread(jvm, (void **) &env, NULL);
94 xbt_assert((error == JNI_OK), "The thread could not be attached to the JVM");
95 context->jenv = get_current_thread_env();
96 //Wait for the first scheduling round to happen.
97 xbt_os_sem_acquire(context->begin);
98 //Execution of the "run" method.
99 jmethodID id = jxbt_get_smethod(env, "org/simgrid/msg/Process", "run", "()V");
100 xbt_assert( (id != NULL), "Method not found...");
101 (*env)->CallVoidMethod(env, context->jprocess, id);
103 smx_ctx_java_stop((smx_context_t)context);
108 static void smx_ctx_java_free(smx_context_t context)
111 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
112 if (ctx_java->thread) { /* We are not in maestro context */
113 xbt_os_thread_join(ctx_java->thread, NULL);
114 xbt_os_sem_destroy(ctx_java->begin);
115 xbt_os_sem_destroy(ctx_java->end);
118 smx_ctx_base_free(context);
122 void smx_ctx_java_stop(smx_context_t context)
124 smx_ctx_java_t ctx_java = (smx_ctx_java_t)context;
125 xbt_assert(context == my_current_context,
126 "The context to stop must be the current one");
127 /* I am the current process and I am dying */
128 smx_ctx_base_stop(context);
129 /* detach the thread and kills it */
130 JNIEnv *env = ctx_java->jenv;
131 (*env)->DeleteGlobalRef(env,ctx_java->jprocess);
132 JavaVM *jvm = get_current_vm();
133 jint error = (*jvm)->DetachCurrentThread(jvm);
134 xbt_assert((error == JNI_OK), "The thread couldn't be detached.");
135 xbt_os_sem_release(((smx_ctx_java_t)context)->end);
136 xbt_os_thread_exit(NULL);
139 static void smx_ctx_java_suspend(smx_context_t context)
141 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
142 xbt_os_sem_release(ctx_java->end);
143 xbt_os_sem_acquire(ctx_java->begin);
146 // FIXME: inline those functions
147 static void smx_ctx_java_resume(smx_context_t new_context)
149 XBT_DEBUG("XXXX Context Resume\n");
150 smx_ctx_java_t ctx_java = (smx_ctx_java_t) new_context;
151 xbt_os_sem_release(ctx_java->begin);
152 xbt_os_sem_acquire(ctx_java->end);
155 static void smx_ctx_java_runall(void)
157 xbt_dynar_t processes = SIMIX_process_get_runnable();
158 XBT_DEBUG("XXXX Run all\n");
159 smx_process_t process;
160 smx_context_t old_context;
162 xbt_dynar_foreach(processes, cursor, process) {
163 old_context = my_current_context;
164 my_current_context = SIMIX_process_get_context(process);
165 smx_ctx_java_resume(my_current_context);
166 my_current_context = old_context;
169 XBT_DEBUG("XXXX End of run all\n");