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