Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move ForcefulKillException to the root namespace, along with the other exceptions
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Mon, 4 Mar 2019 22:28:38 +0000 (23:28 +0100)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Mon, 4 Mar 2019 22:29:20 +0000 (23:29 +0100)
15 files changed:
include/simgrid/Exception.hpp
src/bindings/java/jmsg.cpp
src/bindings/java/jmsg_host.cpp
src/bindings/java/jmsg_process.cpp
src/bindings/java/jmsg_task.cpp
src/bindings/java/jmsg_vm.cpp
src/bindings/python/simgrid_python.cpp
src/kernel/context/Context.cpp
src/kernel/context/Context.hpp
src/kernel/context/ContextSwapped.cpp
src/kernel/context/ContextUnix.cpp
src/simgrid/Exception.cpp [new file with mode: 0644]
src/simix/ActorImpl.cpp
src/xbt/exception.cpp
tools/cmake/DefinePackages.cmake

index 321b956..0f3a5e3 100644 (file)
@@ -15,6 +15,7 @@
 #include <xbt/ex.h>
 
 #include <atomic>
 #include <xbt/ex.h>
 
 #include <atomic>
+#include <functional>
 #include <stdexcept>
 #include <string>
 
 #include <stdexcept>
 #include <string>
 
@@ -164,6 +165,44 @@ public:
   }
 };
 
   }
 };
 
+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
 } // namespace simgrid
 
 #endif
index 17a86a4..ebe9597 100644 (file)
@@ -277,7 +277,7 @@ static void run_jprocess(JNIEnv *env, jobject jprocess)
     env->ExceptionClear();
     XBT_ATTRIB_UNUSED jint error = __java_vm->DetachCurrentThread();
     xbt_assert(error == JNI_OK, "Cannot detach failing thread");
     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();
   }
 }
 
   }
 }
 
index 63d9099..550c172 100644 (file)
@@ -5,6 +5,7 @@
 /* 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. */
 
 /* 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"
 #include "simgrid/plugins/energy.h"
 #include "simgrid/plugins/load.h"
 #include "simgrid/s4u/Host.hpp"
@@ -142,7 +143,7 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Host_on(JNIEnv *env, jobject jhost)
 
 JNIEXPORT void JNICALL Java_org_simgrid_msg_Host_off(JNIEnv *env, jobject jhost) {
   msg_host_t host = jhost_get_native(env, jhost);
 
 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");
 }
 
     jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Host turned off");
 }
 
index 0d0866c..0cfc805 100644 (file)
@@ -229,8 +229,7 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_sleep(JNIEnv *env, jclass cl
  {
   double time =  ((double)jmillis) / 1000 + ((double)jnanos) / 1000000000;
   msg_error_t rv = MSG_OK;
  {
   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) {
     rv = MSG_HOST_FAILURE;
   }
   if (rv != MSG_OK) {
@@ -241,7 +240,7 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_sleep(JNIEnv *env, jclass cl
 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_waitFor(JNIEnv * env, jobject jprocess, jdouble jseconds)
 {
   msg_error_t 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");
           [&jseconds]() { simgrid::s4u::this_actor::sleep_for((double)jseconds); })) {
     rv = MSG_HOST_FAILURE;
     jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Process killed");
@@ -262,7 +261,7 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_kill(JNIEnv * env, jobject j
     jxbt_throw_notbound(env, "process", jprocess);
     return;
   }
     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");
   }
 }
     jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Process killed");
   }
 }
index 67b88f2..1983b13 100644 (file)
@@ -5,6 +5,7 @@
 /* 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. */
 
 /* 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 "simgrid/s4u/Host.hpp"
 #include "src/kernel/context/Context.hpp"
 
@@ -127,8 +128,7 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_execute(JNIEnv * env, jobject j
     return;
   }
   msg_error_t rv;
     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");
   }
 
     jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Process killed");
   }
 
@@ -287,7 +287,7 @@ JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_receive(JNIEnv* env, jclass
 
   const char *alias = env->GetStringUTFChars(jalias, 0);
   msg_error_t rv;
 
   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");
         rv = MSG_task_receive_ext(&task, alias, (double)jtimeout, /*host*/ nullptr);
       })) {
     jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Process killed");
index 7102bb4..b917e08 100644 (file)
@@ -151,7 +151,7 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_VM_nativeMigration(JNIEnv* env, jobj
 {
   msg_vm_t vm = jvm_get_native(env,jvm);
   msg_host_t host = jhost_get_native(env, jhost);
 {
   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");
   }
     XBT_VERB("Caught exception during migration");
     jxbt_throw_host_failure(env, "during migration");
   }
index f9074bf..5a13a8a 100644 (file)
@@ -13,6 +13,7 @@
 #include <pybind11/stl.h>
 
 #include "src/kernel/context/Context.hpp"
 #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>
 #include <simgrid/s4u/Actor.hpp>
 #include <simgrid/s4u/Engine.hpp>
 #include <simgrid/s4u/Host.hpp>
@@ -56,8 +57,7 @@ PYBIND11_MODULE(simgrid, m)
   m.attr("simgrid_version") = simgrid_version;
 
   // Internal exception used to kill actors and sweep the RAII chimney (free objects living on the stack)
   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
 
   /* this_actor namespace */
   void (*sleep_for_fun)(double) = &simgrid::s4u::this_actor::sleep_for; // pick the right overload
index 51b492a..dedbf2c 100644 (file)
@@ -112,25 +112,6 @@ void Context::stop()
 
 AttachContext::~AttachContext() = default;
 
 
 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). */
 }}}
 
 /** @brief Executes all the processes to run (in parallel if possible). */
