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/condition.hpp"
50 #include "threads/mutex.hpp"
52 #include "threads/posix/lock.h"
54 #include "vm/global.h"
57 #if defined(ENABLE_GC_CACAO)
58 # include "vm/jit/replace.h"
61 #include "vm/jit/stacktrace.h"
63 #if defined(ENABLE_INTRP)
64 #include "vm/jit/intrp/intrp.h"
67 #if defined(__DARWIN__)
68 # include <mach/mach.h>
77 # include <semaphore.h>
82 /* current threadobject *******************************************************/
84 #if defined(HAVE___THREAD)
86 #define THREADOBJECT thread_current
88 extern __thread threadobject *thread_current;
90 #else /* defined(HAVE___THREAD) */
92 #define THREADOBJECT \
93 ((threadobject *) pthread_getspecific(thread_current_key))
95 extern pthread_key_t thread_current_key;
97 #endif /* defined(HAVE___THREAD) */
100 /* threadobject ****************************************************************
102 Struct holding thread local variables.
104 *******************************************************************************/
106 #define THREAD_FLAG_JAVA 0x01 /* a normal Java thread */
107 #define THREAD_FLAG_INTERNAL 0x02 /* CACAO internal thread */
108 #define THREAD_FLAG_DAEMON 0x04 /* daemon thread */
109 #define THREAD_FLAG_IN_NATIVE 0x08 /* currently executing native code */
111 #define SUSPEND_REASON_JNI 1 /* suspended from JNI */
112 #define SUSPEND_REASON_STOPWORLD 2 /* suspended from stop-thw-world */
115 struct threadobject {
116 java_object_t *object; /* link to java.lang.Thread object */
118 ptrint thinlock; /* pre-computed thin lock value */
120 s4 index; /* thread index, starting with 1 */
121 u4 flags; /* flag field */
122 u4 state; /* state field */
124 pthread_t tid; /* pthread id */
126 #if defined(__DARWIN__)
127 mach_port_t mach_thread; /* Darwin thread id */
130 /* for the sable tasuki lock extension */
132 struct threadobject *flc_list; /* FLC list head for this thread */
133 struct threadobject *flc_next; /* next pointer for FLC list */
134 java_handle_t *flc_object;
135 Mutex* flc_lock; /* controlling access to these fields */
138 /* these are used for the wait/notify implementation */
142 Mutex* suspendmutex; /* lock before suspending this thread */
143 Condition* 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 #if defined(ENABLE_ESCAPE_REASON)
184 void *escape_reasons;
187 listnode_t linkage; /* threads-list */
188 listnode_t linkage_free; /* free-list */
192 /* native-world flags *********************************************************/
194 #if defined(ENABLE_GC_CACAO)
195 # define THREAD_NATIVEWORLD_ENTER THREADOBJECT->flags |= THREAD_FLAG_IN_NATIVE
196 # define THREAD_NATIVEWORLD_EXIT THREADOBJECT->flags &= ~THREAD_FLAG_IN_NATIVE
198 # define THREAD_NATIVEWORLD_ENTER /*nop*/
199 # define THREAD_NATIVEWORLD_EXIT /*nop*/
203 /* counter for verbose call filter ********************************************/
205 #if defined(ENABLE_DEBUG_FILTER)
206 # define FILTERVERBOSECALLCTR (THREADOBJECT->filterverbosecallctr)
209 /* state for trace java call **************************************************/
212 # define TRACEJAVACALLINDENT (THREADOBJECT->tracejavacallindent)
213 # define TRACEJAVACALLCOUNT (THREADOBJECT->tracejavacallcount)
217 /* inline functions ***********************************************************/
219 /* thread_get_current **********************************************************
221 Return the threadobject of the current thread.
224 the current threadobject *
226 *******************************************************************************/
228 inline static threadobject *thread_get_current(void)
232 #if defined(HAVE___THREAD)
235 t = (threadobject *) pthread_getspecific(thread_current_key);
242 /* thread_set_current **********************************************************
244 Set the current thread object.
247 t ... the thread object to set
249 *******************************************************************************/
251 inline static void thread_set_current(threadobject *t)
253 #if defined(HAVE___THREAD)
258 result = pthread_setspecific(thread_current_key, t);
261 vm_abort_errnum(result, "thread_set_current: pthread_setspecific failed");
266 inline static stackframeinfo_t *threads_get_current_stackframeinfo(void)
268 return THREADOBJECT->_stackframeinfo;
271 inline static void threads_set_current_stackframeinfo(stackframeinfo_t *sfi)
273 THREADOBJECT->_stackframeinfo = sfi;
277 /* functions ******************************************************************/
279 void threads_sem_init(sem_t *sem, bool shared, int value);
280 void threads_sem_wait(sem_t *sem);
281 void threads_sem_post(sem_t *sem);
283 void threads_start_thread(threadobject *thread, functionptr function);
285 void threads_set_thread_priority(pthread_t tid, int priority);
287 #if defined(ENABLE_GC_CACAO)
288 bool threads_suspend_thread(threadobject *thread, s4 reason);
289 void threads_suspend_ack(u1* pc, u1* sp);
290 bool threads_resume_thread(threadobject *thread);
293 void threads_join_all_threads(void);
295 void threads_sleep(int64_t millis, int32_t nanos);
297 void threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
299 void threads_thread_interrupt(threadobject *thread);
301 #if defined(ENABLE_TLH)
302 void threads_tlh_add_frame();
303 void threads_tlh_remove_frame();
306 #endif /* _THREAD_POSIX_H */
310 * These are local overrides for various environment variables in Emacs.
311 * Please do not remove this and leave it at the end of the file, where
312 * Emacs will automagically detect them.
313 * ---------------------------------------------------------------------
316 * indent-tabs-mode: t
320 * vim:noexpandtab:sw=4:ts=4: