Logo AND Algorithmique Numérique Distribuée

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