Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
83895766980cd46a1d1556d0780f4de0ff018135
[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 "private.h"
13 #include "smx_context_java.h"
14
15 XBT_LOG_NEW_DEFAULT_CATEGORY(jmsg, "MSG for Java(TM)");
16
17 static smx_context_t
18 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc, char** argv, 
19                                     void_f_pvoid_t cleanup_func, void* cleanup_arg);
20
21 static int smx_ctx_java_factory_finalize(smx_context_factory_t * factory);
22
23 static void smx_ctx_java_free(smx_context_t context);
24
25 static void smx_ctx_java_start(smx_context_t context);
26
27 static void smx_ctx_java_stop(smx_context_t context);
28
29 static void smx_ctx_java_suspend(smx_context_t context);
30
31 static void
32   smx_ctx_java_resume(smx_context_t old_context, smx_context_t new_context);
33
34 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory)
35 {
36   /* instantiate the context factory */
37   *factory = xbt_new0(s_smx_context_factory_t, 1);
38
39   (*factory)->create_context = smx_ctx_java_factory_create_context;
40   (*factory)->finalize = smx_ctx_java_factory_finalize;
41   (*factory)->free = smx_ctx_java_free;
42   (*factory)->start = smx_ctx_java_start;
43   (*factory)->stop = smx_ctx_java_stop;
44   (*factory)->suspend = smx_ctx_java_suspend;
45   (*factory)->resume = smx_ctx_java_resume;
46
47   (*factory)->name = "ctx_java_factory";
48 }
49
50 static int smx_ctx_java_factory_finalize(smx_context_factory_t * factory)
51 {
52   free(*factory);
53   *factory = NULL;
54   return 0;
55 }
56
57 static smx_context_t
58 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc, char** argv, 
59                                     void_f_pvoid_t cleanup_func, void* cleanup_arg)
60 {
61   smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
62
63   /* If the user provided a function for the process then use it
64      otherwise is the context for maestro */
65   if(code){
66     context->cleanup_func = cleanup_func;
67     context->cleanup_arg = cleanup_arg;
68     context->jprocess = (jobject) code;
69     context->jenv = get_current_thread_env();
70   }
71     
72   return (smx_context_t) context;
73 }
74
75 static void smx_ctx_java_free(smx_context_t context)
76 {
77   if (context) {
78     smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
79
80     if (ctx_java->jprocess) {
81       jobject jprocess = ctx_java->jprocess;
82
83       ctx_java->jprocess = NULL;
84
85       /* if the java process is alive join it */
86       if (jprocess_is_alive(jprocess, get_current_thread_env()))
87         jprocess_join(jprocess, get_current_thread_env());
88     }
89
90     free(context);
91     context = NULL;
92   }
93
94
95 static void smx_ctx_java_start(smx_context_t context)
96 {
97   jprocess_start(((smx_ctx_java_t) context)->jprocess,
98                  get_current_thread_env());
99 }
100
101 static void smx_ctx_java_stop(smx_context_t context)
102 {
103   jobject jprocess = NULL;
104
105   smx_ctx_java_t ctx_java;
106
107   if (context->cleanup_func)
108     (*(context->cleanup_func)) (context->cleanup_arg);
109
110   ctx_java = (smx_ctx_java_t) context;
111
112   /*FIXME: is this really necessary?*/
113   if (simix_global->current_process->iwannadie) {
114     /* The maestro call xbt_context_stop() with an exit code set to one */
115     if (ctx_java->jprocess) {
116       /* if the java process is alive schedule it */
117       if (jprocess_is_alive(ctx_java->jprocess, get_current_thread_env())) {
118         jprocess_schedule(simix_global->current_process->context);
119         jprocess = ctx_java->jprocess;
120         ctx_java->jprocess = NULL;
121
122         /* interrupt the java process */
123         jprocess_exit(jprocess, get_current_thread_env());
124
125       }
126     }
127   } else {
128     /* the java process exits */
129     jprocess = ctx_java->jprocess;
130     ctx_java->jprocess = NULL;
131   }
132
133   /* delete the global reference associated with the java process */
134   jprocess_delete_global_ref(jprocess, get_current_thread_env());
135 }
136
137 /*static void smx_ctx_java_swap(smx_context_t context)
138 {
139   if (context) {
140     smx_context_t self = current_context;
141
142     current_context = context;
143
144     jprocess_schedule(context);
145
146     current_context = self;
147   }
148
149   if (current_context->iwannadie)
150     smx_ctx_java_stop(1);
151 }*/
152
153 static void smx_ctx_java_suspend(smx_context_t context)
154 {
155   jprocess_unschedule(context);
156 }
157
158 static void 
159 smx_ctx_java_resume(smx_context_t old_context, smx_context_t new_context)
160 {
161   jprocess_schedule(new_context);
162 }