index cff4dbe..97b99e2 100644 (file)
@@ -86,43 +86,6 @@ public:
   virtual void attach_stop() = 0;
 };
 
   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 allows Java to hijack the context factory (Java induces factories of factory :) */
 typedef ContextFactory* (*ContextFactoryInitializer)();
index 1147740..9f40217 100644 (file)
@@ -3,6 +3,7 @@
 /* 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. */
 
 /* 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"
 #include "simgrid/modelchecker.h"
 #include "src/internal_config.h"
 #include "src/kernel/context/context_private.hpp"
index 2d6fcce..be6fbc9 100644 (file)
@@ -41,7 +41,7 @@ static void smx_ctx_wrapper(int i1, int i2)
   try {
     (*context)();
     context->Context::stop();
   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());
     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());
diff --git a/src/simgrid/Exception.cpp b/src/simgrid/Exception.cpp
new file mode 100644 (file)
index 0000000..cd4fc1f
--- /dev/null
@@ -0,0 +1,31 @@
+/* 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
index bcbe884..61d4cfb 100644 (file)
@@ -164,7 +164,7 @@ void ActorImpl::exit()
 
   // Forcefully kill the actor if its host is turned off. Not a HostFailureException because you should not survive that
   if (not host_->is_on())
 
   // 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) {
 
   /* destroy the blocking synchro if any */
   if (waiting_synchro != nullptr) {
index a87ac86..41db24d 100644 (file)
@@ -145,7 +145,7 @@ static void handler()
     std::abort();
   }
 
     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?");
     XBT_ERROR("Received a ForcefulKillException at the top-level exception handler. Maybe a Java->C++ call that is not "
               "protected "
               "in a try/catch?");
index 7183200..74880bc 100644 (file)
@@ -444,6 +444,7 @@ set(S4U_SRC
 )
 
 set(SIMGRID_SRC
 )
 
 set(SIMGRID_SRC
+  src/simgrid/Exception.cpp
   src/simgrid/sg_config.cpp
   src/simgrid/util.hpp
   )
   src/simgrid/sg_config.cpp
   src/simgrid/util.hpp
   )