7a7f592d6e0e82771f4618ddabd5ac0c7076c0a8
[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 4908 2006-05-12 16:49:50Z 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 #if defined(HAVE___THREAD)
69
70 #define THREADSPECIFIC    __thread
71 #define THREADOBJECT      threadobj
72 #define THREADINFO        (&threadobj->info)
73
74 extern __thread threadobject *threadobj;
75
76 #else /* defined(HAVE___THREAD) */
77
78 #define THREADSPECIFIC
79 #define THREADOBJECT      pthread_getspecific(tkey_threadinfo)
80 #define THREADINFO        (&((threadobject*) pthread_getspecific(tkey_threadinfo))->info)
81
82 extern pthread_key_t tkey_threadinfo;
83
84 #endif /* defined(HAVE___THREAD) */
85
86
87 /* typedefs *******************************************************************/
88
89 typedef struct nativethread nativethread;
90 typedef struct threadobject threadobject;
91 typedef java_lang_Thread thread;
92
93 struct nativethread {
94         threadobject      *next;
95         threadobject      *prev;
96         java_objectheader *_exceptionptr;
97         stackframeinfo    *_stackframeinfo;
98         localref_table    *_localref_table; /* JNI local references               */
99 #if defined(ENABLE_INTRP)
100         void              *_global_sp;
101 #endif
102         pthread_t          tid;
103 #if defined(__DARWIN__)
104         mach_port_t        mach_thread;
105 #endif
106         pthread_mutex_t    joinMutex;
107         pthread_cond_t     joinCond;
108 };
109
110
111 /* threadobject ****************************************************************
112
113    Every java.lang.VMThread object is actually an instance of this
114    structure.
115
116 *******************************************************************************/
117
118 struct threadobject {
119         java_lang_VMThread  o;
120         nativethread        info;           /* some general pthreads stuff        */
121         lock_execution_env_t     ee;             /* contains our lock record pool      */
122
123         /* these are used for the wait/notify implementation                      */
124         pthread_mutex_t     waitLock;
125         pthread_cond_t      waitCond;
126         bool                interrupted;
127         bool                signaled;
128         bool                isSleeping;
129
130         dumpinfo            dumpinfo;       /* dump memory info structure         */
131 };
132
133 void threads_sem_init(sem_t *sem, bool shared, int value);
134 void threads_sem_wait(sem_t *sem);
135 void threads_sem_post(sem_t *sem);
136
137 void *thread_getself(void);
138
139 void threads_preinit(void);
140 bool threads_init(u1 *stackbottom);
141
142 void lock_init();
143 void initThread(java_lang_VMThread *);
144
145 /* start a thread */
146 void threads_start_thread(thread *t, functionptr function);
147
148 void joinAllThreads();
149
150 void thread_sleep(s8 millis, s4 nanos);
151 void yieldThread();
152
153 bool threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
154
155 void setPriorityThread(thread *t, s4 priority);
156
157 void interruptThread(java_lang_VMThread *);
158 bool interruptedThread();
159 bool isInterruptedThread(java_lang_VMThread *);
160
161 #if defined(ENABLE_JVMTI)
162 void setthreadobject(threadobject *thread);
163 #endif
164
165 extern threadobject *mainthreadobj;
166
167 void cast_stopworld();
168 void cast_startworld();
169
170 /* dumps all threads */
171 void threads_dump(void);
172
173
174 /******************************************************************************/
175 /* Recursive Mutex Implementation for Darwin                                  */
176 /******************************************************************************/
177
178 #if defined(MUTEXSIM)
179
180 /* We need this for older MacOSX (10.1.x) */
181
182 typedef struct {
183         pthread_mutex_t mutex;
184         pthread_t owner;
185         int count;
186 } pthread_mutex_rec_t;
187
188 void pthread_mutex_init_rec(pthread_mutex_rec_t *m);
189 void pthread_mutex_destroy_rec(pthread_mutex_rec_t *m);
190 void pthread_mutex_lock_rec(pthread_mutex_rec_t *m);
191 void pthread_mutex_unlock_rec(pthread_mutex_rec_t *m);
192
193 #else /* !defined(MUTEXSIM) */
194
195 #define pthread_mutex_lock_rec pthread_mutex_lock
196 #define pthread_mutex_unlock_rec pthread_mutex_unlock
197 #define pthread_mutex_rec_t pthread_mutex_t
198
199 #endif /* defined(MUTEXSIM) */
200
201 #endif /* _THREADS_H */
202
203
204 /*
205  * These are local overrides for various environment variables in Emacs.
206  * Please do not remove this and leave it at the end of the file, where
207  * Emacs will automagically detect them.
208  * ---------------------------------------------------------------------
209  * Local variables:
210  * mode: c
211  * indent-tabs-mode: t
212  * c-basic-offset: 4
213  * tab-width: 4
214  * End:
215  * vim:noexpandtab:sw=4:ts=4:
216  */