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