1 /* src/threads/posix/thread-posix.hpp - POSIX thread functions
3 Copyright (C) 1996-2005, 2006, 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
26 #ifndef _THREAD_POSIX_HPP
27 #define _THREAD_POSIX_HPP
37 // Includes required by Thread.
39 #if defined(ENABLE_TLH)
43 #include "threads/condition.hpp"
44 #include "threads/mutex.hpp"
46 #include "toolbox/list.h"
48 #include "vm/global.h"
51 /* threadobject ****************************************************************
53 Struct holding thread local variables.
55 *******************************************************************************/
57 #define THREAD_FLAG_JAVA 0x01 /* a normal Java thread */
58 #define THREAD_FLAG_INTERNAL 0x02 /* CACAO internal thread */
59 #define THREAD_FLAG_DAEMON 0x04 /* daemon thread */
60 #define THREAD_FLAG_IN_NATIVE 0x08 /* currently executing native code */
62 #define SUSPEND_REASON_JNI 1 /* suspended from JNI */
63 #define SUSPEND_REASON_STOPWORLD 2 /* suspended from stop-thw-world */
66 typedef struct threadobject threadobject;
69 java_object_t *object; /* link to java.lang.Thread object */
71 ptrint thinlock; /* pre-computed thin lock value */
73 s4 index; /* thread index, starting with 1 */
74 u4 flags; /* flag field */
75 u4 state; /* state field */
77 pthread_t tid; /* pthread id */
79 #if defined(__DARWIN__)
80 mach_port_t mach_thread; /* Darwin thread id */
83 /* for the sable tasuki lock extension */
85 struct threadobject *flc_list; /* FLC list head for this thread */
86 struct threadobject *flc_next; /* next pointer for FLC list */
87 java_handle_t *flc_object;
88 Mutex* flc_lock; /* controlling access to these fields */
91 /* these are used for the wait/notify implementation */
95 Mutex* suspendmutex; /* lock before suspending this thread */
96 Condition* suspendcond; /* notify to resume this thread */
101 bool suspended; /* is this thread suspended? */
102 s4 suspend_reason; /* reason for suspending */
104 u1 *pc; /* current PC (used for profiling) */
106 java_object_t *_exceptionptr; /* current exception */
107 struct stackframeinfo_t *_stackframeinfo; /* current native stackframeinfo */
108 struct localref_table *_localref_table; /* JNI local references */
110 #if defined(ENABLE_INTRP)
111 Cell *_global_sp; /* stack pointer for interpreter */
114 #if defined(ENABLE_GC_CACAO)
115 bool gc_critical; /* indicates a critical section */
118 executionstate_t *es;
121 struct DumpMemory* _dumpmemory; ///< Dump memory structure.
123 #if defined(ENABLE_DEBUG_FILTER)
124 u2 filterverbosecallctr[2]; /* counters for verbose call filter */
128 s4 tracejavacallindent;
129 u4 tracejavacallcount;
132 #if defined(ENABLE_TLH)
136 #if defined(ENABLE_ESCAPE_REASON)
137 void *escape_reasons;
140 listnode_t linkage; /* threads-list */
141 listnode_t linkage_free; /* free-list */
145 /* current threadobject *******************************************************/
147 #if defined(HAVE___THREAD)
149 #define THREADOBJECT thread_current
151 extern __thread threadobject *thread_current;
153 #else /* defined(HAVE___THREAD) */
155 #define THREADOBJECT \
156 ((threadobject *) pthread_getspecific(thread_current_key))
158 extern pthread_key_t thread_current_key;
160 #endif /* defined(HAVE___THREAD) */
163 /* native-world flags *********************************************************/
165 #if defined(ENABLE_GC_CACAO)
166 # define THREAD_NATIVEWORLD_ENTER THREADOBJECT->flags |= THREAD_FLAG_IN_NATIVE
167 # define THREAD_NATIVEWORLD_EXIT THREADOBJECT->flags &= ~THREAD_FLAG_IN_NATIVE
169 # define THREAD_NATIVEWORLD_ENTER /*nop*/
170 # define THREAD_NATIVEWORLD_EXIT /*nop*/
174 /* counter for verbose call filter ********************************************/
176 #if defined(ENABLE_DEBUG_FILTER)
177 # define FILTERVERBOSECALLCTR (THREADOBJECT->filterverbosecallctr)
180 /* state for trace java call **************************************************/
183 # define TRACEJAVACALLINDENT (THREADOBJECT->tracejavacallindent)
184 # define TRACEJAVACALLCOUNT (THREADOBJECT->tracejavacallcount)
192 inline static threadobject* thread_get_current(void);
199 #include "mm/memory.h"
201 #include "native/localref.h"
203 #include "threads/lock.hpp"
205 #include "vm/global.h"
208 #if defined(ENABLE_GC_CACAO)
209 # include "vm/jit/executionstate.h"
210 # include "vm/jit/replace.hpp"
213 #include "vm/jit/stacktrace.hpp"
215 #if defined(ENABLE_INTRP)
216 #include "vm/jit/intrp/intrp.h"
219 #if defined(__DARWIN__)
220 # include <mach/mach.h>
229 # include <semaphore.h>
238 /* inline functions ***********************************************************/
241 * Return the Thread object of the current thread.
243 * @return The current Thread object.
245 inline static threadobject* thread_get_current(void)
249 #if defined(HAVE___THREAD)
252 t = (threadobject *) pthread_getspecific(thread_current_key);
260 * Set the current Thread object.
262 * @param t The thread object to set.
264 inline static void thread_set_current(threadobject* t)
266 #if defined(HAVE___THREAD)
271 result = pthread_setspecific(thread_current_key, t);
274 vm_abort_errnum(result, "thread_set_current: pthread_setspecific failed");
279 inline static stackframeinfo_t *threads_get_current_stackframeinfo(void)
281 return THREADOBJECT->_stackframeinfo;
284 inline static void threads_set_current_stackframeinfo(stackframeinfo_t *sfi)
286 THREADOBJECT->_stackframeinfo = sfi;
290 /* functions ******************************************************************/
292 void threads_sem_init(sem_t *sem, bool shared, int value);
293 void threads_sem_wait(sem_t *sem);
294 void threads_sem_post(sem_t *sem);
296 void threads_start_thread(threadobject *thread, functionptr function);
298 void threads_set_thread_priority(pthread_t tid, int priority);
300 #if defined(ENABLE_GC_CACAO)
301 bool threads_suspend_thread(threadobject *thread, s4 reason);
302 void threads_suspend_ack(u1* pc, u1* sp);
303 bool threads_resume_thread(threadobject *thread);
306 void threads_join_all_threads(void);
308 void threads_sleep(int64_t millis, int32_t nanos);
310 void threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
312 void threads_thread_interrupt(threadobject *thread);
314 #if defined(ENABLE_TLH)
315 void threads_tlh_add_frame();
316 void threads_tlh_remove_frame();
323 #endif // _THREAD_POSIX_HPP
327 * These are local overrides for various environment variables in Emacs.
328 * Please do not remove this and leave it at the end of the file, where
329 * Emacs will automagically detect them.
330 * ---------------------------------------------------------------------
333 * indent-tabs-mode: t
337 * vim:noexpandtab:sw=4:ts=4: