* src/threads/mutex.h: Renamed to...
[cacao.git] / src / threads / thread.h
1 /* src/threads/thread.h - machine independent thread functions
2
3    Copyright (C) 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #ifndef _THREAD_H
27 #define _THREAD_H
28
29 #include "config.h"
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 #include "vmcore/system.h"
36
37 #include "threads/mutex.hpp"
38
39 #if defined(ENABLE_THREADS)
40 # include "threads/posix/thread-posix.h"
41 #else
42 # include "threads/none/thread-none.h"
43 #endif
44
45 #include "vm/types.h"
46
47 #include "vm/global.h"
48
49 #include "native/jni.h"
50 #include "native/llni.h"
51
52 #include "vmcore/utf8.h"
53
54
55 /* only define the following stuff with thread enabled ************************/
56
57 #if defined(ENABLE_THREADS)
58
59 /* thread states **************************************************************/
60
61 #define THREAD_STATE_NEW              0
62 #define THREAD_STATE_RUNNABLE         1
63 #define THREAD_STATE_BLOCKED          2
64 #define THREAD_STATE_WAITING          3
65 #define THREAD_STATE_TIMED_WAITING    4
66 #define THREAD_STATE_TERMINATED       5
67
68
69 /* thread priorities **********************************************************/
70
71 #define MIN_PRIORITY     1
72 #define NORM_PRIORITY    5
73 #define MAX_PRIORITY     10
74
75
76 /* debug **********************************************************************/
77
78 #if !defined(NDEBUG)
79 # define DEBUGTHREADS(message, thread) \
80         do { \
81                 if (opt_DebugThreads) { \
82                         printf("[Thread %-16s: ", message); \
83                         thread_print_info(thread); \
84                         printf("]\n"); \
85                 } \
86         } while (0)
87 #else
88 # define DEBUGTHREADS(message, thread)
89 #endif
90
91
92 /* global variables ***********************************************************/
93
94 #if defined(__LINUX__)
95 /* XXX Remove for exact-GC. */
96 extern bool threads_pthreads_implementation_nptl;
97 #endif
98
99
100 /* inline functions ***********************************************************/
101
102 /* thread_get_object ***********************************************************
103
104    Return the Java for the given thread.
105
106    ARGUMENTS:
107        t ... thread
108
109    RETURN:
110        the Java object
111
112 *******************************************************************************/
113
114 inline static java_handle_t *thread_get_object(threadobject *t)
115 {
116         return LLNI_WRAP(t->object);
117 }
118
119
120 /* threads_thread_set_object ***************************************************
121
122    Set the Java object for the given thread.
123
124    ARGUMENTS:
125        t ... thread
126            o ... Java object
127
128 *******************************************************************************/
129
130 inline static void thread_set_object(threadobject *t, java_handle_t *o)
131 {
132         t->object = LLNI_DIRECT(o);
133 }
134
135
136 /* thread_get_current_object **************************************************
137
138    Return the Java object of the current thread.
139    
140    RETURN VALUE:
141        the Java object
142
143 *******************************************************************************/
144
145 inline static java_handle_t *thread_get_current_object(void)
146 {
147         threadobject  *t;
148         java_handle_t *o;
149
150         t = THREADOBJECT;
151         o = thread_get_object(t);
152
153         return o;
154 }
155
156
157 /* cacaothread_get_state *******************************************************
158
159    Returns the current state of the given thread.
160
161    ARGUMENTS:
162        t ... the thread to check
163
164    RETURN:
165        thread state
166
167 *******************************************************************************/
168
169 inline static int cacaothread_get_state(threadobject *t)
170 {
171         return t->state;
172 }
173
174
175 /* thread_is_attached **********************************************************
176
177    Returns if the given thread is attached to the VM.
178
179    ARGUMENTS:
180        t ... the thread to check
181
182    RETURN:
183        true .... the thread is attached to the VM
184        false ... the thread is not
185
186 *******************************************************************************/
187
188 inline static bool thread_is_attached(threadobject *t)
189 {
190         java_handle_t *o;
191
192         o = thread_get_object(t);
193
194         if (o != NULL)
195                 return true;
196         else
197                 return false;
198 }
199
200
201 /* thread_is_interrupted *******************************************************
202
203    Check if the given thread has been interrupted.
204
205    ARGUMENTS:
206        t ... the thread to check
207
208    RETURN VALUE:
209       true, if the given thread had been interrupted
210
211 *******************************************************************************/
212
213 inline static bool thread_is_interrupted(threadobject *t)
214 {
215         bool interrupted;
216
217         /* We need the mutex because classpath will call this function when
218            a blocking system call is interrupted. The mutex ensures that it will
219            see the correct value for the interrupted flag. */
220
221         Mutex_lock(t->waitmutex);
222         interrupted = t->interrupted;
223         Mutex_unlock(t->waitmutex);
224
225         return interrupted;
226 }
227
228
229 /* thread_set_interrupted ******************************************************
230
231    Set the interrupted flag to the given value.
232
233    ARGUMENTS:
234        interrupted ... value to set
235
236 *******************************************************************************/
237
238 inline static void thread_set_interrupted(threadobject *t, bool interrupted)
239 {
240         Mutex_lock(t->waitmutex);
241
242         /* Set interrupted flag. */
243
244         t->interrupted = interrupted;
245
246         Mutex_unlock(t->waitmutex);
247 }
248
249
250 /* thread_is_daemon ************************************************************
251
252    Returns if the given thread is a daemon thread.
253
254    ARGUMENTS:
255        t ... the thread to check
256
257    RETURN:
258        true .... the thread is a daemon thread
259        false ... the thread is not
260
261 *******************************************************************************/
262
263 inline static bool thread_is_daemon(threadobject *t)
264 {
265         if (t->flags & THREAD_FLAG_DAEMON)
266                 return true;
267         else
268                 return false;
269 }
270
271
272 /* thread_current_is_attached **************************************************
273
274    Returns if the current thread is attached to the VM.
275
276    RETURN:
277        true .... the thread is attached to the VM
278        false ... the thread is not
279
280 *******************************************************************************/
281
282 inline static bool thread_current_is_attached(void)
283 {
284         threadobject  *t;
285         bool           result;
286
287         t = thread_get_current();
288
289         if (t == NULL)
290                 return false;
291
292         result = thread_is_attached(t);
293
294         return result;
295 }
296
297
298 /* function prototypes ********************************************************/
299
300 void          threads_preinit(void);
301 void          threads_init(void);
302
303 void          thread_free(threadobject *t);
304
305 bool          threads_thread_start_internal(utf *name, functionptr f);
306 void          threads_thread_start(java_handle_t *object);
307
308 bool          thread_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
309 bool          thread_attach_current_external_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
310 bool          thread_detach_current_thread(void);
311
312 bool          thread_detach_current_external_thread(void);
313
314 void          thread_fprint_name(threadobject *t, FILE *stream);
315 void          thread_print_info(threadobject *t);
316
317 intptr_t      threads_get_current_tid(void);
318
319 void          thread_set_state_runnable(threadobject *t);
320 void          thread_set_state_waiting(threadobject *t);
321 void          thread_set_state_timed_waiting(threadobject *t);
322 void          thread_set_state_terminated(threadobject *t);
323
324 threadobject *thread_get_thread(java_handle_t *h);
325
326 bool          threads_thread_is_alive(threadobject *t);
327
328 void          threads_dump(void);
329
330
331 /* implementation specific functions */
332
333 void          threads_impl_preinit(void);
334 void          threads_impl_init(void);
335
336 #if defined(ENABLE_GC_CACAO)
337 void          threads_mutex_gc_lock(void);
338 void          threads_mutex_gc_unlock(void);
339 #endif
340
341 void          threads_mutex_join_lock(void);
342 void          threads_mutex_join_unlock(void);
343
344 void          threads_impl_thread_init(threadobject *t);
345 void          threads_impl_thread_clear(threadobject *t);
346 void          threads_impl_thread_reuse(threadobject *t);
347 void          threads_impl_thread_free(threadobject *t);
348 void          threads_impl_thread_start(threadobject *thread, functionptr f);
349
350 void          threads_yield(void);
351
352 #endif /* ENABLE_THREADS */
353
354 #ifdef __cplusplus
355 }
356 #endif
357
358 #endif /* _THREAD_H */
359
360
361 /*
362  * These are local overrides for various environment variables in Emacs.
363  * Please do not remove this and leave it at the end of the file, where
364  * Emacs will automagically detect them.
365  * ---------------------------------------------------------------------
366  * Local variables:
367  * mode: c
368  * indent-tabs-mode: t
369  * c-basic-offset: 4
370  * tab-width: 4
371  * End:
372  * vim:noexpandtab:sw=4:ts=4:
373  */