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