Logo AND Algorithmique Numérique Distribuée

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