Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2f63009980b627de5f62f10ed327e26fffb64846
[simgrid.git] / src / bindings / java / JavaContext.cpp
1 /* context_java - implementation of context switching for java threads */
2
3 /* Copyright (c) 2009-2010, 2012-2014. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include <functional>
10 #include <utility>
11
12 #include <xbt/function_types.h>
13 #include <simgrid/simix.h>
14 #include <xbt/ex.h>
15 #include <xbt/ex.hpp>
16 #include "JavaContext.hpp"
17 #include "jxbt_utilities.h"
18 #include "xbt/dynar.h"
19 #include "../../simix/smx_private.h"
20
21 extern JavaVM *__java_vm;
22
23 XBT_LOG_NEW_DEFAULT_CATEGORY(jmsg, "MSG for Java(TM)");
24
25 namespace simgrid {
26 namespace java {
27
28 simgrid::simix::ContextFactory* java_factory()
29 {
30   XBT_INFO("Using regular java threads.");
31   return new JavaContextFactory();
32 }
33
34 JavaContextFactory::JavaContextFactory(): ContextFactory("JavaContextFactory")
35 {
36 }
37
38 JavaContextFactory::~JavaContextFactory()=default;
39
40 JavaContext* JavaContextFactory::self()
41 {
42   return static_cast<JavaContext*>(xbt_os_thread_get_extra_data());
43 }
44
45 JavaContext* JavaContextFactory::create_context(
46   std::function<void()> code,
47   void_pfn_smxprocess_t cleanup, smx_process_t process)
48 {
49   return this->new_context<JavaContext>(std::move(code), cleanup, process);
50 }
51
52 void JavaContextFactory::run_all()
53 {
54   xbt_dynar_t processes = SIMIX_process_get_runnable();
55   smx_process_t process;
56   unsigned int cursor;
57   xbt_dynar_foreach(processes, cursor, process) {
58     static_cast<JavaContext*>(SIMIX_process_get_context(process))->resume();
59   }
60 }
61
62 JavaContext::JavaContext(std::function<void()> code,
63         void_pfn_smxprocess_t cleanup_func,
64         smx_process_t process)
65   : Context(std::move(code), cleanup_func, process)
66 {
67   static int thread_amount=0;
68   thread_amount++;
69
70   /* If the user provided a function for the process then use it otherwise is the context for maestro */
71   if (has_code()) {
72     this->jprocess = nullptr;
73     this->begin = xbt_os_sem_init(0);
74     this->end = xbt_os_sem_init(0);
75
76     try {
77        this->thread = xbt_os_thread_create(
78          nullptr, JavaContext::wrapper, this, nullptr);
79     }
80     catch (xbt_ex& ex) {
81       char* str = bprintf(
82         "Failed to create context #%d. You may want to switch to Java coroutines to increase your limits (error: %s)."
83         "See the Install section of simgrid-java documentation (in doc/install.html) for more on coroutines.",
84         thread_amount, ex.what());
85       xbt_ex new_exception(str);
86       new_exception.category = ex.category;
87       new_exception.value = ex.value;
88       new_exception.file = __FILE__;
89       new_exception.line = __LINE__;
90       new_exception.func = __func__;
91       std::throw_with_nested(std::move(new_exception));
92     }
93   } else {
94     this->thread = nullptr;
95     xbt_os_thread_set_extra_data(this);
96   }
97 }
98
99 JavaContext::~JavaContext()
100 {
101   if (this->thread) {
102     // We are not in maestro context
103     xbt_os_thread_join(this->thread, nullptr);
104     xbt_os_sem_destroy(this->begin);
105     xbt_os_sem_destroy(this->end);
106   }
107 }
108
109 void* JavaContext::wrapper(void *data)
110 {
111   JavaContext* context = static_cast<JavaContext*>(data);
112   xbt_os_thread_set_extra_data(context);
113   //Attach the thread to the JVM
114
115   JNIEnv *env;
116   XBT_ATTRIB_UNUSED jint error =
117     __java_vm->AttachCurrentThread((void **)&env, nullptr);
118   xbt_assert((error == JNI_OK), "The thread could not be attached to the JVM");
119   context->jenv = get_current_thread_env();
120   //Wait for the first scheduling round to happen.
121   xbt_os_sem_acquire(context->begin);
122   //Create the "Process" object if needed.
123   (*context)();
124   context->stop();
125   return nullptr;
126 }
127
128 void JavaContext::stop()
129 {
130   /* I am the current process and I am dying */
131   if (this->iwannadie) {
132     this->iwannadie = 0;
133     JNIEnv *env = get_current_thread_env();
134     XBT_DEBUG("Gonna launch Killed Error");
135     // 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
136     // 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.
137     // 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 ...
138     // TODO it will be nice to have the name of the process to help the end-user to know which Process has been killed
139    // 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) ));
140     jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError",
141       bprintf("Process %s killed :) (file JavaContext.cpp)",
142       simcall_process_get_name(this->process()) ));
143     XBT_DEBUG("Trigger a cancel error at the C level");
144     THROWF(cancel_error, 0, "process cancelled");
145   } else {
146     Context::stop();
147     /* detach the thread and kills it */
148     JNIEnv *env = this->jenv;
149     env->DeleteGlobalRef(this->jprocess);
150     XBT_ATTRIB_UNUSED jint error = __java_vm->DetachCurrentThread();
151     xbt_assert((error == JNI_OK), "The thread couldn't be detached.");
152     xbt_os_sem_release(this->end);
153     xbt_os_thread_exit(nullptr);
154   }
155 }
156
157 void JavaContext::suspend()
158 {
159   xbt_os_sem_release(this->end);
160   xbt_os_sem_acquire(this->begin);
161 }
162
163 // FIXME: inline those functions
164 void JavaContext::resume()
165 {
166   xbt_os_sem_release(this->begin);
167   xbt_os_sem_acquire(this->end);
168 }
169
170 }
171 }