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