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