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 5869 2006-10-30 11:52:09Z edwin $
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(ENABLE_INTRP)
64 #include "vm/jit/intrp/intrp.h"
67 #if defined(__DARWIN__)
68 # include <mach/mach.h>
70 /* We need to emulate recursive mutexes. */
74 pthread_mutex_t mutex;
80 # include <semaphore.h>
84 /* thread priorities **********************************************************/
86 #define MIN_PRIORITY 1
87 #define NORM_PRIORITY 5
88 #define MAX_PRIORITY 10
91 /* current threadobject *******************************************************/
93 #if defined(HAVE___THREAD)
95 #define THREADSPECIFIC __thread
96 #define THREADOBJECT threads_current_threadobject
98 extern __thread threadobject *threads_current_threadobject;
100 #else /* defined(HAVE___THREAD) */
102 #define THREADSPECIFIC
103 #define THREADOBJECT \
104 ((threadobject *)pthread_getspecific(threads_current_threadobject_key))
106 extern pthread_key_t threads_current_threadobject_key;
108 #endif /* defined(HAVE___THREAD) */
111 /* threads_table_entry_t *******************************************************
113 An entry in the global threads table.
115 *******************************************************************************/
117 union threads_table_entry_t {
118 threadobject *thread; /* an existing thread */
119 ptrint nextfree; /* next free index */
123 /* threads_table_t *************************************************************
125 Struct for the global threads table.
127 *******************************************************************************/
129 struct threads_table_t {
130 threads_table_entry_t *table; /* the table, threads[0] is the head */
131 /* of the free list. Real entries */
132 /* start at threads[1]. */
133 s4 size; /* current size of the table */
137 /* threadobject ****************************************************************
139 Every java.lang.VMThread object is actually an instance of this
142 *******************************************************************************/
144 #define THREAD_FLAG_JAVA 0x01 /* a normal Java thread */
145 #define THREAD_FLAG_INTERNAL 0x02 /* CACAO internal thread */
148 struct threadobject {
149 java_lang_VMThread o; /* the java.lang.VMThread object */
151 lock_execution_env_t ee; /* data for the lock implementation */
153 threadobject *next; /* next thread in list, or self */
154 threadobject *prev; /* prev thread in list, or self */
156 ptrint thinlock; /* pre-computed thin lock value */
158 s4 index; /* thread index, starting with 1 */
159 u1 flags; /* flag field */
161 pthread_t tid; /* pthread id */
163 #if defined(__DARWIN__)
164 mach_port_t mach_thread; /* Darwin thread id */
167 pthread_mutex_t joinmutex;
168 pthread_cond_t joincond;
170 /* these are used for the wait/notify implementation */
171 pthread_mutex_t waitmutex;
172 pthread_cond_t waitcond;
178 u1 *pc; /* current PC (used for profiling) */
180 java_objectheader *_exceptionptr; /* current exception */
181 stackframeinfo *_stackframeinfo; /* current native stackframeinfo */
182 localref_table *_localref_table; /* JNI local references */
184 #if defined(ENABLE_INTRP)
185 Cell *_global_sp; /* stack pointer for interpreter */
188 dumpinfo_t dumpinfo; /* dump memory info structure */
192 /* variables ******************************************************************/
194 extern threadobject *mainthreadobj;
197 /* functions ******************************************************************/
199 void threads_sem_init(sem_t *sem, bool shared, int value);
200 void threads_sem_wait(sem_t *sem);
201 void threads_sem_post(sem_t *sem);
203 threadobject *threads_get_current_threadobject(void);
205 void threads_preinit(void);
206 bool threads_init(void);
208 void threads_init_threadobject(java_lang_VMThread *);
210 void threads_start_thread(java_lang_Thread *t, functionptr function);
212 bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
213 bool threads_detach_thread(threadobject *thread);
215 void threads_join_all_threads(void);
217 void threads_sleep(s8 millis, s4 nanos);
218 void threads_yield(void);
220 bool threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
222 void threads_thread_interrupt(java_lang_VMThread *);
223 bool threads_check_if_interrupted_and_reset(void);
224 bool threads_thread_has_been_interrupted(java_lang_VMThread *);
226 void threads_java_lang_Thread_set_priority(java_lang_Thread *t, s4 priority);
228 void threads_cast_stopworld(void);
229 void threads_cast_startworld(void);
231 void threads_dump(void);
233 /******************************************************************************/
234 /* Recursive Mutex Implementation for Darwin */
235 /******************************************************************************/
237 #if defined(MUTEXSIM)
239 /* We need this for older MacOSX (10.1.x) */
242 pthread_mutex_t mutex;
245 } pthread_mutex_rec_t;
247 void pthread_mutex_init_rec(pthread_mutex_rec_t *m);
248 void pthread_mutex_destroy_rec(pthread_mutex_rec_t *m);
249 void pthread_mutex_lock_rec(pthread_mutex_rec_t *m);
250 void pthread_mutex_unlock_rec(pthread_mutex_rec_t *m);
252 #else /* !defined(MUTEXSIM) */
254 #define pthread_mutex_lock_rec pthread_mutex_lock
255 #define pthread_mutex_unlock_rec pthread_mutex_unlock
256 #define pthread_mutex_rec_t pthread_mutex_t
258 #endif /* defined(MUTEXSIM) */
260 #endif /* _THREADS_H */
264 * These are local overrides for various environment variables in Emacs.
265 * Please do not remove this and leave it at the end of the file, where
266 * Emacs will automagically detect them.
267 * ---------------------------------------------------------------------
270 * indent-tabs-mode: t
274 * vim:noexpandtab:sw=4:ts=4: