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