* src/threads/thread.hpp (thread_set_object, thread_get_object): Removed.
[cacao.git] / src / threads / thread.hpp
1 /* src/threads/thread.hpp - machine independent thread functions
2
3    Copyright (C) 1996-2011
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_current_object **************************************************
104
105    Return the Java object of the current thread.
106    
107    RETURN VALUE:
108        the Java object
109
110 *******************************************************************************/
111
112 inline static java_handle_t *thread_get_current_object(void)
113 {
114         threadobject  *t;
115         java_handle_t *o;
116
117         t = THREADOBJECT;
118         o = LLNI_WRAP(t->object);
119
120         return o;
121 }
122
123
124 /* cacaothread_get_state *******************************************************
125
126    Returns the current state of the given thread.
127
128    ARGUMENTS:
129        t ... the thread to check
130
131    RETURN:
132        thread state
133
134 *******************************************************************************/
135
136 inline static int cacaothread_get_state(threadobject *t)
137 {
138         return t->state;
139 }
140
141
142 /* thread_is_attached **********************************************************
143
144    Returns if the given thread is attached to the VM.
145
146    ARGUMENTS:
147        t ... the thread to check
148
149    RETURN:
150        true .... the thread is attached to the VM
151        false ... the thread is not
152
153 *******************************************************************************/
154
155 inline static bool thread_is_attached(threadobject *t)
156 {
157         java_handle_t *o;
158
159         o = LLNI_WRAP(t->object);
160
161         return o != NULL;
162 }
163
164
165 /* thread_is_daemon ************************************************************
166
167    Returns if the given thread is a daemon thread.
168
169    ARGUMENTS:
170        t ... the thread to check
171
172    RETURN:
173        true .... the thread is a daemon thread
174        false ... the thread is not
175
176 *******************************************************************************/
177
178 inline static bool thread_is_daemon(threadobject *t)
179 {
180         return (t->flags & THREAD_FLAG_DAEMON) != 0;
181 }
182
183
184 /* thread_current_is_attached **************************************************
185
186    Returns if the current thread is attached to the VM.
187
188    RETURN:
189        true .... the thread is attached to the VM
190        false ... the thread is not
191
192 *******************************************************************************/
193
194 inline static bool thread_current_is_attached(void)
195 {
196         threadobject  *t;
197
198         t = thread_get_current();
199
200         if (t == NULL)
201                 return false;
202
203         return thread_is_attached(t);
204 }
205
206
207 /* function prototypes ********************************************************/
208
209 void          threads_preinit(void);
210 void          threads_init(void);
211
212 void          thread_free(threadobject *t);
213
214 bool          threads_thread_start_internal(utf *name, functionptr f);
215 void          threads_thread_start(java_handle_t *object);
216
217 bool          thread_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
218 bool          thread_attach_current_external_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
219 bool          thread_detach_current_thread(void);
220
221 bool          thread_detach_current_external_thread(void);
222
223 void          thread_fprint_name(threadobject *t, FILE *stream);
224 void          thread_print_info(threadobject *t);
225
226 intptr_t      threads_get_current_tid(void);
227
228 void          thread_set_state_runnable(threadobject *t);
229 void          thread_set_state_waiting(threadobject *t);
230 void          thread_set_state_timed_waiting(threadobject *t);
231 void          thread_set_state_parked(threadobject *t);
232 void          thread_set_state_timed_parked(threadobject *t);
233 void          thread_set_state_terminated(threadobject *t);
234
235 threadobject *thread_get_thread(java_handle_t *h);
236
237 bool          threads_thread_is_alive(threadobject *t);
238 bool          thread_is_interrupted(threadobject *t);
239 void          thread_set_interrupted(threadobject *t, bool interrupted);
240
241 /* implementation specific functions */
242
243 void          threads_impl_preinit(void);
244 void          threads_impl_init(void);
245
246 #if defined(ENABLE_GC_CACAO)
247 void          threads_mutex_gc_lock(void);
248 void          threads_mutex_gc_unlock(void);
249 #endif
250
251 void          threads_mutex_join_lock(void);
252 void          threads_mutex_join_unlock(void);
253
254 void          threads_impl_thread_clear(threadobject *t);
255 void          threads_impl_thread_reuse(threadobject *t);
256 void          threads_impl_clear_heap_pointers(threadobject *t);
257 void          threads_impl_thread_start(threadobject *thread, functionptr f);
258
259 void          threads_yield(void);
260
261 #ifdef __cplusplus
262 }
263 #endif
264
265 #endif /* ENABLE_THREADS */
266
267 #endif // _THREAD_HPP
268
269 void          thread_handle_set_priority(java_handle_t *th, int);
270 bool          thread_handle_is_interrupted(java_handle_t *th);
271 void          thread_handle_interrupt(java_handle_t *th);
272 int           thread_handle_get_state(java_handle_t *th);
273
274 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
275 #include "thread-classpath.hpp"
276 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
277 #include "thread-openjdk.hpp"
278 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_CLDC1_1)
279 #include "thread-cldc11.hpp"
280 #endif
281
282
283 /*
284  * These are local overrides for various environment variables in Emacs.
285  * Please do not remove this and leave it at the end of the file, where
286  * Emacs will automagically detect them.
287  * ---------------------------------------------------------------------
288  * Local variables:
289  * mode: c++
290  * indent-tabs-mode: t
291  * c-basic-offset: 4
292  * tab-width: 4
293  * End:
294  * vim:noexpandtab:sw=4:ts=4:
295  */