#include "JavaContext.hpp"
#include "jxbt_utilities.hpp"
#include "simgrid/Exception.hpp"
+#include "src/kernel/actor/ActorImpl.hpp"
#include <functional>
#include <utility>
this->jenv_ = env;
}
-void JavaContext::stop_hook()
+void JavaContext::stop()
{
- 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");
- }
+ 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");
+ }
+
+ throw ForcefulKillException(); // clean RAII variables with the dedicated exception
}
}}} // namespace simgrid::kernel::context
JavaContext(std::function<void()>&& code, actor::ActorImpl* actor);
void start_hook() override;
- void stop_hook() override;
+ void stop() override;
};
class JavaContextFactory : public ContextFactory {
#include "mc/mc.h"
+#include "simgrid/Exception.hpp"
#include "simgrid/s4u/Host.hpp"
#include "src/kernel/activity/CommImpl.hpp"
#include "src/kernel/context/Context.hpp"
void Context::stop()
{
this->actor_->cleanup_from_self();
+ throw ForcefulKillException(); // clean RAII variables with the dedicated exception
}
void Context::set_wannadie(bool value)
#endif
try {
(*context)();
- context->Context::stop();
+ context->stop();
} catch (simgrid::ForcefulKillException const&) {
XBT_DEBUG("Caught a ForcefulKillException");
} catch (simgrid::Exception const& e) {
#endif
}
-void SwappedContext::stop()
-{
- Context::stop();
- /* We must cut the actor execution using an exception to properly free the C++ RAII variables */
- throw ForcefulKillException();
-}
-
void SwappedContext::swap_into(SwappedContext* to)
{
#if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT
void suspend() override;
virtual void resume();
- XBT_ATTRIB_NORETURN void stop() override;
void swap_into(SwappedContext* to);
try {
(*context)();
- if (not context->is_maestro()) { // Just in case somebody detached maestro
- context->Context::stop();
- context->stop_hook();
- }
+ if (not context->is_maestro()) // Just in case somebody detached maestro
+ context->stop();
} catch (ForcefulKillException const&) {
XBT_DEBUG("Caught a ForcefulKillException in Thread::wrapper");
xbt_assert(not context->is_maestro(), "Maestro shall not receive ForcefulKillExceptions, even when detached.");
this->end_.release();
}
-void ThreadContext::stop()
-{
- Context::stop();
- stop_hook();
- throw ForcefulKillException();
-}
-
void ThreadContext::suspend()
{
this->yield();
ThreadContext(const ThreadContext&) = delete;
ThreadContext& operator=(const ThreadContext&) = delete;
~ThreadContext() override;
- XBT_ATTRIB_NORETURN void stop() override;
void suspend() override;
void attach_start() override;
void attach_stop() override;
void yield(); // match a call to yield()
virtual void start_hook() { /* empty placeholder, called after start(). Used in parallel mode and Java */}
virtual void yield_hook() { /* empty placeholder, called before yield(). Used in parallel mode */}
- virtual void stop_hook() { /* empty placeholder, called at stop(). Used in Java */}
static void wrapper(ThreadContext* context);
};