09a928767c3b2f89fbee4c6c9abdce77d5324f9b
[cacao.git] / src / native / vm / VMThread.c
1 /* src/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             Christian Thalinger
31
32    $Id: VMThread.c 3575 2005-11-05 16:26:56Z twisti $
33
34 */
35
36
37 #include "config.h"
38 #include "vm/types.h"
39
40 #include "native/jni.h"
41 #include "native/native.h"
42 #include "native/include/java_lang_ThreadGroup.h"
43 #include "native/include/java_lang_Object.h"            /* java_lang_Thread.h */
44 #include "native/include/java_lang_Throwable.h"         /* java_lang_Thread.h */
45 #include "native/include/java_lang_VMThread.h"
46 #include "native/include/java_lang_Thread.h"
47
48 #if defined(USE_THREADS)
49 # if defined(NATIVE_THREADS)
50 #  include "threads/native/threads.h"
51 # else
52 #  include "threads/green/threads.h"
53 # endif
54 #endif
55
56 #include "toolbox/logging.h"
57 #include "vm/exceptions.h"
58 #include "vm/options.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 /*
75  * Class:     java/lang/VMThread
76  * Method:    start
77  * Signature: (J)V
78  */
79 JNIEXPORT void JNICALL Java_java_lang_VMThread_start(JNIEnv *env, java_lang_VMThread *this, s8 stacksize)
80 {
81 #if defined(USE_THREADS)
82         this->thread->vmThread = this;
83
84         /* don't pass a function pointer (NULL) since we want Thread.run()V here */
85
86         threads_start_thread((thread *) this->thread, NULL);
87 #endif
88 }
89
90
91 /*
92  * Class:     java/lang/VMThread
93  * Method:    interrupt
94  * Signature: ()V
95  */
96 JNIEXPORT void JNICALL Java_java_lang_VMThread_interrupt(JNIEnv *env, java_lang_VMThread *this)
97 {
98 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
99         interruptThread(this);
100 #else
101         log_text("Java_java_lang_VMThread_interrupt called");
102 #endif
103 }
104
105
106 /*
107  * Class:     java/lang/VMThread
108  * Method:    isInterrupted
109  * Signature: ()Z
110  */
111 JNIEXPORT s4 JNICALL Java_java_lang_VMThread_isInterrupted(JNIEnv *env, java_lang_VMThread *this)
112 {
113 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
114         return isInterruptedThread(this);
115 #else
116         log_text("Java_java_lang_VMThread_isInterrupted called");
117         return 0;
118 #endif
119 }
120
121
122 /*
123  * Class:     java/lang/VMThread
124  * Method:    suspend
125  * Signature: ()V
126  */
127 JNIEXPORT void JNICALL Java_java_lang_VMThread_suspend(JNIEnv *env, java_lang_VMThread *this)
128 {
129         if (runverbose)
130                 log_text("java_lang_VMThread_suspend called");
131
132 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
133         suspendThread((thread *) this->thread);
134 #endif
135 }
136
137
138 /*
139  * Class:     java/lang/VMThread
140  * Method:    resume
141  * Signature: ()V
142  */
143 JNIEXPORT void JNICALL Java_java_lang_VMThread_resume(JNIEnv *env, java_lang_VMThread *this)
144 {
145         if (runverbose)
146                 log_text("java_lang_VMThread_resume0 called");
147
148 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
149         resumeThread((thread *) this->thread);
150 #endif
151 }
152
153
154 /*
155  * Class:     java/lang/VMThread
156  * Method:    nativeSetPriority
157  * Signature: (I)V
158  */
159 JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeSetPriority(JNIEnv *env, java_lang_VMThread *this, s4 priority)
160 {
161     if (runverbose) 
162                 log_text("java_lang_VMThread_nativeSetPriority called");
163
164 #if defined(USE_THREADS)
165         setPriorityThread((thread *) this->thread, priority);
166 #endif
167 }
168
169
170 /*
171  * Class:     java/lang/VMThread
172  * Method:    nativeStop
173  * Signature: (Ljava/lang/Object;)V
174  */
175 JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeStop(JNIEnv *env, java_lang_VMThread *this, java_lang_Throwable *t)
176 {
177         if (runverbose)
178                 log_text ("java_lang_VMThread_nativeStop called");
179
180 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
181         if (currentThread == (thread *) this->thread) {
182                 log_text("killing");
183                 killThread(0);
184                 /*
185                   exceptionptr = proto_java_lang_ThreadDeath;
186                   return;
187                 */
188
189         } else {
190                 /*CONTEXT((thread*)this)*/ this->flags |= THREAD_FLAGS_KILLED;
191                 resumeThread((thread *) this->thread);
192         }
193 #endif
194 }
195
196
197 /*
198  * Class:     java/lang/VMThread
199  * Method:    currentThread
200  * Signature: ()Ljava/lang/Thread;
201  */
202 JNIEXPORT java_lang_Thread* JNICALL Java_java_lang_VMThread_currentThread(JNIEnv *env, jclass clazz)
203 {
204         java_lang_Thread *t;
205
206 #if defined(USE_THREADS)
207 #if defined(NATIVE_THREADS)
208         t = ((threadobject*) THREADOBJECT)->o.thread;
209 #else
210         t = (java_lang_Thread *) currentThread;
211 #endif
212
213         if (t == NULL)
214                 log_text("t ptr is NULL\n");
215   
216         if (!t->group) {
217                 /* ThreadGroup of currentThread is not initialized */
218
219                 t->group = (java_lang_ThreadGroup *)
220                         native_new_and_init(class_java_lang_ThreadGroup);
221
222                 if (t->group == NULL)
223                         log_text("unable to create ThreadGroup");
224         }
225 #else
226         /* we just return a fake java.lang.Thread object, otherwise we get
227        NullPointerException's in GNU classpath */
228
229         t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
230 #endif
231
232         return t;
233 }
234
235
236 /*
237  * Class:     java/lang/VMThread
238  * Method:    yield
239  * Signature: ()V
240  */
241 JNIEXPORT void JNICALL Java_java_lang_VMThread_yield(JNIEnv *env, jclass clazz)
242 {
243         if (runverbose)
244                 log_text("java_lang_VMThread_yield called");
245
246 #if defined(USE_THREADS)
247         yieldThread();
248 #endif
249 }
250
251
252 /*
253  * Class:     java/lang/VMThread
254  * Method:    interrupted
255  * Signature: ()Z
256  */
257 JNIEXPORT s4 JNICALL Java_java_lang_VMThread_interrupted(JNIEnv *env, jclass clazz)
258 {
259 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
260         return interruptedThread();
261 #else
262         log_text("Java_java_lang_VMThread_interrupted");
263         return 0;
264 #endif
265 }
266
267
268 /*
269  * Class:     java/lang/VMThread
270  * Method:    holdsLock
271  * Signature: (Ljava/lang/Object;)Z
272  */
273 JNIEXPORT s4 JNICALL Java_java_lang_VMThread_holdsLock(JNIEnv *env, jclass clazz, java_lang_Object* o)
274 {
275 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
276         return threadHoldsLock((threadobject*) THREADOBJECT,
277                                                    (java_objectheader *) o);
278 #else
279         /* I don't know how to find out [stefan] */
280         return 0;
281 #endif
282 }
283
284
285 /*
286  * These are local overrides for various environment variables in Emacs.
287  * Please do not remove this and leave it at the end of the file, where
288  * Emacs will automagically detect them.
289  * ---------------------------------------------------------------------
290  * Local variables:
291  * mode: c
292  * indent-tabs-mode: t
293  * c-basic-offset: 4
294  * tab-width: 4
295  * End:
296  */