New source tree.
[cacao.git] / src / threads / native / threads.h
1 /* threads/native/threads.h - native threads header
2
3    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4    R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5    M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6    P. Tomsich, J. Wenninger
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 1621 2004-11-30 13:06:55Z twisti $
30
31 */
32
33
34 #ifndef _NATIVETHREAD_H
35 #define _NATIVETHREAD_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 typedef struct {
54         s4 super_baseval;
55         s4 super_diffval;
56         s4 sub_baseval;
57 } castinfo;
58
59 struct _threadobject;
60
61
62 typedef struct monitorLockRecord monitorLockRecord;
63
64 struct monitorLockRecord {
65         struct _threadobject *ownerThread;
66         java_objectheader *o;
67         int                lockCount;
68         monitorLockRecord *nextFree;
69         int                queuers;
70         monitorLockRecord *waiter;
71         monitorLockRecord *incharge;
72         bool               waiting;
73         sem_t              queueSem;
74         pthread_mutex_t    resolveLock;
75         pthread_cond_t     resolveWait;
76 };
77
78
79 struct _lockRecordPool;
80
81 typedef struct {
82         struct _lockRecordPool *next;
83         int size;
84 } lockRecordPoolHeader; 
85
86 typedef struct _lockRecordPool {
87         lockRecordPoolHeader header;
88         monitorLockRecord lr[1];
89 } lockRecordPool;
90
91 /* Monitor lock implementation */
92 typedef struct {
93         monitorLockRecord *firstLR;
94         lockRecordPool *lrpool;
95         int numlr;
96 } ExecEnvironment;
97
98 typedef struct {
99         struct _threadobject *next, *prev;
100         java_objectheader *_exceptionptr;
101         methodinfo *_threadrootmethod;
102         void *_stackframeinfo;
103         pthread_t tid;
104 #if defined(__DARWIN__)
105         mach_port_t mach_thread;
106 #endif
107         pthread_mutex_t joinMutex;
108         pthread_cond_t joinCond;
109 } nativethread;
110
111 typedef java_lang_Thread thread;
112
113
114 /* threadobject ****************************************************************
115
116    TODO
117
118 *******************************************************************************/
119
120 typedef struct _threadobject {
121         java_lang_VMThread  o;
122         nativethread        info;
123         ExecEnvironment     ee;
124
125         pthread_mutex_t     waitLock;
126         pthread_cond_t      waitCond;
127         bool                interrupted;
128         bool                signaled;
129         bool                isSleeping;
130
131         dumpinfo            dumpinfo;       /* dump memory info structure         */
132 } threadobject;
133
134
135 monitorLockRecord *monitorEnter(threadobject *, java_objectheader *);
136 bool monitorExit(threadobject *, java_objectheader *);
137
138 bool threadHoldsLock(threadobject *t, java_objectheader *o);
139 void signal_cond_for_object (java_objectheader *obj);
140 void broadcast_cond_for_object (java_objectheader *obj);
141 void wait_cond_for_object (java_objectheader *obj, s8 time, s4 nanos);
142
143 void initThreadsEarly();
144 void initThreads(u1 *stackbottom);
145 void initObjectLock(java_objectheader *);
146 void initLocks();
147 void initThread(java_lang_VMThread *);
148 void startThread(thread *t);
149 void joinAllThreads();
150
151 void sleepThread(s8 millis, s4 nanos);
152 void yieldThread();
153
154 void setPriorityThread(thread *t, s4 priority);
155
156 void interruptThread(java_lang_VMThread *);
157 bool interruptedThread();
158 bool isInterruptedThread(java_lang_VMThread *);
159
160 #if !defined(HAVE___THREAD)
161 extern pthread_key_t tkey_threadinfo;
162 #define THREADOBJECT ((java_lang_VMThread*) pthread_getspecific(tkey_threadinfo))
163 #define THREADINFO (&((threadobject*) pthread_getspecific(tkey_threadinfo))->info)
164 #else
165 extern __thread threadobject *threadobj;
166 #define THREADOBJECT ((java_lang_VMThread*) threadobj)
167 #define THREADINFO (&threadobj->info)
168 #endif
169
170 /*#include "builtin.h"*/
171
172 /* This must not be changed, it is used in asm_criticalsections */
173 typedef struct {
174         u1 *mcodebegin, *mcodeend, *mcoderestart;
175 } threadcritnode;
176
177 void thread_registercritical(threadcritnode *);
178 u1 *thread_checkcritical(u1*);
179
180 extern volatile int stopworldwhere;
181
182 void cast_stopworld();
183 void cast_startworld();
184
185 #endif /* _NATIVETHREAD_H */
186
187
188 /*
189  * These are local overrides for various environment variables in Emacs.
190  * Please do not remove this and leave it at the end of the file, where
191  * Emacs will automagically detect them.
192  * ---------------------------------------------------------------------
193  * Local variables:
194  * mode: c
195  * indent-tabs-mode: t
196  * c-basic-offset: 4
197  * tab-width: 4
198  * End:
199  */