1 /* src/threads/posix/thread-posix.hpp - POSIX thread functions
3 Copyright (C) 1996-2011
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_NONE 0 /* no reason to suspend */
61 #define SUSPEND_REASON_JAVA 1 /* suspended from java.lang.Thread */
62 #define SUSPEND_REASON_STOPWORLD 2 /* suspended from stop-the-world */
63 #define SUSPEND_REASON_DUMP 3 /* suspended from threadlist dumping */
64 #define SUSPEND_REASON_JVMTI 4 /* suspended from JVMTI agent */
67 typedef struct threadobject threadobject;
70 java_object_t *object; /* link to java.lang.Thread object */
72 ptrint thinlock; /* pre-computed thin lock value */
74 s4 index; /* thread index, starting with 1 */
75 u4 flags; /* flag field */
76 u4 state; /* state field */
77 bool is_in_active_list; /* for debugging only */
79 pthread_t tid; /* pthread id */
81 #if defined(__DARWIN__)
82 mach_port_t mach_thread; /* Darwin thread id */
85 /* for the sable tasuki lock extension */
87 struct threadobject *flc_list; /* FLC list head for this thread */
88 struct threadobject *flc_tail; /* tail pointer for FLC list */
89 struct threadobject *flc_next; /* next pointer for FLC list */
90 java_handle_t *flc_object;
91 Mutex* flc_lock; /* controlling access to these fields */
94 /* these are used for the wait/notify implementation */
98 Mutex* suspendmutex; /* lock before suspending this thread */
99 Condition* suspendcond; /* notify to resume this thread */
105 bool suspended; /* is this thread suspended? */
106 s4 suspend_reason; /* reason for suspending */
108 u1 *pc; /* current PC (used for profiling) */
110 java_object_t *_exceptionptr; /* current exception */
111 struct stackframeinfo_t *_stackframeinfo; /* current native stackframeinfo */
112 struct localref_table *_localref_table; /* JNI local references */
114 #if defined(ENABLE_INTRP)
115 Cell *_global_sp; /* stack pointer for interpreter */
118 #if defined(ENABLE_GC_CACAO)
119 bool gc_critical; /* indicates a critical section */
122 executionstate_t *es;
125 struct DumpMemory* _dumpmemory; ///< Dump memory structure.
127 #if defined(ENABLE_DEBUG_FILTER)
128 u2 filterverbosecallctr[2]; /* counters for verbose call filter */
132 s4 tracejavacallindent;
133 u4 tracejavacallcount;
136 #if defined(ENABLE_TLH)
140 #if defined(ENABLE_ESCAPE_REASON)
141 void *escape_reasons;
146 /* current threadobject *******************************************************/
148 #if defined(HAVE___THREAD)
150 #define THREADOBJECT thread_current
152 extern __thread threadobject *thread_current;
154 #else /* defined(HAVE___THREAD) */
156 #define THREADOBJECT \
157 ((threadobject *) pthread_getspecific(thread_current_key))
159 extern pthread_key_t thread_current_key;
161 #endif /* defined(HAVE___THREAD) */
164 /* native-world flags *********************************************************/
166 #if defined(ENABLE_GC_CACAO)
167 # define THREAD_NATIVEWORLD_ENTER THREADOBJECT->flags |= THREAD_FLAG_IN_NATIVE
168 # define THREAD_NATIVEWORLD_EXIT THREADOBJECT->flags &= ~THREAD_FLAG_IN_NATIVE
170 # define THREAD_NATIVEWORLD_ENTER /*nop*/
171 # define THREAD_NATIVEWORLD_EXIT /*nop*/
175 /* counter for verbose call filter ********************************************/
177 #if defined(ENABLE_DEBUG_FILTER)
178 # define FILTERVERBOSECALLCTR (THREADOBJECT->filterverbosecallctr)
181 /* state for trace java call **************************************************/
184 # define TRACEJAVACALLINDENT (THREADOBJECT->tracejavacallindent)
185 # define TRACEJAVACALLCOUNT (THREADOBJECT->tracejavacallcount)
193 inline static threadobject* thread_get_current(void);
200 #include "mm/memory.hpp"
202 #include "native/localref.hpp"
204 #include "threads/lock.hpp"
206 #include "vm/global.h"
209 #if defined(ENABLE_GC_CACAO)
210 # include "vm/jit/executionstate.h"
211 # include "vm/jit/replace.hpp"
214 #if defined(ENABLE_INTRP)
215 #include "vm/jit/intrp/intrp.h"
224 /* inline functions ***********************************************************/
227 * Return the Thread object of the current thread.
229 * @return The current Thread object.
231 inline static threadobject* thread_get_current(void)
235 #if defined(HAVE___THREAD)
238 t = (threadobject *) pthread_getspecific(thread_current_key);
246 * Set the current Thread object.
248 * @param t The thread object to set.
250 inline static void thread_set_current(threadobject* t)
252 #if defined(HAVE___THREAD)
257 result = pthread_setspecific(thread_current_key, t);
260 //os::abort_errnum(result, "thread_set_current: pthread_setspecific failed");
261 vm_abort("thread_set_current: pthread_setspecific failed");
266 inline static struct stackframeinfo_t* threads_get_current_stackframeinfo(void)
268 return THREADOBJECT->_stackframeinfo;
271 inline static void threads_set_current_stackframeinfo(struct stackframeinfo_t* sfi)
273 THREADOBJECT->_stackframeinfo = sfi;
277 /* functions ******************************************************************/
279 void threads_start_thread(threadobject *thread, functionptr function);
281 void threads_set_thread_priority(pthread_t tid, int priority);
283 bool threads_suspend_thread(threadobject *thread, int32_t reason);
284 bool threads_resume_thread(threadobject *thread, int32_t reason);
285 void threads_suspend_ack();
287 void threads_join_all_threads(void);
289 void threads_sleep(int64_t millis, int32_t nanos);
291 void threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
293 void threads_thread_interrupt(threadobject *thread);
295 void threads_park(bool absolute, int64_t nanos);
296 void threads_unpark(threadobject *thread);
298 #if defined(ENABLE_TLH)
299 void threads_tlh_add_frame();
300 void threads_tlh_remove_frame();
307 #endif // _THREAD_POSIX_HPP
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: