1 /* src/threads/thread.hpp - 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
37 // Include early to get threadobject.
38 #if defined(ENABLE_THREADS)
39 # include "threads/posix/thread-posix.hpp"
41 # include "threads/none/thread-none.h"
46 #include "native/jni.h"
47 #include "native/llni.h"
49 #include "threads/mutex.hpp"
51 #include "vm/global.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. */
222 t->waitmutex->lock();
223 interrupted = t->interrupted;
224 t->waitmutex->unlock();
226 Mutex_lock(t->waitmutex);
227 interrupted = t->interrupted;
228 Mutex_unlock(t->waitmutex);
235 /* thread_set_interrupted ******************************************************
237 Set the interrupted flag to the given value.
240 interrupted ... value to set
242 *******************************************************************************/
244 inline static void thread_set_interrupted(threadobject *t, bool interrupted)
247 t->waitmutex->lock();
248 t->interrupted = interrupted;
249 t->waitmutex->unlock();
251 Mutex_lock(t->waitmutex);
252 t->interrupted = interrupted;
253 Mutex_unlock(t->waitmutex);
258 /* thread_is_daemon ************************************************************
260 Returns if the given thread is a daemon thread.
263 t ... the thread to check
266 true .... the thread is a daemon thread
267 false ... the thread is not
269 *******************************************************************************/
271 inline static bool thread_is_daemon(threadobject *t)
273 if (t->flags & THREAD_FLAG_DAEMON)
280 /* thread_current_is_attached **************************************************
282 Returns if the current thread is attached to the VM.
285 true .... the thread is attached to the VM
286 false ... the thread is not
288 *******************************************************************************/
290 inline static bool thread_current_is_attached(void)
295 t = thread_get_current();
300 result = thread_is_attached(t);
306 /* function prototypes ********************************************************/
308 void threads_preinit(void);
309 void threads_init(void);
311 void thread_free(threadobject *t);
313 bool threads_thread_start_internal(utf *name, functionptr f);
314 void threads_thread_start(java_handle_t *object);
316 bool thread_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
317 bool thread_attach_current_external_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
318 bool thread_detach_current_thread(void);
320 bool thread_detach_current_external_thread(void);
322 void thread_fprint_name(threadobject *t, FILE *stream);
323 void thread_print_info(threadobject *t);
325 intptr_t threads_get_current_tid(void);
327 void thread_set_state_runnable(threadobject *t);
328 void thread_set_state_waiting(threadobject *t);
329 void thread_set_state_timed_waiting(threadobject *t);
330 void thread_set_state_terminated(threadobject *t);
332 threadobject *thread_get_thread(java_handle_t *h);
334 bool threads_thread_is_alive(threadobject *t);
336 void threads_dump(void);
339 /* implementation specific functions */
341 void threads_impl_preinit(void);
342 void threads_impl_init(void);
344 #if defined(ENABLE_GC_CACAO)
345 void threads_mutex_gc_lock(void);
346 void threads_mutex_gc_unlock(void);
349 void threads_mutex_join_lock(void);
350 void threads_mutex_join_unlock(void);
352 void threads_impl_thread_init(threadobject *t);
353 void threads_impl_thread_clear(threadobject *t);
354 void threads_impl_thread_reuse(threadobject *t);
355 void threads_impl_thread_free(threadobject *t);
356 void threads_impl_thread_start(threadobject *thread, functionptr f);
358 void threads_yield(void);
360 #endif /* ENABLE_THREADS */
366 #endif // _THREAD_HPP
370 * These are local overrides for various environment variables in Emacs.
371 * Please do not remove this and leave it at the end of the file, where
372 * Emacs will automagically detect them.
373 * ---------------------------------------------------------------------
376 * indent-tabs-mode: t
380 * vim:noexpandtab:sw=4:ts=4: