* src/threads/posix/thread-posix.cpp: Eliminated some easy-to-fix or pointless compil...
[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_NONE      0      /* no reason to suspend               */
61 #define SUSPEND_REASON_JAVA      1      /* suspended from java.lang.Thread    */
62 #define SUSPEND_REASON_STOPWORLD 2      /* suspended from stop-the-world      */
63 #define SUSPEND_REASON_DUMP      3      /* suspended from threadlist dumping  */
64 #define SUSPEND_REASON_JVMTI     4      /* suspended from JVMTI agent         */
65
66
67 typedef struct threadobject threadobject;
68
69 struct threadobject {
70         java_object_t        *object;       /* link to java.lang.Thread object    */
71
72         ptrint                thinlock;     /* pre-computed thin lock value       */
73
74         s4                    index;        /* thread index, starting with 1      */
75         u4                    flags;        /* flag field                         */
76         u4                    state;        /* state field                        */
77
78         pthread_t             tid;          /* pthread id                         */
79
80 #if defined(__DARWIN__)
81         mach_port_t           mach_thread;       /* Darwin thread id              */
82 #endif
83
84         /* for the sable tasuki lock extension */
85         bool                  flc_bit;
86         struct threadobject  *flc_list;     /* FLC list head for this thread      */
87         struct threadobject  *flc_tail;     /* tail pointer for FLC list          */
88         struct threadobject  *flc_next;     /* next pointer for FLC list          */
89         java_handle_t        *flc_object;
90         Mutex*                flc_lock;     /* controlling access to these fields */
91         Condition*            flc_cond;
92
93         /* these are used for the wait/notify implementation                      */
94         Mutex*                waitmutex;
95         Condition*            waitcond;
96
97         Mutex*                suspendmutex; /* lock before suspending this thread */
98         Condition*            suspendcond;  /* notify to resume this thread       */
99
100         bool                  interrupted;
101         bool                  signaled;
102         bool                  park_permit;
103
104         bool                  suspended;    /* is this thread suspended?          */
105         s4                    suspend_reason; /* reason for suspending            */
106
107         u1                   *pc;           /* current PC (used for profiling)    */
108
109         java_object_t        *_exceptionptr;     /* current exception             */
110         struct stackframeinfo_t     *_stackframeinfo;   /* current native stackframeinfo */
111         struct localref_table       *_localref_table;   /* JNI local references          */
112
113 #if defined(ENABLE_INTRP)
114         Cell                 *_global_sp;        /* stack pointer for interpreter */
115 #endif
116
117 #if defined(ENABLE_GC_CACAO)
118         bool                  gc_critical;  /* indicates a critical section       */
119
120         sourcestate_t        *ss;
121         executionstate_t     *es;
122 #endif
123
124         struct DumpMemory*    _dumpmemory;     ///< Dump memory structure.
125
126 #if defined(ENABLE_DEBUG_FILTER)
127         u2                    filterverbosecallctr[2]; /* counters for verbose call filter */
128 #endif
129
130 #if !defined(NDEBUG)
131         s4                    tracejavacallindent;
132         u4                    tracejavacallcount;
133 #endif
134
135 #if defined(ENABLE_TLH)
136         tlh_t                 tlh;
137 #endif
138
139 #if defined(ENABLE_ESCAPE_REASON)
140         void *escape_reasons;
141 #endif
142 };
143
144
145 /* current threadobject *******************************************************/
146
147 #if defined(HAVE___THREAD)
148
149 #define THREADOBJECT      thread_current
150
151 extern __thread threadobject *thread_current;
152
153 #else /* defined(HAVE___THREAD) */
154
155 #define THREADOBJECT \
156         ((threadobject *) pthread_getspecific(thread_current_key))
157
158 extern pthread_key_t thread_current_key;
159
160 #endif /* defined(HAVE___THREAD) */
161
162
163 /* native-world flags *********************************************************/
164
165 #if defined(ENABLE_GC_CACAO)
166 # define THREAD_NATIVEWORLD_ENTER THREADOBJECT->flags |=  THREAD_FLAG_IN_NATIVE
167 # define THREAD_NATIVEWORLD_EXIT  THREADOBJECT->flags &= ~THREAD_FLAG_IN_NATIVE
168 #else
169 # define THREAD_NATIVEWORLD_ENTER /*nop*/
170 # define THREAD_NATIVEWORLD_EXIT  /*nop*/
171 #endif
172
173
174 /* counter for verbose call filter ********************************************/
175
176 #if defined(ENABLE_DEBUG_FILTER)
177 #       define FILTERVERBOSECALLCTR (THREADOBJECT->filterverbosecallctr)
178 #endif
179
180 /* state for trace java call **************************************************/
181
182 #if !defined(NDEBUG)
183 #       define TRACEJAVACALLINDENT (THREADOBJECT->tracejavacallindent)
184 #       define TRACEJAVACALLCOUNT (THREADOBJECT->tracejavacallcount)
185 #endif
186
187
188 // FIXME
189 #ifdef __cplusplus
190 extern "C" {
191 #endif
192 inline static threadobject* thread_get_current(void);
193 #ifdef __cplusplus
194 }
195 #endif
196
197
198 // Includes.
199 #include "mm/memory.hpp"
200
201 #include "native/localref.hpp"
202
203 #include "threads/lock.hpp"
204
205 #include "vm/global.h"
206 #include "vm/vm.hpp"
207
208 #if defined(ENABLE_GC_CACAO)
209 # include "vm/jit/executionstate.h"
210 # include "vm/jit/replace.hpp"
211 #endif
212
213 #if defined(ENABLE_INTRP)
214 #include "vm/jit/intrp/intrp.h"
215 #endif
216
217
218 // FIXME
219 #ifdef __cplusplus
220 extern "C" {
221 #endif
222
223 /* inline functions ***********************************************************/
224
225 /**
226  * Return the Thread object of the current thread.
227  *
228  * @return The current Thread object.
229  */
230 inline static threadobject* thread_get_current(void)
231 {
232         threadobject *t;
233
234 #if defined(HAVE___THREAD)
235         t = thread_current;
236 #else
237         t = (threadobject *) pthread_getspecific(thread_current_key);
238 #endif
239
240         return t;
241 }
242
243
244 /**
245  * Set the current Thread object.
246  *
247  * @param t The thread object to set.
248  */
249 inline static void thread_set_current(threadobject* t)
250 {
251 #if defined(HAVE___THREAD)
252         thread_current = t;
253 #else
254         int result;
255
256         result = pthread_setspecific(thread_current_key, t);
257
258         if (result != 0)
259                 //os::abort_errnum(result, "thread_set_current: pthread_setspecific failed");
260                 vm_abort("thread_set_current: pthread_setspecific failed");
261 #endif
262 }
263
264
265 inline static struct stackframeinfo_t* threads_get_current_stackframeinfo(void)
266 {
267         return THREADOBJECT->_stackframeinfo;
268 }
269
270 inline static void threads_set_current_stackframeinfo(struct stackframeinfo_t* sfi)
271 {
272         THREADOBJECT->_stackframeinfo = sfi;
273 }
274
275
276 /* functions ******************************************************************/
277
278 void threads_start_thread(threadobject *thread, functionptr function);
279
280 void threads_set_thread_priority(pthread_t tid, int priority);
281
282 bool threads_suspend_thread(threadobject *thread, int32_t reason);
283 bool threads_resume_thread(threadobject *thread, int32_t reason);
284 void threads_suspend_ack();
285
286 void threads_join_all_threads(void);
287
288 void threads_sleep(int64_t millis, int32_t nanos);
289
290 void threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
291
292 void threads_thread_interrupt(threadobject *thread);
293
294 void threads_park(bool absolute, int64_t nanos);
295 void threads_unpark(threadobject *thread);
296
297 #if defined(ENABLE_TLH)
298 void threads_tlh_add_frame();
299 void threads_tlh_remove_frame();
300 #endif
301
302 #ifdef __cplusplus
303 } // extern "C"
304 #endif
305
306 #endif // _THREAD_POSIX_HPP
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  */