0e33fc253d45eeb2230a307bcbc42f79eaa30440
[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 8295 2007-08-11 17:57:24Z michi $
26
27 */
28
29
30 #include "config.h"
31 #include "vm/types.h"
32
33 #include "native/jni.h"
34 #include "native/llni.h"
35 #include "native/native.h"
36
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"
41
42 #if defined(ENABLE_JAVASE)
43 # include "native/include/java_lang_ThreadGroup.h"
44
45 # if defined(WITH_CLASSPATH_GNU)
46 #  include "native/include/java_lang_VMThread.h"
47 # endif
48 #endif
49
50 #include "threads/lock-common.h"
51 #include "threads/threads-common.h"
52
53 #include "toolbox/logging.h"
54
55 #include "vm/builtin.h"
56 #include "vm/exceptions.h"
57 #include "vm/stringlocal.h"
58
59 #include "vmcore/options.h"
60
61
62 /*
63  * Class:     java/lang/Thread
64  * Method:    countStackFrames
65  * Signature: ()I
66  */
67 s4 _Jv_java_lang_Thread_countStackFrames(java_lang_Thread *this)
68 {
69     log_text("java_lang_Thread_countStackFrames called");
70
71     return 0;
72 }
73
74
75 /*
76  * Class:     java/lang/Thread
77  * Method:    sleep
78  * Signature: (J)V
79  */
80 void _Jv_java_lang_Thread_sleep(s8 millis)
81 {
82 #if defined(ENABLE_THREADS)
83         threads_sleep(millis, 0);
84 #endif
85 }
86
87
88 /*
89  * Class:     java/lang/Thread
90  * Method:    start
91  * Signature: (J)V
92  */
93 void _Jv_java_lang_Thread_start(java_lang_Thread *this, s8 stacksize)
94 {
95 #if defined(ENABLE_THREADS)
96         threads_thread_start(this);
97 #endif
98 }
99
100
101 /*
102  * Class:     java/lang/Thread
103  * Method:    interrupt
104  * Signature: ()V
105  */
106 void _Jv_java_lang_Thread_interrupt(java_lang_Thread *this)
107 {
108 #if defined(ENABLE_THREADS)
109         threadobject *thread;
110
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;
115 #endif
116
117         threads_thread_interrupt(thread);
118 #endif
119 }
120
121
122 /*
123  * Class:     java/lang/Thread
124  * Method:    isAlive
125  * Signature: ()Z
126  */
127 s4 _Jv_java_lang_Thread_isAlive(java_lang_Thread *this)
128 {
129 #if defined(ENABLE_THREADS)
130         threadobject *t;
131
132 # if defined(WITH_CLASSPATH_GNU)
133
134         t = (threadobject *) LLNI_field_direct(LLNI_field_direct(this, vmThread), vmdata);
135
136 # elif defined(WITH_CLASSPATH_SUN)
137
138         /* XXX this is just a quick hack */
139
140         for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
141                 if (t->object == this)
142                         break;
143         }
144
145         /* The threadobject is null when a thread is created in Java. The
146            priority is set later during startup. */
147
148         if (t == NULL)
149                 return 0;
150
151 # elif defined(WITH_CLASSPATH_CLDC1_1)
152
153         t = (threadobject *) this->vm_thread;
154
155         if (t == NULL)
156                 return 0;
157
158 # else
159 #  error unknown classpath configuration
160 # endif
161
162         return threads_thread_is_alive(t);
163 #else
164         /* if threads are disabled, the only thread running is alive */
165
166         return 1;
167 #endif
168 }
169
170
171 /*
172  * Class:     java/lang/Thread
173  * Method:    isInterrupted
174  * Signature: ()Z
175  */
176 s4 _Jv_java_lang_Thread_isInterrupted(java_lang_Thread *this)
177 {
178 #if defined(ENABLE_THREADS)
179         threadobject *t;
180
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 */
185
186         for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
187                 if (t->object == this)
188                         break;
189         }
190 # elif defined(WITH_CLASSPATH_CLDC1_1)
191         t = (threadobject *) this->vm_thread;
192 # else
193 #  error unknown classpath configuration
194 # endif
195
196         return threads_thread_has_been_interrupted(t);
197 #else
198         return 0;
199 #endif
200 }
201
202
203 /*
204  * Class:     java/lang/Thread
205  * Method:    suspend
206  * Signature: ()V
207  */
208 void _Jv_java_lang_Thread_suspend(java_lang_Thread *this)
209 {
210 #if defined(ENABLE_THREADS)
211 #endif
212 }
213
214
215 /*
216  * Class:     java/lang/Thread
217  * Method:    resume
218  * Signature: ()V
219  */
220 void _Jv_java_lang_Thread_resume(java_lang_Thread *this)
221 {
222 #if defined(ENABLE_THREADS)
223 #endif
224 }
225
226
227 /*
228  * Class:     java/lang/Thread
229  * Method:    setPriority
230  * Signature: (I)V
231  */
232 void _Jv_java_lang_Thread_setPriority(java_lang_Thread *this, s4 priority)
233 {
234 #if defined(ENABLE_THREADS)
235         threadobject *t;
236
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 */
241
242         for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
243                 if (t->object == this)
244                         break;
245         }
246
247         /* The threadobject is null when a thread is created in Java. The
248            priority is set later during startup. */
249
250         if (t == NULL)
251                 return;
252 # elif defined(WITH_CLASSPATH_CLDC1_1)
253         t = (threadobject *) this->vm_thread;
254
255         /* The threadobject is null when a thread is created in Java. The
256            priority is set later during startup. */
257
258         if (t == NULL)
259                 return;
260 # else
261 #  error unknown classpath configuration
262 # endif
263
264         threads_set_thread_priority(t->tid, priority);
265 #endif
266 }
267
268
269 /*
270  * Class:     java/lang/Thread
271  * Method:    stop
272  * Signature: (Ljava/lang/Object;)V
273  */
274 void _Jv_java_lang_Thread_stop(java_lang_Thread *this, java_lang_Throwable *t)
275 {
276 #if defined(ENABLE_THREADS)
277 #endif
278 }
279
280
281 /*
282  * Class:     java/lang/Thread
283  * Method:    currentThread
284  * Signature: ()Ljava/lang/Thread;
285  */
286 java_lang_Thread *_Jv_java_lang_Thread_currentThread(void)
287 {
288 #if defined(ENABLE_THREADS)
289         threadobject     *thread;
290 #endif
291         java_lang_Thread *t;
292
293 #if defined(ENABLE_THREADS)
294         thread = THREADOBJECT;
295
296         t = thread->object;
297
298         if (t == NULL)
299                 log_text("t ptr is NULL\n");
300
301 # if defined(ENABLE_JAVASE)
302         if (LLNI_field_direct(t, group) == NULL) {
303                 /* ThreadGroup of currentThread is not initialized */
304
305                 LLNI_field_direct(t, group) = (java_lang_ThreadGroup *)
306                         native_new_and_init(class_java_lang_ThreadGroup);
307
308                 if (LLNI_field_direct(t, group) == NULL)
309                         log_text("unable to create ThreadGroup");
310         }
311 # endif
312 #else
313         /* we just return a fake java.lang.Thread object, otherwise we get
314        NullPointerException's in GNU classpath */
315
316         t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
317 #endif
318
319         return t;
320 }
321
322
323 /*
324  * Class:     java/lang/Thread
325  * Method:    yield
326  * Signature: ()V
327  */
328 void _Jv_java_lang_Thread_yield(void)
329 {
330 #if defined(ENABLE_THREADS)
331         threads_yield();
332 #endif
333 }
334
335
336 /*
337  * Class:     java/lang/Thread
338  * Method:    interrupted
339  * Signature: ()Z
340  */
341 s4 _Jv_java_lang_Thread_interrupted(void)
342 {
343 #if defined(ENABLE_THREADS)
344         return threads_check_if_interrupted_and_reset();
345 #else
346         return 0;
347 #endif
348 }
349
350
351 /*
352  * Class:     java/lang/Thread
353  * Method:    holdsLock
354  * Signature: (Ljava/lang/Object;)Z
355  */
356 s4 _Jv_java_lang_Thread_holdsLock(java_lang_Object* obj)
357 {
358 #if defined(ENABLE_THREADS)
359         java_handle_t *o;
360
361         o = (java_handle_t *) obj;
362
363         if (o == NULL) {
364                 exceptions_throw_nullpointerexception();
365                 return 0;
366         }
367
368         return lock_is_held_by_current_thread(o);
369 #else
370         return 0;
371 #endif
372 }
373
374
375 /*
376  * Class:     java/lang/Thread
377  * Method:    getState
378  * Signature: ()Ljava/lang/String;
379  */
380 java_lang_String *_Jv_java_lang_Thread_getState(java_lang_Thread *this)
381 {
382 #if defined(ENABLE_THREADS)
383         threadobject  *thread;
384         utf           *u;
385         java_handle_t *o;
386
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;
391 # endif
392
393         u = threads_thread_get_state(thread);
394         o = javastring_new(u);
395
396         return (java_lang_String *) o;
397 #else
398         return NULL;
399 #endif
400 }
401
402
403 /*
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  * ---------------------------------------------------------------------
408  * Local variables:
409  * mode: c
410  * indent-tabs-mode: t
411  * c-basic-offset: 4
412  * tab-width: 4
413  * End:
414  */