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/executionstate.h"
59 # include "vm/jit/replace.h"
62 #include "vm/jit/stacktrace.h"
64 #if defined(ENABLE_INTRP)
65 #include "vm/jit/intrp/intrp.h"
68 #if defined(__DARWIN__)
69 # include <mach/mach.h>
78 # include <semaphore.h>
83 /* current threadobject *******************************************************/
85 #if defined(HAVE___THREAD)
87 #define THREADOBJECT thread_current
89 extern __thread threadobject *thread_current;
91 #else /* defined(HAVE___THREAD) */
93 #define THREADOBJECT \
94 ((threadobject *) pthread_getspecific(thread_current_key))
96 extern pthread_key_t thread_current_key;
98 #endif /* defined(HAVE___THREAD) */
101 /* threadobject ****************************************************************
103 Struct holding thread local variables.
105 *******************************************************************************/
107 #define THREAD_FLAG_JAVA 0x01 /* a normal Java thread */
108 #define THREAD_FLAG_INTERNAL 0x02 /* CACAO internal thread */
109 #define THREAD_FLAG_DAEMON 0x04 /* daemon thread */
110 #define THREAD_FLAG_IN_NATIVE 0x08 /* currently executing native code */
112 #define SUSPEND_REASON_JNI 1 /* suspended from JNI */
113 #define SUSPEND_REASON_STOPWORLD 2 /* suspended from stop-thw-world */
116 struct threadobject {
117 java_object_t *object; /* link to java.lang.Thread object */
119 ptrint thinlock; /* pre-computed thin lock value */
121 s4 index; /* thread index, starting with 1 */
122 u4 flags; /* flag field */
123 u4 state; /* state field */
125 pthread_t tid; /* pthread id */
127 #if defined(__DARWIN__)
128 mach_port_t mach_thread; /* Darwin thread id */
131 /* for the sable tasuki lock extension */
133 struct threadobject *flc_list; /* FLC list head for this thread */
134 struct threadobject *flc_next; /* next pointer for FLC list */
135 java_handle_t *flc_object;
136 Mutex* flc_lock; /* controlling access to these fields */
139 /* these are used for the wait/notify implementation */
143 Mutex* suspendmutex; /* lock before suspending this thread */
144 Condition* suspendcond; /* notify to resume this thread */
149 bool suspended; /* is this thread suspended? */
150 s4 suspend_reason; /* reason for suspending */
152 u1 *pc; /* current PC (used for profiling) */
154 java_object_t *_exceptionptr; /* current exception */
155 stackframeinfo_t *_stackframeinfo; /* current native stackframeinfo */
156 localref_table *_localref_table; /* JNI local references */
158 #if defined(ENABLE_INTRP)
159 Cell *_global_sp; /* stack pointer for interpreter */
162 #if defined(ENABLE_GC_CACAO)
163 bool gc_critical; /* indicates a critical section */
166 executionstate_t *es;
169 dumpinfo_t dumpinfo; /* dump memory info structure */
171 #if defined(ENABLE_DEBUG_FILTER)
172 u2 filterverbosecallctr[2]; /* counters for verbose call filter */
176 s4 tracejavacallindent;
177 u4 tracejavacallcount;
180 #if defined(ENABLE_TLH)
184 #if defined(ENABLE_ESCAPE_REASON)
185 void *escape_reasons;
188 listnode_t linkage; /* threads-list */
189 listnode_t linkage_free; /* free-list */
193 /* native-world flags *********************************************************/
195 #if defined(ENABLE_GC_CACAO)
196 # define THREAD_NATIVEWORLD_ENTER THREADOBJECT->flags |= THREAD_FLAG_IN_NATIVE
197 # define THREAD_NATIVEWORLD_EXIT THREADOBJECT->flags &= ~THREAD_FLAG_IN_NATIVE
199 # define THREAD_NATIVEWORLD_ENTER /*nop*/
200 # define THREAD_NATIVEWORLD_EXIT /*nop*/
204 /* counter for verbose call filter ********************************************/
206 #if defined(ENABLE_DEBUG_FILTER)
207 # define FILTERVERBOSECALLCTR (THREADOBJECT->filterverbosecallctr)
210 /* state for trace java call **************************************************/
213 # define TRACEJAVACALLINDENT (THREADOBJECT->tracejavacallindent)
214 # define TRACEJAVACALLCOUNT (THREADOBJECT->tracejavacallcount)
218 /* inline functions ***********************************************************/
220 /* thread_get_current **********************************************************
222 Return the threadobject of the current thread.
225 the current threadobject *
227 *******************************************************************************/
229 inline static threadobject *thread_get_current(void)
233 #if defined(HAVE___THREAD)
236 t = (threadobject *) pthread_getspecific(thread_current_key);
243 /* thread_set_current **********************************************************
245 Set the current thread object.
248 t ... the thread object to set
250 *******************************************************************************/
252 inline static void thread_set_current(threadobject *t)
254 #if defined(HAVE___THREAD)
259 result = pthread_setspecific(thread_current_key, t);
262 vm_abort_errnum(result, "thread_set_current: pthread_setspecific failed");
267 inline static stackframeinfo_t *threads_get_current_stackframeinfo(void)
269 return THREADOBJECT->_stackframeinfo;
272 inline static void threads_set_current_stackframeinfo(stackframeinfo_t *sfi)
274 THREADOBJECT->_stackframeinfo = sfi;
278 /* functions ******************************************************************/
280 void threads_sem_init(sem_t *sem, bool shared, int value);
281 void threads_sem_wait(sem_t *sem);
282 void threads_sem_post(sem_t *sem);
284 void threads_start_thread(threadobject *thread, functionptr function);
286 void threads_set_thread_priority(pthread_t tid, int priority);
288 #if defined(ENABLE_GC_CACAO)
289 bool threads_suspend_thread(threadobject *thread, s4 reason);
290 void threads_suspend_ack(u1* pc, u1* sp);
291 bool threads_resume_thread(threadobject *thread);
294 void threads_join_all_threads(void);
296 void threads_sleep(int64_t millis, int32_t nanos);
298 void threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
300 void threads_thread_interrupt(threadobject *thread);
302 #if defined(ENABLE_TLH)
303 void threads_tlh_add_frame();
304 void threads_tlh_remove_frame();
307 #endif /* _THREAD_POSIX_H */
311 * These are local overrides for various environment variables in Emacs.
312 * Please do not remove this and leave it at the end of the file, where
313 * Emacs will automagically detect them.
314 * ---------------------------------------------------------------------
317 * indent-tabs-mode: t
321 * vim:noexpandtab:sw=4:ts=4: