Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Stupid typo
[simgrid.git] / src / simix / smx_context_java.c
1 /* context_java - implementation of context switching for java threads */
2
3 /* Copyright (c) 2007-2008 the SimGrid team. All right reserved */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8
9 #include "xbt/function_types.h"
10 #include "smx_context_java.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(jmsg, bindings, "MSG for Java(TM)");
13
14 static smx_context_t
15 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc, char** argv, 
16                                     void_f_pvoid_t cleanup_func, void* cleanup_arg);
17
18 static void smx_ctx_java_free(smx_context_t context);
19 static void smx_ctx_java_start(smx_context_t context);
20 static void smx_ctx_java_stop(smx_context_t context);
21 static void smx_ctx_java_suspend(smx_context_t context);
22 static void smx_ctx_java_resume(smx_context_t new_context);
23
24 void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory)
25 {
26   /* instantiate the context factory */
27   smx_ctx_base_factory_init(factory);
28
29   (*factory)->create_context = smx_ctx_java_factory_create_context;
30   /* Leave default behavior of (*factory)->finalize */
31   (*factory)->free = smx_ctx_java_free;
32   (*factory)->stop = smx_ctx_java_stop;
33   (*factory)->suspend = smx_ctx_java_suspend;
34   (*factory)->resume = smx_ctx_java_resume;
35
36   (*factory)->name = "ctx_java_factory";
37 }
38
39 static smx_context_t
40 smx_ctx_java_factory_create_context(xbt_main_func_t code, int argc, char** argv, 
41                                     void_f_pvoid_t cleanup_func, void* cleanup_arg)
42 {
43   smx_ctx_java_t context = xbt_new0(s_smx_ctx_java_t, 1);
44
45   /* If the user provided a function for the process then use it
46      otherwise is the context for maestro */
47   if(code){
48     context->super.cleanup_func = cleanup_func;
49     context->super.cleanup_arg = cleanup_arg;
50     context->jprocess = (jobject) code;
51     context->jenv = get_current_thread_env();
52     jprocess_start(((smx_ctx_java_t) context)->jprocess,
53                    get_current_thread_env());
54   }
55     
56   return (smx_context_t) context;
57 }
58
59 static void smx_ctx_java_free(smx_context_t context)
60 {
61   if (context) {
62     smx_ctx_java_t ctx_java = (smx_ctx_java_t) context;
63
64     if (ctx_java->jprocess) {
65       jobject jprocess = ctx_java->jprocess;
66
67       ctx_java->jprocess = NULL;
68
69       /* if the java process is alive join it */
70       if (jprocess_is_alive(jprocess, get_current_thread_env()))
71         jprocess_join(jprocess, get_current_thread_env());
72     }
73   }
74
75   smx_ctx_base_free(context);
76
77
78 static void smx_ctx_java_stop(smx_context_t context)
79 {
80   jobject jprocess = NULL;
81
82   smx_ctx_java_t ctx_java;
83
84   if (context->cleanup_func)
85     (*(context->cleanup_func)) (context->cleanup_arg);
86
87   ctx_java = (smx_ctx_java_t) context;
88
89   /*FIXME: is this really necessary?*/
90   if (simix_global->current_process->iwannadie) {
91     /* The maestro call xbt_context_stop() with an exit code set to one */
92     if (ctx_java->jprocess) {
93       /* if the java process is alive schedule it */
94       if (jprocess_is_alive(ctx_java->jprocess, get_current_thread_env())) {
95         jprocess_schedule(simix_global->current_process->context);
96         jprocess = ctx_java->jprocess;
97         ctx_java->jprocess = NULL;
98
99         /* interrupt the java process */
100         jprocess_exit(jprocess, get_current_thread_env());
101
102       }
103     }
104   } else {
105     /* the java process exits */
106     jprocess = ctx_java->jprocess;
107     ctx_java->jprocess = NULL;
108   }
109
110   /* delete the global reference associated with the java process */
111   jprocess_delete_global_ref(jprocess, get_current_thread_env());
112 }
113
114 /*static void smx_ctx_java_swap(smx_context_t context)
115 {
116   if (context) {
117     smx_context_t self = current_context;
118
119     current_context = context;
120
121     jprocess_schedule(context);
122
123     current_context = self;
124   }
125
126   if (current_context->iwannadie)
127     smx_ctx_java_stop(1);
128 }*/
129
130 static void smx_ctx_java_suspend(smx_context_t context)
131 {
132   jprocess_unschedule(context);
133 }
134
135 // FIXME: inline those functions
136 static void 
137 smx_ctx_java_resume(smx_context_t new_context)
138 {
139   jprocess_schedule(new_context);
140 }