X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/e4acbfc882941415a726fdd87afcbd7403825e9a..4d02714ee138a3bd9b02e0064b7a2aa26407e9e8:/src/bindings/java/jmsg_task.cpp diff --git a/src/bindings/java/jmsg_task.cpp b/src/bindings/java/jmsg_task.cpp index b8ebf844fb..1983b13579 100644 --- a/src/bindings/java/jmsg_task.cpp +++ b/src/bindings/java/jmsg_task.cpp @@ -1,22 +1,22 @@ /* Functions related to the java task instances. */ -/* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ -#include +#include "simgrid/Exception.hpp" +#include "simgrid/s4u/Host.hpp" +#include "src/kernel/context/Context.hpp" -#include "jmsg.h" +#include "jmsg.hpp" #include "jmsg_host.h" -#include "jmsg_task.h" #include "jmsg_process.h" -#include "jxbt_utilities.h" +#include "jmsg_task.h" +#include "jxbt_utilities.hpp" XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(java); -SG_BEGIN_DECL() - static jmethodID jtask_method_Comm_constructor; static jfieldID jtask_field_Task_bind; @@ -76,8 +76,8 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_parallelCreate(JNIEnv * env, jo int host_count = static_cast(env->GetArrayLength(jhosts)); jdouble* jcomputeDurations = env->GetDoubleArrayElements(jcomputeDurations_arg, 0); - msg_host_t* hosts = xbt_new0(msg_host_t, host_count); - double* computeDurations = xbt_new0(double, host_count); + msg_host_t* hosts = new msg_host_t[host_count]; + double* computeDurations = new double[host_count]; for (int index = 0; index < host_count; index++) { jobject jhost = env->GetObjectArrayElement(jhosts, index); hosts[index] = jhost_get_native(env, jhost); @@ -86,7 +86,7 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_parallelCreate(JNIEnv * env, jo env->ReleaseDoubleArrayElements(jcomputeDurations_arg, jcomputeDurations, 0); jdouble* jmessageSizes = env->GetDoubleArrayElements(jmessageSizes_arg, 0); - double* messageSizes = xbt_new0(double, host_count* host_count); + double* messageSizes = new double[host_count * host_count]; for (int index = 0; index < host_count * host_count; index++) { messageSizes[index] = jmessageSizes[index]; } @@ -99,32 +99,39 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_parallelCreate(JNIEnv * env, jo /* associate the java task object and the native task */ jtask_bind(jtask, task, env); + + delete[] hosts; + delete[] computeDurations; + delete[] messageSizes; } JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_cancel(JNIEnv * env, jobject jtask) { msg_task_t ptask = jtask_to_native(jtask, env); - if (!ptask) { + if (not ptask) { jxbt_throw_notbound(env, "task", jtask); return; } msg_error_t rv = MSG_task_cancel(ptask); - jxbt_check_res("MSG_task_cancel()", rv, MSG_OK, bprintf("unexpected error , please report this bug")); + jxbt_check_res("MSG_task_cancel()", rv, MSG_OK, "unexpected error , please report this bug"); } JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_execute(JNIEnv * env, jobject jtask) { msg_task_t task = jtask_to_native(jtask, env); - if (!task) { + if (not task) { jxbt_throw_notbound(env, "task", jtask); return; } msg_error_t rv; - rv = MSG_task_execute(task); + if (not simgrid::ForcefulKillException::try_n_catch([&rv, &task]() { rv = MSG_task_execute(task); })) { + jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Process killed"); + } + if (env->ExceptionOccurred()) return; if (rv != MSG_OK) { @@ -136,7 +143,7 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_setBound(JNIEnv * env, jobject { msg_task_t task = jtask_to_native(jtask, env); - if (!task) { + if (not task) { jxbt_throw_notbound(env, "task", jtask); return; } @@ -146,7 +153,7 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_setBound(JNIEnv * env, jobject JNIEXPORT jstring JNICALL Java_org_simgrid_msg_Task_getName(JNIEnv * env, jobject jtask) { msg_task_t task = jtask_to_native(jtask, env); - if (!task) { + if (not task) { jxbt_throw_notbound(env, "task", jtask); return nullptr; } @@ -159,7 +166,7 @@ JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_getSender(JNIEnv * env, jobj msg_task_t task = jtask_to_native(jtask, env); - if (!task) { + if (not task) { jxbt_throw_notbound(env, "task", jtask); return nullptr; } @@ -176,7 +183,7 @@ JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_getSource(JNIEnv * env, jobj msg_host_t host; msg_task_t task = jtask_to_native(jtask, env); - if (!task) { + if (not task) { jxbt_throw_notbound(env, "task", jtask); return nullptr; } @@ -185,7 +192,7 @@ JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_getSource(JNIEnv * env, jobj if (host == nullptr) { return nullptr; } - if (!host->extension(JAVA_HOST_LEVEL)) { + if (not host->extension(JAVA_HOST_LEVEL)) { jxbt_throw_jni(env, "MSG_task_get_source() failed"); return nullptr; } @@ -197,17 +204,17 @@ JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Task_getFlopsAmount(JNIEnv * env, { msg_task_t ptask = jtask_to_native(jtask, env); - if (!ptask) { + if (not ptask) { jxbt_throw_notbound(env, "task", jtask); return -1; } - return (jdouble) MSG_task_get_flops_amount(ptask); + return (jdouble)MSG_task_get_flops_amount(ptask); } JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_setName(JNIEnv *env, jobject jtask, jobject jname) { msg_task_t task = jtask_to_native(jtask, env); - if (!task) { + if (not task) { jxbt_throw_notbound(env, "task", jtask); return; } @@ -223,7 +230,7 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_setPriority(JNIEnv * env, jobje { msg_task_t task = jtask_to_native(jtask, env); - if (!task) { + if (not task) { jxbt_throw_notbound(env, "task", jtask); return; } @@ -234,7 +241,7 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_setFlopsAmount (JNIEnv *env, jo { msg_task_t task = jtask_to_native(jtask, env); - if (!task) { + if (not task) { jxbt_throw_notbound(env, "task", jtask); return; } @@ -245,7 +252,7 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_setBytesAmount (JNIEnv *env, jo { msg_task_t task = jtask_to_native(jtask, env); - if (!task) { + if (not task) { jxbt_throw_notbound(env, "task", jtask); return; } @@ -257,7 +264,7 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_sendBounded(JNIEnv * env,jobjec jdouble jtimeout,jdouble maxrate) { msg_task_t task = jtask_to_native(jtask, env); - if (!task) { + if (not task) { jxbt_throw_notbound(env, "task", jtask); return; } @@ -279,12 +286,17 @@ JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_receive(JNIEnv* env, jclass msg_task_t task = nullptr; const char *alias = env->GetStringUTFChars(jalias, 0); - msg_error_t rv = MSG_task_receive_ext(&task, alias, (double)jtimeout, /*host*/ nullptr); + msg_error_t rv; + if (not simgrid::ForcefulKillException::try_n_catch([&rv, &task, &alias, &jtimeout]() { + rv = MSG_task_receive_ext(&task, alias, (double)jtimeout, /*host*/ nullptr); + })) { + jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Process killed"); + } env->ReleaseStringUTFChars(jalias, alias); if (env->ExceptionOccurred()) return nullptr; if (rv != MSG_OK) { - jmsg_throw_status(env,rv); + jmsg_throw_status(env, rv); return nullptr; } jobject jtask_global = (jobject) MSG_task_get_data(task); @@ -299,16 +311,15 @@ JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_receive(JNIEnv* env, jclass JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_irecv(JNIEnv * env, jclass cls, jstring jmailbox) { jclass comm_class = env->FindClass("org/simgrid/msg/Comm"); - if (!comm_class) + if (not comm_class) return nullptr; //pointer to store the task object pointer. - msg_task_t *task = xbt_new(msg_task_t,1); - *task = nullptr; + msg_task_t* task = new msg_task_t(nullptr); /* There should be a cache here */ jobject jcomm = env->NewObject(comm_class, jtask_method_Comm_constructor); - if (!jcomm) { + if (not jcomm) { jxbt_throw_jni(env, "Can't create a Comm object."); return nullptr; } @@ -354,14 +365,14 @@ JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_irecvBounded(JNIEnv * env, j jdouble rate) { jclass comm_class = env->FindClass("org/simgrid/msg/Comm"); - if (!comm_class) + if (not comm_class) return nullptr; // pointer to store the task object pointer. - msg_task_t* task = xbt_new0(msg_task_t, 1); + msg_task_t* task = new msg_task_t(nullptr); jobject jcomm = env->NewObject(comm_class, jtask_method_Comm_constructor); - if (!jcomm) { + if (not jcomm) { jxbt_throw_jni(env, "Can't create a Comm object."); return nullptr; } @@ -383,14 +394,15 @@ JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_isend(JNIEnv *env, jobject j jclass comm_class = env->FindClass("org/simgrid/msg/Comm"); - if (!comm_class) return nullptr; + if (not comm_class) + return nullptr; jobject jcomm = env->NewObject(comm_class, jtask_method_Comm_constructor); const char* mailbox = env->GetStringUTFChars(jmailbox, 0); msg_task_t task = jtask_to_native(jtask, env); - if (!task) { + if (not task) { env->ReleaseStringUTFChars(jmailbox, mailbox); env->DeleteLocalRef(jcomm); jxbt_throw_notbound(env, "task", jtask); @@ -418,7 +430,7 @@ JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_isendBounded(JNIEnv *env, jo const char *mailbox; jclass comm_class = env->FindClass("org/simgrid/msg/Comm"); - if (!comm_class) + if (not comm_class) return nullptr; jcomm = env->NewObject(comm_class, jtask_method_Comm_constructor); @@ -426,7 +438,7 @@ JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_isendBounded(JNIEnv *env, jo task = jtask_to_native(jtask, env); - if (!task) { + if (not task) { env->ReleaseStringUTFChars(jmailbox, mailbox); env->DeleteLocalRef(jcomm); jxbt_throw_notbound(env, "task", jtask); @@ -449,7 +461,7 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_nativeFinalize(JNIEnv * env, jo { msg_task_t task = jtask_to_native(jtask, env); - if (!task) { + if (not task) { jxbt_throw_notbound(env, "task", jtask); return; } @@ -459,13 +471,17 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_nativeFinalize(JNIEnv * env, jo static void msg_task_cancel_on_failed_dsend(void*t) { msg_task_t task = (msg_task_t) t; - JNIEnv *env =get_current_thread_env(); - jobject jtask_global = (jobject) MSG_task_get_data(task); - - /* Destroy the global ref so that the JVM can free the stuff */ - env->DeleteGlobalRef(jtask_global); + JNIEnv* env = get_current_thread_env(); + if (env) { + jobject jtask_global = (jobject)MSG_task_get_data(task); + /* Destroy the global ref so that the JVM can free the stuff */ + env->DeleteGlobalRef(jtask_global); + /* Don't free the C data here, to avoid a race condition with the GC also sometimes doing so. + * A rare memleak is seen as preferable to a rare "free(): invalid pointer" failure that + * proves really hard to debug. + */ + } MSG_task_set_data(task, nullptr); - MSG_task_destroy(task); } JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_dsend(JNIEnv * env, jobject jtask, jstring jalias) @@ -474,7 +490,7 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_dsend(JNIEnv * env, jobject jta msg_task_t task = jtask_to_native(jtask, env); - if (!task) { + if (not task) { env->ReleaseStringUTFChars(jalias, alias); jxbt_throw_notbound(env, "task", jtask); return; @@ -494,7 +510,7 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_dsendBounded(JNIEnv * env, jobj msg_task_t task = jtask_to_native(jtask, env); - if (!task) { + if (not task) { env->ReleaseStringUTFChars(jalias, alias); jxbt_throw_notbound(env, "task", jtask); return; @@ -524,5 +540,3 @@ JNIEXPORT jint JNICALL Java_org_simgrid_msg_Task_listenFrom(JNIEnv * env, jclass return (jint) rv; } - -SG_END_DECL()