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