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