Merged revisions 7642-7664 via svnmerge from
[cacao.git] / src / threads / native / threads.h
1 /* src/threads/native/threads.h - native threads header
2
3    Copyright (C) 1996-2005, 2006, 2007 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    $Id: threads.h 7667 2007-04-05 00:16:05Z michi $
26
27 */
28
29
30 #ifndef _THREADS_H
31 #define _THREADS_H
32
33 /* forward typedefs ***********************************************************/
34
35 typedef struct threadobject          threadobject;
36 typedef union  threads_table_entry_t threads_table_entry_t;
37 typedef struct threads_table_t       threads_table_t;
38
39
40 #include "config.h"
41
42 #include <pthread.h>
43 #include <ucontext.h>
44
45 #include "vm/types.h"
46
47 #include "mm/memory.h"
48 #include "native/jni.h"
49 #include "native/include/java_lang_Thread.h"
50
51 #include "threads/native/lock.h"
52
53 #include "vm/global.h"
54
55 #if defined(ENABLE_GC_CACAO)
56 # include "vm/jit/replace.h"
57 #endif
58
59 #include "vm/jit/stacktrace.h"
60
61 #if defined(ENABLE_INTRP)
62 #include "vm/jit/intrp/intrp.h"
63 #endif
64
65 #if defined(__DARWIN__)
66 # include <mach/mach.h>
67
68 typedef struct {
69         pthread_mutex_t mutex;
70         pthread_cond_t cond;
71         int value;
72 } sem_t;
73
74 #else
75 # include <semaphore.h>
76 #endif
77
78
79 /* current threadobject *******************************************************/
80
81 #if defined(HAVE___THREAD)
82
83 #define THREADSPECIFIC    __thread
84 #define THREADOBJECT      threads_current_threadobject
85
86 extern __thread threadobject *threads_current_threadobject;
87
88 #else /* defined(HAVE___THREAD) */
89
90 #define THREADSPECIFIC
91 #define THREADOBJECT \
92         ((threadobject *) pthread_getspecific(threads_current_threadobject_key))
93
94 extern pthread_key_t threads_current_threadobject_key;
95
96 #endif /* defined(HAVE___THREAD) */
97
98
99 /* threads_table_entry_t *******************************************************
100
101    An entry in the global threads table.
102
103 *******************************************************************************/
104
105 union threads_table_entry_t {
106         threadobject       *thread;        /* an existing thread                  */
107         ptrint              nextfree;      /* next free index                     */
108 };
109
110
111 /* threads_table_t *************************************************************
112
113    Struct for the global threads table.
114
115 *******************************************************************************/
116
117 struct threads_table_t {
118         threads_table_entry_t *table;      /* the table, threads[0] is the head   */
119                                            /* of the free list. Real entries      */
120                                                                            /* start at threads[1].                */
121         s4                     size;       /* current size of the table           */
122 };
123
124
125 /* threadobject ****************************************************************
126
127    Struct holding thread local variables.
128
129 *******************************************************************************/
130
131 #define THREAD_FLAG_JAVA        0x01    /* a normal Java thread               */
132 #define THREAD_FLAG_INTERNAL    0x02    /* CACAO internal thread              */
133 #define THREAD_FLAG_DAEMON      0x04    /* daemon thread                      */
134 #define THREAD_FLAG_IN_NATIVE   0x08    /* currently executing native code    */
135
136 #define SUSPEND_REASON_JNI       1      /* suspended from JNI                 */
137 #define SUSPEND_REASON_STOPWORLD 2      /* suspended from stop-thw-world      */
138
139
140 struct threadobject {
141         java_lang_Thread     *object;       /* link to java.lang.Thread object    */
142
143         lock_execution_env_t  ee;           /* data for the lock implementation   */
144
145         threadobject         *next;         /* next thread in list, or self       */
146         threadobject         *prev;         /* prev thread in list, or self       */
147
148         ptrint                thinlock;     /* pre-computed thin lock value       */
149
150         s4                    index;        /* thread index, starting with 1      */
151         u4                    flags;        /* flag field                         */
152
153         pthread_t             tid;          /* pthread id                         */
154
155 #if defined(__DARWIN__)
156         mach_port_t           mach_thread;       /* Darwin thread id              */
157 #endif
158
159         pthread_mutex_t       joinmutex;
160         pthread_cond_t        joincond;
161
162         /* these are used for the wait/notify implementation                      */
163         pthread_mutex_t       waitmutex;
164         pthread_cond_t        waitcond;
165
166         pthread_mutex_t       suspendmutex; /* lock before suspending this thread */
167         pthread_cond_t        suspendcond;  /* notify to resume this thread       */
168
169         bool                  interrupted;
170         bool                  signaled;
171         bool                  sleeping;
172
173         bool                  suspended;    /* is this thread suspended?          */
174         s4                    suspend_reason; /* reason for suspending            */
175
176         u1                   *pc;           /* current PC (used for profiling)    */
177
178         java_objectheader    *_exceptionptr;     /* current exception             */
179         stackframeinfo       *_stackframeinfo;   /* current native stackframeinfo */
180         localref_table       *_localref_table;   /* JNI local references          */
181
182 #if defined(ENABLE_INTRP)
183         Cell                 *_global_sp;        /* stack pointer for interpreter */
184 #endif
185
186 #if defined(ENABLE_GC_CACAO)
187         bool                  gc_critical;  /* indicates a critical section       */
188
189         sourcestate_t        *ss;
190         executionstate_t     *es;
191 #endif
192
193         dumpinfo_t            dumpinfo;     /* dump memory info structure         */
194 };
195
196
197 /* exception pointer **********************************************************/
198
199 #define exceptionptr      (&(THREADOBJECT->_exceptionptr))
200
201
202 /* stackframeinfo *************************************************************/
203
204 #define STACKFRAMEINFO    (THREADOBJECT->_stackframeinfo)
205
206
207 /* variables ******************************************************************/
208
209 extern threadobject *mainthreadobj;
210
211
212 /* functions ******************************************************************/
213
214 void threads_sem_init(sem_t *sem, bool shared, int value);
215 void threads_sem_wait(sem_t *sem);
216 void threads_sem_post(sem_t *sem);
217
218 threadobject *threads_get_current_threadobject(void);
219
220 void threads_preinit(void);
221 bool threads_init(void);
222
223 void threads_start_javathread(java_lang_Thread *object);
224 void threads_start_thread(threadobject *thread, functionptr function);
225
226 void threads_set_thread_priority(pthread_t tid, int priority);
227
228 bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
229 bool threads_detach_thread(threadobject *thread);
230
231 bool threads_suspend_thread(threadobject *thread, s4 reason);
232 void threads_suspend_ack(u1* pc, u1* sp);
233 bool threads_resume_thread(threadobject *thread);
234
235 void threads_join_all_threads(void);
236
237 void threads_sleep(s8 millis, s4 nanos);
238 void threads_yield(void);
239
240 bool threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
241
242 void threads_thread_interrupt(threadobject *thread);
243 bool threads_check_if_interrupted_and_reset(void);
244 bool threads_thread_has_been_interrupted(threadobject *thread);
245
246 #if !defined(DISABLE_GC)
247 void threads_stopworld(void);
248 void threads_startworld(void);
249 #endif
250
251 #endif /* _THREADS_H */
252
253
254 /*
255  * These are local overrides for various environment variables in Emacs.
256  * Please do not remove this and leave it at the end of the file, where
257  * Emacs will automagically detect them.
258  * ---------------------------------------------------------------------
259  * Local variables:
260  * mode: c
261  * indent-tabs-mode: t
262  * c-basic-offset: 4
263  * tab-width: 4
264  * End:
265  * vim:noexpandtab:sw=4:ts=4:
266  */