almost all required function implemented - first integration with jdwp - nothing...
[cacao.git] / src / threads / native / threads.h
1 /* src/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    Changes: Christian Thalinger
30
31    $Id: threads.h 3570 2005-11-04 16:58:36Z motse $
32
33 */
34
35
36 #ifndef _THREADS_H
37 #define _THREADS_H
38
39 #include <pthread.h>
40 #include <semaphore.h>
41 #include <ucontext.h>
42
43 #include "config.h"
44 #include "vm/types.h"
45
46 #include "config.h"
47 #include "vm/types.h"
48
49 #include "mm/memory.h"
50 #include "native/jni.h"
51 #include "native/include/java_lang_Object.h" /* required by java/lang/VMThread*/
52 #include "native/include/java_lang_Thread.h"
53 #include "native/include/java_lang_VMThread.h"
54 #include "vm/global.h"
55 #include "vm/tables.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      ((java_lang_VMThread*) 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 ((java_lang_VMThread*) 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 structure.
129
130 *******************************************************************************/
131
132 struct threadobject {
133         java_lang_VMThread  o;
134         nativethread        info;           /* some general pthreads stuff        */
135         ExecEnvironment     ee;             /* contains our lock record pool      */
136
137         /* these are used for the wait/notify implementation                      */
138         pthread_mutex_t     waitLock;
139         pthread_cond_t      waitCond;
140         bool                interrupted;
141         bool                signaled;
142         bool                isSleeping;
143
144         dumpinfo            dumpinfo;       /* dump memory info structure         */
145 };
146
147 /* monitorLockRecord ***********************************************************
148
149    This is the really interesting stuff.
150    See handbook for a detailed description.
151
152 *******************************************************************************/
153
154 struct monitorLockRecord {
155         threadobject      *ownerThread;
156         java_objectheader *o;
157         s4                 lockCount;
158         monitorLockRecord *nextFree;
159         s4                 queuers;
160         monitorLockRecord *waiter;
161         monitorLockRecord *incharge;
162         bool               waiting;
163         sem_t              queueSem;
164         pthread_mutex_t    resolveLock;
165         pthread_cond_t     resolveWait;
166 };
167
168
169 struct lockRecordPoolHeader {
170         lockRecordPool *next;
171         int             size;
172 }; 
173
174 struct lockRecordPool {
175         lockRecordPoolHeader header;
176         monitorLockRecord    lr[1];
177 };
178
179
180 monitorLockRecord *monitorEnter(threadobject *, java_objectheader *);
181 bool monitorExit(threadobject *, java_objectheader *);
182
183 bool threadHoldsLock(threadobject *t, java_objectheader *o);
184 void signal_cond_for_object(java_objectheader *obj);
185 void broadcast_cond_for_object(java_objectheader *obj);
186 void wait_cond_for_object(java_objectheader *obj, s8 time, s4 nanos);
187
188 void *thread_getself(void);
189
190 void threads_preinit(void);
191 bool threads_init(u1 *stackbottom);
192
193 void initObjectLock(java_objectheader *);
194 monitorLockRecord *get_dummyLR(void);
195 void initLocks();
196 void initThread(java_lang_VMThread *);
197
198 /* start a thread */
199 void threads_start_thread(thread *t, functionptr function);
200
201 void joinAllThreads();
202
203 void sleepThread(s8 millis, s4 nanos);
204 void yieldThread();
205
206 void setPriorityThread(thread *t, s4 priority);
207
208 void interruptThread(java_lang_VMThread *);
209 bool interruptedThread();
210 bool isInterruptedThread(java_lang_VMThread *);
211
212 /* This must not be changed, it is used in asm_criticalsections */
213 typedef struct {
214         u1 *mcodebegin;
215         u1 *mcodeend;
216         u1 *mcoderestart;
217 } threadcritnode;
218
219 void thread_registercritical(threadcritnode *);
220 u1 *thread_checkcritical(u1*);
221
222 extern volatile int stopworldwhere;
223 extern threadobject *mainthreadobj;
224
225 extern pthread_mutex_t pool_lock;
226 extern lockRecordPool *global_pool;
227
228
229 void cast_stopworld();
230 void cast_startworld();
231
232 /* dumps all threads */
233 void threads_dump(void);
234
235 /* this is a machine dependent functions (src/vm/jit/$(ARCH_DIR)/md.c) */
236 void thread_restartcriticalsection(ucontext_t *);
237
238 #endif /* _THREADS_H */
239
240
241 /*
242  * These are local overrides for various environment variables in Emacs.
243  * Please do not remove this and leave it at the end of the file, where
244  * Emacs will automagically detect them.
245  * ---------------------------------------------------------------------
246  * Local variables:
247  * mode: c
248  * indent-tabs-mode: t
249  * c-basic-offset: 4
250  * tab-width: 4
251  * End:
252  */