Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
"new ruby host method"
[simgrid.git] / src / java / jmsg_process.c
1 /*
2  * $Id$
3  *
4  * Copyright 2006,2007 Martin Quinson, Malek Cherier All right 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  * This contains the implementation of the functions in relation with the java
10  * process instance. 
11  */
12
13 #include "jmsg_process.h"
14 #include "jmsg.h"
15 #include "jxbt_utilities.h"
16
17 #include "simix/smx_context_java.h"
18
19 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
20
21 jobject jprocess_new_global_ref(jobject jprocess, JNIEnv * env)
22 {
23   return (*env)->NewGlobalRef(env, jprocess);
24 }
25
26 void jprocess_delete_global_ref(jobject jprocess, JNIEnv * env)
27 {
28   (*env)->DeleteGlobalRef(env, jprocess);
29 }
30
31 jboolean jprocess_is_alive(jobject jprocess, JNIEnv * env)
32 {
33   jmethodID id =
34     jxbt_get_smethod(env, "simgrid/msg/Process", "isAlive", "()Z");
35
36   if (!id)
37     return 0;
38
39   return (*env)->CallBooleanMethod(env, jprocess, id);
40 }
41
42 void jprocess_join(jobject jprocess, JNIEnv * env)
43 {
44   jmethodID id = jxbt_get_smethod(env, "simgrid/msg/Process", "join", "()V");
45
46   if (!id)
47     return;
48
49   (*env)->CallVoidMethod(env, jprocess, id);
50 }
51
52 void jprocess_exit(jobject jprocess, JNIEnv * env)
53 {
54   jmethodID id =
55     jxbt_get_smethod(env, "simgrid/msg/Process", "interrupt", "()V");
56
57   if (!id)
58     return;
59
60   (*env)->CallVoidMethod(env, jprocess, id);
61 }
62
63 void jprocess_yield(jobject jprocess, JNIEnv * env)
64 {
65   jmethodID id =
66     jxbt_get_smethod(env, "simgrid/msg/Process", "switchProcess", "()V");
67
68   if (!id)
69     return;
70
71   (*env)->CallVoidMethod(env, jprocess, id);
72 }
73
74 void jprocess_lock_mutex(jobject jprocess, JNIEnv * env)
75 {
76   jmethodID id =
77     jxbt_get_smethod(env, "simgrid/msg/Process", "lockMutex", "()V");
78
79   if (!id)
80     return;
81
82   (*env)->CallVoidMethod(env, jprocess, id);
83 }
84
85 void jprocess_unlock_mutex(jobject jprocess, JNIEnv * env)
86 {
87   jmethodID id =
88     jxbt_get_smethod(env, "simgrid/msg/Process", "unlockMutex", "()V");
89
90   if (!id)
91     return;
92
93   (*env)->CallVoidMethod(env, jprocess, id);
94 }
95
96
97 void jprocess_signal_cond(jobject jprocess, JNIEnv * env)
98 {
99   jmethodID id =
100     jxbt_get_smethod(env, "simgrid/msg/Process", "signalCond", "()V");
101
102   if (!id)
103     return;
104
105   (*env)->CallVoidMethod(env, jprocess, id);
106 }
107
108 void jprocess_wait_cond(jobject jprocess, JNIEnv * env)
109 {
110   jmethodID id =
111     jxbt_get_smethod(env, "simgrid/msg/Process", "waitCond", "()V");
112
113   if (!id)
114     return;
115
116   (*env)->CallVoidMethod(env, jprocess, id);
117 }
118
119
120 void jprocess_start(jobject jprocess, JNIEnv * env)
121 {
122   jmethodID id = jxbt_get_smethod(env, "simgrid/msg/Process", "start", "()V");
123
124   if (!id)
125     return;
126
127   DEBUG2("jprocess_start(jproc=%p,env=%p)", jprocess, env);
128   (*env)->CallVoidMethod(env, jprocess, id);
129   DEBUG0("jprocess started");
130 }
131
132 m_process_t jprocess_to_native_process(jobject jprocess, JNIEnv * env)
133 {
134   jfieldID id = jxbt_get_sfield(env, "simgrid/msg/Process", "bind", "J");
135
136   if (!id)
137     return NULL;
138
139   return (m_process_t) (long) (*env)->GetLongField(env, jprocess, id);
140 }
141
142 void jprocess_bind(jobject jprocess, m_process_t process, JNIEnv * env)
143 {
144   jfieldID id = jxbt_get_sfield(env, "simgrid/msg/Process", "bind", "J");
145
146   if (!id)
147     return;
148
149   (*env)->SetLongField(env, jprocess, id, (jlong) (long) (process));
150 }
151
152 jlong jprocess_get_id(jobject jprocess, JNIEnv * env)
153 {
154   jfieldID id = jxbt_get_sfield(env, "simgrid/msg/Process", "id", "J");
155
156   if (!id)
157     return 0;
158
159   return (*env)->GetLongField(env, jprocess, id);
160 }
161
162 jstring jprocess_get_name(jobject jprocess, JNIEnv * env)
163 {
164   jfieldID id =
165     jxbt_get_sfield(env, "simgrid/msg/Process", "name", "Ljava/lang/String;");
166   jobject jname;
167
168   if (!id)
169     return NULL;
170
171   jname = (jstring) (*env)->GetObjectField(env, jprocess, id);
172
173   return (*env)->NewGlobalRef(env, jname);
174
175 }
176
177 jboolean jprocess_is_valid(jobject jprocess, JNIEnv * env)
178 {
179   jfieldID id = jxbt_get_sfield(env, "simgrid/msg/Process", "bind", "J");
180
181   if (!id)
182     return JNI_FALSE;
183
184   return (*env)->GetLongField(env, jprocess, id) ? JNI_TRUE : JNI_FALSE;
185 }
186
187 void jprocess_schedule(smx_context_t context)
188 {
189   JNIEnv *env;
190   jmethodID id;
191
192   env = get_current_thread_env();
193
194   id = jxbt_get_smethod(env, "simgrid/msg/Process", "schedule", "()V");
195
196   if (!id)
197     return;
198
199   (*env)->CallVoidMethod(env, ((smx_ctx_java_t) context)->jprocess, id);
200 }
201
202
203
204 void jprocess_unschedule(smx_context_t context)
205 {
206   JNIEnv *env;
207   jmethodID id;
208
209   env = get_current_thread_env();
210
211
212   id = jxbt_get_smethod(env, "simgrid/msg/Process", "unschedule", "()V");
213
214   if (!id)
215     return;
216
217   (*env)->CallVoidMethod(env, ((smx_ctx_java_t) context)->jprocess, id);
218 }