Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Don't need private files without this command line
[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_stop(smx_context_t context);
29 static void smx_ctx_java_suspend(smx_context_t context);
30 static void smx_ctx_java_resume(smx_context_t new_context);
31 static void smx_ctx_java_runall(xbt_dynar_t processes);
32 static void* smx_ctx_java_get_data(smx_context_t context);
33
34 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory)
35 {
36   /* instantiate the context factory */
37   smx_ctx_base_factory_init(factory);
38
39   (*factory)->create_context = smx_ctx_java_factory_create_context;
40   /* Leave default behavior of (*factory)->finalize */
41   (*factory)->free = smx_ctx_java_free;
42   (*factory)->stop = smx_ctx_java_stop;
43   (*factory)->suspend = smx_ctx_java_suspend;
44   (*factory)->runall = smx_ctx_java_runall;
45   (*factory)->name = "ctx_java_factory";
46   //(*factory)->finalize = smx_ctx_base_factory_finalize;
47   (*factory)->self = smx_ctx_java_self;
48   (*factory)->get_data = smx_ctx_base_get_data;
49   (*factory)->get_thread_id = smx_ctx_base_get_thread_id;
50 }
51
52 static smx_context_t smx_ctx_java_self(void)
53 {
54         return my_current_context;
55 }
56
57 static void* smx_ctx_java_get_data(smx_context_t context)
58 {
59         return context->data;
60 }
61
62 static smx_context_t
63 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc,
64                                     char **argv,
65                                     void_pfn_smxprocess_t cleanup_func,
66                                     void* data)
67 {
68   DEBUG0("XXXX Create Context\n");
69   smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
70
71   /* If the user provided a function for the process then use it
72      otherwise is the context for maestro */
73   if (code) {
74     context->super.cleanup_func = cleanup_func;
75     context->jprocess = (jobject) code;
76     context->jenv = get_current_thread_env();
77     jprocess_start(((smx_ctx_java_t) context)->jprocess,
78                    get_current_thread_env());
79   }else{
80     my_current_context = (smx_context_t)context;
81   }
82   context->super.data = data;
83   
84   return (smx_context_t) context;
85 }
86
87 static void smx_ctx_java_free(smx_context_t context)
88 {
89   if (context) {
90     smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
91
92     if (ctx_java->jprocess) {
93       jobject jprocess = ctx_java->jprocess;
94
95       ctx_java->jprocess = NULL;
96
97       /* if the java process is alive join it */
98       if (jprocess_is_alive(jprocess, get_current_thread_env()))
99         jprocess_join(jprocess, get_current_thread_env());
100     }
101   }
102
103   smx_ctx_base_free(context);
104 }
105
106 static void smx_ctx_java_stop(smx_context_t context)
107 {
108   jobject jprocess = NULL;
109   DEBUG0("XXXX Context Stop\n");
110
111   smx_ctx_java_t ctx_java;
112
113   if (context->cleanup_func)
114     (*(context->cleanup_func)) (context->data);
115
116   ctx_java = (smx_ctx_java_t) context;
117
118   /*FIXME: is this really necessary? Seems to. */
119   if (my_current_context->iwannadie) {
120     INFO0("I wannadie");
121     /* The maestro call xbt_context_stop() with an exit code set to one */
122     if (ctx_java->jprocess) {
123       /* if the java process is alive schedule it */
124       if (jprocess_is_alive(ctx_java->jprocess, get_current_thread_env())) {
125         jprocess_schedule(my_current_context);
126         jprocess = ctx_java->jprocess;
127         ctx_java->jprocess = NULL;
128
129         /* interrupt the java process */
130         jprocess_exit(jprocess, get_current_thread_env());
131       }
132     }
133   } else {
134     /* the java process exits */
135     jprocess = ctx_java->jprocess;
136     ctx_java->jprocess = NULL;
137   }
138
139   /* delete the global reference associated with the java process */
140   jprocess_delete_global_ref(jprocess, get_current_thread_env());
141 }
142
143 static void smx_ctx_java_suspend(smx_context_t context)
144 {
145   jprocess_unschedule(context);
146 }
147
148 // FIXME: inline those functions
149 static void smx_ctx_java_resume(smx_context_t new_context)
150 {
151   DEBUG0("XXXX Context Resume\n");
152   jprocess_schedule(new_context);
153 }
154
155 static void smx_ctx_java_runall(xbt_dynar_t processes)
156 {
157   DEBUG0("XXXX Run all\n");
158   smx_process_t process;
159   smx_context_t old_context;
160   unsigned int cursor;
161
162   xbt_dynar_foreach(processes, cursor, process) {
163     old_context = my_current_context;
164     my_current_context = SIMIX_process_get_context(process);
165     smx_ctx_java_resume(my_current_context);
166     my_current_context = old_context;
167   }
168   xbt_dynar_reset(processes);
169
170   DEBUG0("XXXX End of run all\n");
171 }