cc14ef3af5fa76eb0687b30fd719de11ce698a03
[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 900 2004-01-22 13:25:36Z twisti $
32
33 */
34
35
36 #include "jni.h"
37 #include "types.h"
38 #include "native.h"
39 #include "loader.h"
40 #include "tables.h"
41 #include "threads/thread.h"
42 #include "toolbox/loging.h"
43 #include "java_lang_ThreadGroup.h"
44 #include "java_lang_Object.h"         /* needed for java_lang_Thread.h */
45 #include "java_lang_Throwable.h"      /* needed for java_lang_Thread.h */
46 #include "java_lang_Thread.h"
47
48
49 /*
50  * Class:     java/lang/Thread
51  * Method:    countStackFrames
52  * Signature: ()I
53  */
54 JNIEXPORT s4 JNICALL Java_java_lang_Thread_countStackFrames(JNIEnv *env, java_lang_Thread *this)
55 {
56     log_text("java_lang_Thread_countStackFrames called");
57
58     return 0;
59 }
60
61 /*
62  * Class:     java/lang/Thread
63  * Method:    currentThread
64  * Signature: ()Ljava/lang/Thread;
65  */
66 JNIEXPORT java_lang_Thread* JNICALL Java_java_lang_Thread_currentThread(JNIEnv *env, jclass clazz)
67 {
68         java_lang_Thread *t;
69
70         if (runverbose)
71                 log_text("java_lang_Thread_currentThread called");
72
73 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
74         t = (java_lang_Thread *) currentThread;
75   
76         if (!t->group) {
77                 log_text("java_lang_Thread_currentThread: t->group=NULL");
78                 /* ThreadGroup of currentThread is not initialized */
79
80                 t->group = (java_lang_ThreadGroup *) 
81                         native_new_and_init(loader_load(utf_new_char("java/lang/ThreadGroup")));
82
83                 if (t->group == 0) 
84                         log_text("unable to create ThreadGroup");
85         }
86
87         return (java_lang_Thread *) currentThread;
88 #else
89         return 0;       
90 #endif
91 }
92
93
94 /*
95  * Class:     java/lang/Thread
96  * Method:    nativeInterrupt
97  * Signature: ()V
98  */
99 JNIEXPORT void JNICALL Java_java_lang_Thread_nativeInterrupt(JNIEnv *env, java_lang_Thread *this)
100 {
101         log_text("Java_java_lang_Thread_interrupt0 called");
102 }
103
104
105 /*
106  * Class:     java/lang/Thread
107  * Method:    isAlive
108  * Signature: ()Z
109  */
110 JNIEXPORT s4 JNICALL Java_java_lang_Thread_isAlive(JNIEnv *env, java_lang_Thread *this)
111 {
112         if (runverbose)
113                 log_text("java_lang_Thread_isAlive called");
114
115 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
116         return aliveThread((thread *) this);
117 #else
118         return 0;
119 #endif
120 }
121
122
123
124 /*
125  * Class:     java_lang_Thread
126  * Method:    isInterrupted
127  * Signature: ()Z
128  */
129 JNIEXPORT s4 JNICALL Java_java_lang_Thread_isInterrupted(JNIEnv *env, java_lang_Thread *this)
130 {
131         log_text("Java_java_lang_Thread_isInterrupted  called");
132         return 0;
133 }
134
135
136 /*
137  * Class:     java/lang/Thread
138  * Method:    registerNatives
139  * Signature: ()V
140  */
141 JNIEXPORT void JNICALL Java_java_lang_Thread_registerNatives(JNIEnv *env, jclass clazz)
142 {
143         /* empty */
144 }
145
146
147 /*
148  * Class:     java/lang/Thread
149  * Method:    resume0
150  * Signature: ()V
151  */
152 JNIEXPORT void JNICALL Java_java_lang_Thread_nativeResume(JNIEnv *env, java_lang_Thread *this)
153 {
154         if (runverbose)
155                 log_text("java_lang_Thread_resume0 called");
156
157 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
158         resumeThread((thread *) this);
159 #endif
160 }
161
162
163 /*
164  * Class:     java/lang/Thread
165  * Method:    setPriority0
166  * Signature: (I)V
167  */
168 JNIEXPORT void JNICALL Java_java_lang_Thread_nativeSetPriority(JNIEnv *env, java_lang_Thread *this, s4 par1)
169 {
170     if (runverbose) 
171                 log_text("java_lang_Thread_setPriority0 called");
172
173 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
174         setPriorityThread((thread *) this, par1);
175 #endif
176 }
177
178
179 /*
180  * Class:     java_lang_Thread
181  * Method:    sleep
182  * Signature: (JI)V
183  */
184 JNIEXPORT void JNICALL Java_java_lang_Thread_sleep(JNIEnv *env, jclass clazz, s8 millis, s4 par2)
185 {
186         if (runverbose)
187                 log_text("java_lang_Thread_sleep called");
188
189 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
190         sleepThread(millis);
191 #endif
192 }
193
194
195 /*
196  * Class:     java/lang/Thread
197  * Method:    start
198  * Signature: ()V
199  */
200 JNIEXPORT void JNICALL Java_java_lang_Thread_start(JNIEnv *env, java_lang_Thread *this)
201 {
202         if (runverbose) 
203                 log_text("java_lang_Thread_start called");
204
205 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
206         startThread((thread*)this);
207 #endif
208 }
209
210
211 /*
212  * Class:     java/lang/Thread
213  * Method:    stop0
214  * Signature: (Ljava/lang/Object;)V
215  */
216 JNIEXPORT void JNICALL Java_java_lang_Thread_nativeStop(JNIEnv *env, java_lang_Thread *this, java_lang_Throwable *par1)
217 {
218         if (runverbose)
219                 log_text ("java_lang_Thread_stop0 called");
220
221
222 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
223         if (currentThread == (thread*)this) {
224                 log_text("killing");
225                 killThread(0);
226                 /*
227                   exceptionptr = proto_java_lang_ThreadDeath;
228                   return;
229                 */
230
231         } else {
232                 CONTEXT((thread*)this).flags |= THREAD_FLAGS_KILLED;
233                 resumeThread((thread*)this);
234         }
235 #endif
236 }
237
238
239 /*
240  * Class:     java/lang/Thread
241  * Method:    suspend0
242  * Signature: ()V
243  */
244 JNIEXPORT void JNICALL Java_java_lang_Thread_nativeSuspend(JNIEnv *env, java_lang_Thread *this)
245 {
246         if (runverbose)
247                 log_text("java_lang_Thread_suspend0 called");
248
249 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
250         suspendThread((thread*)this);
251 #endif
252 }
253
254
255 /*
256  * Class:     java/lang/Thread
257  * Method:    yield
258  * Signature: ()V
259  */
260 JNIEXPORT void JNICALL Java_java_lang_Thread_yield(JNIEnv *env, jclass clazz)
261 {
262         if (runverbose)
263                 log_text("java_lang_Thread_yield called");
264
265 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
266         yieldThread();
267 #endif
268 }
269
270
271 /*
272  * Class:     java_lang_Thread
273  * Method:    interrupted
274  * Signature: ()Z
275  */
276 JNIEXPORT s4 JNICALL Java_java_lang_Thread_interrupted(JNIEnv *env, jclass clazz)
277 {
278         log_text("Java_java_lang_Thread_interrupted");
279
280         return 0;
281 }
282
283
284 /*
285  * Class:     java_lang_Thread
286  * Method:    nativeInit
287  * Signature: (J)V
288  */
289 JNIEXPORT void JNICALL Java_java_lang_Thread_nativeInit(JNIEnv *env, java_lang_Thread *this, s8 par1)
290 {
291         if (*exceptionptr)
292                 log_text("There has been an exception, strange...");
293
294         this->priority = 5;
295 }
296
297
298 /*
299  * Class:     java_lang_Thread
300  * Method:    holdsLock
301  * Signature: (Ljava/lang/Object;)Z
302  */
303 JNIEXPORT s4 JNICALL Java_java_lang_Thread_holdsLock(JNIEnv *env, jclass clazz, java_lang_Object *par1)
304 {
305         return 0;
306 }
307
308
309 /*
310  * These are local overrides for various environment variables in Emacs.
311  * Please do not remove this and leave it at the end of the file, where
312  * Emacs will automagically detect them.
313  * ---------------------------------------------------------------------
314  * Local variables:
315  * mode: c
316  * indent-tabs-mode: t
317  * c-basic-offset: 4
318  * tab-width: 4
319  * End:
320  */