* src/threads/native/threads.h (native/include/java_lang_Thread.h): Removed.
[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
47 #include "threads/native/lock.h"
48
49 #include "vm/global.h"
50
51 #if defined(ENABLE_GC_CACAO)
52 # include "vm/jit/replace.h"
53 #endif
54
55 #include "vm/jit/stacktrace.h"
56
57 #if defined(ENABLE_INTRP)
58 #include "vm/jit/intrp/intrp.h"
59 #endif
60
61 #if defined(__DARWIN__)
62 # include <mach/mach.h>
63
64 typedef struct {
65         pthread_mutex_t mutex;
66         pthread_cond_t cond;
67         int value;
68 } sem_t;
69
70 #else
71 # include <semaphore.h>
72 #endif
73
74
75 /* current threadobject *******************************************************/
76
77 #if defined(HAVE___THREAD)
78
79 #define THREADSPECIFIC    __thread
80 #define THREADOBJECT      threads_current_threadobject
81
82 extern __thread threadobject *threads_current_threadobject;
83
84 #else /* defined(HAVE___THREAD) */
85
86 #define THREADSPECIFIC
87 #define THREADOBJECT \
88         ((threadobject *) pthread_getspecific(threads_current_threadobject_key))
89
90 extern pthread_key_t threads_current_threadobject_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         /* these are used for the wait/notify implementation                      */
126         pthread_mutex_t       waitmutex;
127         pthread_cond_t        waitcond;
128
129         pthread_mutex_t       suspendmutex; /* lock before suspending this thread */
130         pthread_cond_t        suspendcond;  /* notify to resume this thread       */
131
132         bool                  interrupted;
133         bool                  signaled;
134         bool                  sleeping;
135
136         bool                  suspended;    /* is this thread suspended?          */
137         s4                    suspend_reason; /* reason for suspending            */
138
139         u1                   *pc;           /* current PC (used for profiling)    */
140
141         java_object_t        *_exceptionptr;     /* current exception             */
142         stackframeinfo       *_stackframeinfo;   /* current native stackframeinfo */
143         localref_table       *_localref_table;   /* JNI local references          */
144
145 #if defined(ENABLE_INTRP)
146         Cell                 *_global_sp;        /* stack pointer for interpreter */
147 #endif
148
149 #if defined(ENABLE_GC_CACAO)
150         bool                  gc_critical;  /* indicates a critical section       */
151
152         sourcestate_t        *ss;
153         executionstate_t     *es;
154 #endif
155
156         dumpinfo_t            dumpinfo;     /* dump memory info structure         */
157
158 #if defined(ENABLE_DEBUG_FILTER)
159         u2                    filterverbosecallctr[2]; /* counters for verbose call filter */
160 #endif
161
162 #if !defined(NDEBUG)
163         s4                    tracejavacallindent;
164         u4                    tracejavacallcount;
165 #endif
166
167         listnode_t            linkage;      /* threads-list                       */
168 };
169
170
171 /* stackframeinfo *************************************************************/
172
173 #define STACKFRAMEINFO    (THREADOBJECT->_stackframeinfo)
174
175
176 /* native-world flags *********************************************************/
177
178 #if defined(ENABLE_GC_CACAO)
179 # define THREAD_NATIVEWORLD_ENTER THREADOBJECT->flags |=  THREAD_FLAG_IN_NATIVE
180 # define THREAD_NATIVEWORLD_EXIT  THREADOBJECT->flags &= ~THREAD_FLAG_IN_NATIVE
181 #else
182 # define THREAD_NATIVEWORLD_ENTER /*nop*/
183 # define THREAD_NATIVEWORLD_EXIT  /*nop*/
184 #endif
185
186
187 /* counter for verbose call filter ********************************************/
188
189 #if defined(ENABLE_DEBUG_FILTER)
190 #       define FILTERVERBOSECALLCTR (THREADOBJECT->filterverbosecallctr)
191 #endif
192
193 /* state for trace java call **************************************************/
194
195 #if !defined(NDEBUG)
196 #       define TRACEJAVACALLINDENT (THREADOBJECT->tracejavacallindent)
197 #       define TRACEJAVACALLCOUNT (THREADOBJECT->tracejavacallcount)
198 #endif
199
200 /* functions ******************************************************************/
201
202 void threads_sem_init(sem_t *sem, bool shared, int value);
203 void threads_sem_wait(sem_t *sem);
204 void threads_sem_post(sem_t *sem);
205
206 threadobject *threads_get_current_threadobject(void);
207
208 bool threads_init(void);
209
210 void threads_start_thread(threadobject *thread, functionptr function);
211
212 void threads_set_thread_priority(pthread_t tid, int priority);
213
214 bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
215 bool threads_detach_thread(threadobject *thread);
216
217 bool threads_suspend_thread(threadobject *thread, s4 reason);
218 void threads_suspend_ack(u1* pc, u1* sp);
219 bool threads_resume_thread(threadobject *thread);
220
221 void threads_join_all_threads(void);
222
223 void threads_sleep(s8 millis, s4 nanos);
224
225 bool threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
226
227 void threads_thread_interrupt(threadobject *thread);
228 bool threads_check_if_interrupted_and_reset(void);
229 bool threads_thread_has_been_interrupted(threadobject *thread);
230
231 #if !defined(DISABLE_GC)
232 void threads_stopworld(void);
233 void threads_startworld(void);
234 #endif
235
236 #endif /* _THREADS_H */
237
238
239 /*
240  * These are local overrides for various environment variables in Emacs.
241  * Please do not remove this and leave it at the end of the file, where
242  * Emacs will automagically detect them.
243  * ---------------------------------------------------------------------
244  * Local variables:
245  * mode: c
246  * indent-tabs-mode: t
247  * c-basic-offset: 4
248  * tab-width: 4
249  * End:
250  * vim:noexpandtab:sw=4:ts=4:
251  */