Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Clean smx_ctx_java_stop, but it does not work yet
[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(void);
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 }
48
49 static smx_context_t smx_ctx_java_self(void)
50 {
51         return my_current_context;
52 }
53
54 static smx_context_t
55 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc,
56                                     char **argv,
57                                     void_pfn_smxprocess_t cleanup_func,
58                                     void* data)
59 {
60   XBT_DEBUG("XXXX Create Context\n");
61   smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
62
63   /* If the user provided a function for the process then use it
64      otherwise is the context for maestro */
65   if (code) {
66     context->super.cleanup_func = cleanup_func;
67     context->jprocess = (jobject) code;
68     context->jenv = get_current_thread_env();
69     jprocess_start(((smx_ctx_java_t) context)->jprocess,
70                    get_current_thread_env());
71   }else{
72     my_current_context = (smx_context_t)context;
73   }
74   context->super.data = data;
75   
76   return (smx_context_t) context;
77 }
78
79 static void smx_ctx_java_free(smx_context_t context)
80 {
81   if (context) {
82     smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
83
84     if (ctx_java->jprocess) { /* the java process still exists */
85       jobject jprocess = ctx_java->jprocess;
86       ctx_java->jprocess = NULL;
87
88       /* stop it */
89       XBT_DEBUG("The process still exists, making it exit now");
90       jprocess_exit(jprocess, get_current_thread_env());
91
92       /* it's dead now, remove it from the JVM */
93       jprocess_delete_global_ref(jprocess, get_current_thread_env());
94     }
95   }
96
97   smx_ctx_base_free(context);
98 }
99
100 void smx_ctx_java_stop(smx_context_t context)
101 {
102   xbt_assert(context == my_current_context,
103       "The context to stop must be the current one");
104   /* I am the current process and I am dying */
105   smx_ctx_base_stop(context);
106
107   XBT_DEBUG("I am dying");
108
109   /* suspend myself again, smx_ctx_java_free() will destroy me later
110    * from maeastro */
111   jprocess_unschedule(context);
112   xbt_die("This function was not supposed to return");
113 }
114
115 static void smx_ctx_java_suspend(smx_context_t context)
116 {
117   jprocess_unschedule(context);
118 }
119
120 // FIXME: inline those functions
121 static void smx_ctx_java_resume(smx_context_t new_context)
122 {
123   XBT_DEBUG("XXXX Context Resume\n");
124   jprocess_schedule(new_context);
125 }
126
127 static void smx_ctx_java_runall(void)
128 {
129   xbt_dynar_t processes = SIMIX_process_get_runnable();
130   XBT_DEBUG("XXXX Run all\n");
131   smx_process_t process;
132   smx_context_t old_context;
133   unsigned int cursor;
134   xbt_dynar_foreach(processes, cursor, process) {
135     old_context = my_current_context;
136     my_current_context = SIMIX_process_get_context(process);
137     smx_ctx_java_resume(my_current_context);
138     my_current_context = old_context;
139   }
140
141   XBT_DEBUG("XXXX End of run all\n");
142 }