* src/native/jni.h: Removed.
[cacao.git] / src / native / vm / gnuclasspath / java_lang_VMThread.cpp
1 /* src/native/vm/gnuclasspath/java_lang_VMThread.cpp
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <stdint.h>
29
30 #include "native/jni.hpp"
31 #include "native/llni.h"
32 #include "native/native.h"
33
34 #if defined(ENABLE_JNI_HEADERS)
35 # include "native/vm/include/java_lang_VMThread.h"
36 #endif
37
38 #include "threads/lock-common.h"
39 #include "threads/thread.hpp"
40
41 #include "vm/exceptions.hpp"
42 #include "vm/javaobjects.hpp"
43 #include "vm/string.hpp"
44 #include "vm/utf8.h"
45
46
47 // Native functions are exported as C functions.
48 extern "C" {
49
50 /*
51  * Class:     java/lang/VMThread
52  * Method:    countStackFrames
53  * Signature: ()I
54  */
55 JNIEXPORT jint JNICALL Java_java_lang_VMThread_countStackFrames(JNIEnv *env, jobject _this)
56 {
57         log_println("Java_java_lang_VMThread_countStackFrames: Deprecated.  Not implemented.");
58
59     return 0;
60 }
61
62
63 /*
64  * Class:     java/lang/VMThread
65  * Method:    start
66  * Signature: (J)V
67  */
68 JNIEXPORT void JNICALL Java_java_lang_VMThread_start(JNIEnv *env, jobject _this, jlong stacksize)
69 {
70 #if defined(ENABLE_THREADS)
71         java_lang_VMThread jlvmt(_this);
72
73         threads_thread_start(jlvmt.get_thread());
74 #endif
75 }
76
77
78 /*
79  * Class:     java/lang/VMThread
80  * Method:    interrupt
81  * Signature: ()V
82  */
83 JNIEXPORT void JNICALL Java_java_lang_VMThread_interrupt(JNIEnv *env, jobject _this)
84 {
85 #if defined(ENABLE_THREADS)
86         java_handle_t *h;
87         threadobject  *t;
88
89         h = (java_handle_t *) _this;
90         t = thread_get_thread(h);
91
92         threads_thread_interrupt(t);
93 #endif
94 }
95
96
97 /*
98  * Class:     java/lang/VMThread
99  * Method:    isInterrupted
100  * Signature: ()Z
101  */
102 JNIEXPORT jboolean JNICALL Java_java_lang_VMThread_isInterrupted(JNIEnv *env, jobject _this)
103 {
104 #if defined(ENABLE_THREADS)
105         java_handle_t *h;
106         threadobject  *t;
107
108         h = (java_handle_t *) _this;
109         t = thread_get_thread(h);
110
111         return thread_is_interrupted(t);
112 #else
113         return 0;
114 #endif
115 }
116
117
118 /*
119  * Class:     java/lang/VMThread
120  * Method:    suspend
121  * Signature: ()V
122  */
123 JNIEXPORT void JNICALL Java_java_lang_VMThread_suspend(JNIEnv *env, jobject _this)
124 {
125 #if defined(ENABLE_THREADS)
126         log_println("Java_java_lang_VMThread_suspend: Deprecated.  Not implemented.");
127 #endif
128 }
129
130
131 /*
132  * Class:     java/lang/VMThread
133  * Method:    resume
134  * Signature: ()V
135  */
136 JNIEXPORT void JNICALL Java_java_lang_VMThread_resume(JNIEnv *env, jobject _this)
137 {
138 #if defined(ENABLE_THREADS)
139         log_println("Java_java_lang_VMThread_resume: Deprecated.  Not implemented.");
140 #endif
141 }
142
143
144 /*
145  * Class:     java/lang/VMThread
146  * Method:    nativeSetPriority
147  * Signature: (I)V
148  */
149 JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeSetPriority(JNIEnv *env, jobject _this, jint priority)
150 {
151 #if defined(ENABLE_THREADS)
152         java_handle_t *h;
153         threadobject  *t;
154
155         h = (java_handle_t *) _this;
156         t = thread_get_thread(h);
157
158         threads_set_thread_priority(t->tid, priority);
159 #endif
160 }
161
162
163 /*
164  * Class:     java/lang/VMThread
165  * Method:    nativeStop
166  * Signature: (Ljava/lang/Throwable;)V
167  */
168 JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeStop(JNIEnv *env, jobject _this, jobject t)
169 {
170 #if defined(ENABLE_THREADS)
171         log_println("Java_java_lang_VMThread_nativeStop: Deprecated.  Not implemented.");
172 #endif
173 }
174
175
176 /*
177  * Class:     java/lang/VMThread
178  * Method:    currentThread
179  * Signature: ()Ljava/lang/Thread;
180  */
181 JNIEXPORT jobject JNICALL Java_java_lang_VMThread_currentThread(JNIEnv *env, jclass clazz)
182 {
183         java_handle_t* h;
184
185         h = thread_get_current_object();
186
187         return (jobject) h;
188 }
189
190
191 /*
192  * Class:     java/lang/VMThread
193  * Method:    yield
194  * Signature: ()V
195  */
196 JNIEXPORT void JNICALL Java_java_lang_VMThread_yield(JNIEnv *env, jclass clazz)
197 {
198 #if defined(ENABLE_THREADS)
199         threads_yield();
200 #endif
201 }
202
203
204 /*
205  * Class:     java/lang/VMThread
206  * Method:    sleep
207  * Signature: (JI)V
208  */
209 JNIEXPORT void JNICALL Java_java_lang_VMThread_sleep(JNIEnv *env, jclass clazz, int64_t ms, int32_t ns)
210 {
211 #if defined(ENABLE_THREADS)
212         threads_sleep(ms, ns);
213 #endif
214 }
215
216
217 /*
218  * Class:     java/lang/VMThread
219  * Method:    interrupted
220  * Signature: ()Z
221  */
222 JNIEXPORT jboolean JNICALL Java_java_lang_VMThread_interrupted(JNIEnv *env, jclass clazz)
223 {
224 #if defined(ENABLE_THREADS)
225         threadobject *t;
226         int32_t       interrupted;
227
228         t = thread_get_current();
229
230         interrupted = thread_is_interrupted(t);
231
232         if (interrupted)
233                 thread_set_interrupted(t, false);
234
235         return interrupted;
236 #else
237         return 0;
238 #endif
239 }
240
241
242 /*
243  * Class:     java/lang/VMThread
244  * Method:    holdsLock
245  * Signature: (Ljava/lang/Object;)Z
246  */
247 JNIEXPORT jboolean JNICALL Java_java_lang_VMThread_holdsLock(JNIEnv *env, jclass clazz, jobject o)
248 {
249 #if defined(ENABLE_THREADS)
250         java_handle_t *h;
251
252         h = (java_handle_t *) o;
253
254         if (h == NULL) {
255                 exceptions_throw_nullpointerexception();
256                 return 0;
257         }
258
259         return lock_is_held_by_current_thread(h);
260 #else
261         return 0;
262 #endif
263 }
264
265
266 /*
267  * Class:     java/lang/VMThread
268  * Method:    getState
269  * Signature: ()Ljava/lang/String;
270  */
271 JNIEXPORT jstring JNICALL Java_java_lang_VMThread_getState(JNIEnv *env, jobject _this)
272 {
273 #if defined(ENABLE_THREADS)
274         java_handle_t *h;
275         threadobject  *t;
276         int            state;
277         utf           *u;
278         java_handle_t *o;
279
280         h = (java_handle_t *) _this;
281         t = thread_get_thread(h);
282
283         state = cacaothread_get_state(t);
284         
285         switch (state) {
286         case THREAD_STATE_NEW:
287                 u = utf_new_char("NEW");
288                 break;
289         case THREAD_STATE_RUNNABLE:
290                 u = utf_new_char("RUNNABLE");
291                 break;
292         case THREAD_STATE_BLOCKED:
293                 u = utf_new_char("BLOCKED");
294                 break;
295         case THREAD_STATE_WAITING:
296                 u = utf_new_char("WAITING");
297                 break;
298         case THREAD_STATE_TIMED_WAITING:
299                 u = utf_new_char("TIMED_WAITING");
300                 break;
301         case THREAD_STATE_TERMINATED:
302                 u = utf_new_char("TERMINATED");
303                 break;
304         default:
305                 vm_abort("Java_java_lang_VMThread_getState: unknown thread state %d", state);
306
307                 /* Keep compiler happy. */
308
309                 u = NULL;
310         }
311
312         o = javastring_new(u);
313
314         return (jstring) o;
315 #else
316         return NULL;
317 #endif
318 }
319
320 } // extern "C"
321
322
323 /* native methods implemented by this file ************************************/
324
325 static JNINativeMethod methods[] = {
326         { (char*) "countStackFrames",  (char*) "()I",                      (void*) (uintptr_t) &Java_java_lang_VMThread_countStackFrames  },
327         { (char*) "start",             (char*) "(J)V",                     (void*) (uintptr_t) &Java_java_lang_VMThread_start             },
328         { (char*) "interrupt",         (char*) "()V",                      (void*) (uintptr_t) &Java_java_lang_VMThread_interrupt         },
329         { (char*) "isInterrupted",     (char*) "()Z",                      (void*) (uintptr_t) &Java_java_lang_VMThread_isInterrupted     },
330         { (char*) "suspend",           (char*) "()V",                      (void*) (uintptr_t) &Java_java_lang_VMThread_suspend           },
331         { (char*) "resume",            (char*) "()V",                      (void*) (uintptr_t) &Java_java_lang_VMThread_resume            },
332         { (char*) "nativeSetPriority", (char*) "(I)V",                     (void*) (uintptr_t) &Java_java_lang_VMThread_nativeSetPriority },
333         { (char*) "nativeStop",        (char*) "(Ljava/lang/Throwable;)V", (void*) (uintptr_t) &Java_java_lang_VMThread_nativeStop        },
334         { (char*) "currentThread",     (char*) "()Ljava/lang/Thread;",     (void*) (uintptr_t) &Java_java_lang_VMThread_currentThread     },
335         { (char*) "yield",             (char*) "()V",                      (void*) (uintptr_t) &Java_java_lang_VMThread_yield             },
336         { (char*) "sleep",             (char*) "(JI)V",                    (void*) (uintptr_t) &Java_java_lang_VMThread_sleep             },
337         { (char*) "interrupted",       (char*) "()Z",                      (void*) (uintptr_t) &Java_java_lang_VMThread_interrupted       },
338         { (char*) "holdsLock",         (char*) "(Ljava/lang/Object;)Z",    (void*) (uintptr_t) &Java_java_lang_VMThread_holdsLock         },
339         { (char*) "getState",          (char*) "()Ljava/lang/String;",     (void*) (uintptr_t) &Java_java_lang_VMThread_getState          },
340 };
341
342
343 /* _Jv_java_lang_VMThread_init *************************************************
344
345    Register native functions.
346
347 *******************************************************************************/
348
349 // FIXME
350 extern "C" {
351 void _Jv_java_lang_VMThread_init(void)
352 {
353         utf *u;
354
355         u = utf_new_char("java/lang/VMThread");
356
357         native_method_register(u, methods, NATIVE_METHODS_COUNT);
358 }
359 }
360
361
362 /*
363  * These are local overrides for various environment variables in Emacs.
364  * Please do not remove this and leave it at the end of the file, where
365  * Emacs will automagically detect them.
366  * ---------------------------------------------------------------------
367  * Local variables:
368  * mode: c++
369  * indent-tabs-mode: t
370  * c-basic-offset: 4
371  * tab-width: 4
372  * End:
373  */