8de392338d988032dc19833ec8b5c156b809ac75
[cacao.git] / src / threads / native / threads.h
1 /* src/threads/native/threads.h - native threads header
2
3    Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25 */
26
27
28 #ifndef _THREADS_H
29 #define _THREADS_H
30
31 /* forward typedefs ***********************************************************/
32
33 typedef struct threadobject threadobject;
34
35
36 #include "config.h"
37
38 #include <pthread.h>
39 #include <ucontext.h>
40
41 #include "vm/types.h"
42
43 #include "mm/memory.h"
44 #include "native/jni.h"
45 #include "native/localref.h"
46 #include "native/include/java_lang_Thread.h"
47
48 #include "threads/native/lock.h"
49
50 #include "vm/global.h"
51
52 #if defined(ENABLE_GC_CACAO)
53 # include "vm/jit/replace.h"
54 #endif
55
56 #include "vm/jit/stacktrace.h"
57
58 #if defined(ENABLE_INTRP)
59 #include "vm/jit/intrp/intrp.h"
60 #endif
61
62 #if defined(__DARWIN__)
63 # include <mach/mach.h>
64
65 typedef struct {
66         pthread_mutex_t mutex;
67         pthread_cond_t cond;
68         int value;
69 } sem_t;
70
71 #else
72 # include <semaphore.h>
73 #endif
74
75
76 /* current threadobject *******************************************************/
77
78 #if defined(HAVE___THREAD)
79
80 #define THREADSPECIFIC    __thread
81 #define THREADOBJECT      threads_current_threadobject
82
83 extern __thread threadobject *threads_current_threadobject;
84
85 #else /* defined(HAVE___THREAD) */
86
87 #define THREADSPECIFIC
88 #define THREADOBJECT \
89         ((threadobject *) pthread_getspecific(threads_current_threadobject_key))
90
91 extern pthread_key_t threads_current_threadobject_key;
92
93 #endif /* defined(HAVE___THREAD) */
94
95
96 /* threadobject ****************************************************************
97
98    Struct holding thread local variables.
99
100 *******************************************************************************/
101
102 #define THREAD_FLAG_JAVA        0x01    /* a normal Java thread               */
103 #define THREAD_FLAG_INTERNAL    0x02    /* CACAO internal thread              */
104 #define THREAD_FLAG_DAEMON      0x04    /* daemon thread                      */
105 #define THREAD_FLAG_IN_NATIVE   0x08    /* currently executing native code    */
106
107 #define SUSPEND_REASON_JNI       1      /* suspended from JNI                 */
108 #define SUSPEND_REASON_STOPWORLD 2      /* suspended from stop-thw-world      */
109
110
111 struct threadobject {
112         java_lang_Thread     *object;       /* link to java.lang.Thread object    */
113
114         ptrint                thinlock;     /* pre-computed thin lock value       */
115
116         s4                    index;        /* thread index, starting with 1      */
117         u4                    flags;        /* flag field                         */
118         u4                    state;        /* state field                        */
119
120         pthread_t             tid;          /* pthread id                         */
121
122 #if defined(__DARWIN__)
123         mach_port_t           mach_thread;       /* Darwin thread id              */
124 #endif
125
126         /* these are used for the wait/notify implementation                      */
127         pthread_mutex_t       waitmutex;
128         pthread_cond_t        waitcond;
129
130         pthread_mutex_t       suspendmutex; /* lock before suspending this thread */
131         pthread_cond_t        suspendcond;  /* notify to resume this thread       */
132
133         bool                  interrupted;
134         bool                  signaled;
135         bool                  sleeping;
136
137         bool                  suspended;    /* is this thread suspended?          */
138         s4                    suspend_reason; /* reason for suspending            */
139
140         u1                   *pc;           /* current PC (used for profiling)    */
141
142         java_object_t        *_exceptionptr;     /* current exception             */
143         stackframeinfo       *_stackframeinfo;   /* current native stackframeinfo */
144         localref_table       *_localref_table;   /* JNI local references          */
145
146 #if defined(ENABLE_INTRP)
147         Cell                 *_global_sp;        /* stack pointer for interpreter */
148 #endif
149
150 #if defined(ENABLE_GC_CACAO)
151         bool                  gc_critical;  /* indicates a critical section       */
152
153         sourcestate_t        *ss;
154         executionstate_t     *es;
155 #endif
156
157         dumpinfo_t            dumpinfo;     /* dump memory info structure         */
158
159 #if defined(ENABLE_DEBUG_FILTER)
160         u2                    filterverbosecallctr[2]; /* counters for verbose call filter */
161 #endif
162
163 #if !defined(NDEBUG)
164         s4                    tracejavacallindent;
165         u4                    tracejavacallcount;
166 #endif
167
168         listnode_t            linkage;      /* threads-list                       */
169 };
170
171
172 /* exception pointer **********************************************************/
173
174 #define exceptionptr      (&(THREADOBJECT->_exceptionptr))
175
176
177 /* stackframeinfo *************************************************************/
178
179 #define STACKFRAMEINFO    (THREADOBJECT->_stackframeinfo)
180
181
182 /* native-world flags *********************************************************/
183
184 #if defined(ENABLE_GC_CACAO)
185 # define THREAD_NATIVEWORLD_ENTER THREADOBJECT->flags |=  THREAD_FLAG_IN_NATIVE
186 # define THREAD_NATIVEWORLD_EXIT  THREADOBJECT->flags &= ~THREAD_FLAG_IN_NATIVE
187 #else
188 # define THREAD_NATIVEWORLD_ENTER /*nop*/
189 # define THREAD_NATIVEWORLD_EXIT  /*nop*/
190 #endif
191
192
193 /* counter for verbose call filter ********************************************/
194
195 #if defined(ENABLE_DEBUG_FILTER)
196 #       define FILTERVERBOSECALLCTR (THREADOBJECT->filterverbosecallctr)
197 #endif
198
199 /* state for trace java call **************************************************/
200
201 #if !defined(NDEBUG)
202 #       define TRACEJAVACALLINDENT (THREADOBJECT->tracejavacallindent)
203 #       define TRACEJAVACALLCOUNT (THREADOBJECT->tracejavacallcount)
204 #endif
205
206 /* functions ******************************************************************/
207
208 void threads_sem_init(sem_t *sem, bool shared, int value);
209 void threads_sem_wait(sem_t *sem);
210 void threads_sem_post(sem_t *sem);
211
212 threadobject *threads_get_current_threadobject(void);
213
214 bool threads_init(void);
215
216 void threads_start_thread(threadobject *thread, functionptr function);
217
218 void threads_set_thread_priority(pthread_t tid, int priority);
219
220 bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
221 bool threads_detach_thread(threadobject *thread);
222
223 bool threads_suspend_thread(threadobject *thread, s4 reason);
224 void threads_suspend_ack(u1* pc, u1* sp);
225 bool threads_resume_thread(threadobject *thread);
226
227 void threads_join_all_threads(void);
228
229 void threads_sleep(s8 millis, s4 nanos);
230
231 bool threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
232
233 void threads_thread_interrupt(threadobject *thread);
234 bool threads_check_if_interrupted_and_reset(void);
235 bool threads_thread_has_been_interrupted(threadobject *thread);
236
237 #if !defined(DISABLE_GC)
238 void threads_stopworld(void);
239 void threads_startworld(void);
240 #endif
241
242 #endif /* _THREADS_H */
243
244
245 /*
246  * These are local overrides for various environment variables in Emacs.
247  * Please do not remove this and leave it at the end of the file, where
248  * Emacs will automagically detect them.
249  * ---------------------------------------------------------------------
250  * Local variables:
251  * mode: c
252  * indent-tabs-mode: t
253  * c-basic-offset: 4
254  * tab-width: 4
255  * End:
256  * vim:noexpandtab:sw=4:ts=4:
257  */