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(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
125 s4 _Jv_java_lang_Thread_isAlive(java_lang_Thread *this)
127 #if defined(ENABLE_THREADS)
130 # if defined(WITH_CLASSPATH_GNU)
132 t = (threadobject *) LLNI_field_direct(LLNI_field_direct(this, vmThread), vmdata);
134 # elif defined(WITH_CLASSPATH_SUN)
136 /* XXX this is just a quick hack */
138 for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
139 if (t->object == this)
143 /* The threadobject is null when a thread is created in Java. The
144 priority is set later during startup. */
149 # elif defined(WITH_CLASSPATH_CLDC1_1)
151 t = (threadobject *) this->vm_thread;
157 # error unknown classpath configuration
160 return threads_thread_is_alive(t);
162 /* if threads are disabled, the only thread running is alive */
170 * Class: java/lang/Thread
171 * Method: isInterrupted
174 s4 _Jv_java_lang_Thread_isInterrupted(java_lang_Thread *this)
176 #if defined(ENABLE_THREADS)
179 # if defined(WITH_CLASSPATH_GNU)
180 t = (threadobject *) LLNI_field_direct(LLNI_field_direct(this, vmThread), vmdata);
181 # elif defined(WITH_CLASSPATH_SUN)
182 /* XXX this is just a quick hack */
184 for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
185 if (t->object == this)
188 # elif defined(WITH_CLASSPATH_CLDC1_1)
189 t = (threadobject *) this->vm_thread;
191 # error unknown classpath configuration
194 return threads_thread_has_been_interrupted(t);
202 * Class: java/lang/Thread
206 void _Jv_java_lang_Thread_suspend(java_lang_Thread *this)
208 #if defined(ENABLE_THREADS)
214 * Class: java/lang/Thread
218 void _Jv_java_lang_Thread_resume(java_lang_Thread *this)
220 #if defined(ENABLE_THREADS)
226 * Class: java/lang/Thread
227 * Method: setPriority
230 void _Jv_java_lang_Thread_setPriority(java_lang_Thread *this, s4 priority)
232 #if defined(ENABLE_THREADS)
235 # if defined(WITH_CLASSPATH_GNU)
236 t = (threadobject *) LLNI_field_direct(LLNI_field_direct(this, vmThread), vmdata);
237 # elif defined(WITH_CLASSPATH_SUN)
238 /* XXX this is just a quick hack */
240 for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
241 if (t->object == this)
245 /* The threadobject is null when a thread is created in Java. The
246 priority is set later during startup. */
250 # elif defined(WITH_CLASSPATH_CLDC1_1)
251 t = (threadobject *) this->vm_thread;
253 /* The threadobject is null when a thread is created in Java. The
254 priority is set later during startup. */
259 # error unknown classpath configuration
262 threads_set_thread_priority(t->tid, priority);
268 * Class: java/lang/Thread
270 * Signature: (Ljava/lang/Object;)V
272 void _Jv_java_lang_Thread_stop(java_lang_Thread *this, java_lang_Throwable *t)
274 #if defined(ENABLE_THREADS)
280 * Class: java/lang/Thread
281 * Method: currentThread
282 * Signature: ()Ljava/lang/Thread;
284 java_lang_Thread *_Jv_java_lang_Thread_currentThread(void)
286 #if defined(ENABLE_THREADS)
287 threadobject *thread;
291 #if defined(ENABLE_THREADS)
292 thread = THREADOBJECT;
297 log_text("t ptr is NULL\n");
299 # if defined(ENABLE_JAVASE)
300 if (LLNI_field_direct(t, group) == NULL) {
301 /* ThreadGroup of currentThread is not initialized */
303 LLNI_field_direct(t, group) = (java_lang_ThreadGroup *)
304 native_new_and_init(class_java_lang_ThreadGroup);
306 if (LLNI_field_direct(t, group) == NULL)
307 log_text("unable to create ThreadGroup");
311 /* we just return a fake java.lang.Thread object, otherwise we get
312 NullPointerException's in GNU classpath */
314 t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
322 * Class: java/lang/Thread
326 void _Jv_java_lang_Thread_yield(void)
328 #if defined(ENABLE_THREADS)
335 * Class: java/lang/Thread
336 * Method: interrupted
339 s4 _Jv_java_lang_Thread_interrupted(void)
341 #if defined(ENABLE_THREADS)
342 return threads_check_if_interrupted_and_reset();
350 * Class: java/lang/Thread
352 * Signature: (Ljava/lang/Object;)Z
354 s4 _Jv_java_lang_Thread_holdsLock(java_lang_Object* obj)
356 #if defined(ENABLE_THREADS)
359 o = (java_handle_t *) obj;
362 exceptions_throw_nullpointerexception();
366 return lock_is_held_by_current_thread(o);
374 * Class: java/lang/Thread
376 * Signature: ()Ljava/lang/String;
378 java_lang_String *_Jv_java_lang_Thread_getState(java_lang_Thread *this)
380 #if defined(ENABLE_THREADS)
381 threadobject *thread;
385 # if defined(WITH_CLASSPATH_GNU)
386 thread = (threadobject *) LLNI_field_direct(LLNI_field_direct(this, vmThread), vmdata);
387 # elif defined(WITH_CLASSPATH_CLDC1_1)
388 thread = (threadobject *) this->vm_thread;
391 u = threads_thread_get_state(thread);
392 o = javastring_new(u);
394 return (java_lang_String *) o;
402 * These are local overrides for various environment variables in Emacs.
403 * Please do not remove this and leave it at the end of the file, where
404 * Emacs will automagically detect them.
405 * ---------------------------------------------------------------------
408 * indent-tabs-mode: t