* configure.ac: Define ENABLE_TLH if ENABLE_SSA
[cacao.git] / src / threads / posix / thread-posix.h
1 /* src/threads/posix/thread-posix.h - POSIX thread functions
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #ifndef _THREAD_POSIX_H
27 #define _THREAD_POSIX_H
28
29 /* forward typedefs ***********************************************************/
30
31 typedef struct threadobject threadobject;
32
33
34 #include "config.h"
35
36 #include <pthread.h>
37 #include <ucontext.h>
38
39 #include "vm/types.h"
40
41 #include "mm/memory.h"
42
43 #include "native/localref.h"
44
45 #include "threads/mutex.h"
46
47 #include "threads/posix/lock.h"
48
49 #include "vm/global.h"
50 #include "vm/vm.h"
51
52 #if defined(ENABLE_GC_CACAO)
53 # include "vm/jit/replace.h"
54 #endif
55
56 #include "vm/jit/stacktrace.h"
57
58 #if defined(ENABLE_INTRP)
59 #include "vm/jit/intrp/intrp.h"
60 #endif
61
62 #if defined(__DARWIN__)
63 # include <mach/mach.h>
64
65 typedef struct {
66         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 /* current threadobject *******************************************************/
77
78 #if defined(HAVE___THREAD)
79
80 #define THREADOBJECT      thread_current
81
82 extern __thread threadobject *thread_current;
83
84 #else /* defined(HAVE___THREAD) */
85
86 #define THREADOBJECT \
87         ((threadobject *) pthread_getspecific(thread_current_key))
88
89 extern pthread_key_t thread_current_key;
90
91 #endif /* defined(HAVE___THREAD) */
92
93
94 /* threadobject ****************************************************************
95
96    Struct holding thread local variables.
97
98 *******************************************************************************/
99
100 #define THREAD_FLAG_JAVA        0x01    /* a normal Java thread               */
101 #define THREAD_FLAG_INTERNAL    0x02    /* CACAO internal thread              */
102 #define THREAD_FLAG_DAEMON      0x04    /* daemon thread                      */
103 #define THREAD_FLAG_IN_NATIVE   0x08    /* currently executing native code    */
104
105 #define SUSPEND_REASON_JNI       1      /* suspended from JNI                 */
106 #define SUSPEND_REASON_STOPWORLD 2      /* suspended from stop-thw-world      */
107
108
109 struct threadobject {
110         java_object_t        *object;       /* link to java.lang.Thread object    */
111
112         ptrint                thinlock;     /* pre-computed thin lock value       */
113
114         s4                    index;        /* thread index, starting with 1      */
115         u4                    flags;        /* flag field                         */
116         u4                    state;        /* state field                        */
117
118         pthread_t             tid;          /* pthread id                         */
119
120 #if defined(__DARWIN__)
121         mach_port_t           mach_thread;       /* Darwin thread id              */
122 #endif
123
124         /* for the sable tasuki lock extension */
125         bool                  flc_bit;
126         struct threadobject  *flc_list;     /* FLC list head for this thread      */
127         struct threadobject  *flc_next;     /* next pointer for FLC list          */
128         java_handle_t        *flc_object;
129         mutex_t               flc_lock;     /* controlling access to these fields */
130         pthread_cond_t        flc_cond;
131
132         /* these are used for the wait/notify implementation                      */
133         mutex_t               waitmutex;
134         pthread_cond_t        waitcond;
135
136         mutex_t               suspendmutex; /* lock before suspending this thread */
137         pthread_cond_t        suspendcond;  /* notify to resume this thread       */
138
139         bool                  interrupted;
140         bool                  signaled;
141         bool                  sleeping;
142
143         bool                  suspended;    /* is this thread suspended?          */
144         s4                    suspend_reason; /* reason for suspending            */
145
146         u1                   *pc;           /* current PC (used for profiling)    */
147
148         java_object_t        *_exceptionptr;     /* current exception             */
149         stackframeinfo_t     *_stackframeinfo;   /* current native stackframeinfo */
150         localref_table       *_localref_table;   /* JNI local references          */
151
152 #if defined(ENABLE_INTRP)
153         Cell                 *_global_sp;        /* stack pointer for interpreter */
154 #endif
155
156 #if defined(ENABLE_GC_CACAO)
157         bool                  gc_critical;  /* indicates a critical section       */
158
159         sourcestate_t        *ss;
160         executionstate_t     *es;
161 #endif
162
163         dumpinfo_t            dumpinfo;     /* dump memory info structure         */
164
165 #if defined(ENABLE_DEBUG_FILTER)
166         u2                    filterverbosecallctr[2]; /* counters for verbose call filter */
167 #endif
168
169 #if !defined(NDEBUG)
170         s4                    tracejavacallindent;
171         u4                    tracejavacallcount;
172 #endif
173
174 #if defined(ENABLE_TLH)
175         uint8_t              *tlhstart;
176         uint8_t              *tlhtop;
177         uint8_t              *tlhbase;
178 #endif
179
180         listnode_t            linkage;      /* threads-list                       */
181         listnode_t            linkage_free; /* free-list                          */
182 };
183
184
185 /* native-world flags *********************************************************/
186
187 #if defined(ENABLE_GC_CACAO)
188 # define THREAD_NATIVEWORLD_ENTER THREADOBJECT->flags |=  THREAD_FLAG_IN_NATIVE
189 # define THREAD_NATIVEWORLD_EXIT  THREADOBJECT->flags &= ~THREAD_FLAG_IN_NATIVE
190 #else
191 # define THREAD_NATIVEWORLD_ENTER /*nop*/
192 # define THREAD_NATIVEWORLD_EXIT  /*nop*/
193 #endif
194
195
196 /* counter for verbose call filter ********************************************/
197
198 #if defined(ENABLE_DEBUG_FILTER)
199 #       define FILTERVERBOSECALLCTR (THREADOBJECT->filterverbosecallctr)
200 #endif
201
202 /* state for trace java call **************************************************/
203
204 #if !defined(NDEBUG)
205 #       define TRACEJAVACALLINDENT (THREADOBJECT->tracejavacallindent)
206 #       define TRACEJAVACALLCOUNT (THREADOBJECT->tracejavacallcount)
207 #endif
208
209
210 /* inline functions ***********************************************************/
211
212 /* thread_get_current **********************************************************
213
214    Return the threadobject of the current thread.
215    
216    RETURN:
217        the current threadobject *
218
219 *******************************************************************************/
220
221 inline static threadobject *thread_get_current(void)
222 {
223         threadobject *t;
224
225 #if defined(HAVE___THREAD)
226         t = thread_current;
227 #else
228         t = (threadobject *) pthread_getspecific(thread_current_key);
229 #endif
230
231         return t;
232 }
233
234
235 /* thread_set_current **********************************************************
236
237    Set the current thread object.
238    
239    IN:
240       t ... the thread object to set
241
242 *******************************************************************************/
243
244 inline static void thread_set_current(threadobject *t)
245 {
246 #if defined(HAVE___THREAD)
247         thread_current = t;
248 #else
249         int result;
250
251         result = pthread_setspecific(thread_current_key, t);
252
253         if (result != 0)
254                 vm_abort_errnum(result, "thread_set_current: pthread_setspecific failed");
255 #endif
256 }
257
258
259 inline static stackframeinfo_t *threads_get_current_stackframeinfo(void)
260 {
261         return THREADOBJECT->_stackframeinfo;
262 }
263
264 inline static void threads_set_current_stackframeinfo(stackframeinfo_t *sfi)
265 {
266         THREADOBJECT->_stackframeinfo = sfi;
267 }
268
269
270 /* functions ******************************************************************/
271
272 void threads_sem_init(sem_t *sem, bool shared, int value);
273 void threads_sem_wait(sem_t *sem);
274 void threads_sem_post(sem_t *sem);
275
276 void threads_start_thread(threadobject *thread, functionptr function);
277
278 void threads_set_thread_priority(pthread_t tid, int priority);
279
280 bool threads_detach_thread(threadobject *thread);
281
282 #if defined(ENABLE_GC_CACAO)
283 bool threads_suspend_thread(threadobject *thread, s4 reason);
284 void threads_suspend_ack(u1* pc, u1* sp);
285 bool threads_resume_thread(threadobject *thread);
286 #endif
287
288 void threads_join_all_threads(void);
289
290 void threads_sleep(int64_t millis, int32_t nanos);
291
292 void threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
293
294 void threads_thread_interrupt(threadobject *thread);
295
296 #endif /* _THREAD_POSIX_H */
297
298
299 /*
300  * These are local overrides for various environment variables in Emacs.
301  * Please do not remove this and leave it at the end of the file, where
302  * Emacs will automagically detect them.
303  * ---------------------------------------------------------------------
304  * Local variables:
305  * mode: c
306  * indent-tabs-mode: t
307  * c-basic-offset: 4
308  * tab-width: 4
309  * End:
310  * vim:noexpandtab:sw=4:ts=4:
311  */