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 5698 2006-10-05 17:28:13Z twisti $
50 #include "mm/memory.h"
51 #include "native/jni.h"
52 #include "native/include/java_lang_Object.h" /* required by java/lang/VMThread*/
53 #include "native/include/java_lang_Thread.h"
54 #include "native/include/java_lang_VMThread.h"
55 #include "vm/global.h"
57 #include "threads/native/lock.h"
59 #if defined(__DARWIN__)
60 # include <mach/mach.h>
62 /* We need to emulate recursive mutexes. */
66 pthread_mutex_t mutex;
72 # include <semaphore.h>
76 /* forward typedefs ***********************************************************/
78 typedef struct threadobject threadobject;
79 typedef union threads_table_entry_t threads_table_entry_t;
80 typedef struct threads_table_t threads_table_t;
83 /* thread priorities **********************************************************/
85 #define MIN_PRIORITY 1
86 #define NORM_PRIORITY 5
87 #define MAX_PRIORITY 10
90 /* current threadobject *******************************************************/
92 #if defined(HAVE___THREAD)
94 #define THREADSPECIFIC __thread
95 #define THREADOBJECT threads_current_threadobject
97 extern __thread threadobject *threads_current_threadobject;
99 #else /* defined(HAVE___THREAD) */
101 #define THREADSPECIFIC
102 #define THREADOBJECT \
103 ((threadobject *)pthread_getspecific(threads_current_threadobject_key))
105 extern pthread_key_t threads_current_threadobject_key;
107 #endif /* defined(HAVE___THREAD) */
110 /* threads_table_entry_t *******************************************************
112 An entry in the global threads table.
114 *******************************************************************************/
116 union threads_table_entry_t {
117 threadobject *thread; /* an existing thread */
118 ptrint nextfree; /* next free index */
122 /* threads_table_t *************************************************************
124 Struct for the global threads table.
126 *******************************************************************************/
128 struct threads_table_t {
129 threads_table_entry_t *table; /* the table, threads[0] is the head */
130 /* of the free list. Real entries */
131 /* start at threads[1]. */
132 s4 size; /* current size of the table */
136 /* threadobject ****************************************************************
138 Every java.lang.VMThread object is actually an instance of this
141 *******************************************************************************/
143 #define THREAD_FLAG_JAVA 0x01 /* a normal Java thread */
144 #define THREAD_FLAG_INTERNAL 0x02 /* CACAO internal thread */
147 struct threadobject {
148 java_lang_VMThread o; /* the java.lang.VMThread object */
150 lock_execution_env_t ee; /* data for the lock implementation */
152 threadobject *next; /* next thread in list, or self */
153 threadobject *prev; /* prev thread in list, or self */
155 ptrint thinlock; /* pre-computed thin lock value */
157 s4 index; /* thread index, starting with 1 */
158 u1 flags; /* flag field */
160 pthread_t tid; /* pthread id */
162 #if defined(__DARWIN__)
163 mach_port_t mach_thread; /* Darwin thread id */
166 pthread_mutex_t joinmutex;
167 pthread_cond_t joincond;
169 /* these are used for the wait/notify implementation */
170 pthread_mutex_t waitmutex;
171 pthread_cond_t waitcond;
177 u1 *pc; /* current PC (used for profiling) */
179 java_objectheader *_exceptionptr; /* current exception */
180 stackframeinfo *_stackframeinfo; /* current native stackframeinfo */
181 localref_table *_localref_table; /* JNI local references */
183 #if defined(ENABLE_INTRP)
184 u1 *_global_sp; /* stack pointer for interpreter */
187 dumpinfo dumpinfo; /* dump memory info structure */
191 /* variables ******************************************************************/
193 extern threadobject *mainthreadobj;
196 /* functions ******************************************************************/
198 void threads_sem_init(sem_t *sem, bool shared, int value);
199 void threads_sem_wait(sem_t *sem);
200 void threads_sem_post(sem_t *sem);
202 threadobject *threads_get_current_threadobject(void);
204 void threads_preinit(void);
205 bool threads_init(void);
207 void threads_init_threadobject(java_lang_VMThread *);
209 void threads_start_thread(java_lang_Thread *t, functionptr function);
211 bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
212 bool threads_detach_thread(threadobject *thread);
214 void threads_join_all_threads(void);
216 void threads_sleep(s8 millis, s4 nanos);
217 void threads_yield(void);
219 bool threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
221 void threads_interrupt_thread(java_lang_VMThread *);
222 bool threads_check_if_interrupted_and_reset(void);
223 bool threads_thread_has_been_interrupted(java_lang_VMThread *);
225 void threads_java_lang_Thread_set_priority(java_lang_Thread *t, s4 priority);
227 void threads_cast_stopworld(void);
228 void threads_cast_startworld(void);
230 void threads_dump(void);
232 /******************************************************************************/
233 /* Recursive Mutex Implementation for Darwin */
234 /******************************************************************************/
236 #if defined(MUTEXSIM)
238 /* We need this for older MacOSX (10.1.x) */
241 pthread_mutex_t mutex;
244 } pthread_mutex_rec_t;
246 void pthread_mutex_init_rec(pthread_mutex_rec_t *m);
247 void pthread_mutex_destroy_rec(pthread_mutex_rec_t *m);
248 void pthread_mutex_lock_rec(pthread_mutex_rec_t *m);
249 void pthread_mutex_unlock_rec(pthread_mutex_rec_t *m);
251 #else /* !defined(MUTEXSIM) */
253 #define pthread_mutex_lock_rec pthread_mutex_lock
254 #define pthread_mutex_unlock_rec pthread_mutex_unlock
255 #define pthread_mutex_rec_t pthread_mutex_t
257 #endif /* defined(MUTEXSIM) */
259 #endif /* _THREADS_H */
263 * These are local overrides for various environment variables in Emacs.
264 * Please do not remove this and leave it at the end of the file, where
265 * Emacs will automagically detect them.
266 * ---------------------------------------------------------------------
269 * indent-tabs-mode: t
273 * vim:noexpandtab:sw=4:ts=4: