This commit introduces C++ wrapper classes for Java heap objects.
[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.h"
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/string.hpp"
43
44 #include "vmcore/javaobjects.hpp"
45 #include "vmcore/utf8.h"
46
47
48 // Native functions are exported as C functions.
49 extern "C" {
50
51 /*
52  * Class:     java/lang/VMThread
53  * Method:    countStackFrames
54  * Signature: ()I
55  */
56 JNIEXPORT jint JNICALL Java_java_lang_VMThread_countStackFrames(JNIEnv *env, jobject _this)
57 {
58         log_println("Java_java_lang_VMThread_countStackFrames: Deprecated.  Not implemented.");
59
60     return 0;
61 }
62
63
64 /*
65  * Class:     java/lang/VMThread
66  * Method:    start
67  * Signature: (J)V
68  */
69 JNIEXPORT void JNICALL Java_java_lang_VMThread_start(JNIEnv *env, jobject _this, jlong stacksize)
70 {
71 #if defined(ENABLE_THREADS)
72         java_lang_VMThread jlvmt(_this);
73
74         threads_thread_start(jlvmt.get_thread());
75 #endif
76 }
77
78
79 /*
80  * Class:     java/lang/VMThread
81  * Method:    interrupt
82  * Signature: ()V
83  */
84 JNIEXPORT void JNICALL Java_java_lang_VMThread_interrupt(JNIEnv *env, jobject _this)
85 {
86 #if defined(ENABLE_THREADS)
87         java_handle_t *h;
88         threadobject  *t;
89
90         h = (java_handle_t *) _this;
91         t = thread_get_thread(h);
92
93         threads_thread_interrupt(t);
94 #endif
95 }
96
97
98 /*
99  * Class:     java/lang/VMThread
100  * Method:    isInterrupted
101  * Signature: ()Z
102  */
103 JNIEXPORT jboolean JNICALL Java_java_lang_VMThread_isInterrupted(JNIEnv *env, jobject _this)
104 {
105 #if defined(ENABLE_THREADS)
106         java_handle_t *h;
107         threadobject  *t;
108
109         h = (java_handle_t *) _this;
110         t = thread_get_thread(h);
111
112         return thread_is_interrupted(t);
113 #else
114         return 0;
115 #endif
116 }
117
118
119 /*
120  * Class:     java/lang/VMThread
121  * Method:    suspend
122  * Signature: ()V
123  */
124 JNIEXPORT void JNICALL Java_java_lang_VMThread_suspend(JNIEnv *env, jobject _this)
125 {
126 #if defined(ENABLE_THREADS)
127         log_println("Java_java_lang_VMThread_suspend: Deprecated.  Not implemented.");
128 #endif
129 }
130
131
132 /*
133  * Class:     java/lang/VMThread
134  * Method:    resume
135  * Signature: ()V
136  */
137 JNIEXPORT void JNICALL Java_java_lang_VMThread_resume(JNIEnv *env, jobject _this)
138 {
139 #if defined(ENABLE_THREADS)
140         log_println("Java_java_lang_VMThread_resume: Deprecated.  Not implemented.");
141 #endif
142 }
143
144
145 /*
146  * Class:     java/lang/VMThread
147  * Method:    nativeSetPriority
148  * Signature: (I)V
149  */
150 JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeSetPriority(JNIEnv *env, jobject _this, jint priority)
151 {
152 #if defined(ENABLE_THREADS)
153         java_handle_t *h;
154         threadobject  *t;
155
156         h = (java_handle_t *) _this;
157         t = thread_get_thread(h);
158
159         threads_set_thread_priority(t->tid, priority);
160 #endif
161 }
162
163
164 /*
165  * Class:     java/lang/VMThread
166  * Method:    nativeStop
167  * Signature: (Ljava/lang/Throwable;)V
168  */
169 JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeStop(JNIEnv *env, jobject _this, jobject t)
170 {
171 #if defined(ENABLE_THREADS)
172         log_println("Java_java_lang_VMThread_nativeStop: Deprecated.  Not implemented.");
173 #endif
174 }
175
176
177 /*
178  * Class:     java/lang/VMThread
179  * Method:    currentThread
180  * Signature: ()Ljava/lang/Thread;
181  */
182 JNIEXPORT jobject JNICALL Java_java_lang_VMThread_currentThread(JNIEnv *env, jclass clazz)
183 {
184         java_handle_t* h;
185
186         h = thread_get_current_object();
187
188         return (jobject) h;
189 }
190
191
192 /*
193  * Class:     java/lang/VMThread
194  * Method:    yield
195  * Signature: ()V
196  */
197 JNIEXPORT void JNICALL Java_java_lang_VMThread_yield(JNIEnv *env, jclass clazz)
198 {
199 #if defined(ENABLE_THREADS)
200         threads_yield();
201 #endif
202 }
203
204
205 /*
206  * Class:     java/lang/VMThread
207  * Method:    sleep
208  * Signature: (JI)V
209  */
210 JNIEXPORT void JNICALL Java_java_lang_VMThread_sleep(JNIEnv *env, jclass clazz, int64_t ms, int32_t ns)
211 {
212 #if defined(ENABLE_THREADS)
213         threads_sleep(ms, ns);
214 #endif
215 }
216
217
218 /*
219  * Class:     java/lang/VMThread
220  * Method:    interrupted
221  * Signature: ()Z
222  */
223 JNIEXPORT jboolean JNICALL Java_java_lang_VMThread_interrupted(JNIEnv *env, jclass clazz)
224 {
225 #if defined(ENABLE_THREADS)
226         threadobject *t;
227         int32_t       interrupted;
228
229         t = thread_get_current();
230
231         interrupted = thread_is_interrupted(t);
232
233         if (interrupted)
234                 thread_set_interrupted(t, false);
235
236         return interrupted;
237 #else
238         return 0;
239 #endif
240 }
241
242
243 /*
244  * Class:     java/lang/VMThread
245  * Method:    holdsLock
246  * Signature: (Ljava/lang/Object;)Z
247  */
248 JNIEXPORT jboolean JNICALL Java_java_lang_VMThread_holdsLock(JNIEnv *env, jclass clazz, jobject o)
249 {
250 #if defined(ENABLE_THREADS)
251         java_handle_t *h;
252
253         h = (java_handle_t *) o;
254
255         if (h == NULL) {
256                 exceptions_throw_nullpointerexception();
257                 return 0;
258         }
259
260         return lock_is_held_by_current_thread(h);
261 #else
262         return 0;
263 #endif
264 }
265
266
267 /*
268  * Class:     java/lang/VMThread
269  * Method:    getState
270  * Signature: ()Ljava/lang/String;
271  */
272 JNIEXPORT jstring JNICALL Java_java_lang_VMThread_getState(JNIEnv *env, jobject _this)
273 {
274 #if defined(ENABLE_THREADS)
275         java_handle_t *h;
276         threadobject  *t;
277         int            state;
278         utf           *u;
279         java_handle_t *o;
280
281         h = (java_handle_t *) _this;
282         t = thread_get_thread(h);
283
284         state = cacaothread_get_state(t);
285         
286         switch (state) {
287         case THREAD_STATE_NEW:
288                 u = utf_new_char("NEW");
289                 break;
290         case THREAD_STATE_RUNNABLE:
291                 u = utf_new_char("RUNNABLE");
292                 break;
293         case THREAD_STATE_BLOCKED:
294                 u = utf_new_char("BLOCKED");
295                 break;
296         case THREAD_STATE_WAITING:
297                 u = utf_new_char("WAITING");
298                 break;
299         case THREAD_STATE_TIMED_WAITING:
300                 u = utf_new_char("TIMED_WAITING");
301                 break;
302         case THREAD_STATE_TERMINATED:
303                 u = utf_new_char("TERMINATED");
304                 break;
305         default:
306                 vm_abort("Java_java_lang_VMThread_getState: unknown thread state %d", state);
307
308                 /* Keep compiler happy. */
309
310                 u = NULL;
311         }
312
313         o = javastring_new(u);
314
315         return (jstring) o;
316 #else
317         return NULL;
318 #endif
319 }
320
321 } // extern "C"
322
323
324 /* native methods implemented by this file ************************************/
325
326 static JNINativeMethod methods[] = {
327         { (char*) "countStackFrames",  (char*) "()I",                      (void*) (uintptr_t) &Java_java_lang_VMThread_countStackFrames  },
328         { (char*) "start",             (char*) "(J)V",                     (void*) (uintptr_t) &Java_java_lang_VMThread_start             },
329         { (char*) "interrupt",         (char*) "()V",                      (void*) (uintptr_t) &Java_java_lang_VMThread_interrupt         },
330         { (char*) "isInterrupted",     (char*) "()Z",                      (void*) (uintptr_t) &Java_java_lang_VMThread_isInterrupted     },
331         { (char*) "suspend",           (char*) "()V",                      (void*) (uintptr_t) &Java_java_lang_VMThread_suspend           },
332         { (char*) "resume",            (char*) "()V",                      (void*) (uintptr_t) &Java_java_lang_VMThread_resume            },
333         { (char*) "nativeSetPriority", (char*) "(I)V",                     (void*) (uintptr_t) &Java_java_lang_VMThread_nativeSetPriority },
334         { (char*) "nativeStop",        (char*) "(Ljava/lang/Throwable;)V", (void*) (uintptr_t) &Java_java_lang_VMThread_nativeStop        },
335         { (char*) "currentThread",     (char*) "()Ljava/lang/Thread;",     (void*) (uintptr_t) &Java_java_lang_VMThread_currentThread     },
336         { (char*) "yield",             (char*) "()V",                      (void*) (uintptr_t) &Java_java_lang_VMThread_yield             },
337         { (char*) "sleep",             (char*) "(JI)V",                    (void*) (uintptr_t) &Java_java_lang_VMThread_sleep             },
338         { (char*) "interrupted",       (char*) "()Z",                      (void*) (uintptr_t) &Java_java_lang_VMThread_interrupted       },
339         { (char*) "holdsLock",         (char*) "(Ljava/lang/Object;)Z",    (void*) (uintptr_t) &Java_java_lang_VMThread_holdsLock         },
340         { (char*) "getState",          (char*) "()Ljava/lang/String;",     (void*) (uintptr_t) &Java_java_lang_VMThread_getState          },
341 };
342
343
344 /* _Jv_java_lang_VMThread_init *************************************************
345
346    Register native functions.
347
348 *******************************************************************************/
349
350 // FIXME
351 extern "C" {
352 void _Jv_java_lang_VMThread_init(void)
353 {
354         utf *u;
355
356         u = utf_new_char("java/lang/VMThread");
357
358         native_method_register(u, methods, NATIVE_METHODS_COUNT);
359 }
360 }
361
362
363 /*
364  * These are local overrides for various environment variables in Emacs.
365  * Please do not remove this and leave it at the end of the file, where
366  * Emacs will automagically detect them.
367  * ---------------------------------------------------------------------
368  * Local variables:
369  * mode: c++
370  * indent-tabs-mode: t
371  * c-basic-offset: 4
372  * tab-width: 4
373  * End:
374  */