* src/vm/class.c [ENABLE_JAVASE] (arrayclass_java_lang_Object): Added
[cacao.git] / src / threads / native / threads.h
1 /* src/threads/native/threads.h - native threads header
2
3    Copyright (C) 1996-2005, 2006 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    Contact: cacao@cacaojvm.org
26
27    Authors: Stefan Ring
28             Edwin Steiner
29             Christian Thalinger
30
31    $Id: threads.h 6251 2006-12-27 23:15:56Z twisti $
32
33 */
34
35
36 #ifndef _THREADS_H
37 #define _THREADS_H
38
39 /* forward typedefs ***********************************************************/
40
41 typedef struct threadobject          threadobject;
42 typedef union  threads_table_entry_t threads_table_entry_t;
43 typedef struct threads_table_t       threads_table_t;
44
45
46 #include "config.h"
47
48 #include <pthread.h>
49 #include <ucontext.h>
50
51 #include "vm/types.h"
52
53 #include "mm/memory.h"
54 #include "native/jni.h"
55 #include "native/include/java_lang_Thread.h"
56 #include "vm/global.h"
57
58 #include "threads/native/lock.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 /* thread priorities **********************************************************/
79
80 #define MIN_PRIORITY     1
81 #define NORM_PRIORITY    5
82 #define MAX_PRIORITY     10
83
84
85 /* current threadobject *******************************************************/
86
87 #if defined(HAVE___THREAD)
88
89 #define THREADSPECIFIC    __thread
90 #define THREADOBJECT      threads_current_threadobject
91
92 extern __thread threadobject *threads_current_threadobject;
93
94 #else /* defined(HAVE___THREAD) */
95
96 #define THREADSPECIFIC
97 #define THREADOBJECT \
98         ((threadobject *) pthread_getspecific(threads_current_threadobject_key))
99
100 extern pthread_key_t threads_current_threadobject_key;
101
102 #endif /* defined(HAVE___THREAD) */
103
104
105 /* threads_table_entry_t *******************************************************
106
107    An entry in the global threads table.
108
109 *******************************************************************************/
110
111 union threads_table_entry_t {
112         threadobject       *thread;        /* an existing thread                  */
113         ptrint              nextfree;      /* next free index                     */
114 };
115
116
117 /* threads_table_t *************************************************************
118
119    Struct for the global threads table.
120
121 *******************************************************************************/
122
123 struct threads_table_t {
124         threads_table_entry_t *table;      /* the table, threads[0] is the head   */
125                                            /* of the free list. Real entries      */
126                                                                            /* start at threads[1].                */
127         s4                     size;       /* current size of the table           */
128 };
129
130
131 /* threadobject ****************************************************************
132
133    Every java.lang.Thread object is actually an instance of this
134    structure.
135
136 *******************************************************************************/
137
138 #define THREAD_FLAG_JAVA        0x01    /* a normal Java thread               */
139 #define THREAD_FLAG_INTERNAL    0x02    /* CACAO internal thread              */
140 #define THREAD_FLAG_DAEMON      0x04    /* daemon thread                      */
141
142
143 struct threadobject {
144         java_lang_Thread      o;            /* the java.lang.Thread object        */
145
146         lock_execution_env_t  ee;           /* data for the lock implementation   */
147
148         threadobject         *next;         /* next thread in list, or self       */
149         threadobject         *prev;         /* prev thread in list, or self       */
150
151         ptrint                thinlock;     /* pre-computed thin lock value       */
152
153         s4                    index;        /* thread index, starting with 1      */
154         u4                    flags;        /* flag field                         */
155
156         pthread_t             tid;          /* pthread id                         */
157
158 #if defined(__DARWIN__)
159         mach_port_t           mach_thread;       /* Darwin thread id              */
160 #endif
161
162         pthread_mutex_t       joinmutex;
163         pthread_cond_t        joincond;
164
165         /* these are used for the wait/notify implementation                      */
166         pthread_mutex_t       waitmutex;
167         pthread_cond_t        waitcond;
168
169         bool                  interrupted;
170         bool                  signaled;
171         bool                  sleeping;
172
173         u1                   *pc;           /* current PC (used for profiling)    */
174
175         java_objectheader    *_exceptionptr;     /* current exception             */
176         stackframeinfo       *_stackframeinfo;   /* current native stackframeinfo */
177         localref_table       *_localref_table;   /* JNI local references          */
178
179 #if defined(ENABLE_INTRP)
180         Cell                 *_global_sp;        /* stack pointer for interpreter */
181 #endif
182
183         dumpinfo_t            dumpinfo;     /* dump memory info structure         */
184 };
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_thread(threadobject *thread, functionptr function);
204
205 void threads_set_thread_priority(pthread_t tid, int priority);
206
207 bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
208 bool threads_detach_thread(threadobject *thread);
209
210 void threads_join_all_threads(void);
211
212 void threads_sleep(s8 millis, s4 nanos);
213 void threads_yield(void);
214
215 bool threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
216
217 void threads_thread_interrupt(threadobject *thread);
218 bool threads_check_if_interrupted_and_reset(void);
219 bool threads_thread_has_been_interrupted(threadobject *thread);
220
221 void threads_cast_stopworld(void);
222 void threads_cast_startworld(void);
223
224 void threads_dump(void);
225
226 #endif /* _THREADS_H */
227
228
229 /*
230  * These are local overrides for various environment variables in Emacs.
231  * Please do not remove this and leave it at the end of the file, where
232  * Emacs will automagically detect them.
233  * ---------------------------------------------------------------------
234  * Local variables:
235  * mode: c
236  * indent-tabs-mode: t
237  * c-basic-offset: 4
238  * tab-width: 4
239  * End:
240  * vim:noexpandtab:sw=4:ts=4:
241  */