* This commit implementes C++ wrapper classes for CLDC 1.1. Again to
[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.h"
31 #include "native/native.h"
32
33 #if defined(ENABLE_JNI_HEADERS)
34 # include "native/include/java_lang_Thread.h"
35 #endif
36
37 #include "threads/thread.hpp"
38
39 #include "toolbox/logging.h"
40
41 #include "vm/builtin.h"
42
43 #include "vmcore/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 #if 0
133 /*
134  * Class:     java/lang/Thread
135  * Method:    activeCount
136  * Signature: ()I
137  */
138 JNIEXPORT s4 JNICALL Java_java_lang_Thread_activeCount(JNIEnv *env, jclass clazz)
139 {
140 }
141
142
143 /*
144  * Class:     java/lang/Thread
145  * Method:    interrupt0
146  * Signature: ()V
147  */
148 JNIEXPORT void JNICALL Java_java_lang_Thread_interrupt0(JNIEnv *env, jobject _this)
149 {
150 }
151
152
153 /*
154  * Class:     java/lang/Thread
155  * Method:    internalExit
156  * Signature: ()V
157  */
158 JNIEXPORT void JNICALL Java_java_lang_Thread_internalExit(JNIEnv *env, jobject _this)
159 {
160 }
161 #endif
162
163
164 /*
165  * Class:     java/lang/Thread
166  * Method:    yield
167  * Signature: ()V
168  */
169 JNIEXPORT void JNICALL Java_java_lang_Thread_yield(JNIEnv *env, jclass clazz)
170 {
171 #if defined(ENABLE_THREADS)
172         threads_yield();
173 #endif
174 }
175
176 } // extern "C"
177
178
179 /* native methods implemented by this file ************************************/
180  
181 static JNINativeMethod methods[] = {
182         { (char*) "currentThread", (char*) "()Ljava/lang/Thread;", (void*) (uintptr_t) &Java_java_lang_Thread_currentThread },
183         { (char*) "setPriority0",  (char*) "(II)V",                (void*) (uintptr_t) &Java_java_lang_Thread_setPriority0  },
184         { (char*) "sleep",         (char*) "(J)V",                 (void*) (uintptr_t) &Java_java_lang_Thread_sleep         },
185         { (char*) "start0",        (char*) "()V",                  (void*) (uintptr_t) &Java_java_lang_Thread_start0        },
186         { (char*) "isAlive",       (char*) "()Z",                  (void*) (uintptr_t) &Java_java_lang_Thread_isAlive       },
187 #if 0
188         { (char*) "activeCount",   (char*) "()I",                  (void*) (uintptr_t) &Java_java_lang_Thread_activeCount   },
189         { (char*) "setPriority0",  (char*) "(II)V",                (void*) (uintptr_t) &Java_java_lang_Thread_setPriority0  },
190         { (char*) "interrupt0",    (char*) "()V",                  (void*) (uintptr_t) &Java_java_lang_Thread_interrupt0    },
191         { (char*) "internalExit",  (char*) "()V",                  (void*) (uintptr_t) &Java_java_lang_Thread_internalExit  },
192 #endif
193         { (char*) "yield",         (char*) "()V",                  (void*) (uintptr_t) &Java_java_lang_Thread_yield         },
194 };
195
196
197 /* _Jv_java_lang_Thread_init ***************************************************
198  
199    Register native functions.
200  
201 *******************************************************************************/
202  
203 // FIXME
204 extern "C" {
205 void _Jv_java_lang_Thread_init(void)
206 {
207         utf *u;
208  
209         u = utf_new_char("java/lang/Thread");
210  
211         native_method_register(u, methods, NATIVE_METHODS_COUNT);
212 }
213 }
214
215
216 /*
217  * These are local overrides for various environment variables in Emacs.
218  * Please do not remove this and leave it at the end of the file, where
219  * Emacs will automagically detect them.
220  * ---------------------------------------------------------------------
221  * Local variables:
222  * mode: c++
223  * indent-tabs-mode: t
224  * c-basic-offset: 4
225  * tab-width: 4
226  * End:
227  * vim:noexpandtab:sw=4:ts=4:
228  */