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 3570 2005-11-04 16:58:36Z motse $
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"
55 #include "vm/tables.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 ((java_lang_VMThread*) threadobj)
69 #define THREADINFO (&threadobj->info)
71 extern __thread threadobject *threadobj;
73 #else /* defined(HAVE___THREAD) */
75 #define THREADSPECIFIC
76 #define THREADOBJECT ((java_lang_VMThread*) 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 structure.
130 *******************************************************************************/
132 struct threadobject {
133 java_lang_VMThread o;
134 nativethread info; /* some general pthreads stuff */
135 ExecEnvironment ee; /* contains our lock record pool */
137 /* these are used for the wait/notify implementation */
138 pthread_mutex_t waitLock;
139 pthread_cond_t waitCond;
144 dumpinfo dumpinfo; /* dump memory info structure */
147 /* monitorLockRecord ***********************************************************
149 This is the really interesting stuff.
150 See handbook for a detailed description.
152 *******************************************************************************/
154 struct monitorLockRecord {
155 threadobject *ownerThread;
156 java_objectheader *o;
158 monitorLockRecord *nextFree;
160 monitorLockRecord *waiter;
161 monitorLockRecord *incharge;
164 pthread_mutex_t resolveLock;
165 pthread_cond_t resolveWait;
169 struct lockRecordPoolHeader {
170 lockRecordPool *next;
174 struct lockRecordPool {
175 lockRecordPoolHeader header;
176 monitorLockRecord lr[1];
180 monitorLockRecord *monitorEnter(threadobject *, java_objectheader *);
181 bool monitorExit(threadobject *, java_objectheader *);
183 bool threadHoldsLock(threadobject *t, java_objectheader *o);
184 void signal_cond_for_object(java_objectheader *obj);
185 void broadcast_cond_for_object(java_objectheader *obj);
186 void wait_cond_for_object(java_objectheader *obj, s8 time, s4 nanos);
188 void *thread_getself(void);
190 void threads_preinit(void);
191 bool threads_init(u1 *stackbottom);
193 void initObjectLock(java_objectheader *);
194 monitorLockRecord *get_dummyLR(void);
196 void initThread(java_lang_VMThread *);
199 void threads_start_thread(thread *t, functionptr function);
201 void joinAllThreads();
203 void sleepThread(s8 millis, s4 nanos);
206 void setPriorityThread(thread *t, s4 priority);
208 void interruptThread(java_lang_VMThread *);
209 bool interruptedThread();
210 bool isInterruptedThread(java_lang_VMThread *);
212 /* This must not be changed, it is used in asm_criticalsections */
219 void thread_registercritical(threadcritnode *);
220 u1 *thread_checkcritical(u1*);
222 extern volatile int stopworldwhere;
223 extern threadobject *mainthreadobj;
225 extern pthread_mutex_t pool_lock;
226 extern lockRecordPool *global_pool;
229 void cast_stopworld();
230 void cast_startworld();
232 /* dumps all threads */
233 void threads_dump(void);
235 /* this is a machine dependent functions (src/vm/jit/$(ARCH_DIR)/md.c) */
236 void thread_restartcriticalsection(ucontext_t *);
238 #endif /* _THREADS_H */
242 * These are local overrides for various environment variables in Emacs.
243 * Please do not remove this and leave it at the end of the file, where
244 * Emacs will automagically detect them.
245 * ---------------------------------------------------------------------
248 * indent-tabs-mode: t