Logo AND Algorithmique Numérique Distribuée

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