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