Fixes PR59.
[cacao.git] / src / threads / threads-common.h
1 /* src/threads/threads-common.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/threads.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 extern methodinfo *thread_method_init;
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 /* threads_thread_get_object ***************************************************
97
98    Return the java.lang.Thread object for the given thread.
99
100 *******************************************************************************/
101
102 static inline java_handle_t *threads_thread_get_object(threadobject *t)
103 {
104         return LLNI_WRAP(t->object);
105 }
106
107
108 /* threads_thread_set_object ***************************************************
109
110    Set the java.lang.Thread object for the given thread.
111
112 *******************************************************************************/
113
114 static inline void threads_thread_set_object(threadobject *t, java_handle_t *object)
115 {
116         t->object = LLNI_DIRECT(object);
117 }
118
119
120 /* threads_get_current_object **************************************************
121
122    Return the Java object of the current thread.
123    
124    RETURN VALUE:
125        the Java object
126
127 *******************************************************************************/
128
129 inline static java_handle_t *threads_get_current_object(void)
130 {
131         threadobject  *t;
132         java_handle_t *o;
133
134         t = THREADOBJECT;
135         o = threads_thread_get_object(t);
136
137         return o;
138 }
139
140 /* thread_is_attached **********************************************************
141
142    Returns if the given thread is attached to the VM.
143
144    RETURN:
145        true .... the thread is attached to the VM
146        false ... the thread is not
147
148 *******************************************************************************/
149
150 inline static bool thread_is_attached(threadobject *t)
151 {
152         java_handle_t *o;
153
154         o = threads_thread_get_object(t);
155
156         if (o != NULL)
157                 return true;
158         else
159                 return false;
160 }
161
162
163 /* thread_current_is_attached **************************************************
164
165    Returns if the current thread is attached to the VM.
166
167    RETURN:
168        true .... the thread is attached to the VM
169        false ... the thread is not
170
171 *******************************************************************************/
172
173 inline static bool thread_current_is_attached(void)
174 {
175         threadobject  *t;
176         bool           result;
177
178         t      = THREADOBJECT;
179         result = thread_is_attached(t);
180
181         return result;
182 }
183
184
185 /* function prototypes ********************************************************/
186
187 void          threads_preinit(void);
188 void          threads_init(void);
189
190 threadobject *threads_thread_new(void);
191 void          threads_thread_free(threadobject *t);
192
193 bool          threads_thread_start_internal(utf *name, functionptr f);
194 void          threads_thread_start(java_handle_t *object);
195
196 bool          threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
197
198 void          threads_thread_print_info(threadobject *t);
199
200 intptr_t      threads_get_current_tid(void);
201
202 void          threads_thread_state_runnable(threadobject *t);
203 void          threads_thread_state_waiting(threadobject *t);
204 void          threads_thread_state_timed_waiting(threadobject *t);
205 void          threads_thread_state_terminated(threadobject *t);
206
207 utf          *threads_thread_get_state(threadobject *t);
208 threadobject *thread_get_thread(java_handle_t *h);
209
210 bool          threads_thread_is_alive(threadobject *t);
211
212 void          threads_dump(void);
213 void          threads_thread_print_stacktrace(threadobject *thread);
214 void          threads_print_stacktrace(void);
215
216
217 /* implementation specific functions */
218
219 void          threads_impl_preinit(void);
220 void          threads_impl_init(void);
221
222 #if defined(ENABLE_GC_CACAO)
223 void          threads_mutex_gc_lock(void);
224 void          threads_mutex_gc_unlock(void);
225 #endif
226
227 void          threads_mutex_join_lock(void);
228 void          threads_mutex_join_unlock(void);
229
230 void          threads_impl_thread_init(threadobject *t);
231 void          threads_impl_thread_clear(threadobject *t);
232 void          threads_impl_thread_reuse(threadobject *t);
233 void          threads_impl_thread_free(threadobject *t);
234 void          threads_impl_thread_start(threadobject *thread, functionptr f);
235
236 void          threads_yield(void);
237
238 #endif /* ENABLE_THREADS */
239
240 #endif /* _THREADS_COMMON_H */
241
242
243 /*
244  * These are local overrides for various environment variables in Emacs.
245  * Please do not remove this and leave it at the end of the file, where
246  * Emacs will automagically detect them.
247  * ---------------------------------------------------------------------
248  * Local variables:
249  * mode: c
250  * indent-tabs-mode: t
251  * c-basic-offset: 4
252  * tab-width: 4
253  * End:
254  * vim:noexpandtab:sw=4:ts=4:
255  */