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