* This is a rather huge commit, which changes the build order of
[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 #if defined(ENABLE_JAVASE)
37 # include "native/include/java_lang_ThreadGroup.h"
38 #endif
39
40 #include "native/include/java_lang_String.h"
41 #include "native/include/java_lang_Object.h"            /* java_lang_Thread.h */
42 #include "native/include/java_lang_Throwable.h"         /* java_lang_Thread.h */
43 #include "native/include/java_lang_Thread.h"
44
45 #if defined(ENABLE_THREADS)
46 # include "threads/native/threads.h"
47 #endif
48
49 #include "toolbox/logging.h"
50
51 #include "vm/builtin.h"
52 #include "vm/exceptions.h"
53
54 #include "vmcore/options.h"
55
56 /* XXX REMOVE ME only for vm_abort */
57 #include "vm/vm.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         threadobject *thread;
95
96         thread = (threadobject *) this;
97
98         /* don't pass a function pointer (NULL) since we want Thread.run()V here */
99
100         threads_start_thread(thread, NULL);
101 #endif
102 }
103
104
105 /*
106  * Class:     java/lang/Thread
107  * Method:    interrupt
108  * Signature: ()V
109  */
110 void _Jv_java_lang_Thread_interrupt(java_lang_Thread *this)
111 {
112 #if defined(ENABLE_THREADS)
113         threadobject *thread;
114
115         thread = (threadobject *) this;
116
117         threads_thread_interrupt(thread);
118 #endif
119 }
120
121
122 /*
123  * Class:     java/lang/Thread
124  * Method:    isInterrupted
125  * Signature: ()Z
126  */
127 s4 _Jv_java_lang_Thread_isInterrupted(java_lang_Thread *this)
128 {
129 #if defined(ENABLE_THREADS)
130         threadobject *thread;
131
132         thread = (threadobject *) this;
133
134         return threads_thread_has_been_interrupted(thread);
135 #else
136         return 0;
137 #endif
138 }
139
140
141 /*
142  * Class:     java/lang/Thread
143  * Method:    suspend
144  * Signature: ()V
145  */
146 void _Jv_java_lang_Thread_suspend(java_lang_Thread *this)
147 {
148 #if defined(ENABLE_THREADS)
149 #endif
150 }
151
152
153 /*
154  * Class:     java/lang/Thread
155  * Method:    resume
156  * Signature: ()V
157  */
158 void _Jv_java_lang_Thread_resume(java_lang_Thread *this)
159 {
160 #if defined(ENABLE_THREADS)
161 #endif
162 }
163
164
165 /*
166  * Class:     java/lang/Thread
167  * Method:    setPriority
168  * Signature: (I)V
169  */
170 void _Jv_java_lang_Thread_setPriority(java_lang_Thread *this, s4 priority)
171 {
172 #if defined(ENABLE_THREADS)
173         threadobject *thread;
174
175         thread = (threadobject *) this;
176
177         threads_set_thread_priority(thread->tid, priority);
178 #endif
179 }
180
181
182 /*
183  * Class:     java/lang/Thread
184  * Method:    stop
185  * Signature: (Ljava/lang/Object;)V
186  */
187 void _Jv_java_lang_Thread_stop(java_lang_Thread *this, java_lang_Throwable *t)
188 {
189 #if defined(ENABLE_THREADS)
190 #endif
191 }
192
193
194 /*
195  * Class:     java/lang/Thread
196  * Method:    currentThread
197  * Signature: ()Ljava/lang/Thread;
198  */
199 java_lang_Thread *_Jv_java_lang_Thread_currentThread(void)
200 {
201 #if defined(ENABLE_THREADS)
202         threadobject     *thread;
203 #endif
204         java_lang_Thread *t;
205
206 #if defined(ENABLE_THREADS)
207         thread = THREADOBJECT;
208
209         t = (java_lang_Thread *) thread;
210
211         if (t == NULL)
212                 log_text("t ptr is NULL\n");
213   
214 # if defined(ENABLE_JAVASE)
215         if (t->group == NULL) {
216                 /* ThreadGroup of currentThread is not initialized */
217
218                 t->group = (java_lang_ThreadGroup *)
219                         native_new_and_init(class_java_lang_ThreadGroup);
220
221                 if (t->group == NULL)
222                         log_text("unable to create ThreadGroup");
223         }
224 # endif
225 #else
226         /* we just return a fake java.lang.Thread object, otherwise we get
227        NullPointerException's in GNU classpath */
228
229         t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
230 #endif
231
232         return t;
233 }
234
235
236 /*
237  * Class:     java/lang/Thread
238  * Method:    yield
239  * Signature: ()V
240  */
241 void _Jv_java_lang_Thread_yield(void)
242 {
243 #if defined(ENABLE_THREADS)
244         threads_yield();
245 #endif
246 }
247
248
249 /*
250  * Class:     java/lang/Thread
251  * Method:    interrupted
252  * Signature: ()Z
253  */
254 s4 _Jv_java_lang_Thread_interrupted(void)
255 {
256 #if defined(ENABLE_THREADS)
257         return threads_check_if_interrupted_and_reset();
258 #else
259         return 0;
260 #endif
261 }
262
263
264 /*
265  * Class:     java/lang/Thread
266  * Method:    holdsLock
267  * Signature: (Ljava/lang/Object;)Z
268  */
269 s4 _Jv_java_lang_Thread_holdsLock(java_lang_Object* obj)
270 {
271 #if defined(ENABLE_THREADS)
272         java_objectheader *o;
273
274         o = (java_objectheader *) obj;
275
276         if (o == NULL) {
277                 exceptions_throw_nullpointerexception();
278                 return 0;
279         }
280
281         return lock_is_held_by_current_thread(o);
282 #else
283         return 0;
284 #endif
285 }
286
287
288 /*
289  * Class:     java/lang/Thread
290  * Method:    getState
291  * Signature: ()Ljava/lang/String;
292  */
293 java_lang_String *_Jv_java_lang_Thread_getState(java_lang_Thread *this)
294 {
295         vm_abort("Java_java_lang_Thread_getState: IMPLEMENT ME!");
296
297         return NULL;
298 }
299
300
301 /*
302  * These are local overrides for various environment variables in Emacs.
303  * Please do not remove this and leave it at the end of the file, where
304  * Emacs will automagically detect them.
305  * ---------------------------------------------------------------------
306  * Local variables:
307  * mode: c
308  * indent-tabs-mode: t
309  * c-basic-offset: 4
310  * tab-width: 4
311  * End:
312  */