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