Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ed5758bdbc0876f41e34794f00b0439d6c3792a7
[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 "private.h"
14 #include "smx_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 smx_context_t
25 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc, char** argv, 
26                                     void_f_pvoid_t cleanup_func, void* cleanup_arg);
27
28 static smx_context_t smx_ctx_java_factory_create_maestro_context(void);
29
30 static int smx_ctx_java_factory_finalize(smx_context_factory_t * factory);
31
32 static void smx_ctx_java_free(smx_context_t context);
33
34 static void smx_ctx_java_start(smx_context_t context);
35
36 static void smx_ctx_java_stop(smx_context_t context);
37
38 static void smx_ctx_java_suspend(smx_context_t context);
39
40 static void
41   smx_ctx_java_resume(smx_context_t old_context, smx_context_t new_context);
42
43
44 /* callback: context fetching */
45 static ex_ctx_t *xbt_ctx_java_ex_ctx(void)
46 {
47   return simix_global->current_process->context->exception;
48 }
49
50 /* callback: termination */
51 static void xbt_ctx_java_ex_terminate(xbt_ex_t * e)
52 {
53   xbt_ex_display(e);
54   abort();
55 }
56
57 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory)
58 {
59   /* context exception handlers */
60   __xbt_ex_ctx = xbt_ctx_java_ex_ctx;
61   __xbt_ex_terminate = xbt_ctx_java_ex_terminate;
62
63   /* instantiate the context factory */
64   *factory = xbt_new0(s_smx_context_factory_t, 1);
65
66   (*factory)->create_context = smx_ctx_java_factory_create_context;
67   (*factory)->finalize = smx_ctx_java_factory_finalize;
68   (*factory)->create_maestro_context = smx_ctx_java_factory_create_maestro_context;
69   (*factory)->free = smx_ctx_java_free;
70   (*factory)->start = smx_ctx_java_start;
71   (*factory)->stop = smx_ctx_java_stop;
72   (*factory)->suspend = smx_ctx_java_suspend;
73   (*factory)->resume = smx_ctx_java_resume;
74
75   (*factory)->name = "ctx_java_factory";
76 }
77
78 static smx_context_t smx_ctx_java_factory_create_maestro_context(void)
79 {
80   smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
81
82   context->exception = xbt_new(ex_ctx_t, 1);
83   XBT_CTX_INITIALIZE(context->exception);
84
85   return (smx_context_t) context;
86 }
87
88 static int smx_ctx_java_factory_finalize(smx_context_factory_t * factory)
89 {
90   /*FIXME: free(maestro_context->exception);*/
91   free(*factory);
92   *factory = NULL;
93
94   return 0;
95 }
96
97 static smx_context_t
98 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc, char** argv, 
99                                     void_f_pvoid_t cleanup_func, void* cleanup_arg)
100 {
101   smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
102
103   context->cleanup_func = cleanup_func;
104   context->cleanup_arg = cleanup_arg;
105   context->exception = xbt_new(ex_ctx_t, 1);
106   XBT_CTX_INITIALIZE(context->exception);
107   context->jprocess = (jobject) code;
108   context->jenv = get_current_thread_env();
109
110   return (smx_context_t) context;
111 }
112
113 static void smx_ctx_java_free(smx_context_t context)
114 {
115   if (context) {
116     smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
117
118     if (ctx_java->jprocess) {
119       jobject jprocess = ctx_java->jprocess;
120
121       ctx_java->jprocess = NULL;
122
123       /* if the java process is alive join it */
124       if (jprocess_is_alive(jprocess, get_current_thread_env()))
125         jprocess_join(jprocess, get_current_thread_env());
126     }
127
128     if (ctx_java->exception)
129       free(ctx_java->exception);
130
131     free(context);
132     context = NULL;
133   }
134 }
135
136 static void smx_ctx_java_start(smx_context_t context)
137 {
138   jprocess_start(((smx_ctx_java_t) context)->jprocess,
139                  get_current_thread_env());
140 }
141
142 static void smx_ctx_java_stop(smx_context_t context)
143 {
144   jobject jprocess = NULL;
145
146   smx_ctx_java_t ctx_java;
147
148   if (context->cleanup_func)
149     (*(context->cleanup_func)) (context->cleanup_arg);
150
151   ctx_java = (smx_ctx_java_t) context;
152
153   /*FIXME: is this really necessary?*/
154   if (simix_global->current_process->iwannadie) {
155     /* The maestro call xbt_context_stop() with an exit code set to one */
156     if (ctx_java->jprocess) {
157       /* if the java process is alive schedule it */
158       if (jprocess_is_alive(ctx_java->jprocess, get_current_thread_env())) {
159         jprocess_schedule(simix_global->current_process->context);
160         jprocess = ctx_java->jprocess;
161         ctx_java->jprocess = NULL;
162
163         /* interrupt the java process */
164         jprocess_exit(jprocess, get_current_thread_env());
165
166       }
167     }
168   } else {
169     /* the java process exits */
170     jprocess = ctx_java->jprocess;
171     ctx_java->jprocess = NULL;
172   }
173
174   /* delete the global reference associated with the java process */
175   jprocess_delete_global_ref(jprocess, get_current_thread_env());
176 }
177
178 /*static void smx_ctx_java_swap(smx_context_t context)
179 {
180   if (context) {
181     smx_context_t self = current_context;
182
183     current_context = context;
184
185     jprocess_schedule(context);
186
187     current_context = self;
188   }
189
190   if (current_context->iwannadie)
191     smx_ctx_java_stop(1);
192 }*/
193
194 static void smx_ctx_java_suspend(smx_context_t context)
195 {
196   jprocess_unschedule(context);
197 }
198
199 static void 
200 smx_ctx_java_resume(smx_context_t old_context, smx_context_t new_context)
201 {
202   jprocess_schedule(new_context);
203 }