/* 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"
XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(java);
-extern "C" {
-
static jmethodID jtask_method_Comm_constructor;
static jfieldID jtask_field_Task_bind;
if (jname)
name = env->GetStringUTFChars(jname, 0);
- msg_task_t task = MSG_task_create(name, static_cast<double>(jflopsAmount), static_cast<double>(jbytesAmount), jtask);
+ msg_task_t task = MSG_task_create(name, jflopsAmount, jbytesAmount, jtask);
if (jname)
env->ReleaseStringUTFChars(jname, name);
jobjectArray jhosts, jdoubleArray jcomputeDurations_arg,
jdoubleArray jmessageSizes_arg)
{
- int host_count = static_cast<int>(env->GetArrayLength(jhosts));
+ int host_count = env->GetArrayLength(jhosts);
jdouble* jcomputeDurations = env->GetDoubleArrayElements(jcomputeDurations_arg, 0);
msg_host_t* hosts = new msg_host_t[host_count];
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) {
jxbt_throw_notbound(env, "task", jtask);
return -1;
}
- return (jdouble)MSG_task_get_remaining_work_ratio(ptask);
+ return (jdouble)MSG_task_get_flops_amount(ptask);
}
JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_setName(JNIEnv *env, jobject jtask, jobject jname) {
jxbt_throw_notbound(env, "task", jtask);
return;
}
- MSG_task_set_priority(task, static_cast<double>(priority));
+ MSG_task_set_priority(task, priority);
}
JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_setFlopsAmount (JNIEnv *env, jobject jtask, jdouble computationAmount)
jxbt_throw_notbound(env, "task", jtask);
return;
}
- MSG_task_set_flops_amount(task, static_cast<double>(computationAmount));
+ MSG_task_set_flops_amount(task, computationAmount);
}
JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_setBytesAmount (JNIEnv *env, jobject jtask, jdouble dataSize)
return;
}
env->SetDoubleField(jtask, jtask_field_Task_messageSize, dataSize);
- MSG_task_set_bytes_amount(task, static_cast<double>(dataSize));
+ MSG_task_set_bytes_amount(task, dataSize);
}
JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_sendBounded(JNIEnv * env,jobject jtask, jstring jalias,
MSG_task_set_data(task, (void *) env->NewGlobalRef(jtask));
const char* alias = env->GetStringUTFChars(jalias, 0);
- msg_error_t res =
- MSG_task_send_with_timeout_bounded(task, alias, static_cast<double>(jtimeout), static_cast<double>(maxrate));
+ msg_error_t res = MSG_task_send_with_timeout_bounded(task, alias, jtimeout, maxrate);
env->ReleaseStringUTFChars(jalias, alias);
if (res != MSG_OK)
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);
msg_task_t task = nullptr;
const char *alias = env->GetStringUTFChars(jalias, 0);
- msg_error_t res = MSG_task_receive_ext_bounded(&task, alias, static_cast<double>(jtimeout), /*host*/ nullptr,
- static_cast<double>(rate));
+ msg_error_t res = MSG_task_receive_ext_bounded(&task, alias, jtimeout, /*host*/ nullptr, rate);
if (env->ExceptionOccurred())
return nullptr;
if (res != MSG_OK) {
}
const char* mailbox = env->GetStringUTFChars(jmailbox, 0);
- msg_comm_t comm = MSG_task_irecv_bounded(task, mailbox, static_cast<double>(rate));
+ msg_comm_t comm = MSG_task_irecv_bounded(task, mailbox, rate);
env->ReleaseStringUTFChars(jmailbox, mailbox);
env->SetLongField(jcomm, jtask_field_Comm_bind, (jlong) (uintptr_t)(comm));
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)
/* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
MSG_task_set_data(task, (void *) env->NewGlobalRef(jtask));
- MSG_task_dsend_bounded(task, alias, msg_task_cancel_on_failed_dsend,static_cast<double>(maxrate));
+ MSG_task_dsend_bounded(task, alias, msg_task_cancel_on_failed_dsend, maxrate);
env->ReleaseStringUTFChars(jalias, alias);
}
return (jint) rv;
}
-}