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