4fa18b7b209e6590824f086ccb0b20f48a156c6d
[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                         Edwin Steiner
29
30    Changes: Christian Thalinger
31
32    $Id: threads.h 4953 2006-05-25 12:28:51Z twisti $
33
34 */
35
36
37 #ifndef _THREADS_H
38 #define _THREADS_H
39
40 #include "config.h"
41
42 #include <pthread.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 #include "threads/native/lock.h"
58
59 #if defined(__DARWIN__)
60 # include <mach/mach.h>
61
62 /* We need to emulate recursive mutexes. */
63 # define MUTEXSIM
64
65 typedef struct {
66         pthread_mutex_t mutex;
67         pthread_cond_t cond;
68         int value;
69 } sem_t;
70
71 #else
72 # include <semaphore.h>
73 #endif
74
75
76 /* forward typedefs ***********************************************************/
77
78 typedef struct threadobject          threadobject;
79 typedef union  threads_table_entry_t threads_table_entry_t;
80 typedef struct threads_table_t       threads_table_t;
81
82
83 /* current threadobject *******************************************************/
84
85 #if defined(HAVE___THREAD)
86
87 #define THREADSPECIFIC    __thread
88 #define THREADOBJECT      threads_current_threadobject
89
90 extern __thread threadobject *threads_current_threadobject;
91
92 #else /* defined(HAVE___THREAD) */
93
94 #define THREADSPECIFIC
95 #define THREADOBJECT \
96         ((threadobject *)pthread_getspecific(threads_current_threadobject_key))
97
98 extern pthread_key_t threads_current_threadobject_key;
99
100 #endif /* defined(HAVE___THREAD) */
101
102
103 /* threads_table_entry_t *******************************************************
104
105    An entry in the global threads table.
106
107 *******************************************************************************/
108
109 union threads_table_entry_t {
110         threadobject       *thread;        /* an existing thread                  */
111         ptrint              nextfree;      /* next free index                     */
112 };
113
114
115 /* threads_table_t *************************************************************
116
117    Struct for the global threads table.
118
119 *******************************************************************************/
120
121 struct threads_table_t {
122         threads_table_entry_t *table;      /* the table, threads[0] is the head   */
123                                            /* of the free list. Real entries      */
124                                                                            /* start at threads[1].                */
125         s4                     size;       /* current size of the table           */
126 };
127
128
129 /* threadobject ****************************************************************
130
131    Every java.lang.VMThread object is actually an instance of this
132    structure.
133
134 *******************************************************************************/
135
136 struct threadobject {
137         java_lang_VMThread    o;            /* the java.lang.VMThread object      */
138
139         lock_execution_env_t  ee;           /* data for the lock implementation   */
140
141         threadobject         *next;         /* next thread in list, or self       */
142         threadobject         *prev;         /* prev thread in list, or self       */
143
144         ptrint                thinlock;     /* pre-computed thin lock value       */
145
146         s4                    index;        /* thread index, startin with 1       */
147
148         pthread_t             tid;               /* pthread id                    */
149
150 #if defined(__DARWIN__)
151         mach_port_t           mach_thread;       /* Darwin thread id              */
152 #endif
153
154         pthread_mutex_t       joinmutex;
155         pthread_cond_t        joincond;
156
157         /* these are used for the wait/notify implementation                      */
158         pthread_mutex_t       waitmutex;
159         pthread_cond_t        waitcond;
160
161         bool                  interrupted;
162         bool                  signaled;
163         bool                  sleeping;
164
165         java_objectheader    *_exceptionptr;     /* current exception             */
166         stackframeinfo       *_stackframeinfo;   /* current native stackframeinfo */
167         localref_table       *_localref_table;   /* JNI local references          */
168
169 #if defined(ENABLE_INTRP)
170         u1                   *_global_sp;        /* stack pointer for interpreter */
171 #endif
172
173         dumpinfo              dumpinfo;     /* dump memory info structure         */
174 };
175
176
177 /* variables ******************************************************************/
178
179 extern threadobject *mainthreadobj;
180
181
182 /* functions ******************************************************************/
183
184 void threads_sem_init(sem_t *sem, bool shared, int value);
185 void threads_sem_wait(sem_t *sem);
186 void threads_sem_post(sem_t *sem);
187
188 threadobject *threads_get_current_threadobject(void);
189
190 void threads_preinit(void);
191 bool threads_init(void);
192
193 void threads_init_threadobject(java_lang_VMThread *);
194
195 void threads_start_thread(java_lang_Thread *t, functionptr function);
196
197 void threads_join_all_threads(void);
198
199 void threads_sleep(s8 millis, s4 nanos);
200 void threads_yield(void);
201
202 bool threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
203
204 void threads_interrupt_thread(java_lang_VMThread *);
205 bool threads_check_if_interrupted_and_reset(void);
206 bool threads_thread_has_been_interrupted(java_lang_VMThread *);
207
208 void threads_java_lang_Thread_set_priority(java_lang_Thread *t, s4 priority);
209
210 void threads_cast_stopworld(void);
211 void threads_cast_startworld(void);
212
213 void threads_dump(void);
214
215 #if defined(ENABLE_JVMTI)
216 void jvmti_get_threads_breakpoints(void **brks);
217 #endif
218 /******************************************************************************/
219 /* Recursive Mutex Implementation for Darwin                                  */
220 /******************************************************************************/
221
222 #if defined(MUTEXSIM)
223
224 /* We need this for older MacOSX (10.1.x) */
225
226 typedef struct {
227         pthread_mutex_t mutex;
228         pthread_t owner;
229         int count;
230 } pthread_mutex_rec_t;
231
232 void pthread_mutex_init_rec(pthread_mutex_rec_t *m);
233 void pthread_mutex_destroy_rec(pthread_mutex_rec_t *m);
234 void pthread_mutex_lock_rec(pthread_mutex_rec_t *m);
235 void pthread_mutex_unlock_rec(pthread_mutex_rec_t *m);
236
237 #else /* !defined(MUTEXSIM) */
238
239 #define pthread_mutex_lock_rec pthread_mutex_lock
240 #define pthread_mutex_unlock_rec pthread_mutex_unlock
241 #define pthread_mutex_rec_t pthread_mutex_t
242
243 #endif /* defined(MUTEXSIM) */
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  * vim:noexpandtab:sw=4:ts=4:
260  */