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
25 $Id: java_lang_Thread.c 8295 2007-08-11 17:57:24Z michi $
33 #include "native/jni.h"
34 #include "native/llni.h"
35 #include "native/native.h"
37 #include "native/include/java_lang_String.h"
38 #include "native/include/java_lang_Object.h" /* java_lang_Thread.h */
39 #include "native/include/java_lang_Throwable.h" /* java_lang_Thread.h */
40 #include "native/include/java_lang_Thread.h"
42 #if defined(ENABLE_JAVASE)
43 # include "native/include/java_lang_ThreadGroup.h"
45 # if defined(WITH_CLASSPATH_GNU)
46 # include "native/include/java_lang_VMThread.h"
50 #include "threads/lock-common.h"
51 #include "threads/threads-common.h"
53 #include "toolbox/logging.h"
55 #include "vm/builtin.h"
56 #include "vm/exceptions.h"
57 #include "vm/stringlocal.h"
59 #include "vmcore/options.h"
63 * Class: java/lang/Thread
64 * Method: countStackFrames
67 s4 _Jv_java_lang_Thread_countStackFrames(java_lang_Thread *this)
69 log_text("java_lang_Thread_countStackFrames called");
76 * Class: java/lang/Thread
80 void _Jv_java_lang_Thread_sleep(s8 millis)
82 #if defined(ENABLE_THREADS)
83 threads_sleep(millis, 0);
89 * Class: java/lang/Thread
93 void _Jv_java_lang_Thread_start(java_lang_Thread *this, s8 stacksize)
95 #if defined(ENABLE_THREADS)
96 threads_thread_start(this);
102 * Class: java/lang/Thread
106 void _Jv_java_lang_Thread_interrupt(java_lang_Thread *this)
108 #if defined(ENABLE_THREADS)
109 threadobject *thread;
111 #if defined(WITH_CLASSPATH_GNU)
112 thread = (threadobject *) LLNI_field_direct(LLNI_field_direct(this, vmThread), vmdata);
113 #elif defined(WITH_CLASSPATH_CLDC1_1)
114 thread = (threadobject *) this->vm_thread;
117 threads_thread_interrupt(thread);
123 * Class: java/lang/Thread
127 s4 _Jv_java_lang_Thread_isAlive(java_lang_Thread *this)
129 #if defined(ENABLE_THREADS)
132 # if defined(WITH_CLASSPATH_GNU)
134 t = (threadobject *) LLNI_field_direct(LLNI_field_direct(this, vmThread), vmdata);
136 # elif defined(WITH_CLASSPATH_SUN)
138 /* XXX this is just a quick hack */
140 for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
141 if (t->object == this)
145 /* The threadobject is null when a thread is created in Java. The
146 priority is set later during startup. */
151 # elif defined(WITH_CLASSPATH_CLDC1_1)
153 t = (threadobject *) this->vm_thread;
159 # error unknown classpath configuration
162 return threads_thread_is_alive(t);
164 /* if threads are disabled, the only thread running is alive */
172 * Class: java/lang/Thread
173 * Method: isInterrupted
176 s4 _Jv_java_lang_Thread_isInterrupted(java_lang_Thread *this)
178 #if defined(ENABLE_THREADS)
181 # if defined(WITH_CLASSPATH_GNU)
182 t = (threadobject *) LLNI_field_direct(LLNI_field_direct(this, vmThread), vmdata);
183 # elif defined(WITH_CLASSPATH_SUN)
184 /* XXX this is just a quick hack */
186 for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
187 if (t->object == this)
190 # elif defined(WITH_CLASSPATH_CLDC1_1)
191 t = (threadobject *) this->vm_thread;
193 # error unknown classpath configuration
196 return threads_thread_has_been_interrupted(t);
204 * Class: java/lang/Thread
208 void _Jv_java_lang_Thread_suspend(java_lang_Thread *this)
210 #if defined(ENABLE_THREADS)
216 * Class: java/lang/Thread
220 void _Jv_java_lang_Thread_resume(java_lang_Thread *this)
222 #if defined(ENABLE_THREADS)
228 * Class: java/lang/Thread
229 * Method: setPriority
232 void _Jv_java_lang_Thread_setPriority(java_lang_Thread *this, s4 priority)
234 #if defined(ENABLE_THREADS)
237 # if defined(WITH_CLASSPATH_GNU)
238 t = (threadobject *) LLNI_field_direct(LLNI_field_direct(this, vmThread), vmdata);
239 # elif defined(WITH_CLASSPATH_SUN)
240 /* XXX this is just a quick hack */
242 for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
243 if (t->object == this)
247 /* The threadobject is null when a thread is created in Java. The
248 priority is set later during startup. */
252 # elif defined(WITH_CLASSPATH_CLDC1_1)
253 t = (threadobject *) this->vm_thread;
255 /* The threadobject is null when a thread is created in Java. The
256 priority is set later during startup. */
261 # error unknown classpath configuration
264 threads_set_thread_priority(t->tid, priority);
270 * Class: java/lang/Thread
272 * Signature: (Ljava/lang/Object;)V
274 void _Jv_java_lang_Thread_stop(java_lang_Thread *this, java_lang_Throwable *t)
276 #if defined(ENABLE_THREADS)
282 * Class: java/lang/Thread
283 * Method: currentThread
284 * Signature: ()Ljava/lang/Thread;
286 java_lang_Thread *_Jv_java_lang_Thread_currentThread(void)
288 #if defined(ENABLE_THREADS)
289 threadobject *thread;
293 #if defined(ENABLE_THREADS)
294 thread = THREADOBJECT;
299 log_text("t ptr is NULL\n");
301 # if defined(ENABLE_JAVASE)
302 if (LLNI_field_direct(t, group) == NULL) {
303 /* ThreadGroup of currentThread is not initialized */
305 LLNI_field_direct(t, group) = (java_lang_ThreadGroup *)
306 native_new_and_init(class_java_lang_ThreadGroup);
308 if (LLNI_field_direct(t, group) == NULL)
309 log_text("unable to create ThreadGroup");
313 /* we just return a fake java.lang.Thread object, otherwise we get
314 NullPointerException's in GNU classpath */
316 t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
324 * Class: java/lang/Thread
328 void _Jv_java_lang_Thread_yield(void)
330 #if defined(ENABLE_THREADS)
337 * Class: java/lang/Thread
338 * Method: interrupted
341 s4 _Jv_java_lang_Thread_interrupted(void)
343 #if defined(ENABLE_THREADS)
344 return threads_check_if_interrupted_and_reset();
352 * Class: java/lang/Thread
354 * Signature: (Ljava/lang/Object;)Z
356 s4 _Jv_java_lang_Thread_holdsLock(java_lang_Object* obj)
358 #if defined(ENABLE_THREADS)
361 o = (java_handle_t *) obj;
364 exceptions_throw_nullpointerexception();
368 return lock_is_held_by_current_thread(o);
376 * Class: java/lang/Thread
378 * Signature: ()Ljava/lang/String;
380 java_lang_String *_Jv_java_lang_Thread_getState(java_lang_Thread *this)
382 #if defined(ENABLE_THREADS)
383 threadobject *thread;
387 # if defined(WITH_CLASSPATH_GNU)
388 thread = (threadobject *) LLNI_field_direct(LLNI_field_direct(this, vmThread), vmdata);
389 # elif defined(WITH_CLASSPATH_CLDC1_1)
390 thread = (threadobject *) this->vm_thread;
393 u = threads_thread_get_state(thread);
394 o = javastring_new(u);
396 return (java_lang_String *) o;
404 * These are local overrides for various environment variables in Emacs.
405 * Please do not remove this and leave it at the end of the file, where
406 * Emacs will automagically detect them.
407 * ---------------------------------------------------------------------
410 * indent-tabs-mode: t