* src/threads/native/threads.c: Rewritten such that threadobject
[cacao.git] / src / native / vm / java_lang_Thread.c
1 /* src/native/vm/java_lang_Thread.c - java/lang/Thread functions
2
3    Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Roman Obermaiser
28             Joseph Wenninger
29             Christian Thalinger
30
31    $Id: java_lang_VMThread.c 6213 2006-12-18 17:36:06Z twisti $
32
33 */
34
35
36 #include "config.h"
37 #include "vm/types.h"
38
39 #include "native/jni.h"
40 #include "native/native.h"
41 #include "native/include/java_lang_ThreadGroup.h"
42 #include "native/include/java_lang_Object.h"            /* java_lang_Thread.h */
43 #include "native/include/java_lang_Throwable.h"         /* java_lang_Thread.h */
44 #include "native/include/java_lang_Thread.h"
45
46 #if defined(ENABLE_THREADS)
47 # include "threads/native/threads.h"
48 #endif
49
50 #include "toolbox/logging.h"
51 #include "vm/options.h"
52
53
54 /*
55  * Class:     java/lang/Thread
56  * Method:    countStackFrames
57  * Signature: ()I
58  */
59 s4 _Jv_java_lang_Thread_countStackFrames(java_lang_Thread *this)
60 {
61     log_text("java_lang_Thread_countStackFrames called");
62
63     return 0;
64 }
65
66
67 /*
68  * Class:     java/lang/Thread
69  * Method:    start
70  * Signature: (J)V
71  */
72 void _Jv_java_lang_Thread_start(java_lang_Thread *this, s8 stacksize)
73 {
74 #if defined(ENABLE_THREADS)
75         threadobject *thread;
76
77         thread = (threadobject *) this;
78
79         /* don't pass a function pointer (NULL) since we want Thread.run()V here */
80
81         threads_start_thread(thread, NULL);
82 #endif
83 }
84
85
86 /*
87  * Class:     java/lang/Thread
88  * Method:    interrupt
89  * Signature: ()V
90  */
91 void _Jv_java_lang_Thread_interrupt(java_lang_Thread *this)
92 {
93 #if defined(ENABLE_THREADS)
94         threadobject *thread;
95
96         thread = (threadobject *) this;
97
98         threads_thread_interrupt(thread);
99 #endif
100 }
101
102
103 /*
104  * Class:     java/lang/Thread
105  * Method:    isInterrupted
106  * Signature: ()Z
107  */
108 s4 _Jv_java_lang_Thread_isInterrupted(java_lang_Thread *this)
109 {
110 #if defined(ENABLE_THREADS)
111         threadobject *thread;
112
113         thread = (threadobject *) this;
114
115         return threads_thread_has_been_interrupted(thread);
116 #endif
117 }
118
119
120 /*
121  * Class:     java/lang/Thread
122  * Method:    suspend
123  * Signature: ()V
124  */
125 void _Jv_java_lang_Thread_suspend(java_lang_Thread *this)
126 {
127 #if defined(ENABLE_THREADS)
128 #endif
129 }
130
131
132 /*
133  * Class:     java/lang/Thread
134  * Method:    resume
135  * Signature: ()V
136  */
137 void _Jv_java_lang_Thread_resume(java_lang_Thread *this)
138 {
139 #if defined(ENABLE_THREADS)
140 #endif
141 }
142
143
144 /*
145  * Class:     java/lang/Thread
146  * Method:    setPriority
147  * Signature: (I)V
148  */
149 void _Jv_java_lang_Thread_setPriority(java_lang_Thread *this, s4 priority)
150 {
151 #if defined(ENABLE_THREADS)
152         threadobject *thread;
153
154         thread = (threadobject *) this;
155
156         threads_set_thread_priority(thread->tid, priority);
157 #endif
158 }
159
160
161 /*
162  * Class:     java/lang/Thread
163  * Method:    stop
164  * Signature: (Ljava/lang/Object;)V
165  */
166 void _Jv_java_lang_Thread_stop(java_lang_Thread *this, java_lang_Throwable *t)
167 {
168 #if defined(ENABLE_THREADS)
169 #endif
170 }
171
172
173 /*
174  * Class:     java/lang/Thread
175  * Method:    currentThread
176  * Signature: ()Ljava/lang/Thread;
177  */
178 java_lang_Thread *_Jv_java_lang_Thread_currentThread(void)
179 {
180         threadobject     *thread;
181         java_lang_Thread *t;
182
183 #if defined(ENABLE_THREADS)
184         thread = THREADOBJECT;
185
186         t = (java_lang_Thread *) thread;
187
188         if (t == NULL)
189                 log_text("t ptr is NULL\n");
190   
191         if (t->group == NULL) {
192                 /* ThreadGroup of currentThread is not initialized */
193
194                 t->group = (java_lang_ThreadGroup *)
195                         native_new_and_init(class_java_lang_ThreadGroup);
196
197                 if (t->group == NULL)
198                         log_text("unable to create ThreadGroup");
199         }
200 #else
201         /* we just return a fake java.lang.Thread object, otherwise we get
202        NullPointerException's in GNU classpath */
203
204         t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
205 #endif
206
207         return t;
208 }
209
210
211 /*
212  * Class:     java/lang/Thread
213  * Method:    yield
214  * Signature: ()V
215  */
216 void _Jv_java_lang_Thread_yield(void)
217 {
218 #if defined(ENABLE_THREADS)
219         threads_yield();
220 #endif
221 }
222
223
224 /*
225  * Class:     java/lang/Thread
226  * Method:    interrupted
227  * Signature: ()Z
228  */
229 s4 _Jv_java_lang_Thread_interrupted(void)
230 {
231 #if defined(ENABLE_THREADS)
232         return threads_check_if_interrupted_and_reset();
233 #endif
234 }
235
236
237 /*
238  * Class:     java/lang/Thread
239  * Method:    holdsLock
240  * Signature: (Ljava/lang/Object;)Z
241  */
242 s4 _Jv_java_lang_Thread_holdsLock(java_lang_Object* obj)
243 {
244 #if defined(ENABLE_THREADS)
245         java_objectheader *o;
246
247         o = (java_objectheader *) obj;
248
249         if (o == NULL) {
250                 exceptions_throw_nullpointerexception();
251                 return;
252         }
253
254         return lock_is_held_by_current_thread(o);
255 #endif
256 }
257
258
259 /*
260  * Class:     java/lang/Thread
261  * Method:    getState
262  * Signature: ()Ljava/lang/String;
263  */
264 java_lang_String *_Jv_java_lang_Thread_getState(java_lang_Thread *this)
265 {
266         vm_abort("Java_java_lang_Thread_getState: IMPLEMENT ME!");
267
268         return NULL;
269 }
270
271
272 /*
273  * These are local overrides for various environment variables in Emacs.
274  * Please do not remove this and leave it at the end of the file, where
275  * Emacs will automagically detect them.
276  * ---------------------------------------------------------------------
277  * Local variables:
278  * mode: c
279  * indent-tabs-mode: t
280  * c-basic-offset: 4
281  * tab-width: 4
282  * End:
283  */