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