* src/native/vm/java_lang_Thread.c [ENABLE_THREADS]
[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 7669 2007-04-05 11:39:58Z twisti $
26
27 */
28
29
30 #ifndef _THREADS_H
31 #define _THREADS_H
32
33 /* forward typedefs ***********************************************************/
34
35 typedef struct threadobject          threadobject;
36 typedef union  threads_table_entry_t threads_table_entry_t;
37 typedef struct threads_table_t       threads_table_t;
38
39
40 #include "config.h"
41
42 #include <pthread.h>
43 #include <ucontext.h>
44
45 #include "vm/types.h"
46
47 #include "mm/memory.h"
48 #include "native/jni.h"
49 #include "native/include/java_lang_Thread.h"
50
51 #include "threads/native/lock.h"
52
53 #include "vm/global.h"
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 /* threads_table_entry_t *******************************************************
96
97    An entry in the global threads table.
98
99 *******************************************************************************/
100
101 union threads_table_entry_t {
102         threadobject       *thread;        /* an existing thread                  */
103         ptrint              nextfree;      /* next free index                     */
104 };
105
106
107 /* threads_table_t *************************************************************
108
109    Struct for the global threads table.
110
111 *******************************************************************************/
112
113 struct threads_table_t {
114         threads_table_entry_t *table;      /* the table, threads[0] is the head   */
115                                            /* of the free list. Real entries      */
116                                                                            /* start at threads[1].                */
117         s4                     size;       /* current size of the table           */
118 };
119
120
121 /* threadobject ****************************************************************
122
123    Struct holding thread local variables.
124
125 *******************************************************************************/
126
127 #define THREAD_FLAG_JAVA        0x01    /* a normal Java thread               */
128 #define THREAD_FLAG_INTERNAL    0x02    /* CACAO internal thread              */
129 #define THREAD_FLAG_DAEMON      0x04    /* daemon thread                      */
130
131
132 struct threadobject {
133         java_lang_Thread     *object;       /* link to java.lang.Thread object    */
134
135         lock_execution_env_t  ee;           /* data for the lock implementation   */
136
137         threadobject         *next;         /* next thread in list, or self       */
138         threadobject         *prev;         /* prev thread in list, or self       */
139
140         ptrint                thinlock;     /* pre-computed thin lock value       */
141
142         s4                    index;        /* thread index, starting with 1      */
143         u4                    flags;        /* flag field                         */
144         u4                    state;        /* state field                        */
145
146         pthread_t             tid;          /* pthread id                         */
147
148 #if defined(__DARWIN__)
149         mach_port_t           mach_thread;       /* Darwin thread id              */
150 #endif
151
152         pthread_mutex_t       joinmutex;
153         pthread_cond_t        joincond;
154
155         /* these are used for the wait/notify implementation                      */
156         pthread_mutex_t       waitmutex;
157         pthread_cond_t        waitcond;
158
159         bool                  interrupted;
160         bool                  signaled;
161         bool                  sleeping;
162
163         u1                   *pc;           /* current PC (used for profiling)    */
164
165         java_objectheader    *_exceptionptr;     /* current exception             */
166         stackframeinfo       *_stackframeinfo;   /* current native stackframeinfo */
167         localref_table       *_localref_table;   /* JNI local references          */
168
169 #if defined(ENABLE_INTRP)
170         Cell                 *_global_sp;        /* stack pointer for interpreter */
171 #endif
172
173         dumpinfo_t            dumpinfo;     /* dump memory info structure         */
174 };
175
176
177 /* exception pointer **********************************************************/
178
179 #define exceptionptr      (&(THREADOBJECT->_exceptionptr))
180
181
182 /* stackframeinfo *************************************************************/
183
184 #define STACKFRAMEINFO    (THREADOBJECT->_stackframeinfo)
185
186
187 /* variables ******************************************************************/
188
189 extern threadobject *mainthreadobj;
190
191
192 /* functions ******************************************************************/
193
194 void threads_sem_init(sem_t *sem, bool shared, int value);
195 void threads_sem_wait(sem_t *sem);
196 void threads_sem_post(sem_t *sem);
197
198 threadobject *threads_get_current_threadobject(void);
199
200 void threads_preinit(void);
201 bool threads_init(void);
202
203 void threads_start_javathread(java_lang_Thread *object);
204 void threads_start_thread(threadobject *thread, functionptr function);
205
206 void threads_set_thread_priority(pthread_t tid, int priority);
207
208 bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
209 bool threads_detach_thread(threadobject *thread);
210
211 void threads_join_all_threads(void);
212
213 void threads_sleep(s8 millis, s4 nanos);
214 void threads_yield(void);
215
216 bool threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
217
218 void threads_thread_interrupt(threadobject *thread);
219 bool threads_check_if_interrupted_and_reset(void);
220 bool threads_thread_has_been_interrupted(threadobject *thread);
221
222 void threads_cast_stopworld(void);
223 void threads_cast_startworld(void);
224
225 #endif /* _THREADS_H */
226
227
228 /*
229  * These are local overrides for various environment variables in Emacs.
230  * Please do not remove this and leave it at the end of the file, where
231  * Emacs will automagically detect them.
232  * ---------------------------------------------------------------------
233  * Local variables:
234  * mode: c
235  * indent-tabs-mode: t
236  * c-basic-offset: 4
237  * tab-width: 4
238  * End:
239  * vim:noexpandtab:sw=4:ts=4:
240  */