1 /* src/threads/posix/thread-posix.h - 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_H
27 #define _THREAD_POSIX_H
29 /* forward typedefs ***********************************************************/
31 typedef struct threadobject threadobject;
41 #include "mm/memory.h"
43 #include "native/localref.h"
45 #include "threads/mutex.h"
47 #include "threads/posix/lock.h"
49 #include "vm/global.h"
52 #if defined(ENABLE_GC_CACAO)
53 # include "vm/jit/replace.h"
56 #include "vm/jit/stacktrace.h"
58 #if defined(ENABLE_INTRP)
59 #include "vm/jit/intrp/intrp.h"
62 #if defined(__DARWIN__)
63 # include <mach/mach.h>
72 # include <semaphore.h>
76 /* current threadobject *******************************************************/
78 #if defined(HAVE___THREAD)
80 #define THREADSPECIFIC __thread
81 #define THREADOBJECT thread_current
83 extern __thread threadobject *thread_current;
85 #else /* defined(HAVE___THREAD) */
87 #define THREADSPECIFIC
88 #define THREADOBJECT \
89 ((threadobject *) pthread_getspecific(thread_current_key))
91 extern pthread_key_t thread_current_key;
93 #endif /* defined(HAVE___THREAD) */
96 /* threadobject ****************************************************************
98 Struct holding thread local variables.
100 *******************************************************************************/
102 #define THREAD_FLAG_JAVA 0x01 /* a normal Java thread */
103 #define THREAD_FLAG_INTERNAL 0x02 /* CACAO internal thread */
104 #define THREAD_FLAG_DAEMON 0x04 /* daemon thread */
105 #define THREAD_FLAG_IN_NATIVE 0x08 /* currently executing native code */
107 #define SUSPEND_REASON_JNI 1 /* suspended from JNI */
108 #define SUSPEND_REASON_STOPWORLD 2 /* suspended from stop-thw-world */
111 struct threadobject {
112 java_object_t *object; /* link to java.lang.Thread object */
114 ptrint thinlock; /* pre-computed thin lock value */
116 s4 index; /* thread index, starting with 1 */
117 u4 flags; /* flag field */
118 u4 state; /* state field */
120 pthread_t tid; /* pthread id */
122 #if defined(__DARWIN__)
123 mach_port_t mach_thread; /* Darwin thread id */
126 /* for the sable tasuki lock extension */
128 struct threadobject *flc_list; /* FLC list head for this thread */
129 struct threadobject *flc_next; /* next pointer for FLC list */
130 java_handle_t *flc_object;
131 mutex_t flc_lock; /* controlling access to these fields */
132 pthread_cond_t flc_cond;
134 /* these are used for the wait/notify implementation */
136 pthread_cond_t waitcond;
138 mutex_t suspendmutex; /* lock before suspending this thread */
139 pthread_cond_t suspendcond; /* notify to resume this thread */
145 bool suspended; /* is this thread suspended? */
146 s4 suspend_reason; /* reason for suspending */
148 u1 *pc; /* current PC (used for profiling) */
150 java_object_t *_exceptionptr; /* current exception */
151 stackframeinfo_t *_stackframeinfo; /* current native stackframeinfo */
152 localref_table *_localref_table; /* JNI local references */
154 #if defined(ENABLE_INTRP)
155 Cell *_global_sp; /* stack pointer for interpreter */
158 #if defined(ENABLE_GC_CACAO)
159 bool gc_critical; /* indicates a critical section */
162 executionstate_t *es;
165 dumpinfo_t dumpinfo; /* dump memory info structure */
167 #if defined(ENABLE_DEBUG_FILTER)
168 u2 filterverbosecallctr[2]; /* counters for verbose call filter */
172 s4 tracejavacallindent;
173 u4 tracejavacallcount;
176 listnode_t linkage; /* threads-list */
177 listnode_t linkage_free; /* free-list */
181 /* native-world flags *********************************************************/
183 #if defined(ENABLE_GC_CACAO)
184 # define THREAD_NATIVEWORLD_ENTER THREADOBJECT->flags |= THREAD_FLAG_IN_NATIVE
185 # define THREAD_NATIVEWORLD_EXIT THREADOBJECT->flags &= ~THREAD_FLAG_IN_NATIVE
187 # define THREAD_NATIVEWORLD_ENTER /*nop*/
188 # define THREAD_NATIVEWORLD_EXIT /*nop*/
192 /* counter for verbose call filter ********************************************/
194 #if defined(ENABLE_DEBUG_FILTER)
195 # define FILTERVERBOSECALLCTR (THREADOBJECT->filterverbosecallctr)
198 /* state for trace java call **************************************************/
201 # define TRACEJAVACALLINDENT (THREADOBJECT->tracejavacallindent)
202 # define TRACEJAVACALLCOUNT (THREADOBJECT->tracejavacallcount)
206 /* inline functions ***********************************************************/
208 /* thread_get_current **********************************************************
210 Return the threadobject of the current thread.
213 the current threadobject *
215 *******************************************************************************/
217 inline static threadobject *thread_get_current(void)
221 #if defined(HAVE___THREAD)
224 t = (threadobject *) pthread_getspecific(thread_current_key);
231 /* thread_set_current **********************************************************
233 Set the current thread object.
236 t ... the thread object to set
238 *******************************************************************************/
240 inline static void thread_set_current(threadobject *t)
242 #if defined(HAVE___THREAD)
247 result = pthread_setspecific(thread_current_key, t);
250 vm_abort_errnum(result, "thread_set_current: pthread_setspecific failed");
255 inline static stackframeinfo_t *threads_get_current_stackframeinfo(void)
257 return THREADOBJECT->_stackframeinfo;
260 inline static void threads_set_current_stackframeinfo(stackframeinfo_t *sfi)
262 THREADOBJECT->_stackframeinfo = sfi;
266 /* functions ******************************************************************/
268 void threads_sem_init(sem_t *sem, bool shared, int value);
269 void threads_sem_wait(sem_t *sem);
270 void threads_sem_post(sem_t *sem);
272 void threads_start_thread(threadobject *thread, functionptr function);
274 void threads_set_thread_priority(pthread_t tid, int priority);
276 bool threads_detach_thread(threadobject *thread);
278 #if defined(ENABLE_GC_CACAO)
279 bool threads_suspend_thread(threadobject *thread, s4 reason);
280 void threads_suspend_ack(u1* pc, u1* sp);
281 bool threads_resume_thread(threadobject *thread);
284 void threads_join_all_threads(void);
286 void threads_sleep(int64_t millis, int32_t nanos);
288 void threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
290 void threads_thread_interrupt(threadobject *thread);
292 #endif /* _THREAD_POSIX_H */
296 * These are local overrides for various environment variables in Emacs.
297 * Please do not remove this and leave it at the end of the file, where
298 * Emacs will automagically detect them.
299 * ---------------------------------------------------------------------
302 * indent-tabs-mode: t
306 * vim:noexpandtab:sw=4:ts=4: