4dade8c93473343a5a138727dc0c9eaf95896820
[cacao.git] / src / threads / native / threads.h
1 /* src/threads/native/threads.h - native threads header
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Stefan Ring
28
29    Changes: Christian Thalinger
30
31    $Id: threads.h 3830 2005-12-01 23:07:33Z twisti $
32
33 */
34
35
36 #ifndef _THREADS_H
37 #define _THREADS_H
38
39 #include <pthread.h>
40 #include <semaphore.h>
41 #include <ucontext.h>
42
43 #include "config.h"
44 #include "vm/types.h"
45
46 #include "config.h"
47 #include "vm/types.h"
48
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
56 #if defined(__DARWIN__)
57 #include <mach/mach.h>
58
59 /* We need to emulate recursive mutexes. */
60 #define MUTEXSIM
61 #endif
62
63
64 #if defined(HAVE___THREAD)
65
66 #define THREADSPECIFIC    __thread
67 #define THREADOBJECT      ((java_lang_VMThread*) threadobj)
68 #define THREADINFO        (&threadobj->info)
69
70 extern __thread threadobject *threadobj;
71
72 #else /* defined(HAVE___THREAD) */
73
74 #define THREADSPECIFIC
75 #define THREADOBJECT ((java_lang_VMThread*) pthread_getspecific(tkey_threadinfo))
76 #define THREADINFO (&((threadobject*) pthread_getspecific(tkey_threadinfo))->info)
77
78 extern pthread_key_t tkey_threadinfo;
79
80 #endif /* defined(HAVE___THREAD) */
81
82
83 /* typedefs *******************************************************************/
84
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;
92
93
94 /* ExecEnvironment *************************************************************
95
96    Monitor lock implementation
97
98 *******************************************************************************/
99
100 struct ExecEnvironment {
101         monitorLockRecord *firstLR;
102         lockRecordPool    *lrpool;
103         int                numlr;
104 };
105
106
107 struct nativethread {
108         threadobject      *next;
109         threadobject      *prev;
110         java_objectheader *_exceptionptr;
111         void              *_stackframeinfo;
112         localref_table    *_localref_table; /* JNI local references               */
113 #if defined(ENABLE_INTRP)
114         void              *_global_sp;
115 #endif
116         pthread_t          tid;
117 #if defined(__DARWIN__)
118         mach_port_t        mach_thread;
119 #endif
120         pthread_mutex_t    joinMutex;
121         pthread_cond_t     joinCond;
122 };
123
124
125 /* threadobject ****************************************************************
126
127    Every java.lang.VMThread object is actually an instance of this structure.
128
129 *******************************************************************************/
130
131 struct threadobject {
132         java_lang_VMThread  o;
133         nativethread        info;           /* some general pthreads stuff        */
134         ExecEnvironment     ee;             /* contains our lock record pool      */
135
136         /* these are used for the wait/notify implementation                      */
137         pthread_mutex_t     waitLock;
138         pthread_cond_t      waitCond;
139         bool                interrupted;
140         bool                signaled;
141         bool                isSleeping;
142
143         dumpinfo            dumpinfo;       /* dump memory info structure         */
144 };
145
146 /* monitorLockRecord ***********************************************************
147
148    This is the really interesting stuff.
149    See handbook for a detailed description.
150
151 *******************************************************************************/
152
153 struct monitorLockRecord {
154         threadobject      *ownerThread;
155         java_objectheader *o;
156         s4                 lockCount;
157         monitorLockRecord *nextFree;
158         s4                 queuers;
159         monitorLockRecord *waiter;
160         monitorLockRecord *incharge;
161         bool               waiting;
162         sem_t              queueSem;
163         pthread_mutex_t    resolveLock;
164         pthread_cond_t     resolveWait;
165 };
166
167
168 struct lockRecordPoolHeader {
169         lockRecordPool *next;
170         int             size;
171 }; 
172
173 struct lockRecordPool {
174         lockRecordPoolHeader header;
175         monitorLockRecord    lr[1];
176 };
177
178
179 monitorLockRecord *monitorEnter(threadobject *, java_objectheader *);
180 bool monitorExit(threadobject *, java_objectheader *);
181
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);
186
187 void *thread_getself(void);
188
189 void threads_preinit(void);
190 bool threads_init(u1 *stackbottom);
191
192 void initObjectLock(java_objectheader *);
193 monitorLockRecord *get_dummyLR(void);
194 void initLocks();
195 void initThread(java_lang_VMThread *);
196
197 /* start a thread */
198 void threads_start_thread(thread *t, functionptr function);
199
200 void joinAllThreads();
201
202 void sleepThread(s8 millis, s4 nanos);
203 void yieldThread();
204
205 void setPriorityThread(thread *t, s4 priority);
206
207 void interruptThread(java_lang_VMThread *);
208 bool interruptedThread();
209 bool isInterruptedThread(java_lang_VMThread *);
210
211 /* This must not be changed, it is used in asm_criticalsections */
212 typedef struct {
213         u1 *mcodebegin;
214         u1 *mcodeend;
215         u1 *mcoderestart;
216 } threadcritnode;
217
218 void thread_registercritical(threadcritnode *);
219 u1 *thread_checkcritical(u1*);
220
221 extern volatile int stopworldwhere;
222 extern threadobject *mainthreadobj;
223
224 extern pthread_mutex_t pool_lock;
225 extern lockRecordPool *global_pool;
226
227
228 void cast_stopworld();
229 void cast_startworld();
230
231 /* dumps all threads */
232 void threads_dump(void);
233
234 /* this is a machine dependent functions (src/vm/jit/$(ARCH_DIR)/md.c) */
235 void thread_restartcriticalsection(ucontext_t *);
236
237 #endif /* _THREADS_H */
238
239
240 /*
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  * ---------------------------------------------------------------------
245  * Local variables:
246  * mode: c
247  * indent-tabs-mode: t
248  * c-basic-offset: 4
249  * tab-width: 4
250  * End:
251  */