Logo AND Algorithmique Numérique Distribuée

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