Logo AND Algorithmique Numérique Distribuée

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