Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add an example to test starting_time and kill_time
[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
71   /* If the user provided a function for the process then use it
72      otherwise is the context for maestro */
73   if (code) {
74     context->super.cleanup_func = cleanup_func;
75     context->jprocess = (jobject) code;
76     context->begin = xbt_os_sem_init(0);
77     context->end = xbt_os_sem_init(0);
78     context->thread = xbt_os_thread_create(NULL,smx_ctx_java_thread_run,context,NULL);
79   }
80   else {
81         context->thread = NULL;
82     my_current_context = (smx_context_t)context;
83   }
84   context->super.data = data;
85   
86   return (smx_context_t) context;
87 }
88
89 static void* smx_ctx_java_thread_run(void *data) {
90         smx_ctx_java_t context = (smx_ctx_java_t)data;
91         //Attach the thread to the JVM
92         JNIEnv *env;
93         JavaVM *jvm = get_current_vm();
94   jint error = (*jvm)->AttachCurrentThread(jvm, (void **) &env, NULL);
95   xbt_assert((error == JNI_OK), "The thread could not be attached to the JVM");
96   context->jenv = get_current_thread_env();
97   //Wait for the first scheduling round to happen.
98   xbt_os_sem_acquire(context->begin);
99   //wait for the process to be able to begin
100   //TODO: Cache it
101         jfieldID jprocess_field_Process_startTime = jxbt_get_sfield(env, "org/simgrid/msg/Process", "startTime", "D");
102   jdouble startTime =  (*env)->GetDoubleField(env, context->jprocess, jprocess_field_Process_startTime);
103   if (startTime > MSG_get_clock()) {
104         MSG_process_sleep(startTime - MSG_get_clock());
105   }
106   //Execution of the "run" method.
107   jmethodID id = jxbt_get_smethod(env, "org/simgrid/msg/Process", "run", "()V");
108   xbt_assert( (id != NULL), "Method not found...");
109   (*env)->CallVoidMethod(env, context->jprocess, id);
110   smx_ctx_java_stop((smx_context_t)context);
111
112   return NULL;
113 }
114
115 static void smx_ctx_java_free(smx_context_t context)
116 {
117         if (context) {
118                 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
119                 if (ctx_java->thread) { /* We are not in maestro context */
120                         xbt_os_thread_join(ctx_java->thread, NULL);
121                         xbt_os_sem_destroy(ctx_java->begin);
122                         xbt_os_sem_destroy(ctx_java->end);
123                 }
124   }
125   smx_ctx_base_free(context);
126 }
127
128
129 void smx_ctx_java_stop(smx_context_t context)
130 {
131         smx_ctx_java_t ctx_java = (smx_ctx_java_t)context;
132         xbt_assert(context == my_current_context,
133      "The context to stop must be the current one");
134   /* I am the current process and I am dying */
135         if (context->iwannadie == -1) {
136         context->iwannadie = 0;
137         JNIEnv *env = get_current_thread_env();
138         jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", bprintf("Process killed :)"));
139         THROWF(cancel_error, 0, "process cancelled");
140   }
141   else {
142     smx_ctx_base_stop(context);
143                 /* detach the thread and kills it */
144                 JNIEnv *env = ctx_java->jenv;
145                 (*env)->DeleteGlobalRef(env,ctx_java->jprocess);
146                 JavaVM *jvm = get_current_vm();
147                 jint error = (*jvm)->DetachCurrentThread(jvm);
148                 xbt_assert((error == JNI_OK), "The thread couldn't be detached.");
149                 xbt_os_sem_release(((smx_ctx_java_t)context)->end);
150                 xbt_os_thread_exit(NULL);
151
152   }
153 }
154
155 static void smx_ctx_java_suspend(smx_context_t context)
156 {
157         smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
158         xbt_os_sem_release(ctx_java->end);
159         xbt_os_sem_acquire(ctx_java->begin);
160 }
161
162 // FIXME: inline those functions
163 static void smx_ctx_java_resume(smx_context_t new_context)
164 {
165   XBT_DEBUG("XXXX Context Resume\n");
166         smx_ctx_java_t ctx_java = (smx_ctx_java_t) new_context;
167         xbt_os_sem_release(ctx_java->begin);
168         xbt_os_sem_acquire(ctx_java->end);
169 }
170
171 static void smx_ctx_java_runall(void)
172 {
173   xbt_dynar_t processes = SIMIX_process_get_runnable();
174   XBT_DEBUG("XXXX Run all\n");
175   smx_process_t process;
176   smx_context_t old_context;
177   unsigned int cursor;
178   xbt_dynar_foreach(processes, cursor, process) {
179     old_context = my_current_context;
180     my_current_context = SIMIX_process_get_context(process);
181     smx_ctx_java_resume(my_current_context);
182     my_current_context = old_context;
183   }
184
185   XBT_DEBUG("XXXX End of run all\n");
186 }