Logo AND Algorithmique Numérique Distribuée

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