- /* I am the current process and I am dying */
- if (this->iwannadie) {
- this->iwannadie = 0;
- JNIEnv *env = get_current_thread_env();
- XBT_DEBUG("Gonna launch Killed Error");
- // TODO Adrien, if the process has not been created at the java layer, why should we raise the exception/error at the java level (this happens
- // for instance during the migration process that creates at the C level two processes: one on the SRC node and one on the DST node, if the DST process is killed.
- // it is not required to raise an exception at the JAVA level, the low level should be able to manage such an issue correctly but this is not the case right now unfortunately ...
- // TODO it will be nice to have the name of the process to help the end-user to know which Process has been killed
- // jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", bprintf("Process %s killed :) (file smx_context_java.c)", MSG_process_get_name( (msg_process_t)context) ));
- jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError",
- bprintf("Process %s killed :) (file JavaContext.cpp)",
- simcall_process_get_name((smx_process_t) SIMIX_context_get_process(this))) );
- XBT_DEBUG("Trigger a cancel error at the C level");
- THROWF(cancel_error, 0, "process cancelled");
- } else {
- Context::stop();
- /* detach the thread and kills it */
- JNIEnv *env = this->jenv;
- env->DeleteGlobalRef(this->jprocess);
- XBT_ATTRIB_UNUSED jint error = __java_vm->DetachCurrentThread();
- xbt_assert((error == JNI_OK), "The thread couldn't be detached.");
- xbt_os_sem_release(this->end);
- xbt_os_thread_exit(NULL);
+ 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");