removed the class hash and all functions identifying classes by name only
[cacao.git] / src / native / vm / VMThread.c
1 /* native/vm/VMThread.c - java/lang/VMThread
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    Institut f. Computersprachen - TU Wien
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 2195 2005-04-03 16:53:16Z edwin $
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/VMThread
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/VMThread
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         classinfo *threadgroupclass;
82
83         if (runverbose)
84                 log_text("java_lang_VMThread_currentThread called");
85
86 #if defined(USE_THREADS)
87 #if !defined(NATIVE_THREADS)
88         t = (java_lang_Thread *) currentThread;
89 #else
90         t = ((threadobject*) THREADOBJECT)->o.thread;
91 #endif
92         if (runverbose)
93                 log_text("java_lang_VMThread_currentThread 111");
94         if ((runverbose) && (t == NULL)) 
95                 log_text("t ptr is NULL\n");
96 if (t == NULL) printf("t ptr is NULL\n"); fflush(stdout);
97   
98         if (!t->group) {
99                 /* ThreadGroup of currentThread is not initialized */
100         if (runverbose)
101                 log_text("java_lang_VMThread_currentThread 222");
102
103         if (!load_class_bootstrap(utf_new_char("java/lang/ThreadGroup"),&threadgroupclass))
104                 return NULL;
105
106         t->group = (java_lang_ThreadGroup *) native_new_and_init(threadgroupclass);
107
108         if (runverbose)
109                 log_text("java_lang_VMThread_currentThread 333");
110                 if (t->group == 0) 
111                         log_text("unable to create ThreadGroup");
112         if (runverbose)
113                 log_text("java_lang_VMThread_currentThread 444");
114         }
115         if (runverbose)
116                 log_text("java_lang_VMThread_currentThread 555");
117
118         return t;
119 #else
120         return 0;       
121 #endif
122 }
123
124
125 /*
126  * Class:     java/lang/VMThread
127  * Method:    interrupt
128  * Signature: ()V
129  */
130 JNIEXPORT void JNICALL Java_java_lang_VMThread_interrupt(JNIEnv *env, java_lang_VMThread *this)
131 {
132 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
133         interruptThread(this);
134 #else
135         log_text("Java_java_lang_VMThread_interrupt called");
136 #endif
137 }
138
139
140 /*
141  * Class:     java/lang/VMThread
142  * Method:    isAlive
143  * Signature: ()Z
144  */
145 JNIEXPORT s4 JNICALL Java_java_lang_VMThread_isAlive(JNIEnv *env, java_lang_VMThread *this)
146 {
147         if (runverbose)
148                 log_text("java_lang_VMThread_isAlive called");
149
150 #if defined(USE_THREADS)
151 #if !defined(NATIVE_THREADS)
152         return aliveThread((thread *) this->thread);
153 #else
154         /* This method is implemented in classpath. */
155         throw_cacao_exception_exit(string_java_lang_InternalError, "aliveThread");
156 #endif
157 #endif
158
159         /* keep compiler happy */
160
161         return 0;
162 }
163
164
165
166 /*
167  * Class:     java/lang/VMThread
168  * Method:    isInterrupted
169  * Signature: ()Z
170  */
171 JNIEXPORT s4 JNICALL Java_java_lang_VMThread_isInterrupted(JNIEnv *env, java_lang_VMThread *this)
172 {
173 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
174         return isInterruptedThread(this);
175 #else
176         log_text("Java_java_lang_VMThread_isInterrupted  called");
177         return 0;
178 #endif
179 }
180
181
182 /*
183  * Class:     java/lang/VMThread
184  * Method:    registerNatives
185  * Signature: ()V
186  */
187 JNIEXPORT void JNICALL Java_java_lang_VMThread_registerNatives(JNIEnv *env, jclass clazz)
188 {
189         /* empty */
190 }
191
192
193 /*
194  * Class:     java/lang/VMThread
195  * Method:    resume
196  * Signature: ()V
197  */
198 JNIEXPORT void JNICALL Java_java_lang_VMThread_resume(JNIEnv *env, java_lang_VMThread *this)
199 {
200         if (runverbose)
201                 log_text("java_lang_VMThread_resume0 called");
202
203 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
204         resumeThread((thread *) this->thread);
205 #endif
206 }
207
208
209 /*
210  * Class:     java/lang/VMThread
211  * Method:    nativeSetPriority
212  * Signature: (I)V
213  */
214 JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeSetPriority(JNIEnv *env, java_lang_VMThread *this, s4 par1)
215 {
216     if (runverbose) 
217                 log_text("java_lang_VMThread_nativeSetPriority called");
218
219 #if defined(USE_THREADS)
220         setPriorityThread((thread *) this->thread, par1);
221 #endif
222 }
223
224
225 /*
226  * Class:     java/lang/VMThread
227  * Method:    sleep
228  * Signature: (JI)V
229  */
230 JNIEXPORT void JNICALL Java_java_lang_VMThread_sleep(JNIEnv *env, jclass clazz, s8 millis, s4 nanos)
231 {
232 #if defined(USE_THREADS)
233         sleepThread(millis, nanos);
234 #endif
235 }
236
237
238 /*
239  * Class:     java/lang/VMThread
240  * Method:    start
241  * Signature: ()V
242  */
243 JNIEXPORT void JNICALL Java_java_lang_VMThread_start(JNIEnv *env, java_lang_VMThread *this, s8 par1)
244 {
245         if (runverbose) 
246                 log_text("java_lang_VMThread_start called");
247
248 #if defined(USE_THREADS)
249         this->thread->vmThread = this;
250         startThread((thread *) this->thread);
251 #endif
252 }
253
254
255 /*
256  * Class:     java/lang/VMThread
257  * Method:    nativeStop
258  * Signature: (Ljava/lang/Object;)V
259  */
260 JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeStop(JNIEnv *env, java_lang_VMThread *this, java_lang_Throwable *par1)
261 {
262         if (runverbose)
263                 log_text ("java_lang_VMThread_nativeStop called");
264
265
266 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
267         if (currentThread == (thread*)this->thread) {
268                 log_text("killing");
269                 killThread(0);
270                 /*
271                   exceptionptr = proto_java_lang_ThreadDeath;
272                   return;
273                 */
274
275         } else {
276                 /*CONTEXT((thread*)this)*/ this->flags |= THREAD_FLAGS_KILLED;
277                 resumeThread((thread*)this->thread);
278         }
279 #endif
280 }
281
282
283 /*
284  * Class:     java/lang/VMThread
285  * Method:    suspend
286  * Signature: ()V
287  */
288 JNIEXPORT void JNICALL Java_java_lang_VMThread_suspend(JNIEnv *env, java_lang_VMThread *this)
289 {
290         if (runverbose)
291                 log_text("java_lang_VMThread_suspend called");
292
293 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
294         suspendThread((thread*)this->thread);
295 #endif
296 }
297
298
299 /*
300  * Class:     java/lang/VMThread
301  * Method:    yield
302  * Signature: ()V
303  */
304 JNIEXPORT void JNICALL Java_java_lang_VMThread_yield(JNIEnv *env, jclass clazz)
305 {
306         if (runverbose)
307                 log_text("java_lang_VMThread_yield called");
308
309 #if defined(USE_THREADS)
310         yieldThread();
311 #endif
312 }
313
314
315 /*
316  * Class:     java/lang/VMThread
317  * Method:    interrupted
318  * Signature: ()Z
319  */
320 JNIEXPORT s4 JNICALL Java_java_lang_VMThread_interrupted(JNIEnv *env, jclass clazz)
321 {
322 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
323         return interruptedThread();
324 #else
325         log_text("Java_java_lang_VMThread_interrupted");
326         return 0;
327 #endif
328 }
329
330
331 /*
332  * Class:     java/lang/VMThread
333  * Method:    nativeInit
334  * Signature: (J)V
335  */
336 JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeInit(JNIEnv *env, java_lang_VMThread *this, s8 par1)
337 {
338 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
339         initThread(this);
340 #endif
341 }
342
343
344 /*
345  * Class:     java/lang/VMThread
346  * Method:    holdsLock
347  * Signature: (Ljava/lang/Object;)Z
348  */
349 JNIEXPORT s4 JNICALL Java_java_lang_VMThread_holdsLock(JNIEnv *env, jclass clazz, java_lang_Object* o)
350 {
351 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
352         return threadHoldsLock((threadobject*) THREADOBJECT,
353                                                    (java_objectheader *) o);
354 #else
355         /* I don't know how to find out [stefan] */
356         return 0;
357 #endif
358 }
359
360
361 /*
362  * These are local overrides for various environment variables in Emacs.
363  * Please do not remove this and leave it at the end of the file, where
364  * Emacs will automagically detect them.
365  * ---------------------------------------------------------------------
366  * Local variables:
367  * mode: c
368  * indent-tabs-mode: t
369  * c-basic-offset: 4
370  * tab-width: 4
371  * End:
372  */