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