fix warning - bug #6
[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 3098 2005-07-22 10:54:35Z motse $
32
33 */
34
35
36 #ifndef _THREADS_H
37 #define _THREADS_H
38
39 #include <pthread.h>
40 #include <semaphore.h>
41
42 #include "mm/memory.h"
43 #include "native/jni.h"
44 #include "native/include/java_lang_Object.h" /* required by java/lang/VMThread*/
45 #include "native/include/java_lang_Thread.h"
46 #include "native/include/java_lang_VMThread.h"
47
48 #if defined(__DARWIN__)
49 #include <mach/mach.h>
50
51 /* We need to emulate recursive mutexes. */
52 #define MUTEXSIM
53 #endif
54
55
56 #if defined(HAVE___THREAD)
57
58 #define THREADSPECIFIC    __thread
59 #define THREADOBJECT      ((java_lang_VMThread*) threadobj)
60 #define THREADINFO        (&threadobj->info)
61
62 extern __thread threadobject *threadobj;
63
64 #else /* defined(HAVE___THREAD) */
65
66 #define THREADSPECIFIC
67 #define THREADOBJECT ((java_lang_VMThread*) pthread_getspecific(tkey_threadinfo))
68 #define THREADINFO (&((threadobject*) pthread_getspecific(tkey_threadinfo))->info)
69
70 extern pthread_key_t tkey_threadinfo;
71
72 #endif /* defined(HAVE___THREAD) */
73
74
75 /* typedefs *******************************************************************/
76
77 typedef struct ExecEnvironment ExecEnvironment;
78 typedef struct nativethread nativethread;
79 typedef struct threadobject threadobject;
80 typedef struct monitorLockRecord monitorLockRecord;
81 typedef struct lockRecordPoolHeader lockRecordPoolHeader;
82 typedef struct lockRecordPool lockRecordPool;
83 typedef java_lang_Thread thread;
84
85
86 /* ExecEnvironment *************************************************************
87
88    Monitor lock implementation
89
90 *******************************************************************************/
91
92 struct ExecEnvironment {
93         monitorLockRecord *firstLR;
94         lockRecordPool    *lrpool;
95         int                numlr;
96 };
97
98
99 struct nativethread {
100         threadobject      *next;
101         threadobject      *prev;
102         java_objectheader *_exceptionptr;
103         u1                 _dontfillinexceptionstacktrace;
104         methodinfo        *_threadrootmethod;
105         void              *_stackframeinfo;
106         pthread_t          tid;
107 #if defined(__DARWIN__)
108         mach_port_t        mach_thread;
109 #endif
110         pthread_mutex_t    joinMutex;
111         pthread_cond_t     joinCond;
112 };
113
114
115 /* threadobject ****************************************************************
116
117    Every java.lang.VMThread object is actually an instance of this structure.
118
119 *******************************************************************************/
120
121 struct threadobject {
122         java_lang_VMThread  o;
123         nativethread        info;           /* some general pthreads stuff        */
124         ExecEnvironment     ee;             /* contains our lock record pool      */
125
126         /* these are used for the wait/notify implementation                      */
127         pthread_mutex_t     waitLock;
128         pthread_cond_t      waitCond;
129         bool                interrupted;
130         bool                signaled;
131         bool                isSleeping;
132
133         dumpinfo            dumpinfo;       /* dump memory info structure         */
134 };
135
136 /* monitorLockRecord ***********************************************************
137
138    This is the really interesting stuff.
139    See handbook for a detailed description.
140
141 *******************************************************************************/
142
143 struct monitorLockRecord {
144         threadobject      *ownerThread;
145         java_objectheader *o;
146         int                lockCount;
147         monitorLockRecord *nextFree;
148         int                queuers;
149         monitorLockRecord *waiter;
150         monitorLockRecord *incharge;
151         bool               waiting;
152         sem_t              queueSem;
153         pthread_mutex_t    resolveLock;
154         pthread_cond_t     resolveWait;
155 };
156
157
158 struct lockRecordPoolHeader {
159         lockRecordPool *next;
160         int             size;
161 }; 
162
163 struct lockRecordPool {
164         lockRecordPoolHeader header;
165         monitorLockRecord    lr[1];
166 };
167
168
169 monitorLockRecord *monitorEnter(threadobject *, java_objectheader *);
170 bool monitorExit(threadobject *, java_objectheader *);
171
172 bool threadHoldsLock(threadobject *t, java_objectheader *o);
173 void signal_cond_for_object (java_objectheader *obj);
174 void broadcast_cond_for_object (java_objectheader *obj);
175 void wait_cond_for_object (java_objectheader *obj, s8 time, s4 nanos);
176
177 void *thread_getself(void);
178
179 void initThreadsEarly();
180 void initThreads(u1 *stackbottom);
181 void initObjectLock(java_objectheader *);
182 monitorLockRecord *get_dummyLR(void);
183 void initLocks();
184 void initThread(java_lang_VMThread *);
185 void startThread(thread *t);
186 void joinAllThreads();
187
188 void sleepThread(s8 millis, s4 nanos);
189 void yieldThread();
190
191 void setPriorityThread(thread *t, s4 priority);
192
193 void interruptThread(java_lang_VMThread *);
194 bool interruptedThread();
195 bool isInterruptedThread(java_lang_VMThread *);
196
197 /* This must not be changed, it is used in asm_criticalsections */
198 typedef struct {
199         u1 *mcodebegin;
200         u1 *mcodeend;
201         u1 *mcoderestart;
202 } threadcritnode;
203
204 void thread_registercritical(threadcritnode *);
205 u1 *thread_checkcritical(u1*);
206
207 extern volatile int stopworldwhere;
208 extern threadobject *mainthreadobj;
209
210
211 void cast_stopworld();
212 void cast_startworld();
213
214 /* dumps all threads */
215 void thread_dump(void);
216
217 #endif /* _THREADS_H */
218
219
220 /*
221  * These are local overrides for various environment variables in Emacs.
222  * Please do not remove this and leave it at the end of the file, where
223  * Emacs will automagically detect them.
224  * ---------------------------------------------------------------------
225  * Local variables:
226  * mode: c
227  * indent-tabs-mode: t
228  * c-basic-offset: 4
229  * tab-width: 4
230  * End:
231  */