Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a missing file to the archive
[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   /*FIXME: free(maestro_context->exception);*/
78   free(*factory);
79   *factory = NULL;
80
81   return 0;
82 }
83
84 static smx_context_t
85 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc, char** argv, 
86                                     void_f_pvoid_t cleanup_func, void* cleanup_arg)
87 {
88   smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
89
90   context->exception = xbt_new(ex_ctx_t, 1);
91   XBT_CTX_INITIALIZE(context->exception);
92
93   /* If the user provided a function for the process then use it
94      otherwise is the context for maestro */
95   if(code){
96     context->cleanup_func = cleanup_func;
97     context->cleanup_arg = cleanup_arg;
98     context->jprocess = (jobject) code;
99     context->jenv = get_current_thread_env();
100   }
101     
102   return (smx_context_t) context;
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
110     if (ctx_java->jprocess) {
111       jobject jprocess = ctx_java->jprocess;
112
113       ctx_java->jprocess = NULL;
114
115       /* if the java process is alive join it */
116       if (jprocess_is_alive(jprocess, get_current_thread_env()))
117         jprocess_join(jprocess, get_current_thread_env());
118     }
119
120     if (ctx_java->exception)
121       free(ctx_java->exception);
122
123     free(context);
124     context = NULL;
125   }
126 }
127
128 static void smx_ctx_java_start(smx_context_t context)
129 {
130   jprocess_start(((smx_ctx_java_t) context)->jprocess,
131                  get_current_thread_env());
132 }
133
134 static void smx_ctx_java_stop(smx_context_t context)
135 {
136   jobject jprocess = NULL;
137
138   smx_ctx_java_t ctx_java;
139
140   if (context->cleanup_func)
141     (*(context->cleanup_func)) (context->cleanup_arg);
142
143   ctx_java = (smx_ctx_java_t) context;
144
145   /*FIXME: is this really necessary?*/
146   if (simix_global->current_process->iwannadie) {
147     /* The maestro call xbt_context_stop() with an exit code set to one */
148     if (ctx_java->jprocess) {
149       /* if the java process is alive schedule it */
150       if (jprocess_is_alive(ctx_java->jprocess, get_current_thread_env())) {
151         jprocess_schedule(simix_global->current_process->context);
152         jprocess = ctx_java->jprocess;
153         ctx_java->jprocess = NULL;
154
155         /* interrupt the java process */
156         jprocess_exit(jprocess, get_current_thread_env());
157
158       }
159     }
160   } else {
161     /* the java process exits */
162     jprocess = ctx_java->jprocess;
163     ctx_java->jprocess = NULL;
164   }
165
166   /* delete the global reference associated with the java process */
167   jprocess_delete_global_ref(jprocess, get_current_thread_env());
168 }
169
170 /*static void smx_ctx_java_swap(smx_context_t context)
171 {
172   if (context) {
173     smx_context_t self = current_context;
174
175     current_context = context;
176
177     jprocess_schedule(context);
178
179     current_context = self;
180   }
181
182   if (current_context->iwannadie)
183     smx_ctx_java_stop(1);
184 }*/
185
186 static void smx_ctx_java_suspend(smx_context_t context)
187 {
188   jprocess_unschedule(context);
189 }
190
191 static void 
192 smx_ctx_java_resume(smx_context_t old_context, smx_context_t new_context)
193 {
194   jprocess_schedule(new_context);
195 }