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