1 /* src/threads/native/threads.h - native threads header
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
8 This file is part of CACAO.
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.
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.
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
25 Contact: cacao@cacaojvm.org
30 Changes: Christian Thalinger
32 $Id: threads.h 5806 2006-10-19 10:10:23Z twisti $
40 /* forward typedefs ***********************************************************/
42 typedef struct threadobject threadobject;
43 typedef union threads_table_entry_t threads_table_entry_t;
44 typedef struct threads_table_t threads_table_t;
54 #include "mm/memory.h"
55 #include "native/jni.h"
56 #include "native/include/java_lang_Object.h" /* required by java/lang/VMThread*/
57 #include "native/include/java_lang_Thread.h"
58 #include "native/include/java_lang_VMThread.h"
59 #include "vm/global.h"
61 #include "threads/native/lock.h"
63 #if defined(__DARWIN__)
64 # include <mach/mach.h>
66 /* We need to emulate recursive mutexes. */
70 pthread_mutex_t mutex;
76 # include <semaphore.h>
80 /* thread priorities **********************************************************/
82 #define MIN_PRIORITY 1
83 #define NORM_PRIORITY 5
84 #define MAX_PRIORITY 10
87 /* current threadobject *******************************************************/
89 #if defined(HAVE___THREAD)
91 #define THREADSPECIFIC __thread
92 #define THREADOBJECT threads_current_threadobject
94 extern __thread threadobject *threads_current_threadobject;
96 #else /* defined(HAVE___THREAD) */
98 #define THREADSPECIFIC
99 #define THREADOBJECT \
100 ((threadobject *)pthread_getspecific(threads_current_threadobject_key))
102 extern pthread_key_t threads_current_threadobject_key;
104 #endif /* defined(HAVE___THREAD) */
107 /* threads_table_entry_t *******************************************************
109 An entry in the global threads table.
111 *******************************************************************************/
113 union threads_table_entry_t {
114 threadobject *thread; /* an existing thread */
115 ptrint nextfree; /* next free index */
119 /* threads_table_t *************************************************************
121 Struct for the global threads table.
123 *******************************************************************************/
125 struct threads_table_t {
126 threads_table_entry_t *table; /* the table, threads[0] is the head */
127 /* of the free list. Real entries */
128 /* start at threads[1]. */
129 s4 size; /* current size of the table */
133 /* threadobject ****************************************************************
135 Every java.lang.VMThread object is actually an instance of this
138 *******************************************************************************/
140 #define THREAD_FLAG_JAVA 0x01 /* a normal Java thread */
141 #define THREAD_FLAG_INTERNAL 0x02 /* CACAO internal thread */
144 struct threadobject {
145 java_lang_VMThread o; /* the java.lang.VMThread object */
147 lock_execution_env_t ee; /* data for the lock implementation */
149 threadobject *next; /* next thread in list, or self */
150 threadobject *prev; /* prev thread in list, or self */
152 ptrint thinlock; /* pre-computed thin lock value */
154 s4 index; /* thread index, starting with 1 */
155 u1 flags; /* flag field */
157 pthread_t tid; /* pthread id */
159 #if defined(__DARWIN__)
160 mach_port_t mach_thread; /* Darwin thread id */
163 pthread_mutex_t joinmutex;
164 pthread_cond_t joincond;
166 /* these are used for the wait/notify implementation */
167 pthread_mutex_t waitmutex;
168 pthread_cond_t waitcond;
174 u1 *pc; /* current PC (used for profiling) */
176 java_objectheader *_exceptionptr; /* current exception */
177 stackframeinfo *_stackframeinfo; /* current native stackframeinfo */
178 localref_table *_localref_table; /* JNI local references */
180 #if defined(ENABLE_INTRP)
181 u1 *_global_sp; /* stack pointer for interpreter */
184 dumpinfo_t dumpinfo; /* dump memory info structure */
188 /* variables ******************************************************************/
190 extern threadobject *mainthreadobj;
193 /* functions ******************************************************************/
195 void threads_sem_init(sem_t *sem, bool shared, int value);
196 void threads_sem_wait(sem_t *sem);
197 void threads_sem_post(sem_t *sem);
199 threadobject *threads_get_current_threadobject(void);
201 void threads_preinit(void);
202 bool threads_init(void);
204 void threads_init_threadobject(java_lang_VMThread *);
206 void threads_start_thread(java_lang_Thread *t, functionptr function);
208 bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
209 bool threads_detach_thread(threadobject *thread);
211 void threads_join_all_threads(void);
213 void threads_sleep(s8 millis, s4 nanos);
214 void threads_yield(void);
216 bool threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
218 void threads_thread_interrupt(java_lang_VMThread *);
219 bool threads_check_if_interrupted_and_reset(void);
220 bool threads_thread_has_been_interrupted(java_lang_VMThread *);
222 void threads_java_lang_Thread_set_priority(java_lang_Thread *t, s4 priority);
224 void threads_cast_stopworld(void);
225 void threads_cast_startworld(void);
227 void threads_dump(void);
229 /******************************************************************************/
230 /* Recursive Mutex Implementation for Darwin */
231 /******************************************************************************/
233 #if defined(MUTEXSIM)
235 /* We need this for older MacOSX (10.1.x) */
238 pthread_mutex_t mutex;
241 } pthread_mutex_rec_t;
243 void pthread_mutex_init_rec(pthread_mutex_rec_t *m);
244 void pthread_mutex_destroy_rec(pthread_mutex_rec_t *m);
245 void pthread_mutex_lock_rec(pthread_mutex_rec_t *m);
246 void pthread_mutex_unlock_rec(pthread_mutex_rec_t *m);
248 #else /* !defined(MUTEXSIM) */
250 #define pthread_mutex_lock_rec pthread_mutex_lock
251 #define pthread_mutex_unlock_rec pthread_mutex_unlock
252 #define pthread_mutex_rec_t pthread_mutex_t
254 #endif /* defined(MUTEXSIM) */
256 #endif /* _THREADS_H */
260 * These are local overrides for various environment variables in Emacs.
261 * Please do not remove this and leave it at the end of the file, where
262 * Emacs will automagically detect them.
263 * ---------------------------------------------------------------------
266 * indent-tabs-mode: t
270 * vim:noexpandtab:sw=4:ts=4: