* src/threads/posix/lock.c: Moved to .cpp.
[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 "toolbox/list.h"
47
48 #include "vm/global.h"
49
50
51 /* threadobject ****************************************************************
52
53    Struct holding thread local variables.
54
55 *******************************************************************************/
56
57 #define THREAD_FLAG_JAVA        0x01    /* a normal Java thread               */
58 #define THREAD_FLAG_INTERNAL    0x02    /* CACAO internal thread              */
59 #define THREAD_FLAG_DAEMON      0x04    /* daemon thread                      */
60 #define THREAD_FLAG_IN_NATIVE   0x08    /* currently executing native code    */
61
62 #define SUSPEND_REASON_JNI       1      /* suspended from JNI                 */
63 #define SUSPEND_REASON_STOPWORLD 2      /* suspended from stop-thw-world      */
64
65
66 typedef struct threadobject threadobject;
67
68 struct threadobject {
69         java_object_t        *object;       /* link to java.lang.Thread object    */
70
71         ptrint                thinlock;     /* pre-computed thin lock value       */
72
73         s4                    index;        /* thread index, starting with 1      */
74         u4                    flags;        /* flag field                         */
75         u4                    state;        /* state field                        */
76
77         pthread_t             tid;          /* pthread id                         */
78
79 #if defined(__DARWIN__)
80         mach_port_t           mach_thread;       /* Darwin thread id              */
81 #endif
82
83         /* for the sable tasuki lock extension */
84         bool                  flc_bit;
85         struct threadobject  *flc_list;     /* FLC list head for this thread      */
86         struct threadobject  *flc_next;     /* next pointer for FLC list          */
87         java_handle_t        *flc_object;
88         Mutex*                flc_lock;     /* controlling access to these fields */
89         Condition*            flc_cond;
90
91         /* these are used for the wait/notify implementation                      */
92         Mutex*                waitmutex;
93         Condition*            waitcond;
94
95         Mutex*                suspendmutex; /* lock before suspending this thread */
96         Condition*            suspendcond;  /* notify to resume this thread       */
97
98         bool                  interrupted;
99         bool                  signaled;
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         listnode_t            linkage;      /* threads-list                       */
141         listnode_t            linkage_free; /* free-list                          */
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.h"
200
201 #include "native/localref.h"
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 #include "vm/jit/stacktrace.hpp"
214
215 #if defined(ENABLE_INTRP)
216 #include "vm/jit/intrp/intrp.h"
217 #endif
218
219 #if defined(__DARWIN__)
220 # include <mach/mach.h>
221
222 typedef struct {
223         Mutex* mutex;
224         Condition* cond;
225         int value;
226 } sem_t;
227
228 #else
229 # include <semaphore.h>
230 #endif
231
232
233 // FIXME
234 #ifdef __cplusplus
235 extern "C" {
236 #endif
237
238 /* inline functions ***********************************************************/
239
240 /**
241  * Return the Thread object of the current thread.
242  *
243  * @return The current Thread object.
244  */
245 inline static threadobject* thread_get_current(void)
246 {
247         threadobject *t;
248
249 #if defined(HAVE___THREAD)
250         t = thread_current;
251 #else
252         t = (threadobject *) pthread_getspecific(thread_current_key);
253 #endif
254
255         return t;
256 }
257
258
259 /**
260  * Set the current Thread object.
261  *
262  * @param t The thread object to set.
263  */
264 inline static void thread_set_current(threadobject* t)
265 {
266 #if defined(HAVE___THREAD)
267         thread_current = t;
268 #else
269         int result;
270
271         result = pthread_setspecific(thread_current_key, t);
272
273         if (result != 0)
274                 vm_abort_errnum(result, "thread_set_current: pthread_setspecific failed");
275 #endif
276 }
277
278
279 inline static stackframeinfo_t *threads_get_current_stackframeinfo(void)
280 {
281         return THREADOBJECT->_stackframeinfo;
282 }
283
284 inline static void threads_set_current_stackframeinfo(stackframeinfo_t *sfi)
285 {
286         THREADOBJECT->_stackframeinfo = sfi;
287 }
288
289
290 /* functions ******************************************************************/
291
292 void threads_sem_init(sem_t *sem, bool shared, int value);
293 void threads_sem_wait(sem_t *sem);
294 void threads_sem_post(sem_t *sem);
295
296 void threads_start_thread(threadobject *thread, functionptr function);
297
298 void threads_set_thread_priority(pthread_t tid, int priority);
299
300 #if defined(ENABLE_GC_CACAO)
301 bool threads_suspend_thread(threadobject *thread, s4 reason);
302 void threads_suspend_ack(u1* pc, u1* sp);
303 bool threads_resume_thread(threadobject *thread);
304 #endif
305
306 void threads_join_all_threads(void);
307
308 void threads_sleep(int64_t millis, int32_t nanos);
309
310 void threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
311
312 void threads_thread_interrupt(threadobject *thread);
313
314 #if defined(ENABLE_TLH)
315 void threads_tlh_add_frame();
316 void threads_tlh_remove_frame();
317 #endif
318
319 #ifdef __cplusplus
320 } // extern "C"
321 #endif
322
323 #endif // _THREAD_POSIX_HPP
324
325
326 /*
327  * These are local overrides for various environment variables in Emacs.
328  * Please do not remove this and leave it at the end of the file, where
329  * Emacs will automagically detect them.
330  * ---------------------------------------------------------------------
331  * Local variables:
332  * mode: c++
333  * indent-tabs-mode: t
334  * c-basic-offset: 4
335  * tab-width: 4
336  * End:
337  * vim:noexpandtab:sw=4:ts=4:
338  */