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