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
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
64 /* thread priorities **********************************************************/
66 #define MIN_PRIORITY 1
67 #define NORM_PRIORITY 5
68 #define MAX_PRIORITY 10
71 /* debug **********************************************************************/
74 # define DEBUGTHREADS(message, thread) \
76 if (opt_DebugThreads) { \
77 printf("[Thread %-16s: ", message); \
78 thread_print_info(thread); \
83 # define DEBUGTHREADS(message, thread)
87 /* global variables ***********************************************************/
89 #if defined(__LINUX__)
90 /* XXX Remove for exact-GC. */
91 extern bool threads_pthreads_implementation_nptl;
99 /* inline functions ***********************************************************/
101 /* thread_get_object ***********************************************************
103 Return the Java for the given thread.
111 *******************************************************************************/
113 inline static java_handle_t *thread_get_object(threadobject *t)
115 return LLNI_WRAP(t->object);
119 /* threads_thread_set_object ***************************************************
121 Set the Java object for the given thread.
127 *******************************************************************************/
129 inline static void thread_set_object(threadobject *t, java_handle_t *o)
131 t->object = LLNI_DIRECT(o);
135 /* thread_get_current_object **************************************************
137 Return the Java object of the current thread.
142 *******************************************************************************/
144 inline static java_handle_t *thread_get_current_object(void)
150 o = thread_get_object(t);
156 /* cacaothread_get_state *******************************************************
158 Returns the current state of the given thread.
161 t ... the thread to check
166 *******************************************************************************/
168 inline static int cacaothread_get_state(threadobject *t)
174 /* thread_is_attached **********************************************************
176 Returns if the given thread is attached to the VM.
179 t ... the thread to check
182 true .... the thread is attached to the VM
183 false ... the thread is not
185 *******************************************************************************/
187 inline static bool thread_is_attached(threadobject *t)
191 o = thread_get_object(t);
200 /* thread_is_interrupted *******************************************************
202 Check if the given thread has been interrupted.
205 t ... the thread to check
208 true, if the given thread had been interrupted
210 *******************************************************************************/
212 inline static bool thread_is_interrupted(threadobject *t)
216 /* We need the mutex because classpath will call this function when
217 a blocking system call is interrupted. The mutex ensures that it will
218 see the correct value for the interrupted flag. */
221 t->waitmutex->lock();
222 interrupted = t->interrupted;
223 t->waitmutex->unlock();
225 Mutex_lock(t->waitmutex);
226 interrupted = t->interrupted;
227 Mutex_unlock(t->waitmutex);
234 /* thread_set_interrupted ******************************************************
236 Set the interrupted flag to the given value.
239 interrupted ... value to set
241 *******************************************************************************/
243 inline static void thread_set_interrupted(threadobject *t, bool interrupted)
246 t->waitmutex->lock();
247 t->interrupted = interrupted;
248 t->waitmutex->unlock();
250 Mutex_lock(t->waitmutex);
251 t->interrupted = interrupted;
252 Mutex_unlock(t->waitmutex);
257 /* thread_is_daemon ************************************************************
259 Returns if the given thread is a daemon thread.
262 t ... the thread to check
265 true .... the thread is a daemon thread
266 false ... the thread is not
268 *******************************************************************************/
270 inline static bool thread_is_daemon(threadobject *t)
272 if (t->flags & THREAD_FLAG_DAEMON)
279 /* thread_current_is_attached **************************************************
281 Returns if the current thread is attached to the VM.
284 true .... the thread is attached to the VM
285 false ... the thread is not
287 *******************************************************************************/
289 inline static bool thread_current_is_attached(void)
294 t = thread_get_current();
299 result = thread_is_attached(t);
305 /* function prototypes ********************************************************/
307 void threads_preinit(void);
308 void threads_init(void);
310 void thread_free(threadobject *t);
312 bool threads_thread_start_internal(utf *name, functionptr f);
313 void threads_thread_start(java_handle_t *object);
315 bool thread_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
316 bool thread_attach_current_external_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
317 bool thread_detach_current_thread(void);
319 bool thread_detach_current_external_thread(void);
321 void thread_fprint_name(threadobject *t, FILE *stream);
322 void thread_print_info(threadobject *t);
324 intptr_t threads_get_current_tid(void);
326 void thread_set_state_runnable(threadobject *t);
327 void thread_set_state_waiting(threadobject *t);
328 void thread_set_state_timed_waiting(threadobject *t);
329 void thread_set_state_terminated(threadobject *t);
331 threadobject *thread_get_thread(java_handle_t *h);
333 bool threads_thread_is_alive(threadobject *t);
335 void threads_dump(void);
338 /* implementation specific functions */
340 void threads_impl_preinit(void);
341 void threads_impl_init(void);
343 #if defined(ENABLE_GC_CACAO)
344 void threads_mutex_gc_lock(void);
345 void threads_mutex_gc_unlock(void);
348 void threads_mutex_join_lock(void);
349 void threads_mutex_join_unlock(void);
351 void threads_impl_thread_clear(threadobject *t);
352 void threads_impl_thread_reuse(threadobject *t);
353 void threads_impl_thread_free(threadobject *t);
354 void threads_impl_thread_start(threadobject *thread, functionptr f);
356 void threads_yield(void);
358 #endif /* ENABLE_THREADS */
364 #endif // _THREAD_HPP
368 * These are local overrides for various environment variables in Emacs.
369 * Please do not remove this and leave it at the end of the file, where
370 * Emacs will automagically detect them.
371 * ---------------------------------------------------------------------
374 * indent-tabs-mode: t
378 * vim:noexpandtab:sw=4:ts=4: