Line data Source code
1 : /* src/native/vm/gnuclasspath/java_lang_VMThread.cpp
2 :
3 : Copyright (C) 1996-2013
4 : CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 :
6 : This file is part of CACAO.
7 :
8 : This program is free software; you can redistribute it and/or
9 : modify it under the terms of the GNU General Public License as
10 : published by the Free Software Foundation; either version 2, or (at
11 : your option) any later version.
12 :
13 : This program is distributed in the hope that it will be useful, but
14 : WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 : General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with this program; if not, write to the Free Software
20 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 : 02110-1301, USA.
22 :
23 : */
24 :
25 :
26 : #include "config.h"
27 :
28 : #include <assert.h>
29 : #include <stdint.h>
30 :
31 : #include "native/jni.hpp"
32 : #include "native/llni.hpp"
33 : #include "native/native.hpp"
34 :
35 : #include "toolbox/logging.hpp"
36 :
37 : #if defined(ENABLE_JNI_HEADERS)
38 : # include "native/vm/include/java_lang_VMThread.h"
39 : #endif
40 :
41 : #include "threads/lock.hpp"
42 : #include "threads/thread.hpp"
43 :
44 : #include "vm/exceptions.hpp"
45 : #include "vm/javaobjects.hpp"
46 : #include "vm/string.hpp"
47 : #include "vm/utf8.hpp"
48 : #include "vm/vm.hpp"
49 :
50 :
51 : // Native functions are exported as C functions.
52 : extern "C" {
53 :
54 : /*
55 : * Class: java/lang/VMThread
56 : * Method: countStackFrames
57 : * Signature: ()I
58 : */
59 0 : JNIEXPORT jint JNICALL Java_java_lang_VMThread_countStackFrames(JNIEnv *env, jobject _this)
60 : {
61 0 : log_println("Java_java_lang_VMThread_countStackFrames: Deprecated. Not implemented.");
62 :
63 0 : return 0;
64 : }
65 :
66 :
67 : /*
68 : * Class: java/lang/VMThread
69 : * Method: start
70 : * Signature: (J)V
71 : */
72 10 : JNIEXPORT void JNICALL Java_java_lang_VMThread_start(JNIEnv *env, jobject _this, jlong stacksize)
73 : {
74 10 : java_lang_VMThread jlvmt(_this);
75 :
76 10 : threads_thread_start(jlvmt.get_thread());
77 10 : }
78 :
79 :
80 : /*
81 : * Class: java/lang/VMThread
82 : * Method: interrupt
83 : * Signature: ()V
84 : */
85 20000 : JNIEXPORT void JNICALL Java_java_lang_VMThread_interrupt(JNIEnv *env, jobject _this)
86 : {
87 20000 : thread_handle_interrupt((java_handle_t *) _this);
88 20000 : }
89 :
90 :
91 : /*
92 : * Class: java/lang/VMThread
93 : * Method: isInterrupted
94 : * Signature: ()Z
95 : */
96 0 : JNIEXPORT jboolean JNICALL Java_java_lang_VMThread_isInterrupted(JNIEnv *env, jobject _this)
97 : {
98 0 : return thread_handle_is_interrupted((java_handle_t *) _this);
99 : }
100 :
101 :
102 : /*
103 : * Class: java/lang/VMThread
104 : * Method: suspend
105 : * Signature: ()V
106 : */
107 0 : JNIEXPORT void JNICALL Java_java_lang_VMThread_suspend(JNIEnv *env, jobject _this)
108 : {
109 0 : log_println("Java_java_lang_VMThread_suspend: Deprecated. Not implemented.");
110 0 : }
111 :
112 :
113 : /*
114 : * Class: java/lang/VMThread
115 : * Method: resume
116 : * Signature: ()V
117 : */
118 0 : JNIEXPORT void JNICALL Java_java_lang_VMThread_resume(JNIEnv *env, jobject _this)
119 : {
120 0 : log_println("Java_java_lang_VMThread_resume: Deprecated. Not implemented.");
121 0 : }
122 :
123 :
124 : /*
125 : * Class: java/lang/VMThread
126 : * Method: nativeSetPriority
127 : * Signature: (I)V
128 : */
129 0 : JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeSetPriority(JNIEnv *env, jobject _this, jint priority)
130 : {
131 0 : thread_handle_set_priority((java_handle_t *) _this, priority);
132 0 : }
133 :
134 :
135 : /*
136 : * Class: java/lang/VMThread
137 : * Method: nativeStop
138 : * Signature: (Ljava/lang/Throwable;)V
139 : */
140 0 : JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeStop(JNIEnv *env, jobject _this, jobject t)
141 : {
142 0 : log_println("Java_java_lang_VMThread_nativeStop: Deprecated. Not implemented.");
143 0 : }
144 :
145 :
146 : /*
147 : * Class: java/lang/VMThread
148 : * Method: currentThread
149 : * Signature: ()Ljava/lang/Thread;
150 : */
151 21990 : JNIEXPORT jobject JNICALL Java_java_lang_VMThread_currentThread(JNIEnv *env, jclass clazz)
152 : {
153 21990 : return (jobject) thread_get_current_object();
154 : }
155 :
156 :
157 : /*
158 : * Class: java/lang/VMThread
159 : * Method: yield
160 : * Signature: ()V
161 : */
162 0 : JNIEXPORT void JNICALL Java_java_lang_VMThread_yield(JNIEnv *env, jclass clazz)
163 : {
164 0 : threads_yield();
165 0 : }
166 :
167 :
168 : /*
169 : * Class: java/lang/VMThread
170 : * Method: sleep
171 : * Signature: (JI)V
172 : */
173 1 : JNIEXPORT void JNICALL Java_java_lang_VMThread_sleep(JNIEnv *env, jclass clazz, int64_t ms, int32_t ns)
174 : {
175 1 : threads_sleep(ms, ns);
176 1 : }
177 :
178 :
179 : /*
180 : * Class: java/lang/VMThread
181 : * Method: interrupted
182 : * Signature: ()Z
183 : */
184 1 : JNIEXPORT jboolean JNICALL Java_java_lang_VMThread_interrupted(JNIEnv *env, jclass clazz)
185 : {
186 1 : threadobject *t = thread_get_current();
187 1 : int32_t interrupted = thread_is_interrupted(t);
188 :
189 1 : if (interrupted)
190 0 : thread_set_interrupted(t, false);
191 :
192 1 : return interrupted;
193 : }
194 :
195 :
196 : /*
197 : * Class: java/lang/VMThread
198 : * Method: holdsLock
199 : * Signature: (Ljava/lang/Object;)Z
200 : */
201 2 : JNIEXPORT jboolean JNICALL Java_java_lang_VMThread_holdsLock(JNIEnv *env, jclass clazz, jobject o)
202 : {
203 2 : java_handle_t *h = (java_handle_t *) o;
204 :
205 2 : if (h == NULL) {
206 0 : exceptions_throw_nullpointerexception();
207 0 : return 0;
208 : }
209 :
210 2 : return lock_is_held_by_current_thread(h);
211 : }
212 :
213 :
214 : /*
215 : * Class: java/lang/VMThread
216 : * Method: getState
217 : * Signature: ()Ljava/lang/String;
218 : */
219 0 : JNIEXPORT jstring JNICALL Java_java_lang_VMThread_getState(JNIEnv *env, jobject _this)
220 : {
221 0 : java_handle_t *h = (java_handle_t *) _this;
222 0 : int state = thread_handle_get_state(h);
223 :
224 0 : switch (state) {
225 : case THREAD_STATE_NEW:
226 0 : return (jstring) JavaString::from_utf8("NEW");
227 : case THREAD_STATE_RUNNABLE:
228 0 : return (jstring) JavaString::from_utf8("RUNNABLE");
229 : case THREAD_STATE_BLOCKED:
230 0 : return (jstring) JavaString::from_utf8("BLOCKED");
231 : case THREAD_STATE_WAITING:
232 0 : return (jstring) JavaString::from_utf8("WAITING");
233 : case THREAD_STATE_TIMED_WAITING:
234 0 : return (jstring) JavaString::from_utf8("TIMED_WAITING");
235 : case THREAD_STATE_PARKED:
236 0 : return (jstring) JavaString::from_utf8("PARKED");
237 : case THREAD_STATE_TIMED_PARKED:
238 0 : return (jstring) JavaString::from_utf8("TIMED_PARKED");
239 : case THREAD_STATE_TERMINATED:
240 0 : return (jstring) JavaString::from_utf8("TERMINATED");
241 : default:
242 0 : vm_abort("Java_java_lang_VMThread_getState: unknown thread state %d", state);
243 0 : return NULL; /* Keep compiler happy. */
244 : }
245 : }
246 :
247 : } // extern "C"
248 :
249 :
250 : /* native methods implemented by this file ************************************/
251 :
252 : static JNINativeMethod methods[] = {
253 : { (char*) "countStackFrames", (char*) "()I", (void*) (uintptr_t) &Java_java_lang_VMThread_countStackFrames },
254 : { (char*) "start", (char*) "(J)V", (void*) (uintptr_t) &Java_java_lang_VMThread_start },
255 : { (char*) "interrupt", (char*) "()V", (void*) (uintptr_t) &Java_java_lang_VMThread_interrupt },
256 : { (char*) "isInterrupted", (char*) "()Z", (void*) (uintptr_t) &Java_java_lang_VMThread_isInterrupted },
257 : { (char*) "suspend", (char*) "()V", (void*) (uintptr_t) &Java_java_lang_VMThread_suspend },
258 : { (char*) "resume", (char*) "()V", (void*) (uintptr_t) &Java_java_lang_VMThread_resume },
259 : { (char*) "nativeSetPriority", (char*) "(I)V", (void*) (uintptr_t) &Java_java_lang_VMThread_nativeSetPriority },
260 : { (char*) "nativeStop", (char*) "(Ljava/lang/Throwable;)V", (void*) (uintptr_t) &Java_java_lang_VMThread_nativeStop },
261 : { (char*) "currentThread", (char*) "()Ljava/lang/Thread;", (void*) (uintptr_t) &Java_java_lang_VMThread_currentThread },
262 : { (char*) "yield", (char*) "()V", (void*) (uintptr_t) &Java_java_lang_VMThread_yield },
263 : { (char*) "sleep", (char*) "(JI)V", (void*) (uintptr_t) &Java_java_lang_VMThread_sleep },
264 : { (char*) "interrupted", (char*) "()Z", (void*) (uintptr_t) &Java_java_lang_VMThread_interrupted },
265 : { (char*) "holdsLock", (char*) "(Ljava/lang/Object;)Z", (void*) (uintptr_t) &Java_java_lang_VMThread_holdsLock },
266 : { (char*) "getState", (char*) "()Ljava/lang/String;", (void*) (uintptr_t) &Java_java_lang_VMThread_getState },
267 : };
268 :
269 :
270 : /* _Jv_java_lang_VMThread_init *************************************************
271 :
272 : Register native functions.
273 :
274 : *******************************************************************************/
275 :
276 163 : void _Jv_java_lang_VMThread_init(void)
277 : {
278 163 : Utf8String u = Utf8String::from_utf8("java/lang/VMThread");
279 :
280 163 : NativeMethods& nm = VM::get_current()->get_nativemethods();
281 163 : nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
282 163 : }
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 : */
|