Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change the way the threads are launched: they are now launched as
[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   XBT_DEBUG("XXXX Create Context\n");
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->jenv = get_current_thread_env();
77     context->begin = xbt_os_sem_init(0);
78     context->end = xbt_os_sem_init(0);
79     context->thread = xbt_os_thread_create(NULL,smx_ctx_java_thread_run,context,NULL);
80   }
81   else {
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   (*jvm)->AttachCurrentThread(jvm, (void **) &env, NULL);
95         //Wait for the first scheduling round to happen.
96   xbt_os_sem_acquire(context->begin);
97   //Execution of the "run" method.
98   jmethodID id = jxbt_get_smethod(env, "org/simgrid/msg/Process", "run", "()V");
99   xbt_assert( (id != NULL), "Method not found...");
100   (*env)->CallVoidMethod(env, context->jprocess, id);
101
102   return NULL;
103 }
104
105 static void smx_ctx_java_free(smx_context_t context)
106 {
107   if (context) {
108                 smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
109                 if (ctx_java->jprocess) { /* the java process still exists */
110                         jobject jprocess = ctx_java->jprocess;
111                         ctx_java->jprocess = NULL;                       /* stop it */
112                         XBT_DEBUG("The process still exists, making it exit now");
113                         /* detach the thread and exit it */
114                         JavaVM *jvm = get_current_vm();
115                   (*jvm)->DetachCurrentThread(jvm);
116                   xbt_os_thread_exit(NULL);
117                         /* it's dead now, remove it from the JVM */
118                         jprocess_delete_global_ref(jprocess, get_current_thread_env());
119                 }
120   }
121   smx_ctx_base_free(context);
122 }
123
124
125 void smx_ctx_java_stop(smx_context_t context)
126 {
127  xbt_assert(context == my_current_context,
128      "The context to stop must be the current one");
129   /* I am the current process and I am dying */
130   
131   smx_ctx_base_stop(context);
132
133   XBT_DEBUG("I am dying");
134
135   /* suspend myself again, smx_ctx_java_free() will destroy me later
136    * from maeastro */
137   smx_ctx_java_suspend(context);
138   XBT_DEBUG("Java stop finished");
139 }
140
141 static void smx_ctx_java_suspend(smx_context_t context)
142 {
143         smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
144         xbt_os_sem_release(ctx_java->end);
145         xbt_os_sem_acquire(ctx_java->begin);
146   //jprocess_unschedule(context);
147 }
148
149 // FIXME: inline those functions
150 static void smx_ctx_java_resume(smx_context_t new_context)
151 {
152   XBT_DEBUG("XXXX Context Resume\n");
153         smx_ctx_java_t ctx_java = (smx_ctx_java_t) new_context;
154   xbt_os_sem_release(ctx_java->begin);
155         xbt_os_sem_acquire(ctx_java->end);
156   //jprocess_schedule(new_context);
157 }
158
159 static void smx_ctx_java_runall(void)
160 {
161   xbt_dynar_t processes = SIMIX_process_get_runnable();
162   XBT_DEBUG("XXXX Run all\n");
163   smx_process_t process;
164   smx_context_t old_context;
165   unsigned int cursor;
166   xbt_dynar_foreach(processes, cursor, process) {
167     old_context = my_current_context;
168     my_current_context = SIMIX_process_get_context(process);
169     smx_ctx_java_resume(my_current_context);
170     my_current_context = old_context;
171   }
172
173   XBT_DEBUG("XXXX End of run all\n");
174 }