Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'fix_bindings_mistakenly_set_as_member_functions' into 'master'
[simgrid.git] / src / bindings / java / JavaContext.cpp
1 /* Context switching within the JVM.                                        */
2
3 /* Copyright (c) 2009-2022. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "JavaContext.hpp"
9 #include "jxbt_utilities.hpp"
10 #include "simgrid/Exception.hpp"
11 #include "src/kernel/actor/ActorImpl.hpp"
12
13 #include <functional>
14 #include <utility>
15
16 extern JavaVM* __java_vm;
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(java, "MSG for Java(TM)");
19
20 namespace simgrid {
21 namespace kernel {
22 namespace context {
23
24 ContextFactory* java_factory()
25 {
26   XBT_INFO("Using regular java threads.");
27   return new JavaContextFactory();
28 }
29
30 JavaContextFactory::JavaContextFactory() : ContextFactory()
31 {
32   xbt_assert(xbt::binary_name == "java");
33 }
34
35 JavaContextFactory::~JavaContextFactory()=default;
36
37 Context* JavaContextFactory::create_context(std::function<void()>&& code, actor::ActorImpl* actor)
38 {
39   return this->new_context<JavaContext>(std::move(code), actor);
40 }
41
42 void JavaContextFactory::run_all(std::vector<actor::ActorImpl*> const& actors)
43 {
44   SerialThreadContext::run_all(actors);
45 }
46
47 JavaContext::JavaContext(std::function<void()>&& code, smx_actor_t actor)
48     : SerialThreadContext(std::move(code), actor, false /* not maestro */)
49 {
50   /* ThreadContext already does all we need */
51 }
52
53 void JavaContext::start_hook()
54 {
55   Context::set_current(this); // We need to attach it also for maestro, in contrary to our ancestor
56
57   //Attach the thread to the JVM
58   JNIEnv *env;
59   xbt_assert(__java_vm->AttachCurrentThread((void**)&env, nullptr) == JNI_OK,
60              "The thread could not be attached to the JVM");
61   this->jenv_ = env;
62 }
63
64 void JavaContext::stop()
65 {
66   this->get_actor()->cleanup_from_self();
67
68   /* Unregister the thread from the JVM */
69   JNIEnv* env = this->jenv_;
70   env->DeleteGlobalRef(this->jprocess_);
71   jint error = __java_vm->DetachCurrentThread();
72   if (error != JNI_OK) {
73     /* This is probably a Java thread, ie an actor not created from the XML (and thus from the C++),
74      * but from Java with something like new Process().start().
75      *
76      * We should not even try to detach such threads. Instead, we throw a Java exception that will raise up
77      * until run_jprocess(), IIUC.
78      */
79     jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Process killed");
80     XBT_DEBUG("Cannot detach the current thread");
81   }
82
83   throw ForcefulKillException(); // clean RAII variables with the dedicated exception
84 }
85
86 }}} // namespace simgrid::kernel::context