aeba0e8ec163f9b1fe3957c69d4d33a941b28c62
[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_VMThread.c 6213 2006-12-18 17:36:06Z 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/threads-common.h"
47
48 #include "toolbox/logging.h"
49
50 #include "vm/builtin.h"
51 #include "vm/exceptions.h"
52 #include "vm/stringlocal.h"
53
54 #include "vmcore/options.h"
55
56
57 /*
58  * Class:     java/lang/Thread
59  * Method:    countStackFrames
60  * Signature: ()I
61  */
62 s4 _Jv_java_lang_Thread_countStackFrames(java_lang_Thread *this)
63 {
64     log_text("java_lang_Thread_countStackFrames called");
65
66     return 0;
67 }
68
69
70 /*
71  * Class:     java/lang/Thread
72  * Method:    sleep
73  * Signature: (J)V
74  */
75 void _Jv_java_lang_Thread_sleep(s8 millis)
76 {
77 #if defined(ENABLE_THREADS)
78         threads_sleep(millis, 0);
79 #endif
80 }
81
82
83 /*
84  * Class:     java/lang/Thread
85  * Method:    start
86  * Signature: (J)V
87  */
88 void _Jv_java_lang_Thread_start(java_lang_Thread *this, s8 stacksize)
89 {
90 #if defined(ENABLE_THREADS)
91         threads_start_javathread(this);
92 #endif
93 }
94
95
96 /*
97  * Class:     java/lang/Thread
98  * Method:    interrupt
99  * Signature: ()V
100  */
101 void _Jv_java_lang_Thread_interrupt(java_lang_Thread *this)
102 {
103 #if defined(ENABLE_THREADS)
104         threadobject *thread;
105
106 #if defined(WITH_CLASSPATH_GNU)
107         thread = (threadobject *) this->vmThread->vmdata;
108 #elif defined(WITH_CLASSPATH_CLDC1_1)
109         thread = (threadobject *) this->vm_thread;
110 #endif
111
112         threads_thread_interrupt(thread);
113 #endif
114 }
115
116
117 /*
118  * Class:     java/lang/Thread
119  * Method:    isAlive
120  * Signature: ()Z
121  */
122 s4 _Jv_java_lang_Thread_isAlive(java_lang_Thread *this)
123 {
124 #if defined(ENABLE_THREADS)
125         threadobject *thread;
126
127 #if defined(WITH_CLASSPATH_GNU)
128         thread = (threadobject *) this->vmThread->vmdata;
129 #elif defined(WITH_CLASSPATH_CLDC1_1)
130         thread = (threadobject *) this->vm_thread;
131 #endif
132
133         return threads_thread_is_alive(thread);
134 #else
135         /* if threads are disabled, the only thread running is alive */
136
137         return 1;
138 #endif
139 }
140
141
142 /*
143  * Class:     java/lang/Thread
144  * Method:    isInterrupted
145  * Signature: ()Z
146  */
147 s4 _Jv_java_lang_Thread_isInterrupted(java_lang_Thread *this)
148 {
149 #if defined(ENABLE_THREADS)
150         threadobject *thread;
151
152 #if defined(WITH_CLASSPATH_GNU)
153         thread = (threadobject *) this->vmThread->vmdata;
154 #elif defined(WITH_CLASSPATH_CLDC1_1)
155         thread = (threadobject *) this->vm_thread;
156 #endif
157
158         return threads_thread_has_been_interrupted(thread);
159 #else
160         return 0;
161 #endif
162 }
163
164
165 /*
166  * Class:     java/lang/Thread
167  * Method:    suspend
168  * Signature: ()V
169  */
170 void _Jv_java_lang_Thread_suspend(java_lang_Thread *this)
171 {
172 #if defined(ENABLE_THREADS)
173 #endif
174 }
175
176
177 /*
178  * Class:     java/lang/Thread
179  * Method:    resume
180  * Signature: ()V
181  */
182 void _Jv_java_lang_Thread_resume(java_lang_Thread *this)
183 {
184 #if defined(ENABLE_THREADS)
185 #endif
186 }
187
188
189 /*
190  * Class:     java/lang/Thread
191  * Method:    setPriority
192  * Signature: (I)V
193  */
194 void _Jv_java_lang_Thread_setPriority(java_lang_Thread *this, s4 priority)
195 {
196 #if defined(ENABLE_THREADS)
197         threadobject *thread;
198
199 #if defined(WITH_CLASSPATH_GNU)
200         thread = (threadobject *) this->vmThread->vmdata;
201 #elif defined(WITH_CLASSPATH_CLDC1_1)
202         thread = (threadobject *) this->vm_thread;
203
204         /* The threadobject is null when a thread is created in Java. The
205            priority is set later during startup. */
206
207         if (thread == NULL)
208                 return;
209 #endif
210
211         threads_set_thread_priority(thread->tid, priority);
212 #endif
213 }
214
215
216 /*
217  * Class:     java/lang/Thread
218  * Method:    stop
219  * Signature: (Ljava/lang/Object;)V
220  */
221 void _Jv_java_lang_Thread_stop(java_lang_Thread *this, java_lang_Throwable *t)
222 {
223 #if defined(ENABLE_THREADS)
224 #endif
225 }
226
227
228 /*
229  * Class:     java/lang/Thread
230  * Method:    currentThread
231  * Signature: ()Ljava/lang/Thread;
232  */
233 java_lang_Thread *_Jv_java_lang_Thread_currentThread(void)
234 {
235 #if defined(ENABLE_THREADS)
236         threadobject     *thread;
237 #endif
238         java_lang_Thread *t;
239
240 #if defined(ENABLE_THREADS)
241         thread = THREADOBJECT;
242
243         t = thread->object;
244
245         if (t == NULL)
246                 log_text("t ptr is NULL\n");
247
248 # if defined(ENABLE_JAVASE)
249         if (t->group == NULL) {
250                 /* ThreadGroup of currentThread is not initialized */
251
252                 t->group = (java_lang_ThreadGroup *)
253                         native_new_and_init(class_java_lang_ThreadGroup);
254
255                 if (t->group == NULL)
256                         log_text("unable to create ThreadGroup");
257         }
258 # endif
259 #else
260         /* we just return a fake java.lang.Thread object, otherwise we get
261        NullPointerException's in GNU classpath */
262
263         t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
264 #endif
265
266         return t;
267 }
268
269
270 /*
271  * Class:     java/lang/Thread
272  * Method:    yield
273  * Signature: ()V
274  */
275 void _Jv_java_lang_Thread_yield(void)
276 {
277 #if defined(ENABLE_THREADS)
278         threads_yield();
279 #endif
280 }
281
282
283 /*
284  * Class:     java/lang/Thread
285  * Method:    interrupted
286  * Signature: ()Z
287  */
288 s4 _Jv_java_lang_Thread_interrupted(void)
289 {
290 #if defined(ENABLE_THREADS)
291         return threads_check_if_interrupted_and_reset();
292 #else
293         return 0;
294 #endif
295 }
296
297
298 /*
299  * Class:     java/lang/Thread
300  * Method:    holdsLock
301  * Signature: (Ljava/lang/Object;)Z
302  */
303 s4 _Jv_java_lang_Thread_holdsLock(java_lang_Object* obj)
304 {
305 #if defined(ENABLE_THREADS)
306         java_objectheader *o;
307
308         o = (java_objectheader *) obj;
309
310         if (o == NULL) {
311                 exceptions_throw_nullpointerexception();
312                 return 0;
313         }
314
315         return lock_is_held_by_current_thread(o);
316 #else
317         return 0;
318 #endif
319 }
320
321
322 /*
323  * Class:     java/lang/Thread
324  * Method:    getState
325  * Signature: ()Ljava/lang/String;
326  */
327 java_lang_String *_Jv_java_lang_Thread_getState(java_lang_Thread *this)
328 {
329 #if defined(ENABLE_THREADS)
330         threadobject      *thread;
331         utf               *u;
332         java_objectheader *o;
333
334 # if defined(WITH_CLASSPATH_GNU)
335         thread = (threadobject *) this->vmThread->vmdata;
336 # elif defined(WITH_CLASSPATH_CLDC1_1)
337         thread = (threadobject *) this->vm_thread;
338 # endif
339
340         u = threads_thread_get_state(thread);
341         o = javastring_new(u);
342
343         return (java_lang_String *) o;
344 #else
345         return NULL;
346 #endif
347 }
348
349
350 /*
351  * These are local overrides for various environment variables in Emacs.
352  * Please do not remove this and leave it at the end of the file, where
353  * Emacs will automagically detect them.
354  * ---------------------------------------------------------------------
355  * Local variables:
356  * mode: c
357  * indent-tabs-mode: t
358  * c-basic-offset: 4
359  * tab-width: 4
360  * End:
361  */