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 7833 2007-04-26 13:07:05Z twisti $
33 #include "native/jni.h"
34 #include "native/native.h"
36 #include "native/include/java_lang_String.h"
37 #include "native/include/java_lang_Object.h" /* java_lang_Thread.h */
38 #include "native/include/java_lang_Throwable.h" /* java_lang_Thread.h */
39 #include "native/include/java_lang_Thread.h"
41 #if defined(ENABLE_JAVASE)
42 # include "native/include/java_lang_ThreadGroup.h"
43 # include "native/include/java_lang_VMThread.h"
46 #include "threads/lock-common.h"
47 #include "threads/threads-common.h"
49 #include "toolbox/logging.h"
51 #include "vm/builtin.h"
52 #include "vm/exceptions.h"
53 #include "vm/stringlocal.h"
55 #include "vmcore/options.h"
59 * Class: java/lang/Thread
60 * Method: countStackFrames
63 s4 _Jv_java_lang_Thread_countStackFrames(java_lang_Thread *this)
65 log_text("java_lang_Thread_countStackFrames called");
72 * Class: java/lang/Thread
76 void _Jv_java_lang_Thread_sleep(s8 millis)
78 #if defined(ENABLE_THREADS)
79 threads_sleep(millis, 0);
85 * Class: java/lang/Thread
89 void _Jv_java_lang_Thread_start(java_lang_Thread *this, s8 stacksize)
91 #if defined(ENABLE_THREADS)
92 threads_thread_start(this);
98 * Class: java/lang/Thread
102 void _Jv_java_lang_Thread_interrupt(java_lang_Thread *this)
104 #if defined(ENABLE_THREADS)
105 threadobject *thread;
107 #if defined(WITH_CLASSPATH_GNU)
108 thread = (threadobject *) this->vmThread->vmdata;
109 #elif defined(WITH_CLASSPATH_CLDC1_1)
110 thread = (threadobject *) this->vm_thread;
113 threads_thread_interrupt(thread);
119 * Class: java/lang/Thread
123 s4 _Jv_java_lang_Thread_isAlive(java_lang_Thread *this)
125 #if defined(ENABLE_THREADS)
126 threadobject *thread;
128 #if defined(WITH_CLASSPATH_GNU)
129 thread = (threadobject *) this->vmThread->vmdata;
130 #elif defined(WITH_CLASSPATH_CLDC1_1)
131 thread = (threadobject *) this->vm_thread;
134 return threads_thread_is_alive(thread);
136 /* if threads are disabled, the only thread running is alive */
144 * Class: java/lang/Thread
145 * Method: isInterrupted
148 s4 _Jv_java_lang_Thread_isInterrupted(java_lang_Thread *this)
150 #if defined(ENABLE_THREADS)
151 threadobject *thread;
153 #if defined(WITH_CLASSPATH_GNU)
154 thread = (threadobject *) this->vmThread->vmdata;
155 #elif defined(WITH_CLASSPATH_CLDC1_1)
156 thread = (threadobject *) this->vm_thread;
159 return threads_thread_has_been_interrupted(thread);
167 * Class: java/lang/Thread
171 void _Jv_java_lang_Thread_suspend(java_lang_Thread *this)
173 #if defined(ENABLE_THREADS)
179 * Class: java/lang/Thread
183 void _Jv_java_lang_Thread_resume(java_lang_Thread *this)
185 #if defined(ENABLE_THREADS)
191 * Class: java/lang/Thread
192 * Method: setPriority
195 void _Jv_java_lang_Thread_setPriority(java_lang_Thread *this, s4 priority)
197 #if defined(ENABLE_THREADS)
198 threadobject *thread;
200 #if defined(WITH_CLASSPATH_GNU)
201 thread = (threadobject *) this->vmThread->vmdata;
202 #elif defined(WITH_CLASSPATH_CLDC1_1)
203 thread = (threadobject *) this->vm_thread;
205 /* The threadobject is null when a thread is created in Java. The
206 priority is set later during startup. */
212 threads_set_thread_priority(thread->tid, priority);
218 * Class: java/lang/Thread
220 * Signature: (Ljava/lang/Object;)V
222 void _Jv_java_lang_Thread_stop(java_lang_Thread *this, java_lang_Throwable *t)
224 #if defined(ENABLE_THREADS)
230 * Class: java/lang/Thread
231 * Method: currentThread
232 * Signature: ()Ljava/lang/Thread;
234 java_lang_Thread *_Jv_java_lang_Thread_currentThread(void)
236 #if defined(ENABLE_THREADS)
237 threadobject *thread;
241 #if defined(ENABLE_THREADS)
242 thread = THREADOBJECT;
247 log_text("t ptr is NULL\n");
249 # if defined(ENABLE_JAVASE)
250 if (t->group == NULL) {
251 /* ThreadGroup of currentThread is not initialized */
253 t->group = (java_lang_ThreadGroup *)
254 native_new_and_init(class_java_lang_ThreadGroup);
256 if (t->group == NULL)
257 log_text("unable to create ThreadGroup");
261 /* we just return a fake java.lang.Thread object, otherwise we get
262 NullPointerException's in GNU classpath */
264 t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
272 * Class: java/lang/Thread
276 void _Jv_java_lang_Thread_yield(void)
278 #if defined(ENABLE_THREADS)
285 * Class: java/lang/Thread
286 * Method: interrupted
289 s4 _Jv_java_lang_Thread_interrupted(void)
291 #if defined(ENABLE_THREADS)
292 return threads_check_if_interrupted_and_reset();
300 * Class: java/lang/Thread
302 * Signature: (Ljava/lang/Object;)Z
304 s4 _Jv_java_lang_Thread_holdsLock(java_lang_Object* obj)
306 #if defined(ENABLE_THREADS)
307 java_objectheader *o;
309 o = (java_objectheader *) obj;
312 exceptions_throw_nullpointerexception();
316 return lock_is_held_by_current_thread(o);
324 * Class: java/lang/Thread
326 * Signature: ()Ljava/lang/String;
328 java_lang_String *_Jv_java_lang_Thread_getState(java_lang_Thread *this)
330 #if defined(ENABLE_THREADS)
331 threadobject *thread;
333 java_objectheader *o;
335 # if defined(WITH_CLASSPATH_GNU)
336 thread = (threadobject *) this->vmThread->vmdata;
337 # elif defined(WITH_CLASSPATH_CLDC1_1)
338 thread = (threadobject *) this->vm_thread;
341 u = threads_thread_get_state(thread);
342 o = javastring_new(u);
344 return (java_lang_String *) o;
352 * These are local overrides for various environment variables in Emacs.
353 * Please do not remove this and leave it at the end of the file, where
354 * Emacs will automagically detect them.
355 * ---------------------------------------------------------------------
358 * indent-tabs-mode: t