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