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