Merged with tip.
[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 #if defined(ENABLE_TLH)
44 #include "mm/tlh.h"
45 #endif
46
47 #include "native/localref.h"
48
49 #include "threads/condition.hpp"
50 #include "threads/mutex.hpp"
51
52 #include "threads/posix/lock.h"
53
54 #include "vm/global.h"
55 #include "vm/vm.h"
56
57 #if defined(ENABLE_GC_CACAO)
58 # include "vm/jit/replace.h"
59 #endif
60
61 #include "vm/jit/stacktrace.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 typedef struct {
71         mutex_t mutex;
72         pthread_cond_t cond;
73         int value;
74 } sem_t;
75
76 #else
77 # include <semaphore.h>
78 #endif
79
80
81
82 /* current threadobject *******************************************************/
83
84 #if defined(HAVE___THREAD)
85
86 #define THREADOBJECT      thread_current
87
88 extern __thread threadobject *thread_current;
89
90 #else /* defined(HAVE___THREAD) */
91
92 #define THREADOBJECT \
93         ((threadobject *) pthread_getspecific(thread_current_key))
94
95 extern pthread_key_t thread_current_key;
96
97 #endif /* defined(HAVE___THREAD) */
98
99
100 /* threadobject ****************************************************************
101
102    Struct holding thread local variables.
103
104 *******************************************************************************/
105
106 #define THREAD_FLAG_JAVA        0x01    /* a normal Java thread               */
107 #define THREAD_FLAG_INTERNAL    0x02    /* CACAO internal thread              */
108 #define THREAD_FLAG_DAEMON      0x04    /* daemon thread                      */
109 #define THREAD_FLAG_IN_NATIVE   0x08    /* currently executing native code    */
110
111 #define SUSPEND_REASON_JNI       1      /* suspended from JNI                 */
112 #define SUSPEND_REASON_STOPWORLD 2      /* suspended from stop-thw-world      */
113
114
115 struct threadobject {
116         java_object_t        *object;       /* link to java.lang.Thread object    */
117
118         ptrint                thinlock;     /* pre-computed thin lock value       */
119
120         s4                    index;        /* thread index, starting with 1      */
121         u4                    flags;        /* flag field                         */
122         u4                    state;        /* state field                        */
123
124         pthread_t             tid;          /* pthread id                         */
125
126 #if defined(__DARWIN__)
127         mach_port_t           mach_thread;       /* Darwin thread id              */
128 #endif
129
130         /* for the sable tasuki lock extension */
131         bool                  flc_bit;
132         struct threadobject  *flc_list;     /* FLC list head for this thread      */
133         struct threadobject  *flc_next;     /* next pointer for FLC list          */
134         java_handle_t        *flc_object;
135         Mutex*                flc_lock;     /* controlling access to these fields */
136         Condition*            flc_cond;
137
138         /* these are used for the wait/notify implementation                      */
139         Mutex*                waitmutex;
140         Condition*            waitcond;
141
142         Mutex*                suspendmutex; /* lock before suspending this thread */
143         Condition*            suspendcond;  /* notify to resume this thread       */
144
145         bool                  interrupted;
146         bool                  signaled;
147
148         bool                  suspended;    /* is this thread suspended?          */
149         s4                    suspend_reason; /* reason for suspending            */
150
151         u1                   *pc;           /* current PC (used for profiling)    */
152
153         java_object_t        *_exceptionptr;     /* current exception             */
154         stackframeinfo_t     *_stackframeinfo;   /* current native stackframeinfo */
155         localref_table       *_localref_table;   /* JNI local references          */
156
157 #if defined(ENABLE_INTRP)
158         Cell                 *_global_sp;        /* stack pointer for interpreter */
159 #endif
160
161 #if defined(ENABLE_GC_CACAO)
162         bool                  gc_critical;  /* indicates a critical section       */
163
164         sourcestate_t        *ss;
165         executionstate_t     *es;
166 #endif
167
168         dumpinfo_t            dumpinfo;     /* dump memory info structure         */
169
170 #if defined(ENABLE_DEBUG_FILTER)
171         u2                    filterverbosecallctr[2]; /* counters for verbose call filter */
172 #endif
173
174 #if !defined(NDEBUG)
175         s4                    tracejavacallindent;
176         u4                    tracejavacallcount;
177 #endif
178
179 #if defined(ENABLE_TLH)
180         tlh_t                 tlh;
181 #endif
182
183 #if defined(ENABLE_ESCAPE_REASON)
184         void *escape_reasons;
185 #endif
186
187         listnode_t            linkage;      /* threads-list                       */
188         listnode_t            linkage_free; /* free-list                          */
189 };
190
191
192 /* native-world flags *********************************************************/
193
194 #if defined(ENABLE_GC_CACAO)
195 # define THREAD_NATIVEWORLD_ENTER THREADOBJECT->flags |=  THREAD_FLAG_IN_NATIVE
196 # define THREAD_NATIVEWORLD_EXIT  THREADOBJECT->flags &= ~THREAD_FLAG_IN_NATIVE
197 #else
198 # define THREAD_NATIVEWORLD_ENTER /*nop*/
199 # define THREAD_NATIVEWORLD_EXIT  /*nop*/
200 #endif
201
202
203 /* counter for verbose call filter ********************************************/
204
205 #if defined(ENABLE_DEBUG_FILTER)
206 #       define FILTERVERBOSECALLCTR (THREADOBJECT->filterverbosecallctr)
207 #endif
208
209 /* state for trace java call **************************************************/
210
211 #if !defined(NDEBUG)
212 #       define TRACEJAVACALLINDENT (THREADOBJECT->tracejavacallindent)
213 #       define TRACEJAVACALLCOUNT (THREADOBJECT->tracejavacallcount)
214 #endif
215
216
217 /* inline functions ***********************************************************/
218
219 /* thread_get_current **********************************************************
220
221    Return the threadobject of the current thread.
222    
223    RETURN:
224        the current threadobject *
225
226 *******************************************************************************/
227
228 inline static threadobject *thread_get_current(void)
229 {
230         threadobject *t;
231
232 #if defined(HAVE___THREAD)
233         t = thread_current;
234 #else
235         t = (threadobject *) pthread_getspecific(thread_current_key);
236 #endif
237
238         return t;
239 }
240
241
242 /* thread_set_current **********************************************************
243
244    Set the current thread object.
245    
246    IN:
247       t ... the thread object to set
248
249 *******************************************************************************/
250
251 inline static void thread_set_current(threadobject *t)
252 {
253 #if defined(HAVE___THREAD)
254         thread_current = t;
255 #else
256         int result;
257
258         result = pthread_setspecific(thread_current_key, t);
259
260         if (result != 0)
261                 vm_abort_errnum(result, "thread_set_current: pthread_setspecific failed");
262 #endif
263 }
264
265
266 inline static stackframeinfo_t *threads_get_current_stackframeinfo(void)
267 {
268         return THREADOBJECT->_stackframeinfo;
269 }
270
271 inline static void threads_set_current_stackframeinfo(stackframeinfo_t *sfi)
272 {
273         THREADOBJECT->_stackframeinfo = sfi;
274 }
275
276
277 /* functions ******************************************************************/
278
279 void threads_sem_init(sem_t *sem, bool shared, int value);
280 void threads_sem_wait(sem_t *sem);
281 void threads_sem_post(sem_t *sem);
282
283 void threads_start_thread(threadobject *thread, functionptr function);
284
285 void threads_set_thread_priority(pthread_t tid, int priority);
286
287 #if defined(ENABLE_GC_CACAO)
288 bool threads_suspend_thread(threadobject *thread, s4 reason);
289 void threads_suspend_ack(u1* pc, u1* sp);
290 bool threads_resume_thread(threadobject *thread);
291 #endif
292
293 void threads_join_all_threads(void);
294
295 void threads_sleep(int64_t millis, int32_t nanos);
296
297 void threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
298
299 void threads_thread_interrupt(threadobject *thread);
300
301 #if defined(ENABLE_TLH)
302 void threads_tlh_add_frame();
303 void threads_tlh_remove_frame();
304 #endif
305
306 #endif /* _THREAD_POSIX_H */
307
308
309 /*
310  * These are local overrides for various environment variables in Emacs.
311  * Please do not remove this and leave it at the end of the file, where
312  * Emacs will automagically detect them.
313  * ---------------------------------------------------------------------
314  * Local variables:
315  * mode: c
316  * indent-tabs-mode: t
317  * c-basic-offset: 4
318  * tab-width: 4
319  * End:
320  * vim:noexpandtab:sw=4:ts=4:
321  */