Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ac2f9b64b24b5f44642fea56e9de7ae4392782bc
[simgrid.git] / src / smx_context_java.c
1 /* context_java - implementation of context switching for java threads */
2
3 /* Copyright (c) 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9
10 #include <xbt/function_types.h>
11 #include <simix/simix.h>
12 #include "smx_context_java.h"
13 #include "xbt/dynar.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(jmsg, bindings, "MSG for Java(TM)");
16
17 static smx_context_t my_current_context = NULL;
18
19 static smx_context_t smx_ctx_java_self(void);
20 static smx_context_t
21 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc,
22                                     char **argv,
23                                     void_pfn_smxprocess_t cleanup_func,
24                                     void *data);
25
26 static void smx_ctx_java_free(smx_context_t context);
27 static void smx_ctx_java_start(smx_context_t context);
28 static void smx_ctx_java_suspend(smx_context_t context);
29 static void smx_ctx_java_resume(smx_context_t new_context);
30 static void smx_ctx_java_runall(xbt_dynar_t processes);
31 static void* smx_ctx_java_get_data(smx_context_t context);
32
33 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory)
34 {
35   /* instantiate the context factory */
36   smx_ctx_base_factory_init(factory);
37
38   (*factory)->create_context = smx_ctx_java_factory_create_context;
39   /* Leave default behavior of (*factory)->finalize */
40   (*factory)->free = smx_ctx_java_free;
41   (*factory)->stop = smx_ctx_java_stop;
42   (*factory)->suspend = smx_ctx_java_suspend;
43   (*factory)->runall = smx_ctx_java_runall;
44   (*factory)->name = "ctx_java_factory";
45   //(*factory)->finalize = smx_ctx_base_factory_finalize;
46   (*factory)->self = smx_ctx_java_self;
47   (*factory)->get_data = smx_ctx_base_get_data;
48   (*factory)->get_thread_id = smx_ctx_base_get_thread_id;
49 }
50
51 static smx_context_t smx_ctx_java_self(void)
52 {
53         return my_current_context;
54 }
55
56 static void* smx_ctx_java_get_data(smx_context_t context)
57 {
58         return context->data;
59 }
60
61 static smx_context_t
62 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc,
63                                     char **argv,
64                                     void_pfn_smxprocess_t cleanup_func,
65                                     void* data)
66 {
67   XBT_DEBUG("XXXX Create Context\n");
68   smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
69
70   /* If the user provided a function for the process then use it
71      otherwise is the context for maestro */
72   if (code) {
73     context->super.cleanup_func = cleanup_func;
74     context->jprocess = (jobject) code;
75     context->jenv = get_current_thread_env();
76     jprocess_start(((smx_ctx_java_t) context)->jprocess,
77                    get_current_thread_env());
78   }else{
79     my_current_context = (smx_context_t)context;
80   }
81   context->super.data = data;
82   
83   return (smx_context_t) context;
84 }
85
86 static void smx_ctx_java_free(smx_context_t context)
87 {
88   if (context) {
89     smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
90
91     if (ctx_java->jprocess) {
92       jobject jprocess = ctx_java->jprocess;
93
94       ctx_java->jprocess = NULL;
95
96       /* if the java process is alive join it */
97       if (jprocess_is_alive(jprocess, get_current_thread_env()))
98         jprocess_join(jprocess, get_current_thread_env());
99     }
100   }
101
102   smx_ctx_base_free(context);
103 }
104
105 void smx_ctx_java_stop(smx_context_t context)
106 {
107   jobject jprocess = NULL;
108   XBT_DEBUG("XXXX Context Stop\n");
109
110   smx_ctx_base_stop(context);
111
112   smx_ctx_java_t ctx_java;
113
114   ctx_java = (smx_ctx_java_t) context;
115
116   /*FIXME: is this really necessary? Seems to. */
117   if (my_current_context->iwannadie) {
118     XBT_INFO("I wannadie");
119     /* The maestro call xbt_context_stop() with an exit code set to one */
120     if (ctx_java->jprocess) {
121       /* if the java process is alive schedule it */
122       if (jprocess_is_alive(ctx_java->jprocess, get_current_thread_env())) {
123         jprocess_schedule(my_current_context);
124         jprocess = ctx_java->jprocess;
125         ctx_java->jprocess = NULL;
126
127         /* interrupt the java process */
128         jprocess_exit(jprocess, get_current_thread_env());
129       }
130     }
131   } else {
132     /* the java process exits */
133     jprocess = ctx_java->jprocess;
134     ctx_java->jprocess = NULL;
135   }
136
137   /* delete the global reference associated with the java process */
138   jprocess_delete_global_ref(jprocess, get_current_thread_env());
139 }
140
141 static void smx_ctx_java_suspend(smx_context_t context)
142 {
143   jprocess_unschedule(context);
144 }
145
146 // FIXME: inline those functions
147 static void smx_ctx_java_resume(smx_context_t new_context)
148 {
149   XBT_DEBUG("XXXX Context Resume\n");
150   jprocess_schedule(new_context);
151 }
152
153 static void smx_ctx_java_runall(xbt_dynar_t processes)
154 {
155   XBT_DEBUG("XXXX Run all\n");
156   smx_process_t process;
157   smx_context_t old_context;
158   unsigned int cursor;
159
160   xbt_dynar_foreach(processes, cursor, process) {
161     old_context = my_current_context;
162     my_current_context = SIMIX_process_get_context(process);
163     smx_ctx_java_resume(my_current_context);
164     my_current_context = old_context;
165   }
166   xbt_dynar_reset(processes);
167
168   XBT_DEBUG("XXXX End of run all\n");
169 }