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