Merged revisions 8299-8320 via svnmerge from
[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    $Id: threads.h 8321 2007-08-16 11:37:25Z michi $
26
27 */
28
29
30 #ifndef _THREADS_H
31 #define _THREADS_H
32
33 /* forward typedefs ***********************************************************/
34
35 typedef struct threadobject threadobject;
36
37
38 #include "config.h"
39
40 #include <pthread.h>
41 #include <ucontext.h>
42
43 #include "vm/types.h"
44
45 #include "mm/memory.h"
46 #include "native/jni.h"
47 #include "native/localref.h"
48 #include "native/include/java_lang_Thread.h"
49
50 #include "threads/native/lock.h"
51
52 #include "vm/global.h"
53
54 #if defined(ENABLE_GC_CACAO)
55 # include "vm/jit/replace.h"
56 #endif
57
58 #include "vm/jit/stacktrace.h"
59
60 #if defined(ENABLE_INTRP)
61 #include "vm/jit/intrp/intrp.h"
62 #endif
63
64 #if defined(__DARWIN__)
65 # include <mach/mach.h>
66
67 typedef struct {
68         pthread_mutex_t mutex;
69         pthread_cond_t cond;
70         int value;
71 } sem_t;
72
73 #else
74 # include <semaphore.h>
75 #endif
76
77
78 /* current threadobject *******************************************************/
79
80 #if defined(HAVE___THREAD)
81
82 #define THREADSPECIFIC    __thread
83 #define THREADOBJECT      threads_current_threadobject
84
85 extern __thread threadobject *threads_current_threadobject;
86
87 #else /* defined(HAVE___THREAD) */
88
89 #define THREADSPECIFIC
90 #define THREADOBJECT \
91         ((threadobject *) pthread_getspecific(threads_current_threadobject_key))
92
93 extern pthread_key_t threads_current_threadobject_key;
94
95 #endif /* defined(HAVE___THREAD) */
96
97
98 /* threadobject ****************************************************************
99
100    Struct holding thread local variables.
101
102 *******************************************************************************/
103
104 #define THREAD_FLAG_JAVA        0x01    /* a normal Java thread               */
105 #define THREAD_FLAG_INTERNAL    0x02    /* CACAO internal thread              */
106 #define THREAD_FLAG_DAEMON      0x04    /* daemon thread                      */
107 #define THREAD_FLAG_IN_NATIVE   0x08    /* currently executing native code    */
108
109 #define SUSPEND_REASON_JNI       1      /* suspended from JNI                 */
110 #define SUSPEND_REASON_STOPWORLD 2      /* suspended from stop-thw-world      */
111
112
113 struct threadobject {
114         java_lang_Thread     *object;       /* link to java.lang.Thread object    */
115
116         ptrint                thinlock;     /* pre-computed thin lock value       */
117
118         s4                    index;        /* thread index, starting with 1      */
119         u4                    flags;        /* flag field                         */
120         u4                    state;        /* state field                        */
121
122         pthread_t             tid;          /* pthread id                         */
123
124 #if defined(__DARWIN__)
125         mach_port_t           mach_thread;       /* Darwin thread id              */
126 #endif
127
128         /* these are used for the wait/notify implementation                      */
129         pthread_mutex_t       waitmutex;
130         pthread_cond_t        waitcond;
131
132         pthread_mutex_t       suspendmutex; /* lock before suspending this thread */
133         pthread_cond_t        suspendcond;  /* notify to resume this thread       */
134
135         bool                  interrupted;
136         bool                  signaled;
137         bool                  sleeping;
138
139         bool                  suspended;    /* is this thread suspended?          */
140         s4                    suspend_reason; /* reason for suspending            */
141
142         u1                   *pc;           /* current PC (used for profiling)    */
143
144         java_object_t        *_exceptionptr;     /* current exception             */
145         stackframeinfo       *_stackframeinfo;   /* current native stackframeinfo */
146         localref_table       *_localref_table;   /* JNI local references          */
147
148 #if defined(ENABLE_INTRP)
149         Cell                 *_global_sp;        /* stack pointer for interpreter */
150 #endif
151
152 #if defined(ENABLE_GC_CACAO)
153         bool                  gc_critical;  /* indicates a critical section       */
154
155         sourcestate_t        *ss;
156         executionstate_t     *es;
157 #endif
158
159         dumpinfo_t            dumpinfo;     /* dump memory info structure         */
160
161 #if defined(ENABLE_DEBUG_FILTER)
162         u2                    filterverbosecallctr[2]; /* counters for verbose call filter */
163 #endif
164
165 #if !defined(NDEBUG)
166         s4                    tracejavacallindent;
167         u4                    tracejavacallcount;
168 #endif
169
170         listnode_t            linkage;      /* threads-list                       */
171 };
172
173
174 /* exception pointer **********************************************************/
175
176 #define exceptionptr      (&(THREADOBJECT->_exceptionptr))
177
178
179 /* stackframeinfo *************************************************************/
180
181 #define STACKFRAMEINFO    (THREADOBJECT->_stackframeinfo)
182
183 /* counter for verbose call filter ********************************************/
184
185 #if defined(ENABLE_DEBUG_FILTER)
186 #       define FILTERVERBOSECALLCTR (THREADOBJECT->filterverbosecallctr)
187 #endif
188
189 /* state for trace java call **************************************************/
190
191 #if !defined(NDEBUG)
192 #       define TRACEJAVACALLINDENT (THREADOBJECT->tracejavacallindent)
193 #       define TRACEJAVACALLCOUNT (THREADOBJECT->tracejavacallcount)
194 #endif
195
196 /* functions ******************************************************************/
197
198 void threads_sem_init(sem_t *sem, bool shared, int value);
199 void threads_sem_wait(sem_t *sem);
200 void threads_sem_post(sem_t *sem);
201
202 threadobject *threads_get_current_threadobject(void);
203
204 bool threads_init(void);
205
206 void threads_start_thread(threadobject *thread, functionptr function);
207
208 void threads_set_thread_priority(pthread_t tid, int priority);
209
210 bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
211 bool threads_detach_thread(threadobject *thread);
212
213 bool threads_suspend_thread(threadobject *thread, s4 reason);
214 void threads_suspend_ack(u1* pc, u1* sp);
215 bool threads_resume_thread(threadobject *thread);
216
217 void threads_join_all_threads(void);
218
219 void threads_sleep(s8 millis, s4 nanos);
220
221 bool threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
222
223 void threads_thread_interrupt(threadobject *thread);
224 bool threads_check_if_interrupted_and_reset(void);
225 bool threads_thread_has_been_interrupted(threadobject *thread);
226
227 #if !defined(DISABLE_GC)
228 void threads_stopworld(void);
229 void threads_startworld(void);
230 #endif
231
232 #endif /* _THREADS_H */
233
234
235 /*
236  * These are local overrides for various environment variables in Emacs.
237  * Please do not remove this and leave it at the end of the file, where
238  * Emacs will automagically detect them.
239  * ---------------------------------------------------------------------
240  * Local variables:
241  * mode: c
242  * indent-tabs-mode: t
243  * c-basic-offset: 4
244  * tab-width: 4
245  * End:
246  * vim:noexpandtab:sw=4:ts=4:
247  */