* src/vm/jit/intrp/intrp.h (Cell): Moved the typedef upward, so we can
[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 5869 2006-10-30 11:52:09Z edwin $
33
34 */
35
36
37 #ifndef _THREADS_H
38 #define _THREADS_H
39
40 /* forward typedefs ***********************************************************/
41
42 typedef struct threadobject          threadobject;
43 typedef union  threads_table_entry_t threads_table_entry_t;
44 typedef struct threads_table_t       threads_table_t;
45
46
47 #include "config.h"
48
49 #include <pthread.h>
50 #include <ucontext.h>
51
52 #include "vm/types.h"
53
54 #include "mm/memory.h"
55 #include "native/jni.h"
56 #include "native/include/java_lang_Object.h" /* required by java/lang/VMThread*/
57 #include "native/include/java_lang_Thread.h"
58 #include "native/include/java_lang_VMThread.h"
59 #include "vm/global.h"
60
61 #include "threads/native/lock.h"
62
63 #if defined(ENABLE_INTRP)
64 #include "vm/jit/intrp/intrp.h"
65 #endif
66
67 #if defined(__DARWIN__)
68 # include <mach/mach.h>
69
70 /* We need to emulate recursive mutexes. */
71 # define MUTEXSIM
72
73 typedef struct {
74         pthread_mutex_t mutex;
75         pthread_cond_t cond;
76         int value;
77 } sem_t;
78
79 #else
80 # include <semaphore.h>
81 #endif
82
83
84 /* thread priorities **********************************************************/
85
86 #define MIN_PRIORITY     1
87 #define NORM_PRIORITY    5
88 #define MAX_PRIORITY     10
89
90
91 /* current threadobject *******************************************************/
92
93 #if defined(HAVE___THREAD)
94
95 #define THREADSPECIFIC    __thread
96 #define THREADOBJECT      threads_current_threadobject
97
98 extern __thread threadobject *threads_current_threadobject;
99
100 #else /* defined(HAVE___THREAD) */
101
102 #define THREADSPECIFIC
103 #define THREADOBJECT \
104         ((threadobject *)pthread_getspecific(threads_current_threadobject_key))
105
106 extern pthread_key_t threads_current_threadobject_key;
107
108 #endif /* defined(HAVE___THREAD) */
109
110
111 /* threads_table_entry_t *******************************************************
112
113    An entry in the global threads table.
114
115 *******************************************************************************/
116
117 union threads_table_entry_t {
118         threadobject       *thread;        /* an existing thread                  */
119         ptrint              nextfree;      /* next free index                     */
120 };
121
122
123 /* threads_table_t *************************************************************
124
125    Struct for the global threads table.
126
127 *******************************************************************************/
128
129 struct threads_table_t {
130         threads_table_entry_t *table;      /* the table, threads[0] is the head   */
131                                            /* of the free list. Real entries      */
132                                                                            /* start at threads[1].                */
133         s4                     size;       /* current size of the table           */
134 };
135
136
137 /* threadobject ****************************************************************
138
139    Every java.lang.VMThread object is actually an instance of this
140    structure.
141
142 *******************************************************************************/
143
144 #define THREAD_FLAG_JAVA        0x01    /* a normal Java thread               */
145 #define THREAD_FLAG_INTERNAL    0x02    /* CACAO internal thread              */
146
147
148 struct threadobject {
149         java_lang_VMThread    o;            /* the java.lang.VMThread object      */
150
151         lock_execution_env_t  ee;           /* data for the lock implementation   */
152
153         threadobject         *next;         /* next thread in list, or self       */
154         threadobject         *prev;         /* prev thread in list, or self       */
155
156         ptrint                thinlock;     /* pre-computed thin lock value       */
157
158         s4                    index;        /* thread index, starting with 1      */
159         u1                    flags;        /* flag field                         */
160
161         pthread_t             tid;          /* pthread id                         */
162
163 #if defined(__DARWIN__)
164         mach_port_t           mach_thread;       /* Darwin thread id              */
165 #endif
166
167         pthread_mutex_t       joinmutex;
168         pthread_cond_t        joincond;
169
170         /* these are used for the wait/notify implementation                      */
171         pthread_mutex_t       waitmutex;
172         pthread_cond_t        waitcond;
173
174         bool                  interrupted;
175         bool                  signaled;
176         bool                  sleeping;
177
178         u1                   *pc;           /* current PC (used for profiling)    */
179
180         java_objectheader    *_exceptionptr;     /* current exception             */
181         stackframeinfo       *_stackframeinfo;   /* current native stackframeinfo */
182         localref_table       *_localref_table;   /* JNI local references          */
183
184 #if defined(ENABLE_INTRP)
185         Cell                 *_global_sp;        /* stack pointer for interpreter */
186 #endif
187
188         dumpinfo_t            dumpinfo;     /* dump memory info structure         */
189 };
190
191
192 /* variables ******************************************************************/
193
194 extern threadobject *mainthreadobj;
195
196
197 /* functions ******************************************************************/
198
199 void threads_sem_init(sem_t *sem, bool shared, int value);
200 void threads_sem_wait(sem_t *sem);
201 void threads_sem_post(sem_t *sem);
202
203 threadobject *threads_get_current_threadobject(void);
204
205 void threads_preinit(void);
206 bool threads_init(void);
207
208 void threads_init_threadobject(java_lang_VMThread *);
209
210 void threads_start_thread(java_lang_Thread *t, functionptr function);
211
212 bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
213 bool threads_detach_thread(threadobject *thread);
214
215 void threads_join_all_threads(void);
216
217 void threads_sleep(s8 millis, s4 nanos);
218 void threads_yield(void);
219
220 bool threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
221
222 void threads_thread_interrupt(java_lang_VMThread *);
223 bool threads_check_if_interrupted_and_reset(void);
224 bool threads_thread_has_been_interrupted(java_lang_VMThread *);
225
226 void threads_java_lang_Thread_set_priority(java_lang_Thread *t, s4 priority);
227
228 void threads_cast_stopworld(void);
229 void threads_cast_startworld(void);
230
231 void threads_dump(void);
232
233 /******************************************************************************/
234 /* Recursive Mutex Implementation for Darwin                                  */
235 /******************************************************************************/
236
237 #if defined(MUTEXSIM)
238
239 /* We need this for older MacOSX (10.1.x) */
240
241 typedef struct {
242         pthread_mutex_t mutex;
243         pthread_t owner;
244         int count;
245 } pthread_mutex_rec_t;
246
247 void pthread_mutex_init_rec(pthread_mutex_rec_t *m);
248 void pthread_mutex_destroy_rec(pthread_mutex_rec_t *m);
249 void pthread_mutex_lock_rec(pthread_mutex_rec_t *m);
250 void pthread_mutex_unlock_rec(pthread_mutex_rec_t *m);
251
252 #else /* !defined(MUTEXSIM) */
253
254 #define pthread_mutex_lock_rec pthread_mutex_lock
255 #define pthread_mutex_unlock_rec pthread_mutex_unlock
256 #define pthread_mutex_rec_t pthread_mutex_t
257
258 #endif /* defined(MUTEXSIM) */
259
260 #endif /* _THREADS_H */
261
262
263 /*
264  * These are local overrides for various environment variables in Emacs.
265  * Please do not remove this and leave it at the end of the file, where
266  * Emacs will automagically detect them.
267  * ---------------------------------------------------------------------
268  * Local variables:
269  * mode: c
270  * indent-tabs-mode: t
271  * c-basic-offset: 4
272  * tab-width: 4
273  * End:
274  * vim:noexpandtab:sw=4:ts=4:
275  */