Logo AND Algorithmique Numérique Distribuée

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