a few finalizer testcases, old thread implementation uses now VMThread, no mor contex...
[cacao.git] / src / native / vm / VMThread.c
1 /* nat/Thread.c - java/lang/Thread
2
3    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4    R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5    M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6    P. Tomsich, J. Wenninger
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., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Roman Obermaiser
28
29    Changes: Joseph Wenninger
30
31    $Id: VMThread.c 1173 2004-06-16 14:56:18Z jowenn $
32
33 */
34
35
36 #include "jni.h"
37 #include "builtin.h"
38 #include "types.h"
39 #include "native.h"
40 #include "loader.h"
41 #include "tables.h"
42 #include "threads/thread.h"
43 #include "toolbox/logging.h"
44 #include "java_lang_ThreadGroup.h"
45 #include "java_lang_Object.h"         /* needed for java_lang_Thread.h */
46 #include "java_lang_Throwable.h"      /* needed for java_lang_Thread.h */
47 #include "java_lang_VMThread.h"
48 #include "java_lang_Thread.h"
49
50
51 /*
52  * Class:     java/lang/Thread
53  * Method:    countStackFrames
54  * Signature: ()I
55  */
56 JNIEXPORT s4 JNICALL Java_java_lang_VMThread_countStackFrames(JNIEnv *env, java_lang_VMThread *this)
57 {
58     log_text("java_lang_VMThread_countStackFrames called");
59
60     return 0;
61 }
62
63 /*
64  * Class:     java/lang/Thread
65  * Method:    currentThread
66  * Signature: ()Ljava/lang/Thread;
67  */
68 JNIEXPORT java_lang_Thread* JNICALL Java_java_lang_VMThread_currentThread(JNIEnv *env, jclass clazz)
69 {
70         java_lang_Thread *t;
71
72         if (runverbose)
73                 log_text("java_lang_VMThread_currentThread called");
74
75 #if defined(USE_THREADS)
76 #if !defined(NATIVE_THREADS)
77         t = (java_lang_Thread *) currentThread;
78 #else
79         t = THREADOBJECT;
80 #endif
81   
82         if (!t->group) {
83                 log_text("java_lang_VMThread_currentThread: t->group=NULL");
84                 /* ThreadGroup of currentThread is not initialized */
85
86                 t->group = (java_lang_ThreadGroup *) 
87                         native_new_and_init(class_new(utf_new_char("java/lang/ThreadGroup")));
88
89                 if (t->group == 0) 
90                         log_text("unable to create ThreadGroup");
91         }
92
93         return (java_lang_Thread *) t;
94 #else
95         return 0;       
96 #endif
97 }
98
99
100 /*
101  * Class:     java/lang/Thread
102  * Method:    nativeInterrupt
103  * Signature: ()V
104  */
105 JNIEXPORT void JNICALL Java_java_lang_VMThread_interrupt(JNIEnv *env, java_lang_VMThread *this)
106 {
107         log_text("Java_java_lang_VMThread_interrupt0 called");
108 }
109
110
111 /*
112  * Class:     java/lang/Thread
113  * Method:    isAlive
114  * Signature: ()Z
115  */
116 JNIEXPORT s4 JNICALL Java_java_lang_VMThread_isAlive(JNIEnv *env, java_lang_VMThread *this)
117 {
118         if (runverbose)
119                 log_text("java_lang_VMThread_isAlive called");
120
121 #if defined(USE_THREADS)
122         return aliveThread((thread *) this->thread);
123 #endif
124 }
125
126
127
128 /*
129  * Class:     java_lang_Thread
130  * Method:    isInterrupted
131  * Signature: ()Z
132  */
133 JNIEXPORT s4 JNICALL Java_java_lang_VMThread_isInterrupted(JNIEnv *env, java_lang_VMThread *this)
134 {
135         log_text("Java_java_lang_VMThread_isInterrupted  called");
136         return 0;
137 }
138
139
140 /*
141  * Class:     java/lang/Thread
142  * Method:    registerNatives
143  * Signature: ()V
144  */
145 JNIEXPORT void JNICALL Java_java_lang_VMThread_registerNatives(JNIEnv *env, jclass clazz)
146 {
147         /* empty */
148 }
149
150
151 /*
152  * Class:     java/lang/Thread
153  * Method:    resume0
154  * Signature: ()V
155  */
156 JNIEXPORT void JNICALL Java_java_lang_VMThread_resume(JNIEnv *env, java_lang_VMThread *this)
157 {
158         if (runverbose)
159                 log_text("java_lang_VMThread_resume0 called");
160
161 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
162         resumeThread((thread *) this->thread);
163 #endif
164 }
165
166
167 /*
168  * Class:     java/lang/Thread
169  * Method:    setPriority0
170  * Signature: (I)V
171  */
172 JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeSetPriority(JNIEnv *env, java_lang_VMThread *this, s4 par1)
173 {
174     if (runverbose) 
175                 log_text("java_lang_VMThread_setPriority0 called");
176
177 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
178         setPriorityThread((thread *) this->thread, par1);
179 #endif
180 }
181
182
183 /*
184  * Class:     java_lang_Thread
185  * Method:    sleep
186  * Signature: (JI)V
187  */
188 JNIEXPORT void JNICALL Java_java_lang_VMThread_sleep(JNIEnv *env, jclass clazz, s8 millis, s4 par2)
189 {
190         if (runverbose)
191                 log_text("java_lang_VMThread_sleep called");
192
193 #if defined(USE_THREADS)
194         sleepThread(millis);
195 #endif
196 }
197
198
199 /*
200  * Class:     java/lang/Thread
201  * Method:    start
202  * Signature: ()V
203  */
204 JNIEXPORT void JNICALL Java_java_lang_VMThread_start(JNIEnv *env, java_lang_VMThread *this, s8 par1)
205 {
206         if (runverbose) 
207                 log_text("java_lang_VMThread_start called");
208
209 #if defined(USE_THREADS)
210 #warning perhaps it would be better to always work with the vmthread structure in the thread code (jowenn)
211         if (this->thread->vmThread==0)
212                 this->thread->vmThread=this;
213         startThread((thread*)(this->thread));
214 #endif
215 }
216
217
218 /*
219  * Class:     java/lang/Thread
220  * Method:    stop0
221  * Signature: (Ljava/lang/Object;)V
222  */
223 JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeStop(JNIEnv *env, java_lang_VMThread *this, java_lang_Throwable *par1)
224 {
225         if (runverbose)
226                 log_text ("java_lang_VMThread_stop0 called");
227
228
229 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
230         if (currentThread == (thread*)this->thread) {
231                 log_text("killing");
232                 killThread(0);
233                 /*
234                   exceptionptr = proto_java_lang_ThreadDeath;
235                   return;
236                 */
237
238         } else {
239                 /*CONTEXT((thread*)this)*/ this->flags |= THREAD_FLAGS_KILLED;
240                 resumeThread((thread*)this->thread);
241         }
242 #endif
243 }
244
245
246 /*
247  * Class:     java/lang/Thread
248  * Method:    suspend0
249  * Signature: ()V
250  */
251 JNIEXPORT void JNICALL Java_java_lang_VMThread_suspend(JNIEnv *env, java_lang_VMThread *this)
252 {
253         if (runverbose)
254                 log_text("java_lang_VMThread_suspend0 called");
255
256 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
257         suspendThread((thread*)this->thread);
258 #endif
259 }
260
261
262 /*
263  * Class:     java/lang/Thread
264  * Method:    yield
265  * Signature: ()V
266  */
267 JNIEXPORT void JNICALL Java_java_lang_VMThread_yield(JNIEnv *env, jclass clazz)
268 {
269         if (runverbose)
270                 log_text("java_lang_VMThread_yield called");
271
272 #if defined(USE_THREADS)
273         yieldThread();
274 #endif
275 }
276
277
278 /*
279  * Class:     java_lang_Thread
280  * Method:    interrupted
281  * Signature: ()Z
282  */
283 JNIEXPORT s4 JNICALL Java_java_lang_VMThread_interrupted(JNIEnv *env, jclass clazz)
284 {
285         log_text("Java_java_lang_VMThread_interrupted");
286
287         return 0;
288 }
289
290
291 /*
292  * Class:     java_lang_Thread
293  * Method:    nativeInit
294  * Signature: (J)V
295  */
296 JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeInit(JNIEnv *env, java_lang_VMThread *this, s8 par1)
297 {
298 /*
299         if (*exceptionptr)
300                 log_text("There has been an exception, strange...");*/
301
302 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
303         initThread(this->thread);
304 #endif
305         this->thread->priority = 5;
306 }
307
308
309 /*
310  * Class:     java_lang_Thread
311  * Method:    holdsLock
312  * Signature: (Ljava/lang/Object;)Z
313  */
314 JNIEXPORT s4 JNICALL Java_java_lang_VMThread_holdsLock(JNIEnv *env, jclass clazz, java_lang_Object *par1)
315 {
316         return 0;
317 }
318
319
320 /*
321  * These are local overrides for various environment variables in Emacs.
322  * Please do not remove this and leave it at the end of the file, where
323  * Emacs will automagically detect them.
324  * ---------------------------------------------------------------------
325  * Local variables:
326  * mode: c
327  * indent-tabs-mode: t
328  * c-basic-offset: 4
329  * tab-width: 4
330  * End:
331  */