* src/threads/native/threads.c, src/threads/native/threads.c: Further
[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
29    Changes: Christian Thalinger
30                         Edwin Steiner
31
32    $Id: threads.h 4909 2006-05-13 23:10:21Z edwin $
33
34 */
35
36
37 #ifndef _THREADS_H
38 #define _THREADS_H
39
40 #include "config.h"
41
42 #include <pthread.h>
43 #include <semaphore.h>
44 #include <ucontext.h>
45
46 #include "vm/types.h"
47
48 #include "config.h"
49 #include "vm/types.h"
50
51 #include "mm/memory.h"
52 #include "native/jni.h"
53 #include "native/include/java_lang_Object.h" /* required by java/lang/VMThread*/
54 #include "native/include/java_lang_Thread.h"
55 #include "native/include/java_lang_VMThread.h"
56 #include "vm/global.h"
57
58 #include "threads/native/lock.h"
59
60 #if defined(__DARWIN__)
61 #include <mach/mach.h>
62
63 /* We need to emulate recursive mutexes. */
64 #define MUTEXSIM
65 #endif
66
67
68 /* forward typedefs ***********************************************************/
69
70 typedef struct nativethread nativethread;
71 typedef struct threadobject threadobject;
72
73
74 /* current threadobject *******************************************************/
75
76 #if defined(HAVE___THREAD)
77
78 #define THREADSPECIFIC    __thread
79 #define THREADOBJECT      threads_current_threadobject
80 #define THREADINFO        (&threads_current_threadobject->info)
81
82 extern __thread threadobject *threads_current_threadobject;
83
84 #else /* defined(HAVE___THREAD) */
85
86 #define THREADSPECIFIC
87 #define THREADOBJECT      pthread_getspecific(threads_current_threadobject_key)
88 #define THREADINFO        (&((threadobject*) pthread_getspecific(threads_current_threadobject_key))->info)
89
90 extern pthread_key_t threads_current_threadobject_key;
91
92 #endif /* defined(HAVE___THREAD) */
93
94
95 /* nativethread ****************************************************************
96
97    XXX
98
99 *******************************************************************************/
100
101 struct nativethread {
102         threadobject      *next;
103         threadobject      *prev;
104         java_objectheader *_exceptionptr;
105         stackframeinfo    *_stackframeinfo;
106         localref_table    *_localref_table; /* JNI local references               */
107 #if defined(ENABLE_INTRP)
108         void              *_global_sp;
109 #endif
110         pthread_t          tid;
111 #if defined(__DARWIN__)
112         mach_port_t        mach_thread;
113 #endif
114         pthread_mutex_t    joinMutex;
115         pthread_cond_t     joinCond;
116 };
117
118
119 /* threadobject ****************************************************************
120
121    Every java.lang.VMThread object is actually an instance of this
122    structure.
123
124 *******************************************************************************/
125
126 struct threadobject {
127         java_lang_VMThread    o;
128         nativethread          info;         /* some general pthreads stuff        */
129         lock_execution_env_t  ee;           /* contains our lock record pool      */
130
131         /* these are used for the wait/notify implementation                      */
132         pthread_mutex_t       waitLock;
133         pthread_cond_t        waitCond;
134         bool                  interrupted;
135         bool                  signaled;
136         bool                  isSleeping;
137
138         dumpinfo              dumpinfo;     /* dump memory info structure         */
139 };
140
141
142 /* variables ******************************************************************/
143
144 extern threadobject *mainthreadobj;
145
146
147 /* functions ******************************************************************/
148
149 void threads_sem_init(sem_t *sem, bool shared, int value);
150 void threads_sem_wait(sem_t *sem);
151 void threads_sem_post(sem_t *sem);
152
153 threadobject *threads_get_current_threadobject(void);
154
155 void threads_preinit(void);
156 bool threads_init(u1 *stackbottom);
157
158 void threads_init_threadobject(java_lang_VMThread *);
159
160 void threads_start_thread(java_lang_Thread *t, functionptr function);
161
162 void threads_join_all_threads(void);
163
164 void threads_sleep(s8 millis, s4 nanos);
165 void threads_yield(void);
166
167 bool threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
168
169 void threads_interrupt_thread(java_lang_VMThread *);
170 bool threads_check_if_interrupted_and_reset(void);
171 bool threads_thread_has_been_interrupted(java_lang_VMThread *);
172
173 #if defined(ENABLE_JVMTI)
174 void threads_set_current_threadobject(threadobject *thread);
175 #endif
176
177 void threads_java_lang_Thread_set_priority(java_lang_Thread *t, s4 priority);
178
179 void threads_cast_stopworld(void);
180 void threads_cast_startworld(void);
181
182 void threads_dump(void);
183
184
185 /******************************************************************************/
186 /* Recursive Mutex Implementation for Darwin                                  */
187 /******************************************************************************/
188
189 #if defined(MUTEXSIM)
190
191 /* We need this for older MacOSX (10.1.x) */
192
193 typedef struct {
194         pthread_mutex_t mutex;
195         pthread_t owner;
196         int count;
197 } pthread_mutex_rec_t;
198
199 void pthread_mutex_init_rec(pthread_mutex_rec_t *m);
200 void pthread_mutex_destroy_rec(pthread_mutex_rec_t *m);
201 void pthread_mutex_lock_rec(pthread_mutex_rec_t *m);
202 void pthread_mutex_unlock_rec(pthread_mutex_rec_t *m);
203
204 #else /* !defined(MUTEXSIM) */
205
206 #define pthread_mutex_lock_rec pthread_mutex_lock
207 #define pthread_mutex_unlock_rec pthread_mutex_unlock
208 #define pthread_mutex_rec_t pthread_mutex_t
209
210 #endif /* defined(MUTEXSIM) */
211
212 #endif /* _THREADS_H */
213
214
215 /*
216  * These are local overrides for various environment variables in Emacs.
217  * Please do not remove this and leave it at the end of the file, where
218  * Emacs will automagically detect them.
219  * ---------------------------------------------------------------------
220  * Local variables:
221  * mode: c
222  * indent-tabs-mode: t
223  * c-basic-offset: 4
224  * tab-width: 4
225  * End:
226  * vim:noexpandtab:sw=4:ts=4:
227  */