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 XBT_DEBUG("XXXX Create Context\n");
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->jenv = get_current_thread_env();
77 context->begin = xbt_os_sem_init(0);
78 context->end = xbt_os_sem_init(0);
79 context->thread = xbt_os_thread_create(NULL,smx_ctx_java_thread_run,context,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 (*jvm)->AttachCurrentThread(jvm, (void **) &env, NULL);
95 //Wait for the first scheduling round to happen.
96 xbt_os_sem_acquire(context->begin);
97 //Execution of the "run" method.
98 jmethodID id = jxbt_get_smethod(env, "org/simgrid/msg/Process", "run", "()V");
99 xbt_assert( (id != NULL), "Method not found...");
100 (*env)->CallVoidMethod(env, context->jprocess, id);
105 static void smx_ctx_java_free(smx_context_t context)
108 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
109 if (ctx_java->jprocess) { /* the java process still exists */
110 jobject jprocess = ctx_java->jprocess;
111 ctx_java->jprocess = NULL; /* stop it */
112 XBT_DEBUG("The process still exists, making it exit now");
113 /* detach the thread and exit it */
114 JavaVM *jvm = get_current_vm();
115 (*jvm)->DetachCurrentThread(jvm);
116 xbt_os_thread_exit(NULL);
117 /* it's dead now, remove it from the JVM */
118 jprocess_delete_global_ref(jprocess, get_current_thread_env());
121 smx_ctx_base_free(context);
125 void smx_ctx_java_stop(smx_context_t context)
127 xbt_assert(context == my_current_context,
128 "The context to stop must be the current one");
129 /* I am the current process and I am dying */
131 smx_ctx_base_stop(context);
133 XBT_DEBUG("I am dying");
135 /* suspend myself again, smx_ctx_java_free() will destroy me later
137 smx_ctx_java_suspend(context);
138 XBT_DEBUG("Java stop finished");
141 static void smx_ctx_java_suspend(smx_context_t context)
143 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
144 xbt_os_sem_release(ctx_java->end);
145 xbt_os_sem_acquire(ctx_java->begin);
146 //jprocess_unschedule(context);
149 // FIXME: inline those functions
150 static void smx_ctx_java_resume(smx_context_t new_context)
152 XBT_DEBUG("XXXX Context Resume\n");
153 smx_ctx_java_t ctx_java = (smx_ctx_java_t) new_context;
154 xbt_os_sem_release(ctx_java->begin);
155 xbt_os_sem_acquire(ctx_java->end);
156 //jprocess_schedule(new_context);
159 static void smx_ctx_java_runall(void)
161 xbt_dynar_t processes = SIMIX_process_get_runnable();
162 XBT_DEBUG("XXXX Run all\n");
163 smx_process_t process;
164 smx_context_t old_context;
166 xbt_dynar_foreach(processes, cursor, process) {
167 old_context = my_current_context;
168 my_current_context = SIMIX_process_get_context(process);
169 smx_ctx_java_resume(my_current_context);
170 my_current_context = old_context;
173 XBT_DEBUG("XXXX End of run all\n");