0b44b7452ef3f12e8ebb3b2bec7816eccc810f4e
[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 _THREADS_COMMON_H
27 #define _THREADS_COMMON_H
28
29 #include "config.h"
30
31 #if defined(ENABLE_THREADS)
32 # include "threads/posix/thread-posix.h"
33 #else
34 # include "threads/none/threads.h"
35 #endif
36
37 #include "vm/types.h"
38
39 #include "vm/global.h"
40
41 #include "native/jni.h"
42 #include "native/llni.h"
43
44 #include "vmcore/utf8.h"
45
46
47 /* only define the following stuff with thread enabled ************************/
48
49 #if defined(ENABLE_THREADS)
50
51 /* thread states **************************************************************/
52
53 #define THREAD_STATE_NEW              0
54 #define THREAD_STATE_RUNNABLE         1
55 #define THREAD_STATE_BLOCKED          2
56 #define THREAD_STATE_WAITING          3
57 #define THREAD_STATE_TIMED_WAITING    4
58 #define THREAD_STATE_TERMINATED       5
59
60
61 /* thread priorities **********************************************************/
62
63 #define MIN_PRIORITY     1
64 #define NORM_PRIORITY    5
65 #define MAX_PRIORITY     10
66
67
68 /* debug **********************************************************************/
69
70 #if !defined(NDEBUG)
71 # define DEBUGTHREADS(message, thread) \
72         do { \
73                 if (opt_DebugThreads) { \
74                         printf("[Thread %-16s: ", message); \
75                         threads_thread_print_info(thread); \
76                         printf("]\n"); \
77                 } \
78         } while (0)
79 #else
80 # define DEBUGTHREADS(message, thread)
81 #endif
82
83
84 /* global variables ***********************************************************/
85
86 #if defined(__LINUX__)
87 /* XXX Remove for exact-GC. */
88 extern bool threads_pthreads_implementation_nptl;
89 #endif
90
91
92 /* inline functions ***********************************************************/
93
94 /* threads_thread_get_object ***************************************************
95
96    Return the java.lang.Thread object for the given thread.
97
98 *******************************************************************************/
99
100 static inline java_handle_t *threads_thread_get_object(threadobject *t)
101 {
102         return LLNI_WRAP(t->object);
103 }
104
105
106 /* threads_thread_set_object ***************************************************
107
108    Set the java.lang.Thread object for the given thread.
109
110 *******************************************************************************/
111
112 static inline void threads_thread_set_object(threadobject *t, java_handle_t *object)
113 {
114         t->object = LLNI_DIRECT(object);
115 }
116
117
118 /* threads_get_current_object **************************************************
119
120    Return the Java object of the current thread.
121    
122    RETURN VALUE:
123        the Java object
124
125 *******************************************************************************/
126
127 inline static java_handle_t *threads_get_current_object(void)
128 {
129         threadobject  *t;
130         java_handle_t *o;
131
132         t = THREADOBJECT;
133         o = threads_thread_get_object(t);
134
135         return o;
136 }
137
138
139 /* thread_is_attached **********************************************************
140
141    Returns if the given thread is attached to the VM.
142
143    RETURN:
144        true .... the thread is attached to the VM
145        false ... the thread is not
146
147 *******************************************************************************/
148
149 inline static bool thread_is_attached(threadobject *t)
150 {
151         java_handle_t *o;
152
153         o = threads_thread_get_object(t);
154
155         if (o != NULL)
156                 return true;
157         else
158                 return false;
159 }
160
161
162 /* thread_is_daemon ************************************************************
163
164    Returns if the given thread is a daemon thread.
165
166    RETURN:
167        true .... the thread is a daemon thread
168        false ... the thread is not
169
170 *******************************************************************************/
171
172 inline static bool thread_is_daemon(threadobject *t)
173 {
174         if (t->flags & THREAD_FLAG_DAEMON)
175                 return true;
176         else
177                 return false;
178 }
179
180
181 /* thread_current_is_attached **************************************************
182
183    Returns if the current thread is attached to the VM.
184
185    RETURN:
186        true .... the thread is attached to the VM
187        false ... the thread is not
188
189 *******************************************************************************/
190
191 inline static bool thread_current_is_attached(void)
192 {
193         threadobject  *t;
194         bool           result;
195
196         t      = THREADOBJECT;
197         result = thread_is_attached(t);
198
199         return result;
200 }
201
202
203 /* function prototypes ********************************************************/
204
205 void          threads_preinit(void);
206 void          threads_init(void);
207
208 void          thread_free(threadobject *t);
209
210 bool          threads_thread_start_internal(utf *name, functionptr f);
211 void          threads_thread_start(java_handle_t *object);
212
213 bool          threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
214
215 void          threads_thread_print_info(threadobject *t);
216
217 intptr_t      threads_get_current_tid(void);
218
219 void          threads_thread_state_runnable(threadobject *t);
220 void          threads_thread_state_waiting(threadobject *t);
221 void          threads_thread_state_timed_waiting(threadobject *t);
222 void          threads_thread_state_terminated(threadobject *t);
223
224 utf          *threads_thread_get_state(threadobject *t);
225 threadobject *thread_get_thread(java_handle_t *h);
226
227 bool          threads_thread_is_alive(threadobject *t);
228
229 void          threads_dump(void);
230 void          threads_thread_print_stacktrace(threadobject *thread);
231 void          threads_print_stacktrace(void);
232
233
234 /* implementation specific functions */
235
236 void          threads_impl_preinit(void);
237 void          threads_impl_init(void);
238
239 #if defined(ENABLE_GC_CACAO)
240 void          threads_mutex_gc_lock(void);
241 void          threads_mutex_gc_unlock(void);
242 #endif
243
244 void          threads_mutex_join_lock(void);
245 void          threads_mutex_join_unlock(void);
246
247 void          threads_impl_thread_init(threadobject *t);
248 void          threads_impl_thread_clear(threadobject *t);
249 void          threads_impl_thread_reuse(threadobject *t);
250 void          threads_impl_thread_free(threadobject *t);
251 void          threads_impl_thread_start(threadobject *thread, functionptr f);
252
253 void          threads_yield(void);
254
255 #endif /* ENABLE_THREADS */
256
257 #endif /* _THREADS_COMMON_H */
258
259
260 /*
261  * These are local overrides for various environment variables in Emacs.
262  * Please do not remove this and leave it at the end of the file, where
263  * Emacs will automagically detect them.
264  * ---------------------------------------------------------------------
265  * Local variables:
266  * mode: c
267  * indent-tabs-mode: t
268  * c-basic-offset: 4
269  * tab-width: 4
270  * End:
271  * vim:noexpandtab:sw=4:ts=4:
272  */