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