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