1 /* src/threads/thread.hpp - machine independent thread functions
3 Copyright (C) 1996-2011
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
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.
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.
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
33 // Include early to get threadobject.
34 #if defined(ENABLE_THREADS)
35 # include "threads/posix/thread-posix.hpp"
37 # include "threads/none/thread-none.h"
42 #include "native/llni.h"
44 #include "threads/mutex.hpp"
46 #include "vm/global.h"
50 /* only define the following stuff with thread enabled ************************/
52 #if defined(ENABLE_THREADS)
54 /* thread states **************************************************************/
56 #define THREAD_STATE_NEW 0
57 #define THREAD_STATE_RUNNABLE 1
58 #define THREAD_STATE_BLOCKED 2
59 #define THREAD_STATE_WAITING 3
60 #define THREAD_STATE_TIMED_WAITING 4
61 #define THREAD_STATE_TERMINATED 5
62 #define THREAD_STATE_PARKED 6
63 #define THREAD_STATE_TIMED_PARKED 7
66 /* thread priorities **********************************************************/
68 #define MIN_PRIORITY 1
69 #define NORM_PRIORITY 5
70 #define MAX_PRIORITY 10
73 /* debug **********************************************************************/
76 # define DEBUGTHREADS(message, thread) \
78 if (opt_DebugThreads) { \
79 printf("[Thread %-16s: ", message); \
80 thread_print_info(thread); \
85 # define DEBUGTHREADS(message, thread)
89 /* global variables ***********************************************************/
91 #if defined(__LINUX__)
92 /* XXX Remove for exact-GC. */
93 extern bool threads_pthreads_implementation_nptl;
101 /* inline functions ***********************************************************/
103 /* thread_get_object ***********************************************************
105 Return the Java for the given thread.
113 *******************************************************************************/
115 inline static java_handle_t *thread_get_object(threadobject *t)
117 return LLNI_WRAP(t->object);
121 /* threads_thread_set_object ***************************************************
123 Set the Java object for the given thread.
129 *******************************************************************************/
131 inline static void thread_set_object(threadobject *t, java_handle_t *o)
133 t->object = LLNI_DIRECT(o);
137 /* thread_get_current_object **************************************************
139 Return the Java object of the current thread.
144 *******************************************************************************/
146 inline static java_handle_t *thread_get_current_object(void)
152 o = thread_get_object(t);
158 /* cacaothread_get_state *******************************************************
160 Returns the current state of the given thread.
163 t ... the thread to check
168 *******************************************************************************/
170 inline static int cacaothread_get_state(threadobject *t)
176 /* thread_is_attached **********************************************************
178 Returns if the given thread is attached to the VM.
181 t ... the thread to check
184 true .... the thread is attached to the VM
185 false ... the thread is not
187 *******************************************************************************/
189 inline static bool thread_is_attached(threadobject *t)
193 o = thread_get_object(t);
199 /* thread_is_daemon ************************************************************
201 Returns if the given thread is a daemon thread.
204 t ... the thread to check
207 true .... the thread is a daemon thread
208 false ... the thread is not
210 *******************************************************************************/
212 inline static bool thread_is_daemon(threadobject *t)
214 return (t->flags & THREAD_FLAG_DAEMON) != 0;
218 /* thread_current_is_attached **************************************************
220 Returns if the current thread is attached to the VM.
223 true .... the thread is attached to the VM
224 false ... the thread is not
226 *******************************************************************************/
228 inline static bool thread_current_is_attached(void)
232 t = thread_get_current();
237 return thread_is_attached(t);
241 /* function prototypes ********************************************************/
243 void threads_preinit(void);
244 void threads_init(void);
246 void thread_free(threadobject *t);
248 bool threads_thread_start_internal(utf *name, functionptr f);
249 void threads_thread_start(java_handle_t *object);
251 bool thread_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
252 bool thread_attach_current_external_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
253 bool thread_detach_current_thread(void);
255 bool thread_detach_current_external_thread(void);
257 void thread_fprint_name(threadobject *t, FILE *stream);
258 void thread_print_info(threadobject *t);
260 intptr_t threads_get_current_tid(void);
262 void thread_set_state_runnable(threadobject *t);
263 void thread_set_state_waiting(threadobject *t);
264 void thread_set_state_timed_waiting(threadobject *t);
265 void thread_set_state_parked(threadobject *t);
266 void thread_set_state_timed_parked(threadobject *t);
267 void thread_set_state_terminated(threadobject *t);
269 threadobject *thread_get_thread(java_handle_t *h);
271 bool threads_thread_is_alive(threadobject *t);
272 bool thread_is_interrupted(threadobject *t);
273 void thread_set_interrupted(threadobject *t, bool interrupted);
275 /* implementation specific functions */
277 void threads_impl_preinit(void);
278 void threads_impl_init(void);
280 #if defined(ENABLE_GC_CACAO)
281 void threads_mutex_gc_lock(void);
282 void threads_mutex_gc_unlock(void);
285 void threads_mutex_join_lock(void);
286 void threads_mutex_join_unlock(void);
288 void threads_impl_thread_clear(threadobject *t);
289 void threads_impl_thread_reuse(threadobject *t);
290 void threads_impl_clear_heap_pointers(threadobject *t);
291 void threads_impl_thread_start(threadobject *thread, functionptr f);
293 void threads_yield(void);
299 #endif /* ENABLE_THREADS */
301 #endif // _THREAD_HPP
303 void thread_handle_set_priority(java_handle_t *th, int);
304 bool thread_handle_is_interrupted(java_handle_t *th);
305 void thread_handle_interrupt(java_handle_t *th);
306 int thread_handle_get_state(java_handle_t *th);
308 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
309 #include "thread-classpath.hpp"
310 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
311 #include "thread-openjdk.hpp"
312 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_CLDC1_1)
313 #include "thread-cldc11.hpp"
318 * These are local overrides for various environment variables in Emacs.
319 * Please do not remove this and leave it at the end of the file, where
320 * Emacs will automagically detect them.
321 * ---------------------------------------------------------------------
324 * indent-tabs-mode: t
328 * vim:noexpandtab:sw=4:ts=4: