X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/ca280be5e4f894c66ac254d445962c96a34cbe45..fce8115857a8ecdd9556571c4e456d99e5097003:/src/bindings/java/jmsg_task.cpp diff --git a/src/bindings/java/jmsg_task.cpp b/src/bindings/java/jmsg_task.cpp index add3877580..ede08bb0de 100644 --- a/src/bindings/java/jmsg_task.cpp +++ b/src/bindings/java/jmsg_task.cpp @@ -1,11 +1,13 @@ /* 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 "simgrid/Exception.hpp" #include "simgrid/s4u/Host.hpp" +#include "src/kernel/context/Context.hpp" #include "jmsg.hpp" #include "jmsg_host.h" @@ -15,8 +17,6 @@ XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(java); -extern "C" { - static jmethodID jtask_method_Comm_constructor; static jfieldID jtask_field_Task_bind; @@ -77,7 +77,7 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_parallelCreate(JNIEnv * env, jo jdouble* jcomputeDurations = env->GetDoubleArrayElements(jcomputeDurations_arg, 0); msg_host_t* hosts = new msg_host_t[host_count]; - double* computeDurations = xbt_new0(double, 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]; } @@ -101,6 +101,8 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_parallelCreate(JNIEnv * env, jo jtask_bind(jtask, task, env); delete[] hosts; + delete[] computeDurations; + delete[] messageSizes; } JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_cancel(JNIEnv * env, jobject jtask) @@ -126,7 +128,10 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_execute(JNIEnv * env, jobject j 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) { @@ -203,7 +208,7 @@ JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Task_getFlopsAmount(JNIEnv * env, 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) { @@ -281,12 +286,16 @@ 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_with_timeout(&task, alias, (double)jtimeout); })) { + 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); @@ -461,13 +470,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) @@ -526,4 +539,3 @@ JNIEXPORT jint JNICALL Java_org_simgrid_msg_Task_listenFrom(JNIEnv * env, jclass return (jint) rv; } -}