1 /* src/native/vm/java_lang_Thread.c - java/lang/Thread functions
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
31 #include "native/jni.h"
32 #include "native/llni.h"
33 #include "native/native.h"
35 #include "native/include/java_lang_String.h"
36 #include "native/include/java_lang_Object.h" /* java_lang_Thread.h */
37 #include "native/include/java_lang_Throwable.h" /* java_lang_Thread.h */
38 #include "native/include/java_lang_Thread.h"
40 #if defined(ENABLE_JAVASE)
41 # include "native/include/java_lang_ThreadGroup.h"
43 # if defined(WITH_CLASSPATH_GNU)
44 # include "native/include/java_lang_VMThread.h"
48 #include "threads/lock-common.h"
49 #include "threads/threads-common.h"
51 #include "toolbox/logging.h"
53 #include "vm/builtin.h"
54 #include "vm/exceptions.h"
55 #include "vm/stringlocal.h"
57 #include "vmcore/options.h"
61 * Class: java/lang/Thread
62 * Method: countStackFrames
65 s4 _Jv_java_lang_Thread_countStackFrames(java_lang_Thread *this)
67 log_text("java_lang_Thread_countStackFrames called");
74 * Class: java/lang/Thread
78 void _Jv_java_lang_Thread_sleep(s8 millis)
80 #if defined(ENABLE_THREADS)
81 threads_sleep(millis, 0);
87 * Class: java/lang/Thread
91 void _Jv_java_lang_Thread_start(java_lang_Thread *this, s8 stacksize)
93 #if defined(ENABLE_THREADS)
94 threads_thread_start(this);
100 * Class: java/lang/Thread
104 void _Jv_java_lang_Thread_interrupt(java_lang_Thread *this)
106 #if defined(ENABLE_THREADS)
107 threadobject *thread;
109 #if defined(WITH_CLASSPATH_GNU)
110 thread = (threadobject *) LLNI_field_direct(this, vmThread)->vmdata;
111 #elif defined(WITH_CLASSPATH_CLDC1_1)
112 thread = (threadobject *) this->vm_thread;
115 threads_thread_interrupt(thread);
121 * Class: java/lang/Thread
122 * Method: isInterrupted
125 s4 _Jv_java_lang_Thread_isInterrupted(java_lang_Thread *this)
127 #if defined(ENABLE_THREADS)
130 # if defined(WITH_CLASSPATH_GNU)
131 t = (threadobject *) LLNI_field_direct(this, vmThread)->vmdata;
132 # elif defined(WITH_CLASSPATH_SUN)
133 /* XXX this is just a quick hack */
135 for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
136 if (t->object == this)
139 # elif defined(WITH_CLASSPATH_CLDC1_1)
140 t = (threadobject *) this->vm_thread;
142 # error unknown classpath configuration
145 return threads_thread_has_been_interrupted(t);
153 * Class: java/lang/Thread
157 void _Jv_java_lang_Thread_suspend(java_lang_Thread *this)
159 #if defined(ENABLE_THREADS)
165 * Class: java/lang/Thread
169 void _Jv_java_lang_Thread_resume(java_lang_Thread *this)
171 #if defined(ENABLE_THREADS)
177 * Class: java/lang/Thread
178 * Method: setPriority
181 void _Jv_java_lang_Thread_setPriority(java_lang_Thread *this, s4 priority)
183 #if defined(ENABLE_THREADS)
186 # if defined(WITH_CLASSPATH_GNU)
187 t = (threadobject *) LLNI_field_direct(this, vmThread)->vmdata;
188 # elif defined(WITH_CLASSPATH_SUN)
189 /* XXX this is just a quick hack */
191 for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
192 if (t->object == this)
196 /* The threadobject is null when a thread is created in Java. The
197 priority is set later during startup. */
201 # elif defined(WITH_CLASSPATH_CLDC1_1)
202 t = (threadobject *) this->vm_thread;
204 /* The threadobject is null when a thread is created in Java. The
205 priority is set later during startup. */
210 # error unknown classpath configuration
213 threads_set_thread_priority(t->tid, priority);
219 * Class: java/lang/Thread
221 * Signature: (Ljava/lang/Object;)V
223 void _Jv_java_lang_Thread_stop(java_lang_Thread *this, java_lang_Throwable *t)
225 #if defined(ENABLE_THREADS)
231 * Class: java/lang/Thread
232 * Method: currentThread
233 * Signature: ()Ljava/lang/Thread;
235 java_lang_Thread *_Jv_java_lang_Thread_currentThread(void)
237 #if defined(ENABLE_THREADS)
238 threadobject *thread;
242 #if defined(ENABLE_THREADS)
243 thread = THREADOBJECT;
245 t = LLNI_WRAP(thread->object);
248 log_text("t ptr is NULL\n");
250 # if defined(ENABLE_JAVASE)
251 if (LLNI_field_direct(t, group) == NULL) {
252 /* ThreadGroup of currentThread is not initialized */
254 LLNI_field_direct(t, group) = (java_lang_ThreadGroup *)
255 native_new_and_init(class_java_lang_ThreadGroup);
257 if (LLNI_field_direct(t, group) == NULL)
258 log_text("unable to create ThreadGroup");
262 /* we just return a fake java.lang.Thread object, otherwise we get
263 NullPointerException's in GNU classpath */
265 t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
273 * Class: java/lang/Thread
277 void _Jv_java_lang_Thread_yield(void)
279 #if defined(ENABLE_THREADS)
286 * Class: java/lang/Thread
287 * Method: interrupted
290 s4 _Jv_java_lang_Thread_interrupted(void)
292 #if defined(ENABLE_THREADS)
293 return threads_check_if_interrupted_and_reset();
301 * Class: java/lang/Thread
303 * Signature: (Ljava/lang/Object;)Z
305 s4 _Jv_java_lang_Thread_holdsLock(java_lang_Object* obj)
307 #if defined(ENABLE_THREADS)
310 o = (java_handle_t *) obj;
313 exceptions_throw_nullpointerexception();
317 return lock_is_held_by_current_thread(o);
325 * Class: java/lang/Thread
327 * Signature: ()Ljava/lang/String;
329 java_lang_String *_Jv_java_lang_Thread_getState(java_lang_Thread *this)
331 #if defined(ENABLE_THREADS)
332 threadobject *thread;
336 # if defined(WITH_CLASSPATH_GNU)
337 thread = (threadobject *) LLNI_field_direct(this, vmThread)->vmdata;
338 # elif defined(WITH_CLASSPATH_CLDC1_1)
339 thread = (threadobject *) this->vm_thread;
342 u = threads_thread_get_state(thread);
343 o = javastring_new(u);
345 return (java_lang_String *) o;
353 * These are local overrides for various environment variables in Emacs.
354 * Please do not remove this and leave it at the end of the file, where
355 * Emacs will automagically detect them.
356 * ---------------------------------------------------------------------
359 * indent-tabs-mode: t