1 /* src/threads/thread.h - machine independent thread functions
3 Copyright (C) 2007, 2008
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
35 #include "vmcore/system.h"
37 #include "threads/mutex.hpp"
39 #if defined(ENABLE_THREADS)
40 # include "threads/posix/thread-posix.h"
42 # include "threads/none/thread-none.h"
47 #include "vm/global.h"
49 #include "native/jni.h"
50 #include "native/llni.h"
52 #include "vmcore/utf8.h"
55 /* only define the following stuff with thread enabled ************************/
57 #if defined(ENABLE_THREADS)
59 /* thread states **************************************************************/
61 #define THREAD_STATE_NEW 0
62 #define THREAD_STATE_RUNNABLE 1
63 #define THREAD_STATE_BLOCKED 2
64 #define THREAD_STATE_WAITING 3
65 #define THREAD_STATE_TIMED_WAITING 4
66 #define THREAD_STATE_TERMINATED 5
69 /* thread priorities **********************************************************/
71 #define MIN_PRIORITY 1
72 #define NORM_PRIORITY 5
73 #define MAX_PRIORITY 10
76 /* debug **********************************************************************/
79 # define DEBUGTHREADS(message, thread) \
81 if (opt_DebugThreads) { \
82 printf("[Thread %-16s: ", message); \
83 thread_print_info(thread); \
88 # define DEBUGTHREADS(message, thread)
92 /* global variables ***********************************************************/
94 #if defined(__LINUX__)
95 /* XXX Remove for exact-GC. */
96 extern bool threads_pthreads_implementation_nptl;
100 /* inline functions ***********************************************************/
102 /* thread_get_object ***********************************************************
104 Return the Java for the given thread.
112 *******************************************************************************/
114 inline static java_handle_t *thread_get_object(threadobject *t)
116 return LLNI_WRAP(t->object);
120 /* threads_thread_set_object ***************************************************
122 Set the Java object for the given thread.
128 *******************************************************************************/
130 inline static void thread_set_object(threadobject *t, java_handle_t *o)
132 t->object = LLNI_DIRECT(o);
136 /* thread_get_current_object **************************************************
138 Return the Java object of the current thread.
143 *******************************************************************************/
145 inline static java_handle_t *thread_get_current_object(void)
151 o = thread_get_object(t);
157 /* cacaothread_get_state *******************************************************
159 Returns the current state of the given thread.
162 t ... the thread to check
167 *******************************************************************************/
169 inline static int cacaothread_get_state(threadobject *t)
175 /* thread_is_attached **********************************************************
177 Returns if the given thread is attached to the VM.
180 t ... the thread to check
183 true .... the thread is attached to the VM
184 false ... the thread is not
186 *******************************************************************************/
188 inline static bool thread_is_attached(threadobject *t)
192 o = thread_get_object(t);
201 /* thread_is_interrupted *******************************************************
203 Check if the given thread has been interrupted.
206 t ... the thread to check
209 true, if the given thread had been interrupted
211 *******************************************************************************/
213 inline static bool thread_is_interrupted(threadobject *t)
217 /* We need the mutex because classpath will call this function when
218 a blocking system call is interrupted. The mutex ensures that it will
219 see the correct value for the interrupted flag. */
221 Mutex_lock(t->waitmutex);
222 interrupted = t->interrupted;
223 Mutex_unlock(t->waitmutex);
229 /* thread_set_interrupted ******************************************************
231 Set the interrupted flag to the given value.
234 interrupted ... value to set
236 *******************************************************************************/
238 inline static void thread_set_interrupted(threadobject *t, bool interrupted)
240 Mutex_lock(t->waitmutex);
242 /* Set interrupted flag. */
244 t->interrupted = interrupted;
246 Mutex_unlock(t->waitmutex);
250 /* thread_is_daemon ************************************************************
252 Returns if the given thread is a daemon thread.
255 t ... the thread to check
258 true .... the thread is a daemon thread
259 false ... the thread is not
261 *******************************************************************************/
263 inline static bool thread_is_daemon(threadobject *t)
265 if (t->flags & THREAD_FLAG_DAEMON)
272 /* thread_current_is_attached **************************************************
274 Returns if the current thread is attached to the VM.
277 true .... the thread is attached to the VM
278 false ... the thread is not
280 *******************************************************************************/
282 inline static bool thread_current_is_attached(void)
287 t = thread_get_current();
292 result = thread_is_attached(t);
298 /* function prototypes ********************************************************/
300 void threads_preinit(void);
301 void threads_init(void);
303 void thread_free(threadobject *t);
305 bool threads_thread_start_internal(utf *name, functionptr f);
306 void threads_thread_start(java_handle_t *object);
308 bool thread_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
309 bool thread_attach_current_external_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
310 bool thread_detach_current_thread(void);
312 bool thread_detach_current_external_thread(void);
314 void thread_fprint_name(threadobject *t, FILE *stream);
315 void thread_print_info(threadobject *t);
317 intptr_t threads_get_current_tid(void);
319 void thread_set_state_runnable(threadobject *t);
320 void thread_set_state_waiting(threadobject *t);
321 void thread_set_state_timed_waiting(threadobject *t);
322 void thread_set_state_terminated(threadobject *t);
324 threadobject *thread_get_thread(java_handle_t *h);
326 bool threads_thread_is_alive(threadobject *t);
328 void threads_dump(void);
331 /* implementation specific functions */
333 void threads_impl_preinit(void);
334 void threads_impl_init(void);
336 #if defined(ENABLE_GC_CACAO)
337 void threads_mutex_gc_lock(void);
338 void threads_mutex_gc_unlock(void);
341 void threads_mutex_join_lock(void);
342 void threads_mutex_join_unlock(void);
344 void threads_impl_thread_init(threadobject *t);
345 void threads_impl_thread_clear(threadobject *t);
346 void threads_impl_thread_reuse(threadobject *t);
347 void threads_impl_thread_free(threadobject *t);
348 void threads_impl_thread_start(threadobject *thread, functionptr f);
350 void threads_yield(void);
352 #endif /* ENABLE_THREADS */
358 #endif /* _THREAD_H */
362 * These are local overrides for various environment variables in Emacs.
363 * Please do not remove this and leave it at the end of the file, where
364 * Emacs will automagically detect them.
365 * ---------------------------------------------------------------------
368 * indent-tabs-mode: t
372 * vim:noexpandtab:sw=4:ts=4: