Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Lots of renamings from xbt_ to smx_
[simgrid.git] / src / simix / smx_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 = xbt_ctx_java_factory_create_maestro_context;
84   (*factory)->free = xbt_ctx_java_free;
85   (*factory)->kill = xbt_ctx_java_kill;
86   (*factory)->schedule = xbt_ctx_java_schedule;
87   (*factory)->yield = xbt_ctx_java_yield;
88   (*factory)->start = xbt_ctx_java_start;
89   (*factory)->stop = xbt_ctx_java_stop;
90   (*factory)->name = "ctx_java_factory";
91 }
92
93 static int
94 xbt_ctx_java_factory_create_maestro_context(xbt_context_t * maestro)
95 {
96   xbt_ctx_java_t context = xbt_new0(s_xbt_ctx_java_t, 1);
97
98   context->exception = xbt_new(ex_ctx_t, 1);
99   XBT_CTX_INITIALIZE(context->exception);
100
101   *maestro = (xbt_context_t) context;
102
103   return 0;
104 }
105
106 static int xbt_ctx_java_factory_finalize(xbt_context_factory_t * factory)
107 {
108   free(maestro_context->exception);
109   free(*factory);
110   *factory = NULL;
111
112   return 0;
113 }
114
115 static xbt_context_t
116 xbt_ctx_java_factory_create_context(const char *name, xbt_main_func_t code,
117                                     void_f_pvoid_t startup_func,
118                                     void *startup_arg,
119                                     void_f_pvoid_t cleanup_func,
120                                     void *cleanup_arg, int argc, char **argv)
121 {
122   xbt_ctx_java_t context = xbt_new0(s_xbt_ctx_java_t, 1);
123
124   context->name = xbt_strdup(name);
125   context->cleanup_func = cleanup_func;
126   context->cleanup_arg = cleanup_arg;
127   context->exception = xbt_new(ex_ctx_t, 1);
128   XBT_CTX_INITIALIZE(context->exception);
129   context->jprocess = (jobject) startup_arg;
130   context->jenv = get_current_thread_env();
131
132   return (xbt_context_t) context;
133 }
134
135 static void xbt_ctx_java_free(xbt_context_t context)
136 {
137   if (context) {
138     xbt_ctx_java_t ctx_java = (xbt_ctx_java_t) context;
139
140     free(ctx_java->name);
141
142     if (ctx_java->jprocess) {
143       jobject jprocess = ctx_java->jprocess;
144
145       ctx_java->jprocess = NULL;
146
147       /* if the java process is alive join it */
148       if (jprocess_is_alive(jprocess, get_current_thread_env()))
149         jprocess_join(jprocess, get_current_thread_env());
150     }
151
152     if (ctx_java->exception)
153       free(ctx_java->exception);
154
155     free(context);
156     context = NULL;
157   }
158 }
159
160 static void xbt_ctx_java_kill(xbt_context_t context)
161 {
162   context->iwannadie = 1;
163   xbt_ctx_java_swap(context);
164 }
165
166 /** 
167  * \param context the winner
168  *
169  * Calling this function blocks the current context and schedule \a context.  
170  * When \a context will call xbt_context_yield, it will return
171  * to this function as if nothing had happened.
172  * 
173  * Only the maestro can call this function to run a given process.
174  */
175 static void xbt_ctx_java_schedule(xbt_context_t context)
176 {
177   xbt_assert0((current_context == maestro_context),
178               "You are not supposed to run this function here!");
179   xbt_ctx_java_swap(context);
180 }
181
182 /** 
183  * Calling this function makes the current context yield. The context
184  * that scheduled it returns from xbt_context_schedule as if nothing
185  * had happened.
186  * 
187  * Only the processes can call this function, giving back the control
188  * to the maestro
189  */
190 static void xbt_ctx_java_yield(void)
191 {
192   xbt_assert0((current_context != maestro_context),
193               "You are not supposed to run this function here!");
194   jprocess_unschedule(current_context);
195 }
196
197 static void xbt_ctx_java_start(xbt_context_t context)
198 {
199   jprocess_start(((xbt_ctx_java_t) context)->jprocess,
200                  get_current_thread_env());
201 }
202
203 static void xbt_ctx_java_stop(int exit_code)
204 {
205   jobject jprocess = NULL;
206
207   xbt_ctx_java_t ctx_java;
208
209   if (current_context->cleanup_func)
210     (*(current_context->cleanup_func)) (current_context->cleanup_arg);
211
212   xbt_swag_remove(current_context, context_living);
213   xbt_swag_insert(current_context, context_to_destroy);
214
215   ctx_java = (xbt_ctx_java_t) current_context;
216
217   if (ctx_java->iwannadie) {
218     /* The maestro call xbt_context_stop() with an exit code set to one */
219     if (ctx_java->jprocess) {
220       /* if the java process is alive schedule it */
221       if (jprocess_is_alive(ctx_java->jprocess, get_current_thread_env())) {
222         jprocess_schedule(current_context);
223         jprocess = ctx_java->jprocess;
224         ctx_java->jprocess = NULL;
225
226         /* interrupt the java process */
227         jprocess_exit(jprocess, get_current_thread_env());
228
229       }
230     }
231   } else {
232     /* the java process exits */
233     jprocess = ctx_java->jprocess;
234     ctx_java->jprocess = NULL;
235   }
236
237   /* delete the global reference associated with the java process */
238   jprocess_delete_global_ref(jprocess, get_current_thread_env());
239 }
240
241 static void xbt_ctx_java_swap(xbt_context_t context)
242 {
243   if (context) {
244     xbt_context_t self = current_context;
245
246     current_context = context;
247
248     jprocess_schedule(context);
249
250     current_context = self;
251   }
252
253   if (current_context->iwannadie)
254     xbt_ctx_java_stop(1);
255 }