major rework of jvmti. now we have three processes in jvmti mode. there are still...
[cacao.git] / src / threads / native / threads.h
1 /* src/threads/native/threads.h - native threads header
2
3    Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, 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., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Stefan Ring
28
29    Changes: Christian Thalinger
30
31    $Id: threads.h 4661 2006-03-21 00:04:59Z motse $
32
33 */
34
35
36 #ifndef _THREADS_H
37 #define _THREADS_H
38
39 #include "config.h"
40
41 #include <pthread.h>
42 #include <semaphore.h>
43 #include <ucontext.h>
44
45 #include "vm/types.h"
46
47 #include "config.h"
48 #include "vm/types.h"
49
50 #include "mm/memory.h"
51 #include "native/jni.h"
52 #include "native/include/java_lang_Object.h" /* required by java/lang/VMThread*/
53 #include "native/include/java_lang_Thread.h"
54 #include "native/include/java_lang_VMThread.h"
55 #include "vm/global.h"
56
57 #if defined(__DARWIN__)
58 #include <mach/mach.h>
59
60 /* We need to emulate recursive mutexes. */
61 #define MUTEXSIM
62 #endif
63
64
65 #if defined(HAVE___THREAD)
66
67 #define THREADSPECIFIC    __thread
68 #define THREADOBJECT      threadobj
69 #define THREADINFO        (&threadobj->info)
70
71 extern __thread threadobject *threadobj;
72
73 #else /* defined(HAVE___THREAD) */
74
75 #define THREADSPECIFIC
76 #define THREADOBJECT      pthread_getspecific(tkey_threadinfo)
77 #define THREADINFO        (&((threadobject*) pthread_getspecific(tkey_threadinfo))->info)
78
79 extern pthread_key_t tkey_threadinfo;
80
81 #endif /* defined(HAVE___THREAD) */
82
83
84 /* typedefs *******************************************************************/
85
86 typedef struct ExecEnvironment ExecEnvironment;
87 typedef struct nativethread nativethread;
88 typedef struct threadobject threadobject;
89 typedef struct monitorLockRecord monitorLockRecord;
90 typedef struct lockRecordPoolHeader lockRecordPoolHeader;
91 typedef struct lockRecordPool lockRecordPool;
92 typedef java_lang_Thread thread;
93
94
95 /* ExecEnvironment *************************************************************
96
97    Monitor lock implementation
98
99 *******************************************************************************/
100
101 struct ExecEnvironment {
102         monitorLockRecord *firstLR;
103         lockRecordPool    *lrpool;
104         int                numlr;
105 };
106
107
108 struct nativethread {
109         threadobject      *next;
110         threadobject      *prev;
111         java_objectheader *_exceptionptr;
112         void              *_stackframeinfo;
113         localref_table    *_localref_table; /* JNI local references               */
114 #if defined(ENABLE_INTRP)
115         void              *_global_sp;
116 #endif
117         pthread_t          tid;
118 #if defined(__DARWIN__)
119         mach_port_t        mach_thread;
120 #endif
121         pthread_mutex_t    joinMutex;
122         pthread_cond_t     joinCond;
123 };
124
125
126 /* threadobject ****************************************************************
127
128    Every java.lang.VMThread object is actually an instance of this
129    structure.
130
131 *******************************************************************************/
132
133 struct threadobject {
134         java_lang_VMThread  o;
135         nativethread        info;           /* some general pthreads stuff        */
136         ExecEnvironment     ee;             /* contains our lock record pool      */
137
138         /* these are used for the wait/notify implementation                      */
139         pthread_mutex_t     waitLock;
140         pthread_cond_t      waitCond;
141         bool                interrupted;
142         bool                signaled;
143         bool                isSleeping;
144
145         dumpinfo            dumpinfo;       /* dump memory info structure         */
146 };
147
148 /* monitorLockRecord ***********************************************************
149
150    This is the really interesting stuff.
151    See handbook for a detailed description.
152
153 *******************************************************************************/
154
155 struct monitorLockRecord {
156         threadobject      *ownerThread;
157         java_objectheader *o;
158         s4                 lockCount;
159         monitorLockRecord *nextFree;
160         s4                 queuers;
161         monitorLockRecord *waiter;
162         monitorLockRecord *incharge;
163         java_objectheader *waiting;
164         sem_t              queueSem;
165         pthread_mutex_t    resolveLock;
166         pthread_cond_t     resolveWait;
167 };
168
169
170 struct lockRecordPoolHeader {
171         lockRecordPool *next;
172         int             size;
173 }; 
174
175 struct lockRecordPool {
176         lockRecordPoolHeader header;
177         monitorLockRecord    lr[1];
178 };
179
180
181 monitorLockRecord *monitorEnter(threadobject *, java_objectheader *);
182 bool monitorExit(threadobject *, java_objectheader *);
183
184 bool threadHoldsLock(threadobject *t, java_objectheader *o);
185
186 void wait_cond_for_object(java_objectheader *o, s8 millis, s4 nanos);
187 void signal_cond_for_object(java_objectheader *o);
188 void broadcast_cond_for_object(java_objectheader *o);
189
190 void *thread_getself(void);
191
192 void threads_preinit(void);
193 bool threads_init(u1 *stackbottom);
194
195 void initObjectLock(java_objectheader *);
196 monitorLockRecord *get_dummyLR(void);
197 void initLocks();
198 void initThread(java_lang_VMThread *);
199
200 /* start a thread */
201 void threads_start_thread(thread *t, functionptr function);
202
203 void joinAllThreads();
204
205 void thread_sleep(s8 millis, s4 nanos);
206 void yieldThread();
207
208 void setPriorityThread(thread *t, s4 priority);
209
210 void interruptThread(java_lang_VMThread *);
211 bool interruptedThread();
212 bool isInterruptedThread(java_lang_VMThread *);
213
214 #if defined(ENABLE_JVMTI)
215 void setthreadobject(threadobject *thread);
216 #endif
217
218
219 /* This must not be changed, it is used in asm_criticalsections */
220 typedef struct {
221         u1 *mcodebegin;
222         u1 *mcodeend;
223         u1 *mcoderestart;
224 } threadcritnode;
225
226 void thread_registercritical(threadcritnode *);
227 u1 *thread_checkcritical(u1*);
228
229 extern volatile int stopworldwhere;
230 extern threadobject *mainthreadobj;
231
232 extern pthread_mutex_t pool_lock;
233 extern lockRecordPool *global_pool;
234
235
236 void cast_stopworld();
237 void cast_startworld();
238
239 /* dumps all threads */
240 void threads_dump(void);
241
242 /* this is a machine dependent functions (src/vm/jit/$(ARCH_DIR)/md.c) */
243 void thread_restartcriticalsection(ucontext_t *);
244
245 #endif /* _THREADS_H */
246
247
248 /*
249  * These are local overrides for various environment variables in Emacs.
250  * Please do not remove this and leave it at the end of the file, where
251  * Emacs will automagically detect them.
252  * ---------------------------------------------------------------------
253  * Local variables:
254  * mode: c
255  * indent-tabs-mode: t
256  * c-basic-offset: 4
257  * tab-width: 4
258  * End:
259  */