Logo AND Algorithmique Numérique Distribuée

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