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