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