1 /* context_java - implementation of context switching for java threads */
3 /* Copyright (c) 2009, 2010, 2012. 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 extern JavaVM *__java_vm;
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(jmsg, bindings, "MSG for Java(TM)");
21 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc,
23 void_pfn_smxprocess_t cleanup_func,
26 static void smx_ctx_java_free(smx_context_t context);
27 static void smx_ctx_java_start(smx_context_t context);
28 static void smx_ctx_java_suspend(smx_context_t context);
29 static void smx_ctx_java_resume(smx_context_t new_context);
30 static void smx_ctx_java_runall(void);
31 static void* smx_ctx_java_thread_run(void *data);
32 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory)
34 /* instantiate the context factory */
35 smx_ctx_base_factory_init(factory);
37 (*factory)->create_context = smx_ctx_java_factory_create_context;
38 /* Leave default behavior of (*factory)->finalize */
39 (*factory)->free = smx_ctx_java_free;
40 (*factory)->stop = smx_ctx_java_stop;
41 (*factory)->suspend = smx_ctx_java_suspend;
42 (*factory)->runall = smx_ctx_java_runall;
43 (*factory)->name = "ctx_java_factory";
44 //(*factory)->finalize = smx_ctx_base_factory_finalize;
45 (*factory)->self = smx_ctx_java_self;
46 (*factory)->get_data = smx_ctx_base_get_data;
48 smx_context_t smx_ctx_java_self(void)
50 return (smx_context_t)xbt_os_thread_get_extra_data();
54 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc,
56 void_pfn_smxprocess_t cleanup_func,
59 smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
60 /* If the user provided a function for the process then use it
61 otherwise is the context for maestro */
64 context->jprocess = (jobject) code;
67 context->jprocess = NULL;
69 context->super.cleanup_func = cleanup_func;
70 context->begin = xbt_os_sem_init(0);
71 context->end = xbt_os_sem_init(0);
73 context->super.argc = argc;
74 context->super.argv = argv;
75 context->super.code = code;
77 context->thread = xbt_os_thread_create(NULL,smx_ctx_java_thread_run,context,NULL);
80 context->thread = NULL;
81 xbt_os_thread_set_extra_data(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 xbt_os_thread_set_extra_data(context);
91 //Attach the thread to the JVM
93 jint error = (*__java_vm)->AttachCurrentThread(__java_vm, (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 //Create the "Process" object if needed.
99 if (context->super.argc > 0) {
100 (*(context->super.code))(context->super.argc, context->super.argv);
103 smx_process_t process = SIMIX_process_self();
104 (*env)->SetLongField(env, context->jprocess, jprocess_field_Process_bind, (jlong)process);
106 xbt_assert((context->jprocess != NULL), "Process not created...");
107 //wait for the process to be able to begin
109 jfieldID jprocess_field_Process_startTime = jxbt_get_sfield(env, "org/simgrid/msg/Process", "startTime", "D");
110 jdouble startTime = (*env)->GetDoubleField(env, context->jprocess, jprocess_field_Process_startTime);
111 if (startTime > MSG_get_clock()) {
112 MSG_process_sleep(startTime - MSG_get_clock());
114 //Execution of the "run" method.
115 jmethodID id = jxbt_get_smethod(env, "org/simgrid/msg/Process", "run", "()V");
116 xbt_assert( (id != NULL), "Method not found...");
117 (*env)->CallVoidMethod(env, context->jprocess, id);
118 smx_ctx_java_stop((smx_context_t)context);
123 static void smx_ctx_java_free(smx_context_t context)
126 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
127 if (ctx_java->thread) { /* We are not in maestro context */
128 xbt_os_thread_join(ctx_java->thread, NULL);
129 xbt_os_sem_destroy(ctx_java->begin);
130 xbt_os_sem_destroy(ctx_java->end);
133 smx_ctx_base_free(context);
137 void smx_ctx_java_stop(smx_context_t context)
139 smx_ctx_java_t ctx_java = (smx_ctx_java_t)context;
140 /* I am the current process and I am dying */
141 if (context->iwannadie) {
142 context->iwannadie = 0;
143 JNIEnv *env = get_current_thread_env();
144 jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", bprintf("Process killed :)"));
145 THROWF(cancel_error, 0, "process cancelled");
148 smx_ctx_base_stop(context);
149 /* detach the thread and kills it */
150 JNIEnv *env = ctx_java->jenv;
151 (*env)->DeleteGlobalRef(env,ctx_java->jprocess);
152 jint error = (*__java_vm)->DetachCurrentThread(__java_vm);
153 xbt_assert((error == JNI_OK), "The thread couldn't be detached.");
154 xbt_os_sem_release(((smx_ctx_java_t)context)->end);
155 xbt_os_thread_exit(NULL);
160 static void smx_ctx_java_suspend(smx_context_t context)
162 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
163 xbt_os_sem_release(ctx_java->end);
164 xbt_os_sem_acquire(ctx_java->begin);
167 // FIXME: inline those functions
168 static void smx_ctx_java_resume(smx_context_t new_context)
170 smx_ctx_java_t ctx_java = (smx_ctx_java_t) new_context;
171 xbt_os_sem_release(ctx_java->begin);
172 xbt_os_sem_acquire(ctx_java->end);
175 static void smx_ctx_java_runall(void)
177 xbt_dynar_t processes = SIMIX_process_get_runnable();
178 smx_process_t process;
180 xbt_dynar_foreach(processes, cursor, process) {
181 smx_ctx_java_resume(SIMIX_process_get_context(process));