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