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