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