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