From 7dea4b5d3be5500b5a2ffd92e09f767000549e19 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Wed, 23 Jan 2019 12:29:45 +0100 Subject: [PATCH] Improve process termination in Java world. --- src/bindings/java/JavaContext.cpp | 3 +-- src/bindings/java/JavaContext.hpp | 2 +- src/bindings/java/jmsg.cpp | 7 ------- src/kernel/context/ContextThread.cpp | 5 ++++- src/kernel/context/ContextThread.hpp | 1 + 5 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/bindings/java/JavaContext.cpp b/src/bindings/java/JavaContext.cpp index f65f7e61ad..7adaaaace8 100644 --- a/src/bindings/java/JavaContext.cpp +++ b/src/bindings/java/JavaContext.cpp @@ -62,7 +62,7 @@ void JavaContext::start_hook() this->jenv_ = env; } -void JavaContext::stop() +void JavaContext::stop_hook() { JNIEnv* env = this->jenv_; env->DeleteGlobalRef(this->jprocess_); @@ -78,7 +78,6 @@ void JavaContext::stop() // XBT_ERROR("Cannot detach the current thread"); // simgrid::xbt::Backtrace().display(); } - ThreadContext::stop(); } }}} // namespace simgrid::kernel::context diff --git a/src/bindings/java/JavaContext.hpp b/src/bindings/java/JavaContext.hpp index 108b2f2f59..f1e0570378 100644 --- a/src/bindings/java/JavaContext.hpp +++ b/src/bindings/java/JavaContext.hpp @@ -37,7 +37,7 @@ public: smx_actor_t process); void start_hook() override; - void stop() override; + void stop_hook() override; }; class JavaContextFactory : public simgrid::kernel::context::ContextFactory { diff --git a/src/bindings/java/jmsg.cpp b/src/bindings/java/jmsg.cpp index 1af0c84cfc..5e24128f7c 100644 --- a/src/bindings/java/jmsg.cpp +++ b/src/bindings/java/jmsg.cpp @@ -148,18 +148,11 @@ JNIEXPORT void JNICALL JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass for (auto const& elm : java_storage_map) jstorage_unref(env, elm.second); - // Don't even report the surviving threads, just to pass the tests... - exit(0); - /* Display the status of remaining threads. None should survive, but who knows */ jclass clsProcess = jxbt_get_class(env, "org/simgrid/msg/Process"); jmethodID idDebug = jxbt_get_static_jmethod(env, clsProcess, "debugAllThreads", "()V"); xbt_assert(idDebug != nullptr, "Method Process.debugAllThreads() not found..."); env->CallStaticVoidMethod(clsProcess, idDebug); - - /* FIXME: don't be of such an EXTREM BRUTALITY to stop the jvm. - * Sorry I don't get it working otherwise: some thread survive their own end, and I fail to do anything better */ - exit(0); } JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls, jstring jplatformFile) diff --git a/src/kernel/context/ContextThread.cpp b/src/kernel/context/ContextThread.cpp index cc2529a60a..171e32e62e 100644 --- a/src/kernel/context/ContextThread.cpp +++ b/src/kernel/context/ContextThread.cpp @@ -100,8 +100,10 @@ void *ThreadContext::wrapper(void *param) try { (*context)(); - if (not context->is_maestro()) // Just in case somebody detached maestro + if (not context->is_maestro()) { // Just in case somebody detached maestro context->Context::stop(); + context->stop_hook(); + } } catch (StopRequest const&) { XBT_DEBUG("Caught a StopRequest in Thread::wrapper"); xbt_assert(not context->is_maestro(), "Maestro shall not receive StopRequests, even when detached."); @@ -146,6 +148,7 @@ void ThreadContext::yield() void ThreadContext::stop() { Context::stop(); + stop_hook(); throw StopRequest(); } diff --git a/src/kernel/context/ContextThread.hpp b/src/kernel/context/ContextThread.hpp index 623aec4733..09ac695785 100644 --- a/src/kernel/context/ContextThread.hpp +++ b/src/kernel/context/ContextThread.hpp @@ -44,6 +44,7 @@ private: 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(void *param); }; -- 2.20.1