Merged branch subtype-trunk into default.
[cacao.git] / src / native / vm / cldc1.1 / java_lang_Thread.cpp
1 /* src/native/vm/cldc1.1/java_lang_Thread.cpp
2
3    Copyright (C) 2006, 2007, 2008
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 <stdint.h>
29
30 #include "native/jni.hpp"
31 #include "native/native.hpp"
32
33 #if defined(ENABLE_JNI_HEADERS)
34 # include "native/include/java_lang_Thread.h"
35 #endif
36
37 #include "threads/thread.hpp"
38 #include "threads/threadlist.hpp"
39
40 #include "toolbox/logging.hpp"
41
42 #include "vm/jit/builtin.hpp"
43 #include "vm/javaobjects.hpp"
44
45
46 // Native functions are exported as C functions.
47 extern "C" {
48
49 /*
50  * Class:     java/lang/Thread
51  * Method:    currentThread
52  * Signature: ()Ljava/lang/Thread;
53  */
54 JNIEXPORT jobject JNICALL Java_java_lang_Thread_currentThread(JNIEnv *env, jclass clazz)
55 {
56         return (jobject) thread_get_current_object();
57 }
58
59
60 /*
61  * Class:     java/lang/Thread
62  * Method:    setPriority0
63  * Signature: (II)V
64  */
65 JNIEXPORT void JNICALL Java_java_lang_Thread_setPriority0(JNIEnv *env, jobject _this, jint oldPriority, jint newPriority)
66 {
67 #if defined(ENABLE_THREADS)
68         java_lang_Thread jlt(_this);
69         threadobject* t = jlt.get_vm_thread();
70
71         // The threadobject is null when a thread is created in Java. The
72         // priority is set later during startup.
73         if (t == NULL)
74                 return;
75
76         threads_set_thread_priority(t->tid, newPriority);
77 #endif
78 }
79
80
81 /*
82  * Class:     java/lang/Thread
83  * Method:    sleep
84  * Signature: (J)V
85  */
86 JNIEXPORT void JNICALL Java_java_lang_Thread_sleep(JNIEnv *env, jclass clazz, jlong millis)
87 {
88 #if defined(ENABLE_THREADS)
89         threads_sleep(millis, 0);
90 #endif
91 }
92
93
94 /*
95  * Class:     java/lang/Thread
96  * Method:    start0
97  * Signature: ()V
98  */
99 JNIEXPORT void JNICALL Java_java_lang_Thread_start0(JNIEnv *env, jobject _this)
100 {
101 #if defined(ENABLE_THREADS)
102         java_lang_Thread jlt(_this);
103         threads_thread_start(jlt.get_handle());
104 #endif
105 }
106
107
108 /*
109  * Class:     java/lang/Thread
110  * Method:    isAlive
111  * Signature: ()Z
112  */
113 JNIEXPORT jboolean JNICALL Java_java_lang_Thread_isAlive(JNIEnv *env, jobject _this)
114 {
115 #if defined(ENABLE_THREADS)
116         java_lang_Thread jlt(_this);
117         threadobject* t = jlt.get_vm_thread();
118
119         if (t == NULL)
120                 return 0;
121
122         bool result = threads_thread_is_alive(t);
123
124         return result;
125 #else
126         // If threads are disabled, the only thread running is alive.
127         return 1;
128 #endif
129 }
130
131
132 /*
133  * Class:     java/lang/Thread
134  * Method:    activeCount
135  * Signature: ()I
136  */
137 JNIEXPORT s4 JNICALL Java_java_lang_Thread_activeCount(JNIEnv *env, jclass clazz)
138 {
139 #if defined(ENABLE_THREADS)
140         return ThreadList::get_number_of_non_daemon_threads();
141 #else
142         return 1;
143 #endif
144 }
145
146
147 /*
148  * Class:     java/lang/Thread
149  * Method:    interrupt0
150  * Signature: ()V
151  */
152 JNIEXPORT void JNICALL Java_java_lang_Thread_interrupt0(JNIEnv *env, jobject _this)
153 {
154 #if defined(ENABLE_THREADS)
155         java_lang_Thread jlt(_this);
156         threadobject* t = jlt.get_vm_thread();
157         threads_thread_interrupt(t);
158 #endif
159 }
160
161 #if 0
162 /*
163  * Class:     java/lang/Thread
164  * Method:    internalExit
165  * Signature: ()V
166  */
167 JNIEXPORT void JNICALL Java_java_lang_Thread_internalExit(JNIEnv *env, jobject _this)
168 {
169 }
170 #endif
171
172
173 /*
174  * Class:     java/lang/Thread
175  * Method:    yield
176  * Signature: ()V
177  */
178 JNIEXPORT void JNICALL Java_java_lang_Thread_yield(JNIEnv *env, jclass clazz)
179 {
180 #if defined(ENABLE_THREADS)
181         threads_yield();
182 #endif
183 }
184
185 } // extern "C"
186
187
188 /* native methods implemented by this file ************************************/
189  
190 static JNINativeMethod methods[] = {
191         { (char*) "currentThread", (char*) "()Ljava/lang/Thread;", (void*) (uintptr_t) &Java_java_lang_Thread_currentThread },
192         { (char*) "setPriority0",  (char*) "(II)V",                (void*) (uintptr_t) &Java_java_lang_Thread_setPriority0  },
193         { (char*) "sleep",         (char*) "(J)V",                 (void*) (uintptr_t) &Java_java_lang_Thread_sleep         },
194         { (char*) "start0",        (char*) "()V",                  (void*) (uintptr_t) &Java_java_lang_Thread_start0        },
195         { (char*) "isAlive",       (char*) "()Z",                  (void*) (uintptr_t) &Java_java_lang_Thread_isAlive       },
196         { (char*) "activeCount",   (char*) "()I",                  (void*) (uintptr_t) &Java_java_lang_Thread_activeCount   },
197         { (char*) "setPriority0",  (char*) "(II)V",                (void*) (uintptr_t) &Java_java_lang_Thread_setPriority0  },
198         { (char*) "interrupt0",    (char*) "()V",                  (void*) (uintptr_t) &Java_java_lang_Thread_interrupt0    },
199 #if 0
200         { (char*) "internalExit",  (char*) "()V",                  (void*) (uintptr_t) &Java_java_lang_Thread_internalExit  },
201 #endif
202         { (char*) "yield",         (char*) "()V",                  (void*) (uintptr_t) &Java_java_lang_Thread_yield         },
203 };
204
205
206 /* _Jv_java_lang_Thread_init ***************************************************
207  
208    Register native functions.
209  
210 *******************************************************************************/
211  
212 void _Jv_java_lang_Thread_init(void)
213 {
214         utf* u = utf_new_char("java/lang/Thread");
215  
216         NativeMethods& nm = VM::get_current()->get_nativemethods();
217         nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
218 }
219
220
221 /*
222  * These are local overrides for various environment variables in Emacs.
223  * Please do not remove this and leave it at the end of the file, where
224  * Emacs will automagically detect them.
225  * ---------------------------------------------------------------------
226  * Local variables:
227  * mode: c++
228  * indent-tabs-mode: t
229  * c-basic-offset: 4
230  * tab-width: 4
231  * End:
232  * vim:noexpandtab:sw=4:ts=4:
233  */