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
31 #include "vmcore/system.h"
33 #if defined(ENABLE_THREADS)
34 # include "threads/posix/thread-posix.h"
36 # include "threads/none/thread-none.h"
41 #include "vm/global.h"
43 #include "native/jni.h"
44 #include "native/llni.h"
46 #include "vmcore/utf8.h"
49 /* only define the following stuff with thread enabled ************************/
51 #if defined(ENABLE_THREADS)
53 /* thread states **************************************************************/
55 #define THREAD_STATE_NEW 0
56 #define THREAD_STATE_RUNNABLE 1
57 #define THREAD_STATE_BLOCKED 2
58 #define THREAD_STATE_WAITING 3
59 #define THREAD_STATE_TIMED_WAITING 4
60 #define THREAD_STATE_TERMINATED 5
63 /* thread priorities **********************************************************/
65 #define MIN_PRIORITY 1
66 #define NORM_PRIORITY 5
67 #define MAX_PRIORITY 10
70 /* debug **********************************************************************/
73 # define DEBUGTHREADS(message, thread) \
75 if (opt_DebugThreads) { \
76 printf("[Thread %-16s: ", message); \
77 thread_print_info(thread); \
82 # define DEBUGTHREADS(message, thread)
86 /* global variables ***********************************************************/
88 #if defined(__LINUX__)
89 /* XXX Remove for exact-GC. */
90 extern bool threads_pthreads_implementation_nptl;
94 /* inline functions ***********************************************************/
96 /* thread_get_object ***********************************************************
98 Return the Java for the given thread.
106 *******************************************************************************/
108 inline static java_handle_t *thread_get_object(threadobject *t)
110 return LLNI_WRAP(t->object);
114 /* threads_thread_set_object ***************************************************
116 Set the Java object for the given thread.
122 *******************************************************************************/
124 inline static void thread_set_object(threadobject *t, java_handle_t *o)
126 t->object = LLNI_DIRECT(o);
130 /* thread_get_current_object **************************************************
132 Return the Java object of the current thread.
137 *******************************************************************************/
139 inline static java_handle_t *thread_get_current_object(void)
145 o = thread_get_object(t);
151 /* cacaothread_get_state *******************************************************
153 Returns the current state of the given thread.
156 t ... the thread to check
161 *******************************************************************************/
163 inline static int cacaothread_get_state(threadobject *t)
169 /* thread_is_attached **********************************************************
171 Returns if the given thread is attached to the VM.
174 t ... the thread to check
177 true .... the thread is attached to the VM
178 false ... the thread is not
180 *******************************************************************************/
182 inline static bool thread_is_attached(threadobject *t)
186 o = thread_get_object(t);
195 /* thread_is_interrupted *******************************************************
197 Check if the given thread has been interrupted.
200 t ... the thread to check
203 true, if the given thread had been interrupted
205 *******************************************************************************/
207 inline static bool thread_is_interrupted(threadobject *t)
209 return t->interrupted;
213 /* thread_set_interrupted ******************************************************
215 Set the interrupted flag to the given value.
218 interrupted ... value to set
220 *******************************************************************************/
222 inline static void thread_set_interrupted(threadobject *t, bool interrupted)
224 mutex_lock(&t->waitmutex);
226 /* Set interrupted flag. */
228 t->interrupted = interrupted;
230 mutex_unlock(&t->waitmutex);
234 /* thread_is_daemon ************************************************************
236 Returns if the given thread is a daemon thread.
239 t ... the thread to check
242 true .... the thread is a daemon thread
243 false ... the thread is not
245 *******************************************************************************/
247 inline static bool thread_is_daemon(threadobject *t)
249 if (t->flags & THREAD_FLAG_DAEMON)
256 /* thread_current_is_attached **************************************************
258 Returns if the current thread is attached to the VM.
261 true .... the thread is attached to the VM
262 false ... the thread is not
264 *******************************************************************************/
266 inline static bool thread_current_is_attached(void)
271 t = thread_get_current();
276 result = thread_is_attached(t);
282 /* function prototypes ********************************************************/
284 void threads_preinit(void);
285 void threads_init(void);
287 void thread_free(threadobject *t);
289 bool threads_thread_start_internal(utf *name, functionptr f);
290 void threads_thread_start(java_handle_t *object);
292 bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
294 void thread_fprint_name(threadobject *t, FILE *stream);
295 void thread_print_info(threadobject *t);
297 intptr_t threads_get_current_tid(void);
299 void thread_set_state_runnable(threadobject *t);
300 void thread_set_state_waiting(threadobject *t);
301 void thread_set_state_timed_waiting(threadobject *t);
302 void thread_set_state_terminated(threadobject *t);
304 threadobject *thread_get_thread(java_handle_t *h);
306 bool threads_thread_is_alive(threadobject *t);
308 void threads_dump(void);
311 /* implementation specific functions */
313 void threads_impl_preinit(void);
314 void threads_impl_init(void);
316 #if defined(ENABLE_GC_CACAO)
317 void threads_mutex_gc_lock(void);
318 void threads_mutex_gc_unlock(void);
321 void threads_mutex_join_lock(void);
322 void threads_mutex_join_unlock(void);
324 void threads_impl_thread_init(threadobject *t);
325 void threads_impl_thread_clear(threadobject *t);
326 void threads_impl_thread_reuse(threadobject *t);
327 void threads_impl_thread_free(threadobject *t);
328 void threads_impl_thread_start(threadobject *thread, functionptr f);
330 void threads_yield(void);
332 #endif /* ENABLE_THREADS */
334 #endif /* _THREAD_H */
338 * These are local overrides for various environment variables in Emacs.
339 * Please do not remove this and leave it at the end of the file, where
340 * Emacs will automagically detect them.
341 * ---------------------------------------------------------------------
344 * indent-tabs-mode: t
348 * vim:noexpandtab:sw=4:ts=4: