Logo AND Algorithmique Numérique Distribuée

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