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 #if defined(ENABLE_TLH)
47 #include "native/localref.h"
49 #include "threads/mutex.h"
51 #include "threads/posix/lock.h"
53 #include "vm/global.h"
56 #if defined(ENABLE_GC_CACAO)
57 # include "vm/jit/replace.h"
60 #include "vm/jit/stacktrace.h"
62 #if defined(ENABLE_INTRP)
63 #include "vm/jit/intrp/intrp.h"
66 #if defined(__DARWIN__)
67 # include <mach/mach.h>
76 # include <semaphore.h>
81 /* current threadobject *******************************************************/
83 #if defined(HAVE___THREAD)
85 #define THREADOBJECT thread_current
87 extern __thread threadobject *thread_current;
89 #else /* defined(HAVE___THREAD) */
91 #define THREADOBJECT \
92 ((threadobject *) pthread_getspecific(thread_current_key))
94 extern pthread_key_t thread_current_key;
96 #endif /* defined(HAVE___THREAD) */
99 /* threadobject ****************************************************************
101 Struct holding thread local variables.
103 *******************************************************************************/
105 #define THREAD_FLAG_JAVA 0x01 /* a normal Java thread */
106 #define THREAD_FLAG_INTERNAL 0x02 /* CACAO internal thread */
107 #define THREAD_FLAG_DAEMON 0x04 /* daemon thread */
108 #define THREAD_FLAG_IN_NATIVE 0x08 /* currently executing native code */
110 #define SUSPEND_REASON_JNI 1 /* suspended from JNI */
111 #define SUSPEND_REASON_STOPWORLD 2 /* suspended from stop-thw-world */
114 struct threadobject {
115 java_object_t *object; /* link to java.lang.Thread object */
117 ptrint thinlock; /* pre-computed thin lock value */
119 s4 index; /* thread index, starting with 1 */
120 u4 flags; /* flag field */
121 u4 state; /* state field */
123 pthread_t tid; /* pthread id */
125 #if defined(__DARWIN__)
126 mach_port_t mach_thread; /* Darwin thread id */
129 /* for the sable tasuki lock extension */
131 struct threadobject *flc_list; /* FLC list head for this thread */
132 struct threadobject *flc_next; /* next pointer for FLC list */
133 java_handle_t *flc_object;
134 mutex_t flc_lock; /* controlling access to these fields */
135 pthread_cond_t flc_cond;
137 /* these are used for the wait/notify implementation */
139 pthread_cond_t waitcond;
141 mutex_t suspendmutex; /* lock before suspending this thread */
142 pthread_cond_t suspendcond; /* notify to resume this thread */
148 bool suspended; /* is this thread suspended? */
149 s4 suspend_reason; /* reason for suspending */
151 u1 *pc; /* current PC (used for profiling) */
153 java_object_t *_exceptionptr; /* current exception */
154 stackframeinfo_t *_stackframeinfo; /* current native stackframeinfo */
155 localref_table *_localref_table; /* JNI local references */
157 #if defined(ENABLE_INTRP)
158 Cell *_global_sp; /* stack pointer for interpreter */
161 #if defined(ENABLE_GC_CACAO)
162 bool gc_critical; /* indicates a critical section */
165 executionstate_t *es;
168 dumpinfo_t dumpinfo; /* dump memory info structure */
170 #if defined(ENABLE_DEBUG_FILTER)
171 u2 filterverbosecallctr[2]; /* counters for verbose call filter */
175 s4 tracejavacallindent;
176 u4 tracejavacallcount;
179 #if defined(ENABLE_TLH)
183 listnode_t linkage; /* threads-list */
184 listnode_t linkage_free; /* free-list */
188 /* native-world flags *********************************************************/
190 #if defined(ENABLE_GC_CACAO)
191 # define THREAD_NATIVEWORLD_ENTER THREADOBJECT->flags |= THREAD_FLAG_IN_NATIVE
192 # define THREAD_NATIVEWORLD_EXIT THREADOBJECT->flags &= ~THREAD_FLAG_IN_NATIVE
194 # define THREAD_NATIVEWORLD_ENTER /*nop*/
195 # define THREAD_NATIVEWORLD_EXIT /*nop*/
199 /* counter for verbose call filter ********************************************/
201 #if defined(ENABLE_DEBUG_FILTER)
202 # define FILTERVERBOSECALLCTR (THREADOBJECT->filterverbosecallctr)
205 /* state for trace java call **************************************************/
208 # define TRACEJAVACALLINDENT (THREADOBJECT->tracejavacallindent)
209 # define TRACEJAVACALLCOUNT (THREADOBJECT->tracejavacallcount)
213 /* inline functions ***********************************************************/
215 /* thread_get_current **********************************************************
217 Return the threadobject of the current thread.
220 the current threadobject *
222 *******************************************************************************/
224 inline static threadobject *thread_get_current(void)
228 #if defined(HAVE___THREAD)
231 t = (threadobject *) pthread_getspecific(thread_current_key);
238 /* thread_set_current **********************************************************
240 Set the current thread object.
243 t ... the thread object to set
245 *******************************************************************************/
247 inline static void thread_set_current(threadobject *t)
249 #if defined(HAVE___THREAD)
254 result = pthread_setspecific(thread_current_key, t);
257 vm_abort_errnum(result, "thread_set_current: pthread_setspecific failed");
262 inline static stackframeinfo_t *threads_get_current_stackframeinfo(void)
264 return THREADOBJECT->_stackframeinfo;
267 inline static void threads_set_current_stackframeinfo(stackframeinfo_t *sfi)
269 THREADOBJECT->_stackframeinfo = sfi;
273 /* functions ******************************************************************/
275 void threads_sem_init(sem_t *sem, bool shared, int value);
276 void threads_sem_wait(sem_t *sem);
277 void threads_sem_post(sem_t *sem);
279 void threads_start_thread(threadobject *thread, functionptr function);
281 void threads_set_thread_priority(pthread_t tid, int priority);
283 bool threads_detach_thread(threadobject *thread);
285 #if defined(ENABLE_GC_CACAO)
286 bool threads_suspend_thread(threadobject *thread, s4 reason);
287 void threads_suspend_ack(u1* pc, u1* sp);
288 bool threads_resume_thread(threadobject *thread);
291 void threads_join_all_threads(void);
293 void threads_sleep(int64_t millis, int32_t nanos);
295 void threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
297 void threads_thread_interrupt(threadobject *thread);
299 #endif /* _THREAD_POSIX_H */
303 * These are local overrides for various environment variables in Emacs.
304 * Please do not remove this and leave it at the end of the file, where
305 * Emacs will automagically detect them.
306 * ---------------------------------------------------------------------
309 * indent-tabs-mode: t
313 * vim:noexpandtab:sw=4:ts=4: