Merged revisions 7797-7917 via svnmerge from
[cacao.git] / src / native / vm / java_lang_Thread.c
1 /* src/native/vm/java_lang_Thread.c - java/lang/Thread functions
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02110-1301, USA.
24
25    $Id: java_lang_Thread.c 7833 2007-04-26 13:07:05Z twisti $
26
27 */
28
29
30 #include "config.h"
31 #include "vm/types.h"
32
33 #include "native/jni.h"
34 #include "native/native.h"
35
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"
40
41 #if defined(ENABLE_JAVASE)
42 # include "native/include/java_lang_ThreadGroup.h"
43 # include "native/include/java_lang_VMThread.h"
44 #endif
45
46 #include "threads/lock-common.h"
47 #include "threads/threads-common.h"
48
49 #include "toolbox/logging.h"
50
51 #include "vm/builtin.h"
52 #include "vm/exceptions.h"
53 #include "vm/stringlocal.h"
54
55 #include "vmcore/options.h"
56
57
58 /*
59  * Class:     java/lang/Thread
60  * Method:    countStackFrames
61  * Signature: ()I
62  */
63 s4 _Jv_java_lang_Thread_countStackFrames(java_lang_Thread *this)
64 {
65     log_text("java_lang_Thread_countStackFrames called");
66
67     return 0;
68 }
69
70
71 /*
72  * Class:     java/lang/Thread
73  * Method:    sleep
74  * Signature: (J)V
75  */
76 void _Jv_java_lang_Thread_sleep(s8 millis)
77 {
78 #if defined(ENABLE_THREADS)
79         threads_sleep(millis, 0);
80 #endif
81 }
82
83
84 /*
85  * Class:     java/lang/Thread
86  * Method:    start
87  * Signature: (J)V
88  */
89 void _Jv_java_lang_Thread_start(java_lang_Thread *this, s8 stacksize)
90 {
91 #if defined(ENABLE_THREADS)
92         threads_thread_start(this);
93 #endif
94 }
95
96
97 /*
98  * Class:     java/lang/Thread
99  * Method:    interrupt
100  * Signature: ()V
101  */
102 void _Jv_java_lang_Thread_interrupt(java_lang_Thread *this)
103 {
104 #if defined(ENABLE_THREADS)
105         threadobject *thread;
106
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;
111 #endif
112
113         threads_thread_interrupt(thread);
114 #endif
115 }
116
117
118 /*
119  * Class:     java/lang/Thread
120  * Method:    isAlive
121  * Signature: ()Z
122  */
123 s4 _Jv_java_lang_Thread_isAlive(java_lang_Thread *this)
124 {
125 #if defined(ENABLE_THREADS)
126         threadobject *thread;
127
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;
132 #endif
133
134         return threads_thread_is_alive(thread);
135 #else
136         /* if threads are disabled, the only thread running is alive */
137
138         return 1;
139 #endif
140 }
141
142
143 /*
144  * Class:     java/lang/Thread
145  * Method:    isInterrupted
146  * Signature: ()Z
147  */
148 s4 _Jv_java_lang_Thread_isInterrupted(java_lang_Thread *this)
149 {
150 #if defined(ENABLE_THREADS)
151         threadobject *thread;
152
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;
157 #endif
158
159         return threads_thread_has_been_interrupted(thread);
160 #else
161         return 0;
162 #endif
163 }
164
165
166 /*
167  * Class:     java/lang/Thread
168  * Method:    suspend
169  * Signature: ()V
170  */
171 void _Jv_java_lang_Thread_suspend(java_lang_Thread *this)
172 {
173 #if defined(ENABLE_THREADS)
174 #endif
175 }
176
177
178 /*
179  * Class:     java/lang/Thread
180  * Method:    resume
181  * Signature: ()V
182  */
183 void _Jv_java_lang_Thread_resume(java_lang_Thread *this)
184 {
185 #if defined(ENABLE_THREADS)
186 #endif
187 }
188
189
190 /*
191  * Class:     java/lang/Thread
192  * Method:    setPriority
193  * Signature: (I)V
194  */
195 void _Jv_java_lang_Thread_setPriority(java_lang_Thread *this, s4 priority)
196 {
197 #if defined(ENABLE_THREADS)
198         threadobject *thread;
199
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;
204
205         /* The threadobject is null when a thread is created in Java. The
206            priority is set later during startup. */
207
208         if (thread == NULL)
209                 return;
210 #endif
211
212         threads_set_thread_priority(thread->tid, priority);
213 #endif
214 }
215
216
217 /*
218  * Class:     java/lang/Thread
219  * Method:    stop
220  * Signature: (Ljava/lang/Object;)V
221  */
222 void _Jv_java_lang_Thread_stop(java_lang_Thread *this, java_lang_Throwable *t)
223 {
224 #if defined(ENABLE_THREADS)
225 #endif
226 }
227
228
229 /*
230  * Class:     java/lang/Thread
231  * Method:    currentThread
232  * Signature: ()Ljava/lang/Thread;
233  */
234 java_lang_Thread *_Jv_java_lang_Thread_currentThread(void)
235 {
236 #if defined(ENABLE_THREADS)
237         threadobject     *thread;
238 #endif
239         java_lang_Thread *t;
240
241 #if defined(ENABLE_THREADS)
242         thread = THREADOBJECT;
243
244         t = thread->object;
245
246         if (t == NULL)
247                 log_text("t ptr is NULL\n");
248
249 # if defined(ENABLE_JAVASE)
250         if (t->group == NULL) {
251                 /* ThreadGroup of currentThread is not initialized */
252
253                 t->group = (java_lang_ThreadGroup *)
254                         native_new_and_init(class_java_lang_ThreadGroup);
255
256                 if (t->group == NULL)
257                         log_text("unable to create ThreadGroup");
258         }
259 # endif
260 #else
261         /* we just return a fake java.lang.Thread object, otherwise we get
262        NullPointerException's in GNU classpath */
263
264         t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
265 #endif
266
267         return t;
268 }
269
270
271 /*
272  * Class:     java/lang/Thread
273  * Method:    yield
274  * Signature: ()V
275  */
276 void _Jv_java_lang_Thread_yield(void)
277 {
278 #if defined(ENABLE_THREADS)
279         threads_yield();
280 #endif
281 }
282
283
284 /*
285  * Class:     java/lang/Thread
286  * Method:    interrupted
287  * Signature: ()Z
288  */
289 s4 _Jv_java_lang_Thread_interrupted(void)
290 {
291 #if defined(ENABLE_THREADS)
292         return threads_check_if_interrupted_and_reset();
293 #else
294         return 0;
295 #endif
296 }
297
298
299 /*
300  * Class:     java/lang/Thread
301  * Method:    holdsLock
302  * Signature: (Ljava/lang/Object;)Z
303  */
304 s4 _Jv_java_lang_Thread_holdsLock(java_lang_Object* obj)
305 {
306 #if defined(ENABLE_THREADS)
307         java_objectheader *o;
308
309         o = (java_objectheader *) obj;
310
311         if (o == NULL) {
312                 exceptions_throw_nullpointerexception();
313                 return 0;
314         }
315
316         return lock_is_held_by_current_thread(o);
317 #else
318         return 0;
319 #endif
320 }
321
322
323 /*
324  * Class:     java/lang/Thread
325  * Method:    getState
326  * Signature: ()Ljava/lang/String;
327  */
328 java_lang_String *_Jv_java_lang_Thread_getState(java_lang_Thread *this)
329 {
330 #if defined(ENABLE_THREADS)
331         threadobject      *thread;
332         utf               *u;
333         java_objectheader *o;
334
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;
339 # endif
340
341         u = threads_thread_get_state(thread);
342         o = javastring_new(u);
343
344         return (java_lang_String *) o;
345 #else
346         return NULL;
347 #endif
348 }
349
350
351 /*
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  * ---------------------------------------------------------------------
356  * Local variables:
357  * mode: c
358  * indent-tabs-mode: t
359  * c-basic-offset: 4
360  * tab-width: 4
361  * End:
362  */