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
37 // Includes required by Thread.
39 #if defined(ENABLE_TLH)
43 #include "threads/condition.hpp"
44 #include "threads/mutex.hpp"
46 #include "vm/global.h"
49 /* threadobject ****************************************************************
51 Struct holding thread local variables.
53 *******************************************************************************/
55 #define THREAD_FLAG_JAVA 0x01 /* a normal Java thread */
56 #define THREAD_FLAG_INTERNAL 0x02 /* CACAO internal thread */
57 #define THREAD_FLAG_DAEMON 0x04 /* daemon thread */
58 #define THREAD_FLAG_IN_NATIVE 0x08 /* currently executing native code */
60 #define SUSPEND_REASON_JNI 1 /* suspended from JNI */
61 #define SUSPEND_REASON_STOPWORLD 2 /* suspended from stop-thw-world */
64 typedef struct threadobject threadobject;
67 java_object_t *object; /* link to java.lang.Thread object */
69 ptrint thinlock; /* pre-computed thin lock value */
71 s4 index; /* thread index, starting with 1 */
72 u4 flags; /* flag field */
73 u4 state; /* state field */
75 pthread_t tid; /* pthread id */
77 #if defined(__DARWIN__)
78 mach_port_t mach_thread; /* Darwin thread id */
81 /* for the sable tasuki lock extension */
83 struct threadobject *flc_list; /* FLC list head for this thread */
84 struct threadobject *flc_next; /* next pointer for FLC list */
85 java_handle_t *flc_object;
86 Mutex* flc_lock; /* controlling access to these fields */
89 /* these are used for the wait/notify implementation */
93 Mutex* suspendmutex; /* lock before suspending this thread */
94 Condition* suspendcond; /* notify to resume this thread */
99 bool suspended; /* is this thread suspended? */
100 s4 suspend_reason; /* reason for suspending */
102 u1 *pc; /* current PC (used for profiling) */
104 java_object_t *_exceptionptr; /* current exception */
105 struct stackframeinfo_t *_stackframeinfo; /* current native stackframeinfo */
106 struct localref_table *_localref_table; /* JNI local references */
108 #if defined(ENABLE_INTRP)
109 Cell *_global_sp; /* stack pointer for interpreter */
112 #if defined(ENABLE_GC_CACAO)
113 bool gc_critical; /* indicates a critical section */
116 executionstate_t *es;
119 struct DumpMemory* _dumpmemory; ///< Dump memory structure.
121 #if defined(ENABLE_DEBUG_FILTER)
122 u2 filterverbosecallctr[2]; /* counters for verbose call filter */
126 s4 tracejavacallindent;
127 u4 tracejavacallcount;
130 #if defined(ENABLE_TLH)
134 #if defined(ENABLE_ESCAPE_REASON)
135 void *escape_reasons;
140 /* current threadobject *******************************************************/
142 #if defined(HAVE___THREAD)
144 #define THREADOBJECT thread_current
146 extern __thread threadobject *thread_current;
148 #else /* defined(HAVE___THREAD) */
150 #define THREADOBJECT \
151 ((threadobject *) pthread_getspecific(thread_current_key))
153 extern pthread_key_t thread_current_key;
155 #endif /* defined(HAVE___THREAD) */
158 /* native-world flags *********************************************************/
160 #if defined(ENABLE_GC_CACAO)
161 # define THREAD_NATIVEWORLD_ENTER THREADOBJECT->flags |= THREAD_FLAG_IN_NATIVE
162 # define THREAD_NATIVEWORLD_EXIT THREADOBJECT->flags &= ~THREAD_FLAG_IN_NATIVE
164 # define THREAD_NATIVEWORLD_ENTER /*nop*/
165 # define THREAD_NATIVEWORLD_EXIT /*nop*/
169 /* counter for verbose call filter ********************************************/
171 #if defined(ENABLE_DEBUG_FILTER)
172 # define FILTERVERBOSECALLCTR (THREADOBJECT->filterverbosecallctr)
175 /* state for trace java call **************************************************/
178 # define TRACEJAVACALLINDENT (THREADOBJECT->tracejavacallindent)
179 # define TRACEJAVACALLCOUNT (THREADOBJECT->tracejavacallcount)
187 inline static threadobject* thread_get_current(void);
194 #include "mm/memory.h"
196 #include "native/localref.h"
198 #include "threads/posix/lock.h"
200 #include "vm/global.h"
203 #if defined(ENABLE_GC_CACAO)
204 # include "vm/jit/executionstate.h"
205 # include "vm/jit/replace.hpp"
208 #include "vm/jit/stacktrace.hpp"
210 #if defined(ENABLE_INTRP)
211 #include "vm/jit/intrp/intrp.h"
214 #if defined(__DARWIN__)
215 # include <mach/mach.h>
224 # include <semaphore.h>
233 /* inline functions ***********************************************************/
236 * Return the Thread object of the current thread.
238 * @return The current Thread object.
240 inline static threadobject* thread_get_current(void)
244 #if defined(HAVE___THREAD)
247 t = (threadobject *) pthread_getspecific(thread_current_key);
255 * Set the current Thread object.
257 * @param t The thread object to set.
259 inline static void thread_set_current(threadobject* t)
261 #if defined(HAVE___THREAD)
266 result = pthread_setspecific(thread_current_key, t);
269 vm_abort_errnum(result, "thread_set_current: pthread_setspecific failed");
274 inline static stackframeinfo_t *threads_get_current_stackframeinfo(void)
276 return THREADOBJECT->_stackframeinfo;
279 inline static void threads_set_current_stackframeinfo(stackframeinfo_t *sfi)
281 THREADOBJECT->_stackframeinfo = sfi;
285 /* functions ******************************************************************/
287 void threads_sem_init(sem_t *sem, bool shared, int value);
288 void threads_sem_wait(sem_t *sem);
289 void threads_sem_post(sem_t *sem);
291 void threads_start_thread(threadobject *thread, functionptr function);
293 void threads_set_thread_priority(pthread_t tid, int priority);
295 #if defined(ENABLE_GC_CACAO)
296 bool threads_suspend_thread(threadobject *thread, s4 reason);
297 void threads_suspend_ack(u1* pc, u1* sp);
298 bool threads_resume_thread(threadobject *thread);
301 void threads_join_all_threads(void);
303 void threads_sleep(int64_t millis, int32_t nanos);
305 void threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
307 void threads_thread_interrupt(threadobject *thread);
309 #if defined(ENABLE_TLH)
310 void threads_tlh_add_frame();
311 void threads_tlh_remove_frame();
318 #endif // _THREAD_POSIX_HPP
322 * These are local overrides for various environment variables in Emacs.
323 * Please do not remove this and leave it at the end of the file, where
324 * Emacs will automagically detect them.
325 * ---------------------------------------------------------------------
328 * indent-tabs-mode: t
332 * vim:noexpandtab:sw=4:ts=4: