* src/vmcore/os.cpp: New file.
[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 "vmcore/os.hpp"
36
37 #include "threads/mutex.hpp"
38
39 #if defined(ENABLE_THREADS)
40 # include "threads/posix/thread-posix.hpp"
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 #ifdef __cplusplus
222         t->waitmutex->lock();
223         interrupted = t->interrupted;
224         t->waitmutex->unlock();
225 #else
226         Mutex_lock(t->waitmutex);
227         interrupted = t->interrupted;
228         Mutex_unlock(t->waitmutex);
229 #endif
230
231         return interrupted;
232 }
233
234
235 /* thread_set_interrupted ******************************************************
236
237    Set the interrupted flag to the given value.
238
239    ARGUMENTS:
240        interrupted ... value to set
241
242 *******************************************************************************/
243
244 inline static void thread_set_interrupted(threadobject *t, bool interrupted)
245 {
246 #ifdef __cplusplus
247         t->waitmutex->lock();
248         t->interrupted = interrupted;
249         t->waitmutex->unlock();
250 #else
251         Mutex_lock(t->waitmutex);
252         t->interrupted = interrupted;
253         Mutex_unlock(t->waitmutex);
254 #endif
255 }
256
257
258 /* thread_is_daemon ************************************************************
259
260    Returns if the given thread is a daemon thread.
261
262    ARGUMENTS:
263        t ... the thread to check
264
265    RETURN:
266        true .... the thread is a daemon thread
267        false ... the thread is not
268
269 *******************************************************************************/
270
271 inline static bool thread_is_daemon(threadobject *t)
272 {
273         if (t->flags & THREAD_FLAG_DAEMON)
274                 return true;
275         else
276                 return false;
277 }
278
279
280 /* thread_current_is_attached **************************************************
281
282    Returns if the current thread is attached to the VM.
283
284    RETURN:
285        true .... the thread is attached to the VM
286        false ... the thread is not
287
288 *******************************************************************************/
289
290 inline static bool thread_current_is_attached(void)
291 {
292         threadobject  *t;
293         bool           result;
294
295         t = thread_get_current();
296
297         if (t == NULL)
298                 return false;
299
300         result = thread_is_attached(t);
301
302         return result;
303 }
304
305
306 /* function prototypes ********************************************************/
307
308 void          threads_preinit(void);
309 void          threads_init(void);
310
311 void          thread_free(threadobject *t);
312
313 bool          threads_thread_start_internal(utf *name, functionptr f);
314 void          threads_thread_start(java_handle_t *object);
315
316 bool          thread_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
317 bool          thread_attach_current_external_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
318 bool          thread_detach_current_thread(void);
319
320 bool          thread_detach_current_external_thread(void);
321
322 void          thread_fprint_name(threadobject *t, FILE *stream);
323 void          thread_print_info(threadobject *t);
324
325 intptr_t      threads_get_current_tid(void);
326
327 void          thread_set_state_runnable(threadobject *t);
328 void          thread_set_state_waiting(threadobject *t);
329 void          thread_set_state_timed_waiting(threadobject *t);
330 void          thread_set_state_terminated(threadobject *t);
331
332 threadobject *thread_get_thread(java_handle_t *h);
333
334 bool          threads_thread_is_alive(threadobject *t);
335
336 void          threads_dump(void);
337
338
339 /* implementation specific functions */
340
341 void          threads_impl_preinit(void);
342 void          threads_impl_init(void);
343
344 #if defined(ENABLE_GC_CACAO)
345 void          threads_mutex_gc_lock(void);
346 void          threads_mutex_gc_unlock(void);
347 #endif
348
349 void          threads_mutex_join_lock(void);
350 void          threads_mutex_join_unlock(void);
351
352 void          threads_impl_thread_init(threadobject *t);
353 void          threads_impl_thread_clear(threadobject *t);
354 void          threads_impl_thread_reuse(threadobject *t);
355 void          threads_impl_thread_free(threadobject *t);
356 void          threads_impl_thread_start(threadobject *thread, functionptr f);
357
358 void          threads_yield(void);
359
360 #endif /* ENABLE_THREADS */
361
362 #ifdef __cplusplus
363 }
364 #endif
365
366 #endif // _THREAD_HPP
367
368
369 /*
370  * These are local overrides for various environment variables in Emacs.
371  * Please do not remove this and leave it at the end of the file, where
372  * Emacs will automagically detect them.
373  * ---------------------------------------------------------------------
374  * Local variables:
375  * mode: c++
376  * indent-tabs-mode: t
377  * c-basic-offset: 4
378  * tab-width: 4
379  * End:
380  * vim:noexpandtab:sw=4:ts=4:
381  */