Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove explicit conversion to std::string when it's not required.
[simgrid.git] / src / bindings / java / JavaContext.cpp
index f65f7e6..a9ea072 100644 (file)
@@ -1,6 +1,6 @@
 /* Context switching within the JVM.                                        */
 
-/* Copyright (c) 2009-2019. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2009-2022. 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. */
@@ -8,7 +8,7 @@
 #include "JavaContext.hpp"
 #include "jxbt_utilities.hpp"
 #include "simgrid/Exception.hpp"
-#include "src/simix/smx_private.hpp"
+#include "src/kernel/actor/ActorImpl.hpp"
 
 #include <functional>
 #include <utility>
@@ -17,36 +17,27 @@ extern JavaVM* __java_vm;
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(java, "MSG for Java(TM)");
 
-namespace simgrid {
-namespace kernel {
-namespace context {
-
-ContextFactory* java_factory()
-{
-  XBT_INFO("Using regular java threads.");
-  return new JavaContextFactory();
-}
+namespace simgrid::kernel::context {
 
 JavaContextFactory::JavaContextFactory() : ContextFactory()
 {
-  xbt_binary_name = xbt_strdup("java"); // Used by the backtrace displayer
+  xbt_assert(xbt::binary_name == "java");
 }
 
 JavaContextFactory::~JavaContextFactory()=default;
 
-JavaContext* JavaContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_fun,
-                                                smx_actor_t actor)
+Context* JavaContextFactory::create_context(std::function<void()>&& code, actor::ActorImpl* actor)
 {
-  return this->new_context<JavaContext>(std::move(code), cleanup_fun, actor);
+  return this->new_context<JavaContext>(std::move(code), actor);
 }
 
-void JavaContextFactory::run_all()
+void JavaContextFactory::run_all(std::vector<actor::ActorImpl*> const& actors)
 {
-  SerialThreadContext::run_all();
+  SerialThreadContext::run_all(actors);
 }
 
-JavaContext::JavaContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
-    : SerialThreadContext(std::move(code), cleanup_func, process, false /* not maestro */)
+JavaContext::JavaContext(std::function<void()>&& code, actor::ActorImpl* actor)
+    : SerialThreadContext(std::move(code), actor, false /* not maestro */)
 {
   /* ThreadContext already does all we need */
 }
@@ -57,28 +48,31 @@ void JavaContext::start_hook()
 
   //Attach the thread to the JVM
   JNIEnv *env;
-  XBT_ATTRIB_UNUSED jint error = __java_vm->AttachCurrentThread((void**)&env, nullptr);
-  xbt_assert((error == JNI_OK), "The thread could not be attached to the JVM");
+  xbt_assert(__java_vm->AttachCurrentThread((void**)&env, nullptr) == JNI_OK,
+             "The thread could not be attached to the JVM");
   this->jenv_ = env;
 }
 
 void JavaContext::stop()
 {
-    JNIEnv* env = this->jenv_;
-    env->DeleteGlobalRef(this->jprocess_);
-    XBT_ATTRIB_UNUSED jint error = __java_vm->DetachCurrentThread();
-    if (error != JNI_OK) {
-      /* This is probably a Java thread, ie an actor not created from the XML (and thus from the C++),
-       * but from Java with something like new Process().start().
-       *
-       * We should not even try to detach such threads. Instead, we throw a Java exception that will raise up
-       * until run_jprocess(), IIUC.
-       */
-      jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Process killed");
-      // XBT_ERROR("Cannot detach the current thread");
-      // simgrid::xbt::Backtrace().display();
-    }
-    ThreadContext::stop();
+  this->get_actor()->cleanup_from_self();
+
+  /* Unregister the thread from the JVM */
+  JNIEnv* env = this->jenv_;
+  env->DeleteGlobalRef(this->jprocess_);
+  jint error = __java_vm->DetachCurrentThread();
+  if (error != JNI_OK) {
+    /* This is probably a Java thread, ie an actor not created from the XML (and thus from the C++),
+     * but from Java with something like new Process().start().
+     *
+     * We should not even try to detach such threads. Instead, we throw a Java exception that will raise up
+     * until run_jprocess(), IIUC.
+     */
+    jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Process killed");
+    XBT_DEBUG("Cannot detach the current thread");
+  }
+
+  simgrid::ForcefulKillException::do_throw(); // clean RAII variables with the dedicated exception
 }
 
-}}} // namespace simgrid::kernel::context
+} // namespace simgrid::kernel::context