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