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
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.hpp"
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>
87 /* current threadobject *******************************************************/
89 #if defined(HAVE___THREAD)
91 #define THREADOBJECT thread_current
93 extern __thread threadobject *thread_current;
95 #else /* defined(HAVE___THREAD) */
97 #define THREADOBJECT \
98 ((threadobject *) pthread_getspecific(thread_current_key))
100 extern pthread_key_t thread_current_key;
102 #endif /* defined(HAVE___THREAD) */
105 /* threadobject ****************************************************************
107 Struct holding thread local variables.
109 *******************************************************************************/
111 #define THREAD_FLAG_JAVA 0x01 /* a normal Java thread */
112 #define THREAD_FLAG_INTERNAL 0x02 /* CACAO internal thread */
113 #define THREAD_FLAG_DAEMON 0x04 /* daemon thread */
114 #define THREAD_FLAG_IN_NATIVE 0x08 /* currently executing native code */
116 #define SUSPEND_REASON_JNI 1 /* suspended from JNI */
117 #define SUSPEND_REASON_STOPWORLD 2 /* suspended from stop-thw-world */
120 struct threadobject {
121 java_object_t *object; /* link to java.lang.Thread object */
123 ptrint thinlock; /* pre-computed thin lock value */
125 s4 index; /* thread index, starting with 1 */
126 u4 flags; /* flag field */
127 u4 state; /* state field */
129 pthread_t tid; /* pthread id */
131 #if defined(__DARWIN__)
132 mach_port_t mach_thread; /* Darwin thread id */
135 /* for the sable tasuki lock extension */
137 struct threadobject *flc_list; /* FLC list head for this thread */
138 struct threadobject *flc_next; /* next pointer for FLC list */
139 java_handle_t *flc_object;
140 Mutex* flc_lock; /* controlling access to these fields */
143 /* these are used for the wait/notify implementation */
147 Mutex* suspendmutex; /* lock before suspending this thread */
148 Condition* suspendcond; /* notify to resume this thread */
153 bool suspended; /* is this thread suspended? */
154 s4 suspend_reason; /* reason for suspending */
156 u1 *pc; /* current PC (used for profiling) */
158 java_object_t *_exceptionptr; /* current exception */
159 stackframeinfo_t *_stackframeinfo; /* current native stackframeinfo */
160 localref_table *_localref_table; /* JNI local references */
162 #if defined(ENABLE_INTRP)
163 Cell *_global_sp; /* stack pointer for interpreter */
166 #if defined(ENABLE_GC_CACAO)
167 bool gc_critical; /* indicates a critical section */
170 executionstate_t *es;
173 dumpinfo_t dumpinfo; /* dump memory info structure */
175 #if defined(ENABLE_DEBUG_FILTER)
176 u2 filterverbosecallctr[2]; /* counters for verbose call filter */
180 s4 tracejavacallindent;
181 u4 tracejavacallcount;
184 #if defined(ENABLE_TLH)
188 #if defined(ENABLE_ESCAPE_REASON)
189 void *escape_reasons;
192 listnode_t linkage; /* threads-list */
193 listnode_t linkage_free; /* free-list */
197 /* native-world flags *********************************************************/
199 #if defined(ENABLE_GC_CACAO)
200 # define THREAD_NATIVEWORLD_ENTER THREADOBJECT->flags |= THREAD_FLAG_IN_NATIVE
201 # define THREAD_NATIVEWORLD_EXIT THREADOBJECT->flags &= ~THREAD_FLAG_IN_NATIVE
203 # define THREAD_NATIVEWORLD_ENTER /*nop*/
204 # define THREAD_NATIVEWORLD_EXIT /*nop*/
208 /* counter for verbose call filter ********************************************/
210 #if defined(ENABLE_DEBUG_FILTER)
211 # define FILTERVERBOSECALLCTR (THREADOBJECT->filterverbosecallctr)
214 /* state for trace java call **************************************************/
217 # define TRACEJAVACALLINDENT (THREADOBJECT->tracejavacallindent)
218 # define TRACEJAVACALLCOUNT (THREADOBJECT->tracejavacallcount)
222 /* inline functions ***********************************************************/
224 /* thread_get_current **********************************************************
226 Return the threadobject of the current thread.
229 the current threadobject *
231 *******************************************************************************/
233 inline static threadobject *thread_get_current(void)
237 #if defined(HAVE___THREAD)
240 t = (threadobject *) pthread_getspecific(thread_current_key);
247 /* thread_set_current **********************************************************
249 Set the current thread object.
252 t ... the thread object to set
254 *******************************************************************************/
256 inline static void thread_set_current(threadobject *t)
258 #if defined(HAVE___THREAD)
263 result = pthread_setspecific(thread_current_key, t);
266 vm_abort_errnum(result, "thread_set_current: pthread_setspecific failed");
271 inline static stackframeinfo_t *threads_get_current_stackframeinfo(void)
273 return THREADOBJECT->_stackframeinfo;
276 inline static void threads_set_current_stackframeinfo(stackframeinfo_t *sfi)
278 THREADOBJECT->_stackframeinfo = sfi;
282 /* functions ******************************************************************/
284 void threads_sem_init(sem_t *sem, bool shared, int value);
285 void threads_sem_wait(sem_t *sem);
286 void threads_sem_post(sem_t *sem);
288 void threads_start_thread(threadobject *thread, functionptr function);
290 void threads_set_thread_priority(pthread_t tid, int priority);
292 #if defined(ENABLE_GC_CACAO)
293 bool threads_suspend_thread(threadobject *thread, s4 reason);
294 void threads_suspend_ack(u1* pc, u1* sp);
295 bool threads_resume_thread(threadobject *thread);
298 void threads_join_all_threads(void);
300 void threads_sleep(int64_t millis, int32_t nanos);
302 void threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
304 void threads_thread_interrupt(threadobject *thread);
306 #if defined(ENABLE_TLH)
307 void threads_tlh_add_frame();
308 void threads_tlh_remove_frame();
315 #endif // _THREAD_POSIX_HPP
319 * These are local overrides for various environment variables in Emacs.
320 * Please do not remove this and leave it at the end of the file, where
321 * Emacs will automagically detect them.
322 * ---------------------------------------------------------------------
325 * indent-tabs-mode: t
329 * vim:noexpandtab:sw=4:ts=4: