Logo AND Algorithmique Numérique Distribuée

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