Add Interface Test
[cacao.git] / nat / Thread.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: Thread.c 1515 2004-11-17 11:53:23Z twisti $
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 "nat/java_lang_ThreadGroup.h"
47 #include "nat/java_lang_Object.h"       /* needed for java_lang_Thread.h      */
48 #include "nat/java_lang_Throwable.h"    /* needed for java_lang_Thread.h      */
49 #include "nat/java_lang_VMThread.h"
50 #include "nat/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         throw_cacao_exception_exit(string_java_lang_InternalError, "aliveThread");
133 #endif
134 #endif
135
136         /* keep compiler happy */
137
138         return 0;
139 }
140
141
142
143 /*
144  * Class:     java_lang_Thread
145  * Method:    isInterrupted
146  * Signature: ()Z
147  */
148 JNIEXPORT s4 JNICALL Java_java_lang_VMThread_isInterrupted(JNIEnv *env, java_lang_VMThread *this)
149 {
150 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
151         return isInterruptedThread(this);
152 #else
153         log_text("Java_java_lang_VMThread_isInterrupted  called");
154         return 0;
155 #endif
156 }
157
158
159 /*
160  * Class:     java/lang/Thread
161  * Method:    registerNatives
162  * Signature: ()V
163  */
164 JNIEXPORT void JNICALL Java_java_lang_VMThread_registerNatives(JNIEnv *env, jclass clazz)
165 {
166         /* empty */
167 }
168
169
170 /*
171  * Class:     java/lang/Thread
172  * Method:    resume0
173  * Signature: ()V
174  */
175 JNIEXPORT void JNICALL Java_java_lang_VMThread_resume(JNIEnv *env, java_lang_VMThread *this)
176 {
177         if (runverbose)
178                 log_text("java_lang_VMThread_resume0 called");
179
180 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
181         resumeThread((thread *) this->thread);
182 #endif
183 }
184
185
186 /*
187  * Class:     java/lang/Thread
188  * Method:    setPriority0
189  * Signature: (I)V
190  */
191 JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeSetPriority(JNIEnv *env, java_lang_VMThread *this, s4 par1)
192 {
193     if (runverbose) 
194                 log_text("java_lang_VMThread_setPriority0 called");
195
196 #if defined(USE_THREADS)
197         setPriorityThread((thread *) this->thread, par1);
198 #endif
199 }
200
201
202 /*
203  * Class:     java_lang_Thread
204  * Method:    sleep
205  * Signature: (JI)V
206  */
207 JNIEXPORT void JNICALL Java_java_lang_VMThread_sleep(JNIEnv *env, jclass clazz, s8 millis, s4 nanos)
208 {
209 #if defined(USE_THREADS)
210         sleepThread(millis, nanos);
211 #endif
212 }
213
214
215 /*
216  * Class:     java/lang/Thread
217  * Method:    start
218  * Signature: ()V
219  */
220 JNIEXPORT void JNICALL Java_java_lang_VMThread_start(JNIEnv *env, java_lang_VMThread *this, s8 par1)
221 {
222         if (runverbose) 
223                 log_text("java_lang_VMThread_start called");
224
225 #if defined(USE_THREADS)
226         this->thread->vmThread = this;
227         startThread((thread *) this->thread);
228 #endif
229 }
230
231
232 /*
233  * Class:     java/lang/Thread
234  * Method:    stop0
235  * Signature: (Ljava/lang/Object;)V
236  */
237 JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeStop(JNIEnv *env, java_lang_VMThread *this, java_lang_Throwable *par1)
238 {
239         if (runverbose)
240                 log_text ("java_lang_VMThread_stop0 called");
241
242
243 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
244         if (currentThread == (thread*)this->thread) {
245                 log_text("killing");
246                 killThread(0);
247                 /*
248                   exceptionptr = proto_java_lang_ThreadDeath;
249                   return;
250                 */
251
252         } else {
253                 /*CONTEXT((thread*)this)*/ this->flags |= THREAD_FLAGS_KILLED;
254                 resumeThread((thread*)this->thread);
255         }
256 #endif
257 }
258
259
260 /*
261  * Class:     java/lang/Thread
262  * Method:    suspend0
263  * Signature: ()V
264  */
265 JNIEXPORT void JNICALL Java_java_lang_VMThread_suspend(JNIEnv *env, java_lang_VMThread *this)
266 {
267         if (runverbose)
268                 log_text("java_lang_VMThread_suspend0 called");
269
270 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
271         suspendThread((thread*)this->thread);
272 #endif
273 }
274
275
276 /*
277  * Class:     java/lang/Thread
278  * Method:    yield
279  * Signature: ()V
280  */
281 JNIEXPORT void JNICALL Java_java_lang_VMThread_yield(JNIEnv *env, jclass clazz)
282 {
283         if (runverbose)
284                 log_text("java_lang_VMThread_yield called");
285
286 #if defined(USE_THREADS)
287         yieldThread();
288 #endif
289 }
290
291
292 /*
293  * Class:     java_lang_Thread
294  * Method:    interrupted
295  * Signature: ()Z
296  */
297 JNIEXPORT s4 JNICALL Java_java_lang_VMThread_interrupted(JNIEnv *env, jclass clazz)
298 {
299 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
300         return interruptedThread();
301 #else
302         log_text("Java_java_lang_VMThread_interrupted");
303         return 0;
304 #endif
305 }
306
307
308 /*
309  * Class:     java_lang_Thread
310  * Method:    nativeInit
311  * Signature: (J)V
312  */
313 JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeInit(JNIEnv *env, java_lang_VMThread *this, s8 par1)
314 {
315 /*
316         if (*exceptionptr)
317                 log_text("There has been an exception, strange...");*/
318
319 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
320         initThread(this);
321 #endif
322 }
323
324
325 /*
326  * Class:     java/lang/VMThread
327  * Method:    holdsLock
328  * Signature: (Ljava/lang/Object;)Z
329  */
330 JNIEXPORT s4 JNICALL Java_java_lang_VMThread_holdsLock(JNIEnv *env, jclass clazz, java_lang_Object* o)
331 {
332 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
333         return threadHoldsLock((threadobject*) THREADOBJECT,
334                                                    (java_objectheader *) o);
335 #else
336         /* I don't know how to find out [stefan] */
337         return 0;
338 #endif
339 }
340
341
342 /*
343  * These are local overrides for various environment variables in Emacs.
344  * Please do not remove this and leave it at the end of the file, where
345  * Emacs will automagically detect them.
346  * ---------------------------------------------------------------------
347  * Local variables:
348  * mode: c
349  * indent-tabs-mode: t
350  * c-basic-offset: 4
351  * tab-width: 4
352  * End:
353  */