Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
10fa3ea4f57208bb6b51d8e2058a0745e912d55f
[simgrid.git] / src / smx_context_java.c
1 /* context_java - implementation of context switching for java threads */
2
3 /* Copyright (c) 2009, 2010. 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
10 #include <xbt/function_types.h>
11 #include <simgrid/simix.h>
12 #include <xbt/ex.h>
13 #include "smx_context_java.h"
14 #include "jxbt_utilities.h"
15 #include "xbt/dynar.h"
16 JavaVM *get_current_vm(void);
17 JavaVM *get_current_vm(void)
18 {
19         JavaVM *jvm;
20         JNI_GetCreatedJavaVMs(&jvm,1,NULL);
21   return jvm;
22 }
23
24
25
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(jmsg, bindings, "MSG for Java(TM)");
27
28 static smx_context_t my_current_context = NULL;
29
30 static smx_context_t
31 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc,
32                                     char **argv,
33                                     void_pfn_smxprocess_t cleanup_func,
34                                     void *data);
35
36 static void smx_ctx_java_free(smx_context_t context);
37 static void smx_ctx_java_start(smx_context_t context);
38 static void smx_ctx_java_suspend(smx_context_t context);
39 static void smx_ctx_java_resume(smx_context_t new_context);
40 static void smx_ctx_java_runall(void);
41 static void* smx_ctx_java_thread_run(void *data);
42 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory)
43 {
44   /* instantiate the context factory */
45   smx_ctx_base_factory_init(factory);
46
47   (*factory)->create_context = smx_ctx_java_factory_create_context;
48   /* Leave default behavior of (*factory)->finalize */
49   (*factory)->free = smx_ctx_java_free;
50   (*factory)->stop = smx_ctx_java_stop;
51   (*factory)->suspend = smx_ctx_java_suspend;
52   (*factory)->runall = smx_ctx_java_runall;
53   (*factory)->name = "ctx_java_factory";
54   //(*factory)->finalize = smx_ctx_base_factory_finalize;
55   (*factory)->self = smx_ctx_java_self;
56   (*factory)->get_data = smx_ctx_base_get_data;
57 }
58 smx_context_t smx_ctx_java_self(void)
59 {
60         return my_current_context;
61 }
62
63 static smx_context_t
64 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc,
65                                     char **argv,
66                                     void_pfn_smxprocess_t cleanup_func,
67                                     void* data)
68 {
69         smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
70   /* If the user provided a function for the process then use it
71      otherwise is the context for maestro */
72   if (code) {
73                 if (argc == 0) {
74                         context->jprocess = (jobject) code;
75                 }
76                 else {
77                         context->jprocess = NULL;
78                 }
79                 context->super.cleanup_func = cleanup_func;
80                 context->begin = xbt_os_sem_init(0);
81                 context->end = xbt_os_sem_init(0);
82
83                 context->super.argc = argc;
84                 context->super.argv = argv;
85                 context->super.code = code;
86
87                 context->thread = xbt_os_thread_create(NULL,smx_ctx_java_thread_run,context,NULL);
88   }
89   else {
90         context->thread = NULL;
91     my_current_context = (smx_context_t)context;
92   }
93   context->super.data = data;
94   
95   return (smx_context_t) context;
96 }
97
98 static void* smx_ctx_java_thread_run(void *data) {
99         smx_ctx_java_t context = (smx_ctx_java_t)data;
100         //Attach the thread to the JVM
101         JNIEnv *env;
102         JavaVM *jvm = get_current_vm();
103   jint error = (*jvm)->AttachCurrentThread(jvm, (void **) &env, NULL);
104   xbt_assert((error == JNI_OK), "The thread could not be attached to the JVM");
105   context->jenv = get_current_thread_env();
106   //Wait for the first scheduling round to happen.
107   xbt_os_sem_acquire(context->begin);
108   //Create the "Process" object if needed.
109         if (context->super.argc > 0) {
110                 (*(context->super.code))(context->super.argc, context->super.argv);
111         }
112         xbt_assert((context->jprocess != NULL), "Process not created...");
113   //wait for the process to be able to begin
114   //TODO: Cache it
115         jfieldID jprocess_field_Process_startTime = jxbt_get_sfield(env, "org/simgrid/msg/Process", "startTime", "D");
116   jdouble startTime =  (*env)->GetDoubleField(env, context->jprocess, jprocess_field_Process_startTime);
117   if (startTime > MSG_get_clock()) {
118         MSG_process_sleep(startTime - MSG_get_clock());
119   }
120   //Execution of the "run" method.
121   jmethodID id = jxbt_get_smethod(env, "org/simgrid/msg/Process", "run", "()V");
122   xbt_assert( (id != NULL), "Method not found...");
123   (*env)->CallVoidMethod(env, context->jprocess, id);
124   smx_ctx_java_stop((smx_context_t)context);
125
126   return NULL;
127 }
128
129 static void smx_ctx_java_free(smx_context_t context)
130 {
131         if (context) {
132                 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
133                 if (ctx_java->thread) { /* We are not in maestro context */
134                         xbt_os_thread_join(ctx_java->thread, NULL);
135                         xbt_os_sem_destroy(ctx_java->begin);
136                         xbt_os_sem_destroy(ctx_java->end);
137                 }
138   }
139   smx_ctx_base_free(context);
140 }
141
142
143 void smx_ctx_java_stop(smx_context_t context)
144 {
145         smx_ctx_java_t ctx_java = (smx_ctx_java_t)context;
146         xbt_assert(context == my_current_context,
147      "The context to stop must be the current one");
148   /* I am the current process and I am dying */
149         if (context->iwannadie == -1) {
150         context->iwannadie = 0;
151         JNIEnv *env = get_current_thread_env();
152         jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", bprintf("Process killed :)"));
153         THROWF(cancel_error, 0, "process cancelled");
154   }
155   else {
156     smx_ctx_base_stop(context);
157                 /* detach the thread and kills it */
158                 JNIEnv *env = ctx_java->jenv;
159                 (*env)->DeleteGlobalRef(env,ctx_java->jprocess);
160                 JavaVM *jvm = get_current_vm();
161                 jint error = (*jvm)->DetachCurrentThread(jvm);
162                 xbt_assert((error == JNI_OK), "The thread couldn't be detached.");
163                 xbt_os_sem_release(((smx_ctx_java_t)context)->end);
164                 xbt_os_thread_exit(NULL);
165
166   }
167 }
168
169 static void smx_ctx_java_suspend(smx_context_t context)
170 {
171         smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
172         xbt_os_sem_release(ctx_java->end);
173         xbt_os_sem_acquire(ctx_java->begin);
174 }
175
176 // FIXME: inline those functions
177 static void smx_ctx_java_resume(smx_context_t new_context)
178 {
179   XBT_DEBUG("XXXX Context Resume\n");
180         smx_ctx_java_t ctx_java = (smx_ctx_java_t) new_context;
181         xbt_os_sem_release(ctx_java->begin);
182         xbt_os_sem_acquire(ctx_java->end);
183 }
184
185 static void smx_ctx_java_runall(void)
186 {
187   xbt_dynar_t processes = SIMIX_process_get_runnable();
188   XBT_DEBUG("XXXX Run all\n");
189   smx_process_t process;
190   smx_context_t old_context;
191   unsigned int cursor;
192   xbt_dynar_foreach(processes, cursor, process) {
193     old_context = my_current_context;
194     my_current_context = SIMIX_process_get_context(process);
195     smx_ctx_java_resume(my_current_context);
196     my_current_context = old_context;
197   }
198
199   XBT_DEBUG("XXXX End of run all\n");
200 }