Logo AND Algorithmique Numérique Distribuée

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