Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not require doxygen in maintainer mode
[simgrid.git] / src / xbt / xbt_context_java.c
1 /* $Id$ */
2
3 /* context_java - implementation of context switching for java threads */
4
5 /* Copyright (c) 2007-2008 the SimGrid team. All right reserved */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10
11 #include "xbt/function_types.h"
12 #include "xbt/ex_interface.h"
13 #include "xbt/xbt_context_private.h"
14 #include "xbt/xbt_context_java.h"
15
16 XBT_LOG_NEW_DEFAULT_CATEGORY(jmsg, "MSG for Java(TM)");
17
18 /* callback: context fetching */
19 static ex_ctx_t *xbt_ctx_java_ex_ctx(void);
20
21 /* callback: termination */
22 static void xbt_ctx_java_ex_terminate(xbt_ex_t * e);
23
24 static xbt_context_t
25 xbt_ctx_java_factory_create_context(const char *name, xbt_main_func_t code,
26                                     void_f_pvoid_t startup_func,
27                                     void *startup_arg,
28                                     void_f_pvoid_t cleanup_func,
29                                     void *cleanup_arg, int argc, char **argv);
30
31 static int
32 xbt_ctx_java_factory_create_maestro_context(xbt_context_t * maestro);
33
34 static int xbt_ctx_java_factory_finalize(xbt_context_factory_t * factory);
35
36 static void xbt_ctx_java_free(xbt_context_t context);
37
38 static void xbt_ctx_java_kill(xbt_context_t context);
39
40 static void xbt_ctx_java_schedule(xbt_context_t context);
41
42 static void xbt_ctx_java_yield(void);
43
44 static void xbt_ctx_java_start(xbt_context_t context);
45
46 static void xbt_ctx_java_stop(int exit_code);
47
48 static void xbt_ctx_java_swap(xbt_context_t context);
49
50 static void xbt_ctx_java_schedule(xbt_context_t context);
51
52 static void xbt_ctx_java_yield(void);
53
54 static void xbt_ctx_java_suspend(xbt_context_t context);
55
56 static void xbt_ctx_java_resume(xbt_context_t context);
57
58
59 /* callback: context fetching */
60 static ex_ctx_t *xbt_ctx_java_ex_ctx(void)
61 {
62   return current_context->exception;
63 }
64
65 /* callback: termination */
66 static void xbt_ctx_java_ex_terminate(xbt_ex_t * e)
67 {
68   xbt_ex_display(e);
69   abort();
70 }
71
72 void xbt_ctx_java_factory_init(xbt_context_factory_t * factory)
73 {
74   /* context exception handlers */
75   __xbt_ex_ctx = xbt_ctx_java_ex_ctx;
76   __xbt_ex_terminate = xbt_ctx_java_ex_terminate;
77
78   /* instantiate the context factory */
79   *factory = xbt_new0(s_xbt_context_factory_t, 1);
80
81   (*factory)->create_context = xbt_ctx_java_factory_create_context;
82   (*factory)->finalize = xbt_ctx_java_factory_finalize;
83   (*factory)->create_maestro_context =
84     xbt_ctx_java_factory_create_maestro_context;
85   (*factory)->name = "ctx_java_factory";
86 }
87
88 static int
89 xbt_ctx_java_factory_create_maestro_context(xbt_context_t * maestro)
90 {
91   xbt_ctx_java_t context = xbt_new0(s_xbt_ctx_java_t, 1);
92
93   context->exception = xbt_new(ex_ctx_t, 1);
94   XBT_CTX_INITIALIZE(context->exception);
95
96   *maestro = (xbt_context_t) context;
97
98   return 0;
99 }
100
101 static int xbt_ctx_java_factory_finalize(xbt_context_factory_t * factory)
102 {
103   free(maestro_context->exception);
104   free(*factory);
105   *factory = NULL;
106
107   return 0;
108 }
109
110 static xbt_context_t
111 xbt_ctx_java_factory_create_context(const char *name, xbt_main_func_t code,
112                                     void_f_pvoid_t startup_func,
113                                     void *startup_arg,
114                                     void_f_pvoid_t cleanup_func,
115                                     void *cleanup_arg, int argc, char **argv)
116 {
117   xbt_ctx_java_t context = xbt_new0(s_xbt_ctx_java_t, 1);
118
119   context->name = xbt_strdup(name);
120
121   context->cleanup_func = cleanup_func;
122   context->cleanup_arg = cleanup_arg;
123
124   context->exception = xbt_new(ex_ctx_t, 1);
125   XBT_CTX_INITIALIZE(context->exception);
126
127   context->free = xbt_ctx_java_free;
128   context->kill = xbt_ctx_java_kill;
129   context->schedule = xbt_ctx_java_schedule;
130   context->yield = xbt_ctx_java_yield;
131   context->start = xbt_ctx_java_start;
132   context->stop = xbt_ctx_java_stop;
133   context->jprocess = (jobject) startup_arg;
134   context->jenv = get_current_thread_env();
135
136   return (xbt_context_t) context;
137 }
138
139 static void xbt_ctx_java_free(xbt_context_t context)
140 {
141   if (context) {
142     xbt_ctx_java_t ctx_java = (xbt_ctx_java_t) context;
143
144     free(ctx_java->name);
145
146     if (ctx_java->jprocess) {
147       jobject jprocess = ctx_java->jprocess;
148
149       ctx_java->jprocess = NULL;
150
151       /* if the java process is alive join it */
152       if (jprocess_is_alive(jprocess, get_current_thread_env()))
153         jprocess_join(jprocess, get_current_thread_env());
154     }
155
156     if (ctx_java->exception)
157       free(ctx_java->exception);
158
159     free(context);
160     context = NULL;
161   }
162 }
163
164 static void xbt_ctx_java_kill(xbt_context_t context)
165 {
166   context->iwannadie = 1;
167   xbt_ctx_java_swap(context);
168 }
169
170 /** 
171  * \param context the winner
172  *
173  * Calling this function blocks the current context and schedule \a context.  
174  * When \a context will call xbt_context_yield, it will return
175  * to this function as if nothing had happened.
176  * 
177  * Only the maestro can call this function to run a given process.
178  */
179 static void xbt_ctx_java_schedule(xbt_context_t context)
180 {
181   xbt_assert0((current_context == maestro_context),
182               "You are not supposed to run this function here!");
183   xbt_ctx_java_swap(context);
184 }
185
186 /** 
187  * Calling this function makes the current context yield. The context
188  * that scheduled it returns from xbt_context_schedule as if nothing
189  * had happened.
190  * 
191  * Only the processes can call this function, giving back the control
192  * to the maestro
193  */
194 static void xbt_ctx_java_yield(void)
195 {
196   xbt_assert0((current_context != maestro_context),
197               "You are not supposed to run this function here!");
198   jprocess_unschedule(current_context);
199 }
200
201 static void xbt_ctx_java_start(xbt_context_t context)
202 {
203   jprocess_start(((xbt_ctx_java_t) context)->jprocess,
204                  get_current_thread_env());
205 }
206
207 static void xbt_ctx_java_stop(int exit_code)
208 {
209   jobject jprocess = NULL;
210
211   xbt_ctx_java_t ctx_java;
212
213   if (current_context->cleanup_func)
214     (*(current_context->cleanup_func)) (current_context->cleanup_arg);
215
216   xbt_swag_remove(current_context, context_living);
217   xbt_swag_insert(current_context, context_to_destroy);
218
219   ctx_java = (xbt_ctx_java_t) current_context;
220
221   if (ctx_java->iwannadie) {
222     /* The maestro call xbt_context_stop() with an exit code set to one */
223     if (ctx_java->jprocess) {
224       /* if the java process is alive schedule it */
225       if (jprocess_is_alive(ctx_java->jprocess, get_current_thread_env())) {
226         jprocess_schedule(current_context);
227         jprocess = ctx_java->jprocess;
228         ctx_java->jprocess = NULL;
229
230         /* interrupt the java process */
231         jprocess_exit(jprocess, get_current_thread_env());
232
233       }
234     }
235   } else {
236     /* the java process exits */
237     jprocess = ctx_java->jprocess;
238     ctx_java->jprocess = NULL;
239   }
240
241   /* delete the global reference associated with the java process */
242   jprocess_delete_global_ref(jprocess, get_current_thread_env());
243 }
244
245 static void xbt_ctx_java_swap(xbt_context_t context)
246 {
247   if (context) {
248     xbt_context_t self = current_context;
249
250     current_context = context;
251
252     jprocess_schedule(context);
253
254     current_context = self;
255   }
256
257   if (current_context->iwannadie)
258     xbt_ctx_java_stop(1);
259 }