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