CACAO
java_lang_VMThread.cpp
Go to the documentation of this file.
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 JNIEXPORT jint JNICALL Java_java_lang_VMThread_countStackFrames(JNIEnv *env, jobject _this)
60 {
61  log_println("Java_java_lang_VMThread_countStackFrames: Deprecated. Not implemented.");
62 
63  return 0;
64 }
65 
66 
67 /*
68  * Class: java/lang/VMThread
69  * Method: start
70  * Signature: (J)V
71  */
72 JNIEXPORT void JNICALL Java_java_lang_VMThread_start(JNIEnv *env, jobject _this, jlong stacksize)
73 {
74  java_lang_VMThread jlvmt(_this);
75 
77 }
78 
79 
80 /*
81  * Class: java/lang/VMThread
82  * Method: interrupt
83  * Signature: ()V
84  */
85 JNIEXPORT void JNICALL Java_java_lang_VMThread_interrupt(JNIEnv *env, jobject _this)
86 {
88 }
89 
90 
91 /*
92  * Class: java/lang/VMThread
93  * Method: isInterrupted
94  * Signature: ()Z
95  */
96 JNIEXPORT jboolean JNICALL Java_java_lang_VMThread_isInterrupted(JNIEnv *env, jobject _this)
97 {
99 }
100 
101 
102 /*
103  * Class: java/lang/VMThread
104  * Method: suspend
105  * Signature: ()V
106  */
107 JNIEXPORT void JNICALL Java_java_lang_VMThread_suspend(JNIEnv *env, jobject _this)
108 {
109  log_println("Java_java_lang_VMThread_suspend: Deprecated. Not implemented.");
110 }
111 
112 
113 /*
114  * Class: java/lang/VMThread
115  * Method: resume
116  * Signature: ()V
117  */
118 JNIEXPORT void JNICALL Java_java_lang_VMThread_resume(JNIEnv *env, jobject _this)
119 {
120  log_println("Java_java_lang_VMThread_resume: Deprecated. Not implemented.");
121 }
122 
123 
124 /*
125  * Class: java/lang/VMThread
126  * Method: nativeSetPriority
127  * Signature: (I)V
128  */
129 JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeSetPriority(JNIEnv *env, jobject _this, jint priority)
130 {
131  thread_handle_set_priority((java_handle_t *) _this, priority);
132 }
133 
134 
135 /*
136  * Class: java/lang/VMThread
137  * Method: nativeStop
138  * Signature: (Ljava/lang/Throwable;)V
139  */
140 JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeStop(JNIEnv *env, jobject _this, jobject t)
141 {
142  log_println("Java_java_lang_VMThread_nativeStop: Deprecated. Not implemented.");
143 }
144 
145 
146 /*
147  * Class: java/lang/VMThread
148  * Method: currentThread
149  * Signature: ()Ljava/lang/Thread;
150  */
152 {
154 }
155 
156 
157 /*
158  * Class: java/lang/VMThread
159  * Method: yield
160  * Signature: ()V
161  */
162 JNIEXPORT void JNICALL Java_java_lang_VMThread_yield(JNIEnv *env, jclass clazz)
163 {
164  threads_yield();
165 }
166 
167 
168 /*
169  * Class: java/lang/VMThread
170  * Method: sleep
171  * Signature: (JI)V
172  */
173 JNIEXPORT void JNICALL Java_java_lang_VMThread_sleep(JNIEnv *env, jclass clazz, int64_t ms, int32_t ns)
174 {
175  threads_sleep(ms, ns);
176 }
177 
178 
179 /*
180  * Class: java/lang/VMThread
181  * Method: interrupted
182  * Signature: ()Z
183  */
184 JNIEXPORT jboolean JNICALL Java_java_lang_VMThread_interrupted(JNIEnv *env, jclass clazz)
185 {
187  int32_t interrupted = thread_is_interrupted(t);
188 
189  if (interrupted)
190  thread_set_interrupted(t, false);
191 
192  return interrupted;
193 }
194 
195 
196 /*
197  * Class: java/lang/VMThread
198  * Method: holdsLock
199  * Signature: (Ljava/lang/Object;)Z
200  */
201 JNIEXPORT jboolean JNICALL Java_java_lang_VMThread_holdsLock(JNIEnv *env, jclass clazz, jobject o)
202 {
203  java_handle_t *h = (java_handle_t *) o;
204 
205  if (h == NULL) {
207  return 0;
208  }
209 
211 }
212 
213 
214 /*
215  * Class: java/lang/VMThread
216  * Method: getState
217  * Signature: ()Ljava/lang/String;
218  */
220 {
221  java_handle_t *h = (java_handle_t *) _this;
222  int state = thread_handle_get_state(h);
223 
224  switch (state) {
225  case THREAD_STATE_NEW:
226  return (jstring) JavaString::from_utf8("NEW");
228  return (jstring) JavaString::from_utf8("RUNNABLE");
230  return (jstring) JavaString::from_utf8("BLOCKED");
232  return (jstring) JavaString::from_utf8("WAITING");
234  return (jstring) JavaString::from_utf8("TIMED_WAITING");
235  case THREAD_STATE_PARKED:
236  return (jstring) JavaString::from_utf8("PARKED");
238  return (jstring) JavaString::from_utf8("TIMED_PARKED");
240  return (jstring) JavaString::from_utf8("TERMINATED");
241  default:
242  vm_abort("Java_java_lang_VMThread_getState: unknown thread state %d", state);
243  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 
277 {
278  Utf8String u = Utf8String::from_utf8("java/lang/VMThread");
279 
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  */
Table containing all native methods registered with the VM.
Definition: native.hpp:132
NativeMethods & get_nativemethods()
Definition: vm.hpp:128
void register_methods(Utf8String classname, const JNINativeMethod *methods, size_t count)
Register native methods with the VM.
Definition: native.cpp:242
GNU Classpath java/lang/VMThread.
_Jv_JNIEnv JNIEnv
Definition: jni.hpp:112
JNIEXPORT void JNICALL Java_java_lang_VMThread_start(JNIEnv *env, jobject _this, jlong stacksize)
#define NATIVE_METHODS_COUNT
Definition: native.hpp:45
static JavaString from_utf8(Utf8String)
Definition: string.cpp:184
static java_handle_t * thread_get_current_object(void)
Definition: thread.hpp:226
JNIEXPORT jint JNICALL Java_java_lang_VMThread_countStackFrames(JNIEnv *env, jobject _this)
bool thread_handle_is_interrupted(java_handle_t *th)
Definition: thread.cpp:981
JNIEXPORT jobject JNICALL Java_java_lang_VMThread_currentThread(JNIEnv *env, jclass clazz)
int thread_handle_get_state(java_handle_t *th)
Definition: thread.cpp:1012
JNIEXPORT jboolean JNICALL Java_java_lang_VMThread_interrupted(JNIEnv *env, jclass clazz)
void log_println(const char *text,...)
Definition: logging.cpp:193
void vm_abort(const char *text,...)
Definition: vm.cpp:2586
void threads_thread_start(java_handle_t *object)
Definition: thread.cpp:445
java_handle_t * get_thread() const
JNIEXPORT void JNICALL Java_java_lang_VMThread_interrupt(JNIEnv *env, jobject _this)
JNIEXPORT void JNICALL Java_java_lang_VMThread_yield(JNIEnv *env, jclass clazz)
static JNINativeMethod methods[]
void exceptions_throw_nullpointerexception(void)
static Utf8String from_utf8(const char *, size_t)
Definition: utf8.cpp:335
bool thread_is_interrupted(threadobject *t)
Definition: thread.cpp:926
JNIEXPORT jboolean JNICALL Java_java_lang_VMThread_holdsLock(JNIEnv *env, jclass clazz, jobject o)
void thread_handle_set_priority(java_handle_t *th, int priority)
Definition: thread.cpp:964
JNIEXPORT void JNICALL Java_java_lang_VMThread_resume(JNIEnv *env, jobject _this)
void thread_set_interrupted(threadobject *t, bool interrupted)
Definition: thread.cpp:949
JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeStop(JNIEnv *env, jobject _this, jobject t)
JNIEXPORT void JNICALL Java_java_lang_VMThread_nativeSetPriority(JNIEnv *env, jobject _this, jint priority)
static threadobject * thread_get_current()
Return the threadobject for the current thread.
Definition: thread-none.hpp:56
bool lock_is_held_by_current_thread(java_handle_t *o)
Definition: lock.cpp:1280
JNIEXPORT jstring JNICALL Java_java_lang_VMThread_getState(JNIEnv *env, jobject _this)
JNIEXPORT jboolean JNICALL Java_java_lang_VMThread_isInterrupted(JNIEnv *env, jobject _this)
JNIEXPORT void JNICALL Java_java_lang_VMThread_sleep(JNIEnv *env, jclass clazz, int64_t ms, int32_t ns)
void _Jv_java_lang_VMThread_init(void)
JNIEXPORT void JNICALL Java_java_lang_VMThread_suspend(JNIEnv *env, jobject _this)
void threads_sleep(int64_t millis, int32_t nanos)
void threads_yield(void)
static VM * get_current()
Definition: vm.hpp:99
void thread_handle_interrupt(java_handle_t *th)
Definition: thread.cpp:995