#include <xbt/ex.h>
#include <atomic>
+#include <functional>
#include <stdexcept>
#include <string>
}
};
+class XBT_PUBLIC ForcefulKillException {
+ /** @brief Exception launched to kill an actor; DO NOT BLOCK IT!
+ *
+ * This exception is thrown whenever the actor's host is turned off. The actor stack is properly unwinded to release
+ * all objects allocated on the stack (RAII powa).
+ *
+ * You may want to catch this exception to perform some extra cleanups in your simulation, but YOUR ACTORS MUST NEVER
+ * SURVIVE a ForcefulKillException, or your simulation will segfault.
+ *
+ * @verbatim
+ * void* payload = malloc(512);
+ *
+ * try {
+ * simgrid::s4u::this_actor::execute(100000);
+ * } catch (simgrid::kernel::context::ForcefulKillException& e) { // oops, my host just turned off
+ * free(malloc);
+ * throw; // I shall never survive on an host that was switched off
+ * }
+ * @endverbatim
+ */
+ /* Nope, Sonar, this should not inherit of std::exception nor of simgrid::Exception.
+ * Otherwise, users may accidentally catch it with a try {} catch (std::exception)
+ */
+public:
+ ForcefulKillException() = default;
+ explicit ForcefulKillException(const std::string& msg) : msg_(std::string("Actor killed (") + msg + std::string(")."))
+ {
+ }
+ ~ForcefulKillException();
+ const char* what() const noexcept { return msg_.c_str(); }
+
+ static void do_throw();
+ static bool try_n_catch(std::function<void(void)> try_block);
+
+private:
+ std::string msg_ = std::string("Actor killed.");
+};
+
} // namespace simgrid
#endif
env->ExceptionClear();
XBT_ATTRIB_UNUSED jint error = __java_vm->DetachCurrentThread();
xbt_assert(error == JNI_OK, "Cannot detach failing thread");
- simgrid::kernel::context::ForcefulKillException::do_throw();
+ simgrid::ForcefulKillException::do_throw();
}
}
/* 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/plugins/energy.h"
#include "simgrid/plugins/load.h"
#include "simgrid/s4u/Host.hpp"
JNIEXPORT void JNICALL Java_org_simgrid_msg_Host_off(JNIEnv *env, jobject jhost) {
msg_host_t host = jhost_get_native(env, jhost);
- if (not simgrid::kernel::context::ForcefulKillException::try_n_catch([host]() { MSG_host_off(host); }))
+ if (not simgrid::ForcefulKillException::try_n_catch([host]() { MSG_host_off(host); }))
jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Host turned off");
}
{
double time = ((double)jmillis) / 1000 + ((double)jnanos) / 1000000000;
msg_error_t rv = MSG_OK;
- if (not simgrid::kernel::context::ForcefulKillException::try_n_catch(
- [&time]() { simgrid::s4u::this_actor::sleep_for(time); })) {
+ if (not simgrid::ForcefulKillException::try_n_catch([&time]() { simgrid::s4u::this_actor::sleep_for(time); })) {
rv = MSG_HOST_FAILURE;
}
if (rv != MSG_OK) {
JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_waitFor(JNIEnv * env, jobject jprocess, jdouble jseconds)
{
msg_error_t rv = MSG_OK;
- if (not simgrid::kernel::context::ForcefulKillException::try_n_catch(
+ if (not simgrid::ForcefulKillException::try_n_catch(
[&jseconds]() { simgrid::s4u::this_actor::sleep_for((double)jseconds); })) {
rv = MSG_HOST_FAILURE;
jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Process killed");
jxbt_throw_notbound(env, "process", jprocess);
return;
}
- if (not simgrid::kernel::context::ForcefulKillException::try_n_catch([&process]() { MSG_process_kill(process); })) {
+ if (not simgrid::ForcefulKillException::try_n_catch([&process]() { MSG_process_kill(process); })) {
jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Process killed");
}
}
/* 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"
return;
}
msg_error_t rv;
- if (not simgrid::kernel::context::ForcefulKillException::try_n_catch(
- [&rv, &task]() { 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");
}
const char *alias = env->GetStringUTFChars(jalias, 0);
msg_error_t rv;
- if (not simgrid::kernel::context::ForcefulKillException::try_n_catch([&rv, &task, &alias, &jtimeout]() {
+ 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");
{
msg_vm_t vm = jvm_get_native(env,jvm);
msg_host_t host = jhost_get_native(env, jhost);
- if (not simgrid::kernel::context::ForcefulKillException::try_n_catch([&vm, &host]() { MSG_vm_migrate(vm, host); })) {
+ if (not simgrid::ForcefulKillException::try_n_catch([&vm, &host]() { MSG_vm_migrate(vm, host); })) {
XBT_VERB("Caught exception during migration");
jxbt_throw_host_failure(env, "during migration");
}
#include <pybind11/stl.h>
#include "src/kernel/context/Context.hpp"
+#include <simgrid/Exception.hpp>
#include <simgrid/s4u/Actor.hpp>
#include <simgrid/s4u/Engine.hpp>
#include <simgrid/s4u/Host.hpp>
m.attr("simgrid_version") = simgrid_version;
// Internal exception used to kill actors and sweep the RAII chimney (free objects living on the stack)
- py::object pyForcefulKillEx =
- py::register_exception<simgrid::kernel::context::ForcefulKillException>(m, "ActorKilled");
+ py::object pyForcefulKillEx = py::register_exception<simgrid::ForcefulKillException>(m, "ActorKilled");
/* this_actor namespace */
void (*sleep_for_fun)(double) = &simgrid::s4u::this_actor::sleep_for; // pick the right overload
AttachContext::~AttachContext() = default;
-ForcefulKillException::~ForcefulKillException() = default;
-
-void ForcefulKillException::do_throw()
-{
- throw ForcefulKillException();
-}
-
-bool ForcefulKillException::try_n_catch(std::function<void(void)> try_block)
-{
- bool res;
- try {
- try_block();
- res = true;
- } catch (ForcefulKillException const&) {
- XBT_DEBUG("Caught a ForcefulKillException");
- res = false;
- }
- return res;
-}
}}}
/** @brief Executes all the processes to run (in parallel if possible). */
virtual void attach_stop() = 0;
};
-class XBT_PUBLIC ForcefulKillException {
- /** @brief Exception launched to kill an actor; DO NOT BLOCK IT!
- *
- * This exception is thrown whenever the actor's host is turned off. The actor stack is properly unwinded to release
- * all objects allocated on the stack (RAII powa).
- *
- * You may want to catch this exception to perform some extra cleanups in your simulation, but YOUR ACTORS MUST NEVER
- * SURVIVE a ForcefulKillException, or your simulation will segfault.
- *
- * @verbatim
- * void* payload = malloc(512);
- *
- * try {
- * simgrid::s4u::this_actor::execute(100000);
- * } catch (simgrid::kernel::context::ForcefulKillException& e) { // oops, my host just turned off
- * free(malloc);
- * throw; // I shall never survive on an host that was switched off
- * }
- * @endverbatim
- *
- * Nope, Sonar, this should not inherit of std::exception nor of simgrid::Exception.
- * Otherwise, users may accidentally catch it with a try {} catch (std::exception)
- */
-public:
- ForcefulKillException() = default;
- explicit ForcefulKillException(const std::string& msg) : msg_(std::string("Actor killed (") + msg + std::string(")."))
- {
- }
- ~ForcefulKillException();
- const char* what() const noexcept { return msg_.c_str(); }
-
- static void do_throw();
- static bool try_n_catch(std::function<void(void)> try_block);
-
-private:
- std::string msg_ = std::string("Actor killed.");
-};
/* This allows Java to hijack the context factory (Java induces factories of factory :) */
typedef ContextFactory* (*ContextFactoryInitializer)();
/* 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/modelchecker.h"
#include "src/internal_config.h"
#include "src/kernel/context/context_private.hpp"
try {
(*context)();
context->Context::stop();
- } catch (simgrid::kernel::context::ForcefulKillException const&) {
+ } catch (simgrid::ForcefulKillException const&) {
XBT_DEBUG("Caught a ForcefulKillException");
} catch (simgrid::Exception const& e) {
XBT_INFO("Actor killed by an uncatched exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
--- /dev/null
+/* Copyright (c) 2018-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>
+
+XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
+
+namespace simgrid {
+
+ForcefulKillException::~ForcefulKillException() = default;
+
+void ForcefulKillException::do_throw()
+{
+ throw ForcefulKillException();
+}
+
+bool ForcefulKillException::try_n_catch(std::function<void(void)> try_block)
+{
+ bool res;
+ try {
+ try_block();
+ res = true;
+ } catch (ForcefulKillException const&) {
+ XBT_DEBUG("Caught a ForcefulKillException");
+ res = false;
+ }
+ return res;
+}
+} // namespace simgrid
// Forcefully kill the actor if its host is turned off. Not a HostFailureException because you should not survive that
if (not host_->is_on())
- this->throw_exception(std::make_exception_ptr(simgrid::kernel::context::ForcefulKillException("host failed")));
+ this->throw_exception(std::make_exception_ptr(ForcefulKillException("host failed")));
/* destroy the blocking synchro if any */
if (waiting_synchro != nullptr) {
std::abort();
}
- catch (simgrid::kernel::context::ForcefulKillException& e) {
+ catch (simgrid::ForcefulKillException const& e) {
XBT_ERROR("Received a ForcefulKillException at the top-level exception handler. Maybe a Java->C++ call that is not "
"protected "
"in a try/catch?");
)
set(SIMGRID_SRC
+ src/simgrid/Exception.cpp
src/simgrid/sg_config.cpp
src/simgrid/util.hpp
)