023fc7050f156ef79f8dcd6b7c22be80f58b8b28
[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 6228 2006-12-26 19:56:58Z 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
141
142 struct threadobject {
143         java_lang_Thread      o;            /* the java.lang.Thread object        */
144
145         lock_execution_env_t  ee;           /* data for the lock implementation   */
146
147         threadobject         *next;         /* next thread in list, or self       */
148         threadobject         *prev;         /* prev thread in list, or self       */
149
150         ptrint                thinlock;     /* pre-computed thin lock value       */
151
152         s4                    index;        /* thread index, starting with 1      */
153         u1                    flags;        /* flag field                         */
154
155         pthread_t             tid;          /* pthread id                         */
156
157 #if defined(__DARWIN__)
158         mach_port_t           mach_thread;       /* Darwin thread id              */
159 #endif
160
161         pthread_mutex_t       joinmutex;
162         pthread_cond_t        joincond;
163
164         /* these are used for the wait/notify implementation                      */
165         pthread_mutex_t       waitmutex;
166         pthread_cond_t        waitcond;
167
168         bool                  interrupted;
169         bool                  signaled;
170         bool                  sleeping;
171
172         u1                   *pc;           /* current PC (used for profiling)    */
173
174         java_objectheader    *_exceptionptr;     /* current exception             */
175         stackframeinfo       *_stackframeinfo;   /* current native stackframeinfo */
176         localref_table       *_localref_table;   /* JNI local references          */
177
178 #if defined(ENABLE_INTRP)
179         Cell                 *_global_sp;        /* stack pointer for interpreter */
180 #endif
181
182         dumpinfo_t            dumpinfo;     /* dump memory info structure         */
183 };
184
185
186 /* variables ******************************************************************/
187
188 extern threadobject *mainthreadobj;
189
190
191 /* functions ******************************************************************/
192
193 void threads_sem_init(sem_t *sem, bool shared, int value);
194 void threads_sem_wait(sem_t *sem);
195 void threads_sem_post(sem_t *sem);
196
197 threadobject *threads_get_current_threadobject(void);
198
199 void threads_preinit(void);
200 bool threads_init(void);
201
202 void threads_start_thread(threadobject *thread, functionptr function);
203
204 void threads_set_thread_priority(pthread_t tid, int priority);
205
206 bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
207 bool threads_detach_thread(threadobject *thread);
208
209 void threads_join_all_threads(void);
210
211 void threads_sleep(s8 millis, s4 nanos);
212 void threads_yield(void);
213
214 bool threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
215
216 void threads_thread_interrupt(threadobject *thread);
217 bool threads_check_if_interrupted_and_reset(void);
218 bool threads_thread_has_been_interrupted(threadobject *thread);
219
220 void threads_cast_stopworld(void);
221 void threads_cast_startworld(void);
222
223 void threads_dump(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  */