1 /* src/threads/native/threads.h - native threads header
3 Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 Contact: cacao@cacaojvm.org
29 Changes: Christian Thalinger
31 $Id: threads.h 4661 2006-03-21 00:04:59Z motse $
42 #include <semaphore.h>
50 #include "mm/memory.h"
51 #include "native/jni.h"
52 #include "native/include/java_lang_Object.h" /* required by java/lang/VMThread*/
53 #include "native/include/java_lang_Thread.h"
54 #include "native/include/java_lang_VMThread.h"
55 #include "vm/global.h"
57 #if defined(__DARWIN__)
58 #include <mach/mach.h>
60 /* We need to emulate recursive mutexes. */
65 #if defined(HAVE___THREAD)
67 #define THREADSPECIFIC __thread
68 #define THREADOBJECT threadobj
69 #define THREADINFO (&threadobj->info)
71 extern __thread threadobject *threadobj;
73 #else /* defined(HAVE___THREAD) */
75 #define THREADSPECIFIC
76 #define THREADOBJECT pthread_getspecific(tkey_threadinfo)
77 #define THREADINFO (&((threadobject*) pthread_getspecific(tkey_threadinfo))->info)
79 extern pthread_key_t tkey_threadinfo;
81 #endif /* defined(HAVE___THREAD) */
84 /* typedefs *******************************************************************/
86 typedef struct ExecEnvironment ExecEnvironment;
87 typedef struct nativethread nativethread;
88 typedef struct threadobject threadobject;
89 typedef struct monitorLockRecord monitorLockRecord;
90 typedef struct lockRecordPoolHeader lockRecordPoolHeader;
91 typedef struct lockRecordPool lockRecordPool;
92 typedef java_lang_Thread thread;
95 /* ExecEnvironment *************************************************************
97 Monitor lock implementation
99 *******************************************************************************/
101 struct ExecEnvironment {
102 monitorLockRecord *firstLR;
103 lockRecordPool *lrpool;
108 struct nativethread {
111 java_objectheader *_exceptionptr;
112 void *_stackframeinfo;
113 localref_table *_localref_table; /* JNI local references */
114 #if defined(ENABLE_INTRP)
118 #if defined(__DARWIN__)
119 mach_port_t mach_thread;
121 pthread_mutex_t joinMutex;
122 pthread_cond_t joinCond;
126 /* threadobject ****************************************************************
128 Every java.lang.VMThread object is actually an instance of this
131 *******************************************************************************/
133 struct threadobject {
134 java_lang_VMThread o;
135 nativethread info; /* some general pthreads stuff */
136 ExecEnvironment ee; /* contains our lock record pool */
138 /* these are used for the wait/notify implementation */
139 pthread_mutex_t waitLock;
140 pthread_cond_t waitCond;
145 dumpinfo dumpinfo; /* dump memory info structure */
148 /* monitorLockRecord ***********************************************************
150 This is the really interesting stuff.
151 See handbook for a detailed description.
153 *******************************************************************************/
155 struct monitorLockRecord {
156 threadobject *ownerThread;
157 java_objectheader *o;
159 monitorLockRecord *nextFree;
161 monitorLockRecord *waiter;
162 monitorLockRecord *incharge;
163 java_objectheader *waiting;
165 pthread_mutex_t resolveLock;
166 pthread_cond_t resolveWait;
170 struct lockRecordPoolHeader {
171 lockRecordPool *next;
175 struct lockRecordPool {
176 lockRecordPoolHeader header;
177 monitorLockRecord lr[1];
181 monitorLockRecord *monitorEnter(threadobject *, java_objectheader *);
182 bool monitorExit(threadobject *, java_objectheader *);
184 bool threadHoldsLock(threadobject *t, java_objectheader *o);
186 void wait_cond_for_object(java_objectheader *o, s8 millis, s4 nanos);
187 void signal_cond_for_object(java_objectheader *o);
188 void broadcast_cond_for_object(java_objectheader *o);
190 void *thread_getself(void);
192 void threads_preinit(void);
193 bool threads_init(u1 *stackbottom);
195 void initObjectLock(java_objectheader *);
196 monitorLockRecord *get_dummyLR(void);
198 void initThread(java_lang_VMThread *);
201 void threads_start_thread(thread *t, functionptr function);
203 void joinAllThreads();
205 void thread_sleep(s8 millis, s4 nanos);
208 void setPriorityThread(thread *t, s4 priority);
210 void interruptThread(java_lang_VMThread *);
211 bool interruptedThread();
212 bool isInterruptedThread(java_lang_VMThread *);
214 #if defined(ENABLE_JVMTI)
215 void setthreadobject(threadobject *thread);
219 /* This must not be changed, it is used in asm_criticalsections */
226 void thread_registercritical(threadcritnode *);
227 u1 *thread_checkcritical(u1*);
229 extern volatile int stopworldwhere;
230 extern threadobject *mainthreadobj;
232 extern pthread_mutex_t pool_lock;
233 extern lockRecordPool *global_pool;
236 void cast_stopworld();
237 void cast_startworld();
239 /* dumps all threads */
240 void threads_dump(void);
242 /* this is a machine dependent functions (src/vm/jit/$(ARCH_DIR)/md.c) */
243 void thread_restartcriticalsection(ucontext_t *);
245 #endif /* _THREADS_H */
249 * These are local overrides for various environment variables in Emacs.
250 * Please do not remove this and leave it at the end of the file, where
251 * Emacs will automagically detect them.
252 * ---------------------------------------------------------------------
255 * indent-tabs-mode: t