1 /* src/threads/native/threads.h - native threads header
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 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., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
29 Changes: Christian Thalinger
31 $Id: threads.h 3830 2005-12-01 23:07:33Z twisti $
40 #include <semaphore.h>
49 #include "mm/memory.h"
50 #include "native/jni.h"
51 #include "native/include/java_lang_Object.h" /* required by java/lang/VMThread*/
52 #include "native/include/java_lang_Thread.h"
53 #include "native/include/java_lang_VMThread.h"
54 #include "vm/global.h"
56 #if defined(__DARWIN__)
57 #include <mach/mach.h>
59 /* We need to emulate recursive mutexes. */
64 #if defined(HAVE___THREAD)
66 #define THREADSPECIFIC __thread
67 #define THREADOBJECT ((java_lang_VMThread*) threadobj)
68 #define THREADINFO (&threadobj->info)
70 extern __thread threadobject *threadobj;
72 #else /* defined(HAVE___THREAD) */
74 #define THREADSPECIFIC
75 #define THREADOBJECT ((java_lang_VMThread*) pthread_getspecific(tkey_threadinfo))
76 #define THREADINFO (&((threadobject*) pthread_getspecific(tkey_threadinfo))->info)
78 extern pthread_key_t tkey_threadinfo;
80 #endif /* defined(HAVE___THREAD) */
83 /* typedefs *******************************************************************/
85 typedef struct ExecEnvironment ExecEnvironment;
86 typedef struct nativethread nativethread;
87 typedef struct threadobject threadobject;
88 typedef struct monitorLockRecord monitorLockRecord;
89 typedef struct lockRecordPoolHeader lockRecordPoolHeader;
90 typedef struct lockRecordPool lockRecordPool;
91 typedef java_lang_Thread thread;
94 /* ExecEnvironment *************************************************************
96 Monitor lock implementation
98 *******************************************************************************/
100 struct ExecEnvironment {
101 monitorLockRecord *firstLR;
102 lockRecordPool *lrpool;
107 struct nativethread {
110 java_objectheader *_exceptionptr;
111 void *_stackframeinfo;
112 localref_table *_localref_table; /* JNI local references */
113 #if defined(ENABLE_INTRP)
117 #if defined(__DARWIN__)
118 mach_port_t mach_thread;
120 pthread_mutex_t joinMutex;
121 pthread_cond_t joinCond;
125 /* threadobject ****************************************************************
127 Every java.lang.VMThread object is actually an instance of this structure.
129 *******************************************************************************/
131 struct threadobject {
132 java_lang_VMThread o;
133 nativethread info; /* some general pthreads stuff */
134 ExecEnvironment ee; /* contains our lock record pool */
136 /* these are used for the wait/notify implementation */
137 pthread_mutex_t waitLock;
138 pthread_cond_t waitCond;
143 dumpinfo dumpinfo; /* dump memory info structure */
146 /* monitorLockRecord ***********************************************************
148 This is the really interesting stuff.
149 See handbook for a detailed description.
151 *******************************************************************************/
153 struct monitorLockRecord {
154 threadobject *ownerThread;
155 java_objectheader *o;
157 monitorLockRecord *nextFree;
159 monitorLockRecord *waiter;
160 monitorLockRecord *incharge;
163 pthread_mutex_t resolveLock;
164 pthread_cond_t resolveWait;
168 struct lockRecordPoolHeader {
169 lockRecordPool *next;
173 struct lockRecordPool {
174 lockRecordPoolHeader header;
175 monitorLockRecord lr[1];
179 monitorLockRecord *monitorEnter(threadobject *, java_objectheader *);
180 bool monitorExit(threadobject *, java_objectheader *);
182 bool threadHoldsLock(threadobject *t, java_objectheader *o);
183 void signal_cond_for_object(java_objectheader *obj);
184 void broadcast_cond_for_object(java_objectheader *obj);
185 void wait_cond_for_object(java_objectheader *obj, s8 time, s4 nanos);
187 void *thread_getself(void);
189 void threads_preinit(void);
190 bool threads_init(u1 *stackbottom);
192 void initObjectLock(java_objectheader *);
193 monitorLockRecord *get_dummyLR(void);
195 void initThread(java_lang_VMThread *);
198 void threads_start_thread(thread *t, functionptr function);
200 void joinAllThreads();
202 void sleepThread(s8 millis, s4 nanos);
205 void setPriorityThread(thread *t, s4 priority);
207 void interruptThread(java_lang_VMThread *);
208 bool interruptedThread();
209 bool isInterruptedThread(java_lang_VMThread *);
211 /* This must not be changed, it is used in asm_criticalsections */
218 void thread_registercritical(threadcritnode *);
219 u1 *thread_checkcritical(u1*);
221 extern volatile int stopworldwhere;
222 extern threadobject *mainthreadobj;
224 extern pthread_mutex_t pool_lock;
225 extern lockRecordPool *global_pool;
228 void cast_stopworld();
229 void cast_startworld();
231 /* dumps all threads */
232 void threads_dump(void);
234 /* this is a machine dependent functions (src/vm/jit/$(ARCH_DIR)/md.c) */
235 void thread_restartcriticalsection(ucontext_t *);
237 #endif /* _THREADS_H */
241 * These are local overrides for various environment variables in Emacs.
242 * Please do not remove this and leave it at the end of the file, where
243 * Emacs will automagically detect them.
244 * ---------------------------------------------------------------------
247 * indent-tabs-mode: t