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