}
void jmsg_throw_status(JNIEnv *env, MSG_error_t status) {
- switch (status) {
- case MSG_TIMEOUT:
- jxbt_throw_time_out_failure(env,NULL);
- break;
- case MSG_TRANSFER_FAILURE:
- jxbt_throw_transfer_failure(env,NULL);
- break;
- case MSG_HOST_FAILURE:
- jxbt_throw_host_failure(env,NULL);
- break;
- default:
- jxbt_throw_native(env,bprintf("communication failed"));
- }
+ switch (status) {
+ case MSG_TIMEOUT:
+ jxbt_throw_time_out_failure(env,NULL);
+ break;
+ case MSG_TRANSFER_FAILURE:
+ jxbt_throw_transfer_failure(env,NULL);
+ break;
+ case MSG_HOST_FAILURE:
+ jxbt_throw_host_failure(env,NULL);
+ break;
+ default:
+ jxbt_throw_native(env,bprintf("communication failed"));
+ }
}
* it HAS to be executed on the process context, else really bad things will happen.
*/
static int create_jprocess(int argc, char *argv[]) {
- JNIEnv *env = get_current_thread_env();
- //Change the "." in class name for "/".
- xbt_str_subst(argv[0],'.','/',0);
- jclass class_Process = (*env)->FindClass(env, argv[0]);
- xbt_str_subst(argv[0],'/','.',0);
- //Retrieve the methodID for the constructor
- xbt_assert((class_Process != NULL), "Class not found.");
- jmethodID constructor_Process = (*env)->GetMethodID(env, class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
- xbt_assert((constructor_Process != NULL), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
-
- //Retrieve the name of the process.
- jstring jname = (*env)->NewStringUTF(env, argv[0]);
- //Build the arguments
- jobjectArray args = (jobjectArray)((*env)->NewObjectArray(env,argc - 1,
- (*env)->FindClass(env,"java/lang/String"),
- (*env)->NewStringUTF(env,"")));
- int i;
- for (i = 1; i < argc; i++)
- (*env)->SetObjectArrayElement(env,args,i - 1,(*env)->NewStringUTF(env, argv[i]));
- //Retrieve the host for the process.
- jstring jhostName = (*env)->NewStringUTF(env, MSG_host_get_name(MSG_host_self()));
- jobject jhost = Java_org_simgrid_msg_Host_getByName(env, NULL, jhostName);
- //creates the process
- jobject jprocess = (*env)->NewObject(env, class_Process, constructor_Process, jhost, jname, args);
- xbt_assert((jprocess != NULL), "Process allocation failed.");
- jprocess = (*env)->NewGlobalRef(env, jprocess);
- //bind the process to the context
- m_process_t process = MSG_process_self();
- smx_ctx_java_t context = (smx_ctx_java_t)MSG_process_get_smx_ctx(process);
- context->jprocess = jprocess;
- /* sets the PID and the PPID of the process */
- (*env)->SetIntField(env, jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
- (*env)->SetIntField(env, jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
- jprocess_bind(jprocess, process, env);
-
- return 0;
+ JNIEnv *env = get_current_thread_env();
+ //Change the "." in class name for "/".
+ xbt_str_subst(argv[0],'.','/',0);
+ jclass class_Process = (*env)->FindClass(env, argv[0]);
+ xbt_str_subst(argv[0],'/','.',0);
+ //Retrieve the methodID for the constructor
+ xbt_assert((class_Process != NULL), "Class not found.");
+ jmethodID constructor_Process = (*env)->GetMethodID(env, class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
+ xbt_assert((constructor_Process != NULL), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
+
+ //Retrieve the name of the process.
+ jstring jname = (*env)->NewStringUTF(env, argv[0]);
+ //Build the arguments
+ jobjectArray args = (jobjectArray)((*env)->NewObjectArray(env,argc - 1,
+ (*env)->FindClass(env,"java/lang/String"),
+ (*env)->NewStringUTF(env,"")));
+ int i;
+ for (i = 1; i < argc; i++)
+ (*env)->SetObjectArrayElement(env,args,i - 1,(*env)->NewStringUTF(env, argv[i]));
+ //Retrieve the host for the process.
+ jstring jhostName = (*env)->NewStringUTF(env, MSG_host_get_name(MSG_host_self()));
+ jobject jhost = Java_org_simgrid_msg_Host_getByName(env, NULL, jhostName);
+ //creates the process
+ jobject jprocess = (*env)->NewObject(env, class_Process, constructor_Process, jhost, jname, args);
+ xbt_assert((jprocess != NULL), "Process allocation failed.");
+ jprocess = (*env)->NewGlobalRef(env, jprocess);
+ //bind the process to the context
+ m_process_t process = MSG_process_self();
+ smx_ctx_java_t context = (smx_ctx_java_t)MSG_process_get_smx_ctx(process);
+ context->jprocess = jprocess;
+/* sets the PID and the PPID of the process */
+(*env)->SetIntField(env, jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
+(*env)->SetIntField(env, jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
+ jprocess_bind(jprocess, process, env);
+
+ return 0;
}
static jfieldID jcomm_field_Comm_taskBind;
void jcomm_bind_task(JNIEnv *env, jobject jcomm) {
- msg_comm_t comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
- //test if we are receiving or sending a task.
- jboolean jreceiving = (*env)->GetBooleanField(env, jcomm, jcomm_field_Comm_receiving);
- if (jreceiving == JNI_TRUE) {
- //bind the task object.
- m_task_t task = MSG_comm_get_task(comm);
- xbt_assert(task != NULL, "Task is NULL");
- jobject jtask_global = MSG_task_get_data(task);
- //case where the data has already been retrieved
- if (jtask_global == NULL) {
- return;
- }
-
- //Make sure the data will be correctly gc.
- jobject jtask_local = (*env)->NewLocalRef(env, jtask_global);
- (*env)->DeleteGlobalRef(env, jtask_global);
-
- (*env)->SetObjectField(env, jcomm, jtask_field_Comm_task, jtask_local);
-
- MSG_task_set_data(task, NULL);
- }
+ msg_comm_t comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
+ //test if we are receiving or sending a task.
+ jboolean jreceiving = (*env)->GetBooleanField(env, jcomm, jcomm_field_Comm_receiving);
+ if (jreceiving == JNI_TRUE) {
+ //bind the task object.
+ m_task_t task = MSG_comm_get_task(comm);
+ xbt_assert(task != NULL, "Task is NULL");
+ jobject jtask_global = MSG_task_get_data(task);
+ //case where the data has already been retrieved
+ if (jtask_global == NULL) {
+ return;
+ }
+
+ //Make sure the data will be correctly gc.
+ jobject jtask_local = (*env)->NewLocalRef(env, jtask_global);
+ (*env)->DeleteGlobalRef(env, jtask_global);
+
+ (*env)->SetObjectField(env, jcomm, jtask_field_Comm_task, jtask_local);
+
+ MSG_task_set_data(task, NULL);
+ }
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_Comm_nativeInit(JNIEnv *env, jclass cls) {
- jclass jfield_class_Comm = (*env)->FindClass(env, "org/simgrid/msg/Comm");
- if (!jfield_class_Comm) {
- jxbt_throw_native(env,bprintf("Can't find the org/simgrid/msg/Comm class."));
- return;
- }
- jcomm_field_Comm_bind = jxbt_get_jfield(env, jfield_class_Comm, "bind", "J");
- jcomm_field_Comm_taskBind = jxbt_get_jfield(env, jfield_class_Comm, "taskBind", "J");
- jcomm_field_Comm_receiving = jxbt_get_jfield(env, jfield_class_Comm, "receiving", "Z");
- jtask_field_Comm_task = jxbt_get_jfield(env, jfield_class_Comm, "task", "Lorg/simgrid/msg/Task;");
- jcomm_field_Comm_finished = jxbt_get_jfield(env, jfield_class_Comm, "finished", "Z");
- if (!jcomm_field_Comm_bind || !jcomm_field_Comm_taskBind || !jcomm_field_Comm_receiving || !jtask_field_Comm_task || !jcomm_field_Comm_finished) {
- jxbt_throw_native(env,bprintf("Can't find some fields in Java class."));
- }
+ jclass jfield_class_Comm = (*env)->FindClass(env, "org/simgrid/msg/Comm");
+ if (!jfield_class_Comm) {
+ jxbt_throw_native(env,bprintf("Can't find the org/simgrid/msg/Comm class."));
+ return;
+ }
+ jcomm_field_Comm_bind = jxbt_get_jfield(env, jfield_class_Comm, "bind", "J");
+ jcomm_field_Comm_taskBind = jxbt_get_jfield(env, jfield_class_Comm, "taskBind", "J");
+ jcomm_field_Comm_receiving = jxbt_get_jfield(env, jfield_class_Comm, "receiving", "Z");
+ jtask_field_Comm_task = jxbt_get_jfield(env, jfield_class_Comm, "task", "Lorg/simgrid/msg/Task;");
+ jcomm_field_Comm_finished = jxbt_get_jfield(env, jfield_class_Comm, "finished", "Z");
+ if (!jcomm_field_Comm_bind || !jcomm_field_Comm_taskBind || !jcomm_field_Comm_receiving || !jtask_field_Comm_task || !jcomm_field_Comm_finished) {
+ jxbt_throw_native(env,bprintf("Can't find some fields in Java class."));
+ }
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_Comm_destroy(JNIEnv *env, jobject jcomm) {
- msg_comm_t comm;
- m_task_t *task_received;
+ msg_comm_t comm;
+ m_task_t *task_received;
- task_received = (m_task_t*) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_taskBind);
- xbt_free(task_received);
+ task_received = (m_task_t*) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_taskBind);
+ xbt_free(task_received);
- comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
- MSG_comm_destroy(comm);
+ comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
+ MSG_comm_destroy(comm);
}
JNIEXPORT jboolean JNICALL
Java_org_simgrid_msg_Comm_test(JNIEnv *env, jobject jcomm) {
- msg_comm_t comm;
- comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
-
- jboolean finished = (*env)->GetBooleanField(env, jcomm, jcomm_field_Comm_finished);
- if (finished == JNI_TRUE) {
- return JNI_TRUE;
- }
-
- if (!comm) {
- jxbt_throw_native(env,bprintf("comm is null"));
- return JNI_FALSE;
- }
- TRY {
- if (MSG_comm_test(comm)) {
- MSG_error_t status = MSG_comm_get_status(comm);
- if (status == MSG_OK) {
- jcomm_bind_task(env,jcomm);
- return JNI_TRUE;
- }
- else {
- //send the correct exception
- jmsg_throw_status(env,status);
- return JNI_FALSE;
- }
- }
- else {
- return JNI_FALSE;
- }
- }
- CATCH_ANONYMOUS {
-
- }
- return JNI_FALSE;
+ msg_comm_t comm;
+ comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
+
+ jboolean finished = (*env)->GetBooleanField(env, jcomm, jcomm_field_Comm_finished);
+ if (finished == JNI_TRUE) {
+ return JNI_TRUE;
+ }
+
+ if (!comm) {
+ jxbt_throw_native(env,bprintf("comm is null"));
+ return JNI_FALSE;
+ }
+ TRY {
+ if (MSG_comm_test(comm)) {
+ MSG_error_t status = MSG_comm_get_status(comm);
+ if (status == MSG_OK) {
+ jcomm_bind_task(env,jcomm);
+ return JNI_TRUE;
+ }
+ else {
+ //send the correct exception
+ jmsg_throw_status(env,status);
+ return JNI_FALSE;
+ }
+ }
+ else {
+ return JNI_FALSE;
+ }
+ }
+ CATCH_ANONYMOUS {
+
+ }
+ return JNI_FALSE;
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_Comm_waitCompletion(JNIEnv *env, jobject jcomm, jdouble timeout) {
- msg_comm_t comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
- if (!comm) {
- jxbt_throw_native(env,bprintf("comm is null"));
- return;
- }
-
- jboolean finished = (*env)->GetBooleanField(env, jcomm, jcomm_field_Comm_finished);
- if (finished == JNI_TRUE) {
- return;
- }
-
- MSG_error_t status;
- TRY {
- status = MSG_comm_wait(comm,(double)timeout);
- }
- CATCH_ANONYMOUS {
- return;
- }
- (*env)->SetBooleanField(env, jcomm, jcomm_field_Comm_finished, JNI_TRUE);
- if (status == MSG_OK) {
- jcomm_bind_task(env,jcomm);
- return;
- }
- else {
- jmsg_throw_status(env,status);
- }
-
+ msg_comm_t comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
+ if (!comm) {
+ jxbt_throw_native(env,bprintf("comm is null"));
+ return;
+ }
+
+ jboolean finished = (*env)->GetBooleanField(env, jcomm, jcomm_field_Comm_finished);
+ if (finished == JNI_TRUE) {
+ return;
+ }
+
+ MSG_error_t status;
+ TRY {
+ status = MSG_comm_wait(comm,(double)timeout);
+ }
+ CATCH_ANONYMOUS {
+ return;
+ }
+ (*env)->SetBooleanField(env, jcomm, jcomm_field_Comm_finished, JNI_TRUE);
+ if (status == MSG_OK) {
+ jcomm_bind_task(env,jcomm);
+ return;
+ }
+ else {
+ jmsg_throw_status(env,status);
+ }
}
}
msg_file_t jfile_get_native(JNIEnv *env, jobject jfile) {
- msg_file_t file = (msg_file_t)(*env)->GetLongField(env, jfile, jfile_field_bind);
- return file;
+ msg_file_t file = (msg_file_t)(*env)->GetLongField(env, jfile, jfile_field_bind);
+ return file;
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_File_nativeInit(JNIEnv *env, jclass cls) {
- jclass class_File = (*env)->FindClass(env, "org/simgrid/msg/File");
- jfile_field_bind = jxbt_get_jfield(env , class_File, "bind", "J");
- xbt_assert((jfile_field_bind != NULL), "Can't find \"bind\" field in File class.");
+ jclass class_File = (*env)->FindClass(env, "org/simgrid/msg/File");
+ jfile_field_bind = jxbt_get_jfield(env , class_File, "bind", "J");
+ xbt_assert((jfile_field_bind != NULL), "Can't find \"bind\" field in File class.");
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_File_open(JNIEnv *env, jobject jfile, jobject jstorage, jobject jpath, jobject jmode) {
+ const char *storage = (*env)->GetStringUTFChars(env, jstorage, 0);
+ const char *path = (*env)->GetStringUTFChars(env, jpath, 0);
+ const char *mode = (*env)->GetStringUTFChars(env, jmode, 0);
- const char *storage = (*env)->GetStringUTFChars(env, jstorage, 0);
- const char *path = (*env)->GetStringUTFChars(env, jpath, 0);
- const char *mode = (*env)->GetStringUTFChars(env, jmode, 0);
+ msg_file_t file = MSG_file_open(storage, path, mode);
- msg_file_t file = MSG_file_open(storage, path, mode);
+ jfile_bind(env, jfile, file);
- jfile_bind(env, jfile, file);
-
- (*env)->ReleaseStringUTFChars(env, jstorage, storage);
- (*env)->ReleaseStringUTFChars(env, jpath, path);
- (*env)->ReleaseStringUTFChars(env, jmode, mode);
+ (*env)->ReleaseStringUTFChars(env, jstorage, storage);
+ (*env)->ReleaseStringUTFChars(env, jpath, path);
+ (*env)->ReleaseStringUTFChars(env, jmode, mode);
}
JNIEXPORT jlong JNICALL
Java_org_simgrid_msg_File_read(JNIEnv *env, jobject jfile, jlong jsize, jlong jnmemb) {
- msg_file_t file = jfile_get_native(env, jfile);
+ msg_file_t file = jfile_get_native(env, jfile);
- size_t n = MSG_file_read(NULL,(size_t)jsize, (size_t)jnmemb, file);
+ size_t n = MSG_file_read(NULL,(size_t)jsize, (size_t)jnmemb, file);
- return (jlong)n;
+ return (jlong)n;
}
JNIEXPORT jlong JNICALL
Java_org_simgrid_msg_File_write(JNIEnv *env, jobject jfile, jlong jsize, jlong jnmemb) {
- msg_file_t file = jfile_get_native(env, jfile);
+ msg_file_t file = jfile_get_native(env, jfile);
- size_t n = MSG_file_write(NULL, (size_t)jsize, (size_t)jnmemb, file);
+ size_t n = MSG_file_write(NULL, (size_t)jsize, (size_t)jnmemb, file);
- return (jlong)n;
+ return (jlong)n;
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_File_close(JNIEnv *env, jobject jfile) {
- msg_file_t file = jfile_get_native(env, jfile);
+ msg_file_t file = jfile_get_native(env, jfile);
- MSG_file_close(file);
- jfile_bind(env, jfile, NULL);
+ MSG_file_close(file);
+ jfile_bind(env, jfile, NULL);
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_Host_nativeInit(JNIEnv *env, jclass cls) {
- jclass class_Host = (*env)->FindClass(env, "org/simgrid/msg/Host");
- jhost_method_Host_constructor = (*env)->GetMethodID(env, class_Host, "<init>", "()V");
- jhost_field_Host_bind = jxbt_get_jfield(env,class_Host, "bind", "J");
- jhost_field_Host_name = jxbt_get_jfield(env, class_Host, "name", "Ljava/lang/String;");
- if (!class_Host || !jhost_field_Host_name || !jhost_method_Host_constructor || !jhost_field_Host_bind) {
- jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
- }
+ jclass class_Host = (*env)->FindClass(env, "org/simgrid/msg/Host");
+ jhost_method_Host_constructor = (*env)->GetMethodID(env, class_Host, "<init>", "()V");
+ jhost_field_Host_bind = jxbt_get_jfield(env,class_Host, "bind", "J");
+ jhost_field_Host_name = jxbt_get_jfield(env, class_Host, "name", "Ljava/lang/String;");
+ if (!class_Host || !jhost_field_Host_name || !jhost_method_Host_constructor || !jhost_field_Host_bind) {
+ jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
+ }
}
JNIEXPORT jobject JNICALL
Java_org_simgrid_msg_Host_getByName(JNIEnv * env, jclass cls,
JNIEXPORT void JNICALL
Java_org_simgrid_msg_Process_exit(JNIEnv *env, jobject jprocess) {
- if (smx_factory_initializer_to_use == SIMIX_ctx_cojava_factory_init) {
- m_process_t process = jprocess_to_native_process(jprocess, env);
- smx_context_t context = MSG_process_get_smx_ctx(process);
- smx_ctx_cojava_stop(context);
- }
+ if (smx_factory_initializer_to_use == SIMIX_ctx_cojava_factory_init) {
+ m_process_t process = jprocess_to_native_process(jprocess, env);
+ smx_context_t context = MSG_process_get_smx_ctx(process);
+ smx_ctx_cojava_stop(context);
+ }
}
jobject native_to_java_process(m_process_t process)
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_Process_nativeInit(JNIEnv *env, jclass cls) {
- jclass jprocess_class_Process = (*env)->FindClass(env, "org/simgrid/msg/Process");
-
- jprocess_field_Process_name = jxbt_get_jfield(env, jprocess_class_Process, "name", "Ljava/lang/String;");
- jprocess_field_Process_bind = jxbt_get_jfield(env, jprocess_class_Process, "bind", "J");
- jprocess_field_Process_id = jxbt_get_jfield(env, jprocess_class_Process, "id", "J");
- jprocess_field_Process_pid = jxbt_get_jfield(env, jprocess_class_Process, "pid", "I");
- jprocess_field_Process_ppid = jxbt_get_jfield(env, jprocess_class_Process, "ppid", "I");
- jprocess_field_Process_host = jxbt_get_jfield(env, jprocess_class_Process, "host", "Lorg/simgrid/msg/Host;");
- jprocess_field_Process_killTime = jxbt_get_jfield(env, jprocess_class_Process, "killTime", "D");
- if (!jprocess_class_Process || !jprocess_field_Process_id || !jprocess_field_Process_name || !jprocess_field_Process_pid ||
- !jprocess_field_Process_ppid || !jprocess_field_Process_host) {
- jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
- }
+ jclass jprocess_class_Process = (*env)->FindClass(env, "org/simgrid/msg/Process");
+
+ jprocess_field_Process_name = jxbt_get_jfield(env, jprocess_class_Process, "name", "Ljava/lang/String;");
+ jprocess_field_Process_bind = jxbt_get_jfield(env, jprocess_class_Process, "bind", "J");
+ jprocess_field_Process_id = jxbt_get_jfield(env, jprocess_class_Process, "id", "J");
+ jprocess_field_Process_pid = jxbt_get_jfield(env, jprocess_class_Process, "pid", "I");
+ jprocess_field_Process_ppid = jxbt_get_jfield(env, jprocess_class_Process, "ppid", "I");
+ jprocess_field_Process_host = jxbt_get_jfield(env, jprocess_class_Process, "host", "Lorg/simgrid/msg/Host;");
+ jprocess_field_Process_killTime = jxbt_get_jfield(env, jprocess_class_Process, "killTime", "D");
+ if (!jprocess_class_Process || !jprocess_field_Process_id || !jprocess_field_Process_name || !jprocess_field_Process_pid ||
+ !jprocess_field_Process_ppid || !jprocess_field_Process_host) {
+ jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
+ }
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_Process_create(JNIEnv * env,
hostname = (*env)->GetStringUTFChars(env, jhostname, 0);
- XBT_DEBUG("Java_org_simgrid_msg_MsgNative_processCreate(env=%p,jproc=%p,host=%s)",
- env, jprocess_arg, hostname);
-
-
/* get the name of the java process */
jname = jprocess_get_name(jprocess_arg, env);
if (!jname) {
Java_org_simgrid_msg_Process_waitFor(JNIEnv * env, jobject jprocess,
jdouble jseconds)
{
- MSG_error_t rv;
+ MSG_error_t rv;
TRY {
- rv = MSG_process_sleep((double)jseconds);
+ rv = MSG_process_sleep((double)jseconds);
}
CATCH_ANONYMOUS {
return;
jfieldID jrngstream_bind;
RngStream jrngstream_to_native(JNIEnv *env, jobject jrngstream) {
- RngStream rngstream = (RngStream) (*env)->GetLongField(env, jrngstream, jrngstream_bind);
- if (!rngstream) {
+ RngStream rngstream = (RngStream) (*env)->GetLongField(env, jrngstream, jrngstream_bind);
+ if (!rngstream) {
jxbt_throw_notbound(env, "rngstream", jrngstream);
return NULL;
- }
- return rngstream;
+ }
+ return rngstream;
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_RngStream_nativeInit(JNIEnv *env, jclass cls) {
- jclass class_RngStream = (*env)->FindClass(env, "org/simgrid/msg/RngStream");
+ jclass class_RngStream = (*env)->FindClass(env, "org/simgrid/msg/RngStream");
- jrngstream_bind = jxbt_get_jfield(env, class_RngStream, "bind", "J");
+ jrngstream_bind = jxbt_get_jfield(env, class_RngStream, "bind", "J");
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_RngStream_create(JNIEnv *env, jobject jrngstream, jstring jname) {
- const char *name = (*env)->GetStringUTFChars(env, jname, 0);
- RngStream rngstream = RngStream_CreateStream(name);
- //Bind the RngStream object
- (*env)->SetLongField(env, jrngstream, jrngstream_bind, (jlong)rngstream);
+ const char *name = (*env)->GetStringUTFChars(env, jname, 0);
+ RngStream rngstream = RngStream_CreateStream(name);
+ //Bind the RngStream object
+ (*env)->SetLongField(env, jrngstream, jrngstream_bind, (jlong)rngstream);
(*env)->ReleaseStringUTFChars(env, jname, name);
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_RngStream_destroy(JNIEnv *env, jobject jrngstream) {
- RngStream rngstream = jrngstream_to_native(env, jrngstream);
- RngStream_DeleteStream(&rngstream);
- (*env)->SetLongField(env, jrngstream, jrngstream_bind, (jlong)NULL);
+ RngStream rngstream = jrngstream_to_native(env, jrngstream);
+ RngStream_DeleteStream(&rngstream);
+ (*env)->SetLongField(env, jrngstream, jrngstream_bind, (jlong)NULL);
}
JNIEXPORT jboolean JNICALL
Java_org_simgrid_msg_RngStream_setPackageSeed(JNIEnv *env, jobject jrngstream, jintArray jseed) {
- jint buffer[6];
+ jint buffer[6];
- (*env)->GetIntArrayRegion(env, jseed, 0, 6, buffer);
+ (*env)->GetIntArrayRegion(env, jseed, 0, 6, buffer);
- RngStream rngstream = jrngstream_to_native(env, jrngstream);
- if (!rngstream)
- return JNI_FALSE;
+ RngStream rngstream = jrngstream_to_native(env, jrngstream);
+ if (!rngstream)
+ return JNI_FALSE;
- int result = RngStream_SetPackageSeed((unsigned long*)buffer);
+ int result = RngStream_SetPackageSeed((unsigned long*)buffer);
- return result == -1 ? JNI_FALSE : JNI_TRUE;
+ return result == -1 ? JNI_FALSE : JNI_TRUE;
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_RngStream_resetStart(JNIEnv *env, jobject jrngstream) {
- RngStream rngstream = jrngstream_to_native(env, jrngstream);
- if (!rngstream)
- return;
+ RngStream rngstream = jrngstream_to_native(env, jrngstream);
+ if (!rngstream)
+ return;
- RngStream_ResetStartStream(rngstream);
+ RngStream_ResetStartStream(rngstream);
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_RngStream_resetStartSubstream(JNIEnv *env, jobject jrngstream) {
- RngStream rngstream = jrngstream_to_native(env, jrngstream);
- if (!rngstream)
- return;
+ RngStream rngstream = jrngstream_to_native(env, jrngstream);
+ if (!rngstream)
+ return;
- RngStream_ResetStartSubstream(rngstream);
+ RngStream_ResetStartSubstream(rngstream);
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_RngStream_resetNextSubstream(JNIEnv *env, jobject jrngstream) {
- RngStream rngstream = jrngstream_to_native(env, jrngstream);
- if (!rngstream)
- return;
+ RngStream rngstream = jrngstream_to_native(env, jrngstream);
+ if (!rngstream)
+ return;
- RngStream_ResetNextSubstream(rngstream);
+ RngStream_ResetNextSubstream(rngstream);
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_RngStream_setAntithetic(JNIEnv *env, jobject jrngstream, jboolean ja) {
- RngStream rngstream = jrngstream_to_native(env, jrngstream);
- if (!rngstream)
- return;
-
- if (ja == JNI_TRUE) {
- RngStream_SetAntithetic(rngstream,-1);
- }
- else {
- RngStream_SetAntithetic(rngstream,0);
- }
+ RngStream rngstream = jrngstream_to_native(env, jrngstream);
+ if (!rngstream)
+ return;
+
+ if (ja == JNI_TRUE) {
+ RngStream_SetAntithetic(rngstream,-1);
+ }
+ else {
+ RngStream_SetAntithetic(rngstream,0);
+ }
}
JNIEXPORT jboolean JNICALL
Java_org_simgrid_msg_RngStream_setSeed(JNIEnv *env, jobject jrngstream, jintArray jseed) {
- jint buffer[6];
+ jint buffer[6];
- (*env)->GetIntArrayRegion(env, jseed, 0, 6, buffer);
+ (*env)->GetIntArrayRegion(env, jseed, 0, 6, buffer);
- RngStream rngstream = jrngstream_to_native(env, jrngstream);
- if (!rngstream)
- return JNI_FALSE;
+ RngStream rngstream = jrngstream_to_native(env, jrngstream);
+ if (!rngstream)
+ return JNI_FALSE;
- int result = RngStream_SetSeed(rngstream, (unsigned long*)buffer);
+ int result = RngStream_SetSeed(rngstream, (unsigned long*)buffer);
- return result == -1 ? JNI_FALSE : JNI_TRUE;
+ return result == -1 ? JNI_FALSE : JNI_TRUE;
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_RngStream_advanceState(JNIEnv *env, jobject jrngstream, jint e, jint g) {
- RngStream rngstream = jrngstream_to_native(env, jrngstream);
- if (!rngstream)
- return;
+ RngStream rngstream = jrngstream_to_native(env, jrngstream);
+ if (!rngstream)
+ return;
- RngStream_AdvanceState(rngstream, (long)e, (long)g);
+ RngStream_AdvanceState(rngstream, (long)e, (long)g);
}
JNIEXPORT jdouble JNICALL
Java_org_simgrid_msg_RngStream_randU01(JNIEnv *env, jobject jrngstream) {
- RngStream rngstream = jrngstream_to_native(env, jrngstream);
- if (!rngstream)
- return 0;
+ RngStream rngstream = jrngstream_to_native(env, jrngstream);
+ if (!rngstream)
+ return 0;
- return (jdouble)RngStream_RandU01(rngstream);
+ return (jdouble)RngStream_RandU01(rngstream);
}
JNIEXPORT jint JNICALL
Java_org_simgrid_msg_RngStream_randInt(JNIEnv *env, jobject jrngstream, jint i, jint j) {
- RngStream rngstream = jrngstream_to_native(env, jrngstream);
- if (!rngstream)
- return 0;
+ RngStream rngstream = jrngstream_to_native(env, jrngstream);
+ if (!rngstream)
+ return 0;
- return (jint)RngStream_RandInt(rngstream, (int)i, (int)j);
+ return (jint)RngStream_RandInt(rngstream, (int)i, (int)j);
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_Mutex_nativeInit(JNIEnv *env, jclass cls) {
- jsyncro_field_Mutex_bind = jxbt_get_sfield(env, "org/simgrid/msg/Mutex", "bind", "J");
- if (!jsyncro_field_Mutex_bind) {
- jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
- }
+ jsyncro_field_Mutex_bind = jxbt_get_sfield(env, "org/simgrid/msg/Mutex", "bind", "J");
+ if (!jsyncro_field_Mutex_bind) {
+ jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
+ }
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_Mutex_init(JNIEnv * env, jobject obj) {
- xbt_mutex_t mutex = xbt_mutex_init();
+ xbt_mutex_t mutex = xbt_mutex_init();
- (*env)->SetLongField(env, obj, jsyncro_field_Mutex_bind, (jlong) (long) (mutex));
+ (*env)->SetLongField(env, obj, jsyncro_field_Mutex_bind, (jlong) (long) (mutex));
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_Mutex_acquire(JNIEnv * env, jobject obj) {
- xbt_mutex_t mutex;
+ xbt_mutex_t mutex;
- mutex = (xbt_mutex_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Mutex_bind);
- xbt_mutex_acquire(mutex);
+ mutex = (xbt_mutex_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Mutex_bind);
+ xbt_mutex_acquire(mutex);
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_Mutex_release(JNIEnv * env, jobject obj) {
- xbt_mutex_t mutex;
+ xbt_mutex_t mutex;
- mutex = (xbt_mutex_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Mutex_bind);
- xbt_mutex_release(mutex);
+ mutex = (xbt_mutex_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Mutex_bind);
+ xbt_mutex_release(mutex);
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_Mutex_exit(JNIEnv * env, jobject obj) {
- xbt_mutex_t mutex;
+ xbt_mutex_t mutex;
- mutex = (xbt_mutex_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Mutex_bind);
- xbt_mutex_destroy(mutex);
+ mutex = (xbt_mutex_t) (long) (*env)->GetLongField(env, obj, jsyncro_field_Mutex_bind);
+ xbt_mutex_destroy(mutex);
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_Task_nativeInit(JNIEnv *env, jclass cls) {
- jclass jtask_class_Comm = (*env)->FindClass(env, "org/simgrid/msg/Comm");
- jclass jtask_class_Task = (*env)->FindClass(env, "org/simgrid/msg/Task");
-
- jtask_method_Comm_constructor = (*env)->GetMethodID(env, jtask_class_Comm, "<init>", "()V");
- jtask_field_Task_bind = jxbt_get_jfield(env, jtask_class_Task, "bind", "J");
- jtask_field_Task_name = jxbt_get_jfield(env, jtask_class_Task, "name", "Ljava/lang/String;");
- jtask_field_Comm_bind = jxbt_get_jfield(env, jtask_class_Comm, "bind", "J");
- jtask_field_Comm_taskBind = jxbt_get_jfield(env, jtask_class_Comm, "taskBind", "J");
- jtask_field_Comm_receiving = jxbt_get_jfield(env, jtask_class_Comm, "receiving", "Z");
- if (!jtask_field_Task_bind || !jtask_class_Task || !jtask_field_Comm_bind || !jtask_field_Comm_taskBind ||
- !jtask_field_Comm_receiving || !jtask_method_Comm_constructor) {
- jxbt_throw_native(env,bprintf("Can't find some fields in Java class."));
- }
+ jclass jtask_class_Comm = (*env)->FindClass(env, "org/simgrid/msg/Comm");
+ jclass jtask_class_Task = (*env)->FindClass(env, "org/simgrid/msg/Task");
+
+ jtask_method_Comm_constructor = (*env)->GetMethodID(env, jtask_class_Comm, "<init>", "()V");
+ jtask_field_Task_bind = jxbt_get_jfield(env, jtask_class_Task, "bind", "J");
+ jtask_field_Task_name = jxbt_get_jfield(env, jtask_class_Task, "name", "Ljava/lang/String;");
+ jtask_field_Comm_bind = jxbt_get_jfield(env, jtask_class_Comm, "bind", "J");
+ jtask_field_Comm_taskBind = jxbt_get_jfield(env, jtask_class_Comm, "taskBind", "J");
+ jtask_field_Comm_receiving = jxbt_get_jfield(env, jtask_class_Comm, "receiving", "Z");
+ if (!jtask_field_Task_bind || !jtask_class_Task || !jtask_field_Comm_bind || !jtask_field_Comm_taskBind ||
+ !jtask_field_Comm_receiving || !jtask_method_Comm_constructor) {
+ jxbt_throw_native(env,bprintf("Can't find some fields in Java class."));
+ }
}
JNIEXPORT void JNICALL
if (jmessageSize < 0) {
jxbt_throw_illegal(env,
bprintf("Task MessageSize (%f) cannot be negative",
- (double) jmessageSize));
+ (double) jmessageSize));
return;
}
name = (*env)->GetStringUTFChars(env, jname, 0);
}
-
/* create the task */
task =
MSG_task_create(name, (double) jcomputeDuration,
- (double) jmessageSize, NULL);
+ (double) jmessageSize, NULL);
if (jname)
(*env)->ReleaseStringUTFChars(env, jname, name);
/* sets the task name */
JNIEXPORT void JNICALL
Java_org_simgrid_msg_Task_parallelCreate(JNIEnv * env,
- jobject jtask,
- jstring jname,
- jobjectArray jhosts,
- jdoubleArray
- jcomputeDurations_arg,
- jdoubleArray
- jmessageSizes_arg) {
+ jobject jtask,
+ jstring jname,
+ jobjectArray jhosts,
+ jdoubleArray
+ jcomputeDurations_arg,
+ jdoubleArray
+ jmessageSizes_arg) {
m_task_t task; /* the native parallel task to create */
const char *name; /* the name of the task */
jobject jhost;
int index;
-
if (!jcomputeDurations_arg) {
jxbt_throw_null(env,
xbt_strdup
JNIEXPORT jobject JNICALL
Java_org_simgrid_msg_Task_isend(JNIEnv *env, jobject jtask, jstring jmailbox) {
- jclass comm_class;
+ jclass comm_class;
- const char *mailbox;
+ const char *mailbox;
- m_task_t task;
+ m_task_t task;
- jobject jcomm;
- msg_comm_t comm;
+ jobject jcomm;
+ msg_comm_t comm;
- comm_class = (*env)->FindClass(env, "org/simgrid/msg/Comm");
+ comm_class = (*env)->FindClass(env, "org/simgrid/msg/Comm");
- if (!comm_class) return NULL;
+ if (!comm_class) return NULL;
- jcomm = (*env)->NewObject(env, comm_class, jtask_method_Comm_constructor);
- mailbox = (*env)->GetStringUTFChars(env, jmailbox, 0);
+ jcomm = (*env)->NewObject(env, comm_class, jtask_method_Comm_constructor);
+ mailbox = (*env)->GetStringUTFChars(env, jmailbox, 0);
- task = jtask_to_native_task(jtask, env);
+ task = jtask_to_native_task(jtask, env);
- if (!task) {
+ if (!task) {
(*env)->ReleaseStringUTFChars(env, jmailbox, mailbox);
(*env)->DeleteLocalRef(env, jcomm);
jxbt_throw_notbound(env, "task", jtask);
- return NULL;
- }
+ return NULL;
+ }
- MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
- comm = MSG_task_isend(task,mailbox);
+MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
+ comm = MSG_task_isend(task,mailbox);
- (*env)->SetLongField(env, jcomm, jtask_field_Comm_bind, (jlong) (long)(comm));
- (*env)->SetLongField(env, jcomm, jtask_field_Comm_taskBind, (jlong) (long)(NULL));
- (*env)->SetBooleanField(env, jcomm, jtask_field_Comm_receiving, JNI_FALSE);
+ (*env)->SetLongField(env, jcomm, jtask_field_Comm_bind, (jlong) (long)(comm));
+ (*env)->SetLongField(env, jcomm, jtask_field_Comm_taskBind, (jlong) (long)(NULL));
+ (*env)->SetBooleanField(env, jcomm, jtask_field_Comm_receiving, JNI_FALSE);
- (*env)->ReleaseStringUTFChars(env, jmailbox, mailbox);
+ (*env)->ReleaseStringUTFChars(env, jmailbox, mailbox);
- return jcomm;
+ return jcomm;
}
static void msg_task_cancel_on_failed_dsend(void*t) {
- m_task_t task = t;
- JNIEnv *env =get_current_thread_env();
- jobject jtask_global = MSG_task_get_data(task);
-
- /* Destroy the global ref so that the JVM can free the stuff */
- (*env)->DeleteGlobalRef(env, jtask_global);
- MSG_task_set_data(task, NULL);
- MSG_task_destroy(task);
+ m_task_t task = t;
+ JNIEnv *env =get_current_thread_env();
+ jobject jtask_global = MSG_task_get_data(task);
+
+ /* Destroy the global ref so that the JVM can free the stuff */
+ (*env)->DeleteGlobalRef(env, jtask_global);
+ MSG_task_set_data(task, NULL);
+ MSG_task_destroy(task);
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_Task_dsend(JNIEnv * env, jobject jtask,
jstring jalias) {
-
const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
m_task_t task = jtask_to_native_task(jtask, env);
JNIEXPORT void JNICALL
Java_org_simgrid_msg_VM_nativeInit(JNIEnv *env, jclass cls) {
- jclass jprocess_class_VM = (*env)->FindClass(env, "org/simgrid/msg/VM");
- jvm_field_bind = jxbt_get_jfield(env, jprocess_class_VM, "bind", "J");
- if (!jvm_field_bind ) {
- jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
- }
+ jclass jprocess_class_VM = (*env)->FindClass(env, "org/simgrid/msg/VM");
+ jvm_field_bind = jxbt_get_jfield(env, jprocess_class_VM, "bind", "J");
+ if (!jvm_field_bind ) {
+ jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
+ }
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_VM_start(JNIEnv *env, jobject jvm, jobject jhost, jint jcoreamount) {
- m_host_t host = jhost_get_native(env, jhost);
+ m_host_t host = jhost_get_native(env, jhost);
- msg_vm_t vm = MSG_vm_start(host, (int)jcoreamount);
+ msg_vm_t vm = MSG_vm_start(host, (int)jcoreamount);
- jvm_bind(env,jvm,vm);
+ jvm_bind(env,jvm,vm);
}
JNIEXPORT jboolean JNICALL
Java_org_simgrid_msg_VM_isSuspended(JNIEnv *env, jobject jvm) {
- msg_vm_t vm = jvm_get_native(env,jvm);
+ msg_vm_t vm = jvm_get_native(env,jvm);
- return MSG_vm_is_suspended(vm) ? JNI_TRUE : JNI_FALSE;
+ return MSG_vm_is_suspended(vm) ? JNI_TRUE : JNI_FALSE;
}
JNIEXPORT jboolean JNICALL
Java_org_simgrid_msg_VM_isRunning(JNIEnv *env, jobject jvm) {
- msg_vm_t vm = jvm_get_native(env,jvm);
+ msg_vm_t vm = jvm_get_native(env,jvm);
- return MSG_vm_is_running(vm) ? JNI_TRUE : JNI_FALSE;
+ return MSG_vm_is_running(vm) ? JNI_TRUE : JNI_FALSE;
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_VM_bind(JNIEnv *env, jobject jvm, jobject jprocess) {
- msg_vm_t vm = jvm_get_native(env,jvm);
- m_process_t process = jprocess_to_native_process(jprocess,env);
+ msg_vm_t vm = jvm_get_native(env,jvm);
+ m_process_t process = jprocess_to_native_process(jprocess,env);
- xbt_assert((vm != NULL), "VM object is not binded");
- xbt_assert((process != NULL), "Process object is not binded.");
+ xbt_assert((vm != NULL), "VM object is not binded");
+ xbt_assert((process != NULL), "Process object is not binded.");
- MSG_vm_bind(vm,process);
+ MSG_vm_bind(vm,process);
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_VM_unbind(JNIEnv *env, jobject jvm, jobject jprocess) {
- msg_vm_t vm = jvm_get_native(env,jvm);
- m_process_t process = jprocess_to_native_process(jprocess,env);
+ msg_vm_t vm = jvm_get_native(env,jvm);
+ m_process_t process = jprocess_to_native_process(jprocess,env);
- MSG_vm_unbind(vm,process);
+ MSG_vm_unbind(vm,process);
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_VM_migrate(JNIEnv *env, jobject jvm, jobject jhost) {
- msg_vm_t vm = jvm_get_native(env,jvm);
- m_host_t host = jhost_get_native(env, jhost);
+ msg_vm_t vm = jvm_get_native(env,jvm);
+ m_host_t host = jhost_get_native(env, jhost);
- MSG_vm_migrate(vm,host);
+ MSG_vm_migrate(vm,host);
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_VM_suspend(JNIEnv *env, jobject jvm) {
- msg_vm_t vm = jvm_get_native(env,jvm);
+ msg_vm_t vm = jvm_get_native(env,jvm);
- MSG_vm_suspend(vm);
+ MSG_vm_suspend(vm);
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_VM_resume(JNIEnv *env, jobject jvm) {
- msg_vm_t vm = jvm_get_native(env,jvm);
+ msg_vm_t vm = jvm_get_native(env,jvm);
- MSG_vm_resume(vm);
+ MSG_vm_resume(vm);
}
JNIEXPORT void JNICALL
Java_org_simgrid_msg_VM_shutdown(JNIEnv *env, jobject jvm) {
- msg_vm_t vm = jvm_get_native(env,jvm);
+ msg_vm_t vm = jvm_get_native(env,jvm);
- MSG_vm_shutdown(vm);
+ MSG_vm_shutdown(vm);
}
coroutine_stop = (*global_env)->GetMethodID(global_env, coclass, "stop", "()V");
xbt_assert((coroutine_stop != NULL), "Method not found...");
coroutine_yield = (*global_env)->GetStaticMethodID(global_env, coclass, "yield", "()V");
- xbt_assert((coroutine_yield != NULL), "Method yield not found.");
- coroutine_yieldTo = (*global_env)->GetStaticMethodID(global_env, coclass, "yieldTo", "(Ljava/dyn/Coroutine;)V");
- xbt_assert((coroutine_yieldTo != NULL), "Method yieldTo not found.");
-
- jclass class_thread = (*global_env)->FindClass(global_env, "java/lang/Thread");
- xbt_assert((class_thread != NULL), "Can't find java.lang.Thread class");
- jclass class_coroutine_support = (*global_env)->FindClass(global_env, "java/dyn/CoroutineSupport");
- xbt_assert((class_coroutine_support != NULL), "Can't find java.dyn.CoroutineSupport class");
- jmethodID thread_get_current = (*global_env)->GetStaticMethodID(global_env, class_thread, "currentThread", "()Ljava/lang/Thread;");
- xbt_assert((thread_get_current != NULL), "Can't find Thread.currentThread() method.");
-
- /**
- * Retrieve maetro coroutine object
- */
- jobject jthread;
- jthread = (*global_env)->CallStaticObjectMethod(global_env, class_thread, thread_get_current);
- xbt_assert((jthread != NULL), "Can't find current thread.");
-
- jmethodID thread_get_coroutine_support = (*global_env)->GetMethodID(global_env, class_thread, "getCoroutineSupport", "()Ljava/dyn/CoroutineSupport;");
- xbt_assert((thread_get_coroutine_support != NULL), "Can't find Thread.getCoroutineSupport method");
-
- jobject jcoroutine_support;
- jcoroutine_support = (*global_env)->CallObjectMethod(global_env, jthread, thread_get_coroutine_support);
- xbt_assert((jcoroutine_support != NULL), "Can't find coroutine support object");
- //FIXME ? Be careful, might change in the implementation (we are relying on private fields, so...).
- jfieldID coroutine_support_thread_coroutine = (*global_env)->GetFieldID(global_env, class_coroutine_support, "threadCoroutine", "Ljava/dyn/Coroutine;");
- xbt_assert((coroutine_support_thread_coroutine != NULL), "Can't find threadCoroutine field");
- cojava_maestro_coroutine = (jobject)(*global_env)->GetObjectField(global_env, jcoroutine_support, coroutine_support_thread_coroutine);
- xbt_assert((cojava_maestro_coroutine != NULL), "Can't find the thread coroutine.");
- cojava_maestro_coroutine = (*global_env)->NewGlobalRef(global_env, cojava_maestro_coroutine);
- xbt_assert((cojava_maestro_coroutine != NULL), "Can't get a global reference to the thread coroutine.");
+ xbt_assert((coroutine_yield != NULL), "Method yield not found.");
+ coroutine_yieldTo = (*global_env)->GetStaticMethodID(global_env, coclass, "yieldTo", "(Ljava/dyn/Coroutine;)V");
+ xbt_assert((coroutine_yieldTo != NULL), "Method yieldTo not found.");
+
+ jclass class_thread = (*global_env)->FindClass(global_env, "java/lang/Thread");
+ xbt_assert((class_thread != NULL), "Can't find java.lang.Thread class");
+ jclass class_coroutine_support = (*global_env)->FindClass(global_env, "java/dyn/CoroutineSupport");
+ xbt_assert((class_coroutine_support != NULL), "Can't find java.dyn.CoroutineSupport class");
+ jmethodID thread_get_current = (*global_env)->GetStaticMethodID(global_env, class_thread, "currentThread", "()Ljava/lang/Thread;");
+ xbt_assert((thread_get_current != NULL), "Can't find Thread.currentThread() method.");
+
+ /**
+ * Retrieve maetro coroutine object
+ */
+ jobject jthread;
+ jthread = (*global_env)->CallStaticObjectMethod(global_env, class_thread, thread_get_current);
+ xbt_assert((jthread != NULL), "Can't find current thread.");
+
+ jmethodID thread_get_coroutine_support = (*global_env)->GetMethodID(global_env, class_thread, "getCoroutineSupport", "()Ljava/dyn/CoroutineSupport;");
+ xbt_assert((thread_get_coroutine_support != NULL), "Can't find Thread.getCoroutineSupport method");
+
+ jobject jcoroutine_support;
+ jcoroutine_support = (*global_env)->CallObjectMethod(global_env, jthread, thread_get_coroutine_support);
+ xbt_assert((jcoroutine_support != NULL), "Can't find coroutine support object");
+ //FIXME ? Be careful, might change in the implementation (we are relying on private fields, so...).
+ jfieldID coroutine_support_thread_coroutine = (*global_env)->GetFieldID(global_env, class_coroutine_support, "threadCoroutine", "Ljava/dyn/Coroutine;");
+ xbt_assert((coroutine_support_thread_coroutine != NULL), "Can't find threadCoroutine field");
+ cojava_maestro_coroutine = (jobject)(*global_env)->GetObjectField(global_env, jcoroutine_support, coroutine_support_thread_coroutine);
+ xbt_assert((cojava_maestro_coroutine != NULL), "Can't find the thread coroutine.");
+ cojava_maestro_coroutine = (*global_env)->NewGlobalRef(global_env, cojava_maestro_coroutine);
+ xbt_assert((cojava_maestro_coroutine != NULL), "Can't get a global reference to the thread coroutine.");
}
smx_context_t smx_ctx_cojava_self(void)
{
/* If the user provided a function for the process then use it
otherwise is the context for maestro */
if (code) {
- if (argc == 0) {
- context->jprocess = (jobject) code;
- }
- else {
- context->jprocess = NULL;
- }
- context->super.cleanup_func = cleanup_func;
-
- context->super.argc = argc;
- context->super.argv = argv;
- context->super.code = code;
-
- smx_ctx_cojava_run(context);
+ if (argc == 0) {
+ context->jprocess = (jobject) code;
+ }
+ else {
+ context->jprocess = NULL;
+ }
+ context->super.cleanup_func = cleanup_func;
+
+ context->super.argc = argc;
+ context->super.argv = argv;
+ context->super.code = code;
+
+ smx_ctx_cojava_run(context);
}
else {
context->jcoroutine = NULL;
}
static void* smx_ctx_cojava_run(void *data) {
- smx_ctx_cojava_t context = (smx_ctx_cojava_t)data;
- my_current_context = (smx_context_t)context;
- //Create the "Process" object if needed.
- if (context->super.argc <= 0) {
- smx_ctx_cojava_create_coroutine(context);
- }
- my_current_context = maestro_context;
+ smx_ctx_cojava_t context = (smx_ctx_cojava_t)data;
+ my_current_context = (smx_context_t)context;
+ //Create the "Process" object if needed.
+ if (context->super.argc <= 0) {
+ smx_ctx_cojava_create_coroutine(context);
+ }
+ my_current_context = maestro_context;
return NULL;
}
static void smx_ctx_cojava_free(smx_context_t context)
{
- if (context) {
- smx_ctx_cojava_t ctx_java = (smx_ctx_cojava_t) context;
- if (ctx_java->jcoroutine) { /* We are not in maestro context */
- JNIEnv *env = get_current_thread_env();
- (*env)->DeleteGlobalRef(env, ctx_java->jcoroutine);
- (*env)->DeleteGlobalRef(env, ctx_java->jprocess);
- }
+ if (context) {
+ smx_ctx_cojava_t ctx_java = (smx_ctx_cojava_t) context;
+ if (ctx_java->jcoroutine) { /* We are not in maestro context */
+ JNIEnv *env = get_current_thread_env();
+ (*env)->DeleteGlobalRef(env, ctx_java->jcoroutine);
+ (*env)->DeleteGlobalRef(env, ctx_java->jprocess);
+ }
}
smx_ctx_base_free(context);
}
void smx_ctx_cojava_stop(smx_context_t context)
{
- /*
- * The java stack needs to be empty, otherwise weird stuff
- * will happen
- */
- if (context->iwannadie) {
- context->iwannadie = 0;
- JNIEnv *env = get_current_thread_env();
- jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", bprintf("Process killed :)"));
- THROWF(cancel_error, 0, "process cancelled");
+ /*
+ * The java stack needs to be empty, otherwise weird stuff
+ * will happen
+ */
+ if (context->iwannadie) {
+ context->iwannadie = 0;
+ JNIEnv *env = get_current_thread_env();
+ jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", bprintf("Process killed :)"));
+ THROWF(cancel_error, 0, "process cancelled");
+ }
+ else {
+ smx_ctx_base_stop(context);
+ smx_ctx_cojava_suspend(context);
}
- else {
- smx_ctx_base_stop(context);
- smx_ctx_cojava_suspend(context);
- }
}
static void smx_ctx_cojava_suspend(smx_context_t context)
{
- smx_context_t previous_context = context;
- unsigned long int i = cojava_process_index++;
- jobject next_coroutine;
-
- if (i < xbt_dynar_length(cojava_processes)) {
- smx_context_t next_context = SIMIX_process_get_context(xbt_dynar_get_as(
- cojava_processes,i, smx_process_t));
- my_current_context = next_context;
- XBT_DEBUG("Switching to %p",my_current_context);
- smx_ctx_cojava_t java_context = (smx_ctx_cojava_t)(next_context);
-
- if (!java_context->jprocess) {
- (*(java_context->super.code))(java_context->super.argc, java_context->super.argv);
- smx_ctx_cojava_create_coroutine(java_context);
- }
- else if (!java_context->bound) {
- java_context->bound = 1;
- smx_process_t process = SIMIX_process_self();
- (*global_env)->SetLongField(global_env, java_context->jprocess, jprocess_field_Process_bind, (jlong)process);
- }
-
- next_coroutine = java_context->jcoroutine;
- }
- else {
- //Give maestro the control back.
- next_coroutine = cojava_maestro_coroutine;
- my_current_context = maestro_context;
- }
+ smx_context_t previous_context = context;
+ unsigned long int i = cojava_process_index++;
+ jobject next_coroutine;
+
+ if (i < xbt_dynar_length(cojava_processes)) {
+ smx_context_t next_context = SIMIX_process_get_context(xbt_dynar_get_as(
+ cojava_processes,i, smx_process_t));
+ my_current_context = next_context;
+ XBT_DEBUG("Switching to %p",my_current_context);
+ smx_ctx_cojava_t java_context = (smx_ctx_cojava_t)(next_context);
+ if (!java_context->jprocess) {
+ (*(java_context->super.code))(java_context->super.argc, java_context->super.argv);
+ smx_ctx_cojava_create_coroutine(java_context);
+ }
+ else if (!java_context->bound) {
+ java_context->bound = 1;
+ smx_process_t process = SIMIX_process_self();
+ (*global_env)->SetLongField(global_env, java_context->jprocess, jprocess_field_Process_bind, (jlong)process);
+ }
+
+ next_coroutine = java_context->jcoroutine;
+ }
+ else {
+ //Give maestro the control back.
+ next_coroutine = cojava_maestro_coroutine;
+ my_current_context = maestro_context;
+ }
(*global_env)->CallStaticVoidMethod(global_env, coclass, coroutine_yieldTo, next_coroutine);
my_current_context = previous_context;
}
static void smx_ctx_cojava_resume(smx_context_t new_context) {
- my_current_context = new_context;
- smx_ctx_cojava_t java_context = (smx_ctx_cojava_t)(new_context);
-
- if (!java_context->jprocess) {
- (*(java_context->super.code))(java_context->super.argc, java_context->super.argv);
- smx_ctx_cojava_create_coroutine(java_context);
- java_context->bound = 1;
- }
- else if (!java_context->bound) {
- java_context->bound = 1;
- smx_process_t process = SIMIX_process_self();
- (*global_env)->SetLongField(global_env, java_context->jprocess, jprocess_field_Process_bind, (jlong)process);
- }
+ my_current_context = new_context;
+ smx_ctx_cojava_t java_context = (smx_ctx_cojava_t)(new_context);
+
+ if (!java_context->jprocess) {
+ (*(java_context->super.code))(java_context->super.argc, java_context->super.argv);
+ smx_ctx_cojava_create_coroutine(java_context);
+ java_context->bound = 1;
+ }
+ else if (!java_context->bound) {
+ java_context->bound = 1;
+ smx_process_t process = SIMIX_process_self();
+ (*global_env)->SetLongField(global_env, java_context->jprocess, jprocess_field_Process_bind, (jlong)process);
+ }
(*global_env)->CallStaticVoidMethod(global_env, coclass, coroutine_yieldTo, java_context->jcoroutine);
}
static void smx_ctx_cojava_runall(void)
{
cojava_processes = SIMIX_process_get_runnable();
- smx_process_t process;
- process = xbt_dynar_get_as(cojava_processes, 0, smx_process_t);
- cojava_process_index = 1;
- /* Execute the first process */
- smx_ctx_cojava_resume(SIMIX_process_get_context(process));
+ smx_process_t process;
+ process = xbt_dynar_get_as(cojava_processes, 0, smx_process_t);
+ cojava_process_index = 1;
+ /* Execute the first process */
+ smx_ctx_cojava_resume(SIMIX_process_get_context(process));
}
static void smx_ctx_cojava_create_coroutine(smx_ctx_cojava_t context) {
- JNIEnv *env = get_current_thread_env();
- jclass coclass = (*env)->FindClass(env, "java/dyn/Coroutine");
+ JNIEnv *env = get_current_thread_env();
+ jclass coclass = (*env)->FindClass(env, "java/dyn/Coroutine");
xbt_assert((coclass != NULL), "Can't find coroutine class ! :(");
jobject jcoroutine = (*env)->NewObject(env, coclass, coroutine_init, context->jprocess);
xbt_assert((jcoroutine != NULL), "Can't create coroutine object.");
/* If the user provided a function for the process then use it
otherwise is the context for maestro */
if (code) {
- if (argc == 0) {
- context->jprocess = (jobject) code;
- }
- else {
- context->jprocess = NULL;
- }
- context->super.cleanup_func = cleanup_func;
- context->begin = xbt_os_sem_init(0);
- context->end = xbt_os_sem_init(0);
-
- context->super.argc = argc;
- context->super.argv = argv;
- context->super.code = code;
-
- context->thread = xbt_os_thread_create(NULL,smx_ctx_java_thread_run,context,NULL);
+ if (argc == 0) {
+ context->jprocess = (jobject) code;
+ }
+ else {
+ context->jprocess = NULL;
+ }
+ context->super.cleanup_func = cleanup_func;
+ context->begin = xbt_os_sem_init(0);
+ context->end = xbt_os_sem_init(0);
+
+ context->super.argc = argc;
+ context->super.argv = argv;
+ context->super.code = code;
+
+ context->thread = xbt_os_thread_create(NULL,smx_ctx_java_thread_run,context,NULL);
}
else {
context->thread = NULL;
}
static void* smx_ctx_java_thread_run(void *data) {
- smx_ctx_java_t context = (smx_ctx_java_t)data;
- xbt_os_thread_set_extra_data(context);
- //Attach the thread to the JVM
- JNIEnv *env;
+ smx_ctx_java_t context = (smx_ctx_java_t)data;
+ xbt_os_thread_set_extra_data(context);
+ //Attach the thread to the JVM
+ JNIEnv *env;
jint error = (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
xbt_assert((error == JNI_OK), "The thread could not be attached to the JVM");
context->jenv = get_current_thread_env();
//Wait for the first scheduling round to happen.
xbt_os_sem_acquire(context->begin);
//Create the "Process" object if needed.
- if (context->super.argc > 0) {
- (*(context->super.code))(context->super.argc, context->super.argv);
- }
- else {
- smx_process_t process = SIMIX_process_self();
- (*env)->SetLongField(env, context->jprocess, jprocess_field_Process_bind, (jlong)process);
- }
- xbt_assert((context->jprocess != NULL), "Process not created...");
+ if (context->super.argc > 0) {
+ (*(context->super.code))(context->super.argc, context->super.argv);
+ }
+ else {
+ smx_process_t process = SIMIX_process_self();
+ (*env)->SetLongField(env, context->jprocess, jprocess_field_Process_bind, (jlong)process);
+ }
+ xbt_assert((context->jprocess != NULL), "Process not created...");
//wait for the process to be able to begin
//TODO: Cache it
jfieldID jprocess_field_Process_startTime = jxbt_get_sfield(env, "org/simgrid/msg/Process", "startTime", "D");
static void smx_ctx_java_free(smx_context_t context)
{
- if (context) {
- smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
- if (ctx_java->thread) { /* We are not in maestro context */
- xbt_os_thread_join(ctx_java->thread, NULL);
- xbt_os_sem_destroy(ctx_java->begin);
- xbt_os_sem_destroy(ctx_java->end);
- }
+ if (context) {
+ smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
+ if (ctx_java->thread) { /* We are not in maestro context */
+ xbt_os_thread_join(ctx_java->thread, NULL);
+ xbt_os_sem_destroy(ctx_java->begin);
+ xbt_os_sem_destroy(ctx_java->end);
+ }
}
smx_ctx_base_free(context);
}
void smx_ctx_java_stop(smx_context_t context)
{
- smx_ctx_java_t ctx_java = (smx_ctx_java_t)context;
+ smx_ctx_java_t ctx_java = (smx_ctx_java_t)context;
/* I am the current process and I am dying */
- if (context->iwannadie) {
+ if (context->iwannadie) {
context->iwannadie = 0;
JNIEnv *env = get_current_thread_env();
jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", bprintf("Process killed :)"));
}
else {
smx_ctx_base_stop(context);
- /* detach the thread and kills it */
- JNIEnv *env = ctx_java->jenv;
- (*env)->DeleteGlobalRef(env,ctx_java->jprocess);
- jint error = (*__java_vm)->DetachCurrentThread(__java_vm);
- xbt_assert((error == JNI_OK), "The thread couldn't be detached.");
- xbt_os_sem_release(((smx_ctx_java_t)context)->end);
- xbt_os_thread_exit(NULL);
-
+ /* detach the thread and kills it */
+ JNIEnv *env = ctx_java->jenv;
+ (*env)->DeleteGlobalRef(env,ctx_java->jprocess);
+ jint error = (*__java_vm)->DetachCurrentThread(__java_vm);
+ xbt_assert((error == JNI_OK), "The thread couldn't be detached.");
+ xbt_os_sem_release(((smx_ctx_java_t)context)->end);
+ xbt_os_thread_exit(NULL);
}
}
static void smx_ctx_java_suspend(smx_context_t context)
{
- smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
- xbt_os_sem_release(ctx_java->end);
- xbt_os_sem_acquire(ctx_java->begin);
+ smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
+ xbt_os_sem_release(ctx_java->end);
+ xbt_os_sem_acquire(ctx_java->begin);
}
// FIXME: inline those functions
static void smx_ctx_java_resume(smx_context_t new_context)
{
- smx_ctx_java_t ctx_java = (smx_ctx_java_t) new_context;
- xbt_os_sem_release(ctx_java->begin);
- xbt_os_sem_acquire(ctx_java->end);
+ smx_ctx_java_t ctx_java = (smx_ctx_java_t) new_context;
+ xbt_os_sem_release(ctx_java->begin);
+ xbt_os_sem_acquire(ctx_java->end);
}
static void smx_ctx_java_runall(void)