Added #include "exceptions.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 1344 2004-07-21 17:12:53Z 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 "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;
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 (java_lang_Thread *) 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         log_text("Java_java_lang_VMThread_interrupt0 called");
110 }
111
112
113 /*
114  * Class:     java/lang/Thread
115  * Method:    isAlive
116  * Signature: ()Z
117  */
118 JNIEXPORT s4 JNICALL Java_java_lang_VMThread_isAlive(JNIEnv *env, java_lang_VMThread *this)
119 {
120         if (runverbose)
121                 log_text("java_lang_VMThread_isAlive called");
122
123 #if defined(USE_THREADS)
124         return aliveThread((thread *) this->thread);
125 #endif
126 }
127
128
129
130 /*
131  * Class:     java_lang_Thread
132  * Method:    isInterrupted
133  * Signature: ()Z
134  */
135 JNIEXPORT s4 JNICALL Java_java_lang_VMThread_isInterrupted(JNIEnv *env, java_lang_VMThread *this)
136 {
137         log_text("Java_java_lang_VMThread_isInterrupted  called");
138         return 0;
139 }
140
141
142 /*
143  * Class:     java/lang/Thread
144  * Method:    registerNatives
145  * Signature: ()V
146  */
147 JNIEXPORT void JNICALL Java_java_lang_VMThread_registerNatives(JNIEnv *env, jclass clazz)
148 {
149         /* empty */
150 }
151
152
153 /*
154  * Class:     java/lang/Thread
155  * Method:    resume0
156  * Signature: ()V
157  */
158 JNIEXPORT void JNICALL Java_java_lang_VMThread_resume(JNIEnv *env, java_lang_VMThread *this)
159 {
160         if (runverbose)
161                 log_text("java_lang_VMThread_resume0 called");
162
163 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
164         resumeThread((thread *) this->thread);
165 #endif
166 }
167
168
169 /*
170  * Class:     java/lang/Thread
171  * Method:    setPriority0
172  * Signature: (I)V
173  */
174 JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeSetPriority(JNIEnv *env, java_lang_VMThread *this, s4 par1)
175 {
176     if (runverbose) 
177                 log_text("java_lang_VMThread_setPriority0 called");
178
179 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
180         setPriorityThread((thread *) this->thread, par1);
181 #endif
182 }
183
184
185 /*
186  * Class:     java_lang_Thread
187  * Method:    sleep
188  * Signature: (JI)V
189  */
190 JNIEXPORT void JNICALL Java_java_lang_VMThread_sleep(JNIEnv *env, jclass clazz, s8 millis, s4 nanos)
191 {
192         if (millis < 0) {
193                 *exceptionptr =
194                         new_exception_message(string_java_lang_IllegalArgumentException,
195                                                                   "timeout value is negative");
196
197                 return;
198         }
199
200         if (nanos < 0 || nanos > 999999) {
201                 *exceptionptr =
202                         new_exception_message(string_java_lang_IllegalArgumentException,
203                                                                   "nanosecond timeout value out of range");
204
205                 return;
206         }
207
208 #if defined(USE_THREADS)
209         sleepThread(millis, nanos);
210 #endif
211 }
212
213
214 /*
215  * Class:     java/lang/Thread
216  * Method:    start
217  * Signature: ()V
218  */
219 JNIEXPORT void JNICALL Java_java_lang_VMThread_start(JNIEnv *env, java_lang_VMThread *this, s8 par1)
220 {
221         if (runverbose) 
222                 log_text("java_lang_VMThread_start called");
223
224 #if defined(USE_THREADS)
225 #if defined(__GNUC__)
226 #warning perhaps it would be better to always work with the vmthread structure in the thread code (jowenn)
227 #endif
228         if (this->thread->vmThread == 0)
229                 this->thread->vmThread = this;
230
231         startThread((thread *) (this->thread));
232 #endif
233 }
234
235
236 /*
237  * Class:     java/lang/Thread
238  * Method:    stop0
239  * Signature: (Ljava/lang/Object;)V
240  */
241 JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeStop(JNIEnv *env, java_lang_VMThread *this, java_lang_Throwable *par1)
242 {
243         if (runverbose)
244                 log_text ("java_lang_VMThread_stop0 called");
245
246
247 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
248         if (currentThread == (thread*)this->thread) {
249                 log_text("killing");
250                 killThread(0);
251                 /*
252                   exceptionptr = proto_java_lang_ThreadDeath;
253                   return;
254                 */
255
256         } else {
257                 /*CONTEXT((thread*)this)*/ this->flags |= THREAD_FLAGS_KILLED;
258                 resumeThread((thread*)this->thread);
259         }
260 #endif
261 }
262
263
264 /*
265  * Class:     java/lang/Thread
266  * Method:    suspend0
267  * Signature: ()V
268  */
269 JNIEXPORT void JNICALL Java_java_lang_VMThread_suspend(JNIEnv *env, java_lang_VMThread *this)
270 {
271         if (runverbose)
272                 log_text("java_lang_VMThread_suspend0 called");
273
274 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
275         suspendThread((thread*)this->thread);
276 #endif
277 }
278
279
280 /*
281  * Class:     java/lang/Thread
282  * Method:    yield
283  * Signature: ()V
284  */
285 JNIEXPORT void JNICALL Java_java_lang_VMThread_yield(JNIEnv *env, jclass clazz)
286 {
287         if (runverbose)
288                 log_text("java_lang_VMThread_yield called");
289
290 #if defined(USE_THREADS)
291         yieldThread();
292 #endif
293 }
294
295
296 /*
297  * Class:     java_lang_Thread
298  * Method:    interrupted
299  * Signature: ()Z
300  */
301 JNIEXPORT s4 JNICALL Java_java_lang_VMThread_interrupted(JNIEnv *env, jclass clazz)
302 {
303         log_text("Java_java_lang_VMThread_interrupted");
304
305         return 0;
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->thread);
322 #endif
323         this->thread->priority = 5;
324 }
325
326
327 /*
328  * These are local overrides for various environment variables in Emacs.
329  * Please do not remove this and leave it at the end of the file, where
330  * Emacs will automagically detect them.
331  * ---------------------------------------------------------------------
332  * Local variables:
333  * mode: c
334  * indent-tabs-mode: t
335  * c-basic-offset: 4
336  * tab-width: 4
337  * End:
338  */