CACAO
thread.hpp
Go to the documentation of this file.
1 /* src/threads/thread.hpp - machine independent thread functions
2 
3  Copyright (C) 1996-2013
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_ 1
28 
29 #include "config.h"
30 
31 #include "native/llni.hpp"
32 #include "vm/global.hpp"
33 #include "vm/options.hpp" // for opt_DebugThreads
34 #include "vm/os.hpp"
35 #include "vm/types.hpp"
36 #include "vm/utf8.hpp" // for Utf8String
37 
38 class Condition;
39 class DumpMemory;
40 struct localref_table;
41 class Mutex;
42 struct stackframeinfo_t;
43 struct JavaVMAttachArgs;
44 
45 /* thread states **************************************************************/
46 
56 };
57 
58 enum ThreadFlag {
59  THREAD_FLAG_JAVA = 0x01, // a normal Java thread
60  THREAD_FLAG_INTERNAL = 0x02, // CACAO internal thread
61  THREAD_FLAG_DAEMON = 0x04, // daemon thread
62  THREAD_FLAG_IN_NATIVE = 0x08 // currently executing native code
63 };
64 
66  SUSPEND_REASON_NONE = 0, // no reason to suspend
67  SUSPEND_REASON_JAVA = 1, // suspended from java.lang.Thread
68  SUSPEND_REASON_STOPWORLD = 2, // suspended from stop-the-world
69  SUSPEND_REASON_DUMP = 3, // suspended from threadlist dumping
70  SUSPEND_REASON_JVMTI = 4 // suspended from JVMTI agent
71 };
72 
73 /* thread priorities **********************************************************/
74 
79 };
80 
81 /* threadobject ***************************************************************/
82 
83 #if defined(ENABLE_THREADS)
85 #else
87 #endif
88 
89 struct threadobject {
90  //***** platform specific thread data
92 
93  java_object_t *object; /* link to java.lang.Thread object */
94 
95  ptrint thinlock; /* pre-computed thin lock value */
96  u4 flags; /* flag field */
97  ThreadState state; /* state field */
98 
99  //***** for ThreadList
100  bool is_in_active_list; /* for debugging only */
101  s4 index; /* thread index, starting with 1 */
102 
103  //***** for the sable tasuki lock extension */
104  bool flc_bit;
105  struct threadobject *flc_list; /* FLC list head for this thread */
106  struct threadobject *flc_tail; /* tail pointer for FLC list */
107  struct threadobject *flc_next; /* next pointer for FLC list */
109  Mutex* flc_lock; /* controlling access to these fields */
111 
112  //***** these are used for the wait/notify implementation
115 
116  Mutex* suspendmutex; /* lock before suspending this thread */
117  Condition* suspendcond; /* notify to resume this thread */
118 
120  bool signaled;
122 
123  bool suspended; /* is this thread suspended? */
124  SuspendReason suspend_reason; /* reason for suspending */
125 
126  u1 *pc; /* current PC (used for profiling) */
127 
128  java_object_t *_exceptionptr; /* current exception */
129  stackframeinfo_t *_stackframeinfo; /* current native stackframeinfo */
130  localref_table *_localref_table; /* JNI local references */
131 
132 #if defined(ENABLE_INTRP)
133  Cell *_global_sp; /* stack pointer for interpreter */
134 #endif
135 
136 #if defined(ENABLE_GC_CACAO)
137  bool gc_critical; /* indicates a critical section */
138 
139  sourcestate_t *ss;
140  executionstate_t *es;
141 #endif
142 
143  DumpMemory* _dumpmemory; ///< Dump memory structure.
144 
145 #if defined(ENABLE_DEBUG_FILTER)
146  u2 filterverbosecallctr[2]; /* counters for verbose call filter */
147 #endif
148 
149 #if !defined(NDEBUG)
152 #endif
153 
154 #if defined(ENABLE_TLH)
155  tlh_t tlh;
156 #endif
157 
158 #if defined(ENABLE_ESCAPE_REASON)
159  void *escape_reasons;
160 #endif
161 };
162 
163 /* debug **********************************************************************/
164 
165 #if !defined(NDEBUG)
166 # define DEBUGTHREADS(message, thread) \
167  do { \
168  if (opt_DebugThreads) { \
169  printf("[Thread %-16s: ", message); \
170  thread_print_info(thread); \
171  printf("]\n"); \
172  } \
173  } while (0)
174 #else
175 # define DEBUGTHREADS(message, thread)
176 #endif
177 
178 
179 /* global variables ***********************************************************/
180 
181 #if defined(__LINUX__)
182 /* XXX Remove for exact-GC. */
183 extern bool threads_pthreads_implementation_nptl;
184 #endif
185 
186 
187 /* state for trace java call **************************************************/
188 
189 #if !defined(NDEBUG)
190 # define TRACEJAVACALLINDENT (THREADOBJECT->tracejavacallindent)
191 # define TRACEJAVACALLCOUNT (THREADOBJECT->tracejavacallcount)
192 #endif
193 
194 
195 /* counter for verbose call filter ********************************************/
196 
197 #if defined(ENABLE_DEBUG_FILTER)
198 # define FILTERVERBOSECALLCTR (THREADOBJECT->filterverbosecallctr)
199 #endif
200 
201 
202 /* native-world flags *********************************************************/
203 
204 #if defined(ENABLE_GC_CACAO)
205 # define THREAD_NATIVEWORLD_ENTER THREADOBJECT->flags |= THREAD_FLAG_IN_NATIVE
206 # define THREAD_NATIVEWORLD_EXIT THREADOBJECT->flags &= ~THREAD_FLAG_IN_NATIVE
207 #else
208 # define THREAD_NATIVEWORLD_ENTER /*nop*/
209 # define THREAD_NATIVEWORLD_EXIT /*nop*/
210 #endif
211 
212 
213 /* inline functions ***********************************************************/
214 
215 inline static threadobject* thread_get_current(void);
216 
217 #if defined(ENABLE_THREADS)
219 #else
221 #endif
222 
223 /***
224  * Return the java.lang.Thread object for the current thread.
225  */
227  return LLNI_WRAP(thread_get_current()->object);
228 }
229 
230 
231 /* cacaothread_get_state *******************************************************
232 
233  Returns the current state of the given thread.
234 
235  ARGUMENTS:
236  t ... the thread to check
237 
238  RETURN:
239  thread state
240 
241 *******************************************************************************/
242 
243 inline static int cacaothread_get_state(threadobject *t)
244 {
245  return t->state;
246 }
247 
248 
249 /* thread_is_attached **********************************************************
250 
251  Returns if the given thread is attached to the VM.
252 
253  ARGUMENTS:
254  t ... the thread to check
255 
256  RETURN:
257  true .... the thread is attached to the VM
258  false ... the thread is not
259 
260 *******************************************************************************/
261 
262 inline static bool thread_is_attached(threadobject *t)
263 {
264  java_handle_t *o;
265 
266  o = LLNI_WRAP(t->object);
267 
268  return o != NULL;
269 }
270 
271 
272 /* thread_is_daemon ************************************************************
273 
274  Returns if the given thread is a daemon thread.
275 
276  ARGUMENTS:
277  t ... the thread to check
278 
279  RETURN:
280  true .... the thread is a daemon thread
281  false ... the thread is not
282 
283 *******************************************************************************/
284 
285 inline static bool thread_is_daemon(threadobject *t)
286 {
287  return (t->flags & THREAD_FLAG_DAEMON) != 0;
288 }
289 
290 
291 /* thread_current_is_attached **************************************************
292 
293  Returns if the current thread is attached to the VM.
294 
295  RETURN:
296  true .... the thread is attached to the VM
297  false ... the thread is not
298 
299 *******************************************************************************/
300 
301 inline static bool thread_current_is_attached(void)
302 {
303  threadobject *t;
304 
305  t = thread_get_current();
306 
307  if (t == NULL)
308  return false;
309 
310  return thread_is_attached(t);
311 }
312 
314 {
315  return THREADOBJECT->_stackframeinfo;
316 }
317 
319 {
320  THREADOBJECT->_stackframeinfo = sfi;
321 }
322 
323 
324 /* function prototypes ********************************************************/
325 
326 void threads_preinit(void);
327 void threads_init(void);
328 
329 void thread_free(threadobject *t);
330 
332 void threads_thread_start(java_handle_t *object);
333 
334 bool thread_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
335 bool thread_attach_current_external_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon);
337 
339 
340 void thread_fprint_name(threadobject *t, FILE *stream);
342 
343 intptr_t threads_get_current_tid(void);
344 intptr_t threads_get_tid(threadobject*);
345 
352 
354 
357 void thread_set_interrupted(threadobject *t, bool interrupted);
359 
360 
362 
363 void threads_set_thread_priority(threadobject *t, int priority);
364 
367 void threads_suspend_ack();
368 
369 void threads_join_all_threads(void);
370 
371 void threads_sleep(int64_t millis, int32_t nanos);
372 
373 void threads_wait_with_timeout_relative(threadobject *t, s8 millis, s4 nanos);
374 
375 void threads_park(bool absolute, int64_t nanos);
377 
378 #if defined(ENABLE_TLH)
379 void threads_tlh_add_frame();
380 void threads_tlh_remove_frame();
381 #endif
382 
383 /* implementation specific functions */
384 
385 void threads_impl_preinit(void);
386 void threads_impl_init(void);
387 
388 #if defined(ENABLE_GC_CACAO)
389 void threads_mutex_gc_lock(void);
390 void threads_mutex_gc_unlock(void);
391 #endif
392 
397 
398 void threads_yield(void);
399 
404 
405 #endif // THREAD_HPP_
406 
407 
408 /*
409  * These are local overrides for various environment variables in Emacs.
410  * Please do not remove this and leave it at the end of the file, where
411  * Emacs will automagically detect them.
412  * ---------------------------------------------------------------------
413  * Local variables:
414  * mode: c++
415  * indent-tabs-mode: t
416  * c-basic-offset: 4
417  * tab-width: 4
418  * End:
419  * vim:noexpandtab:sw=4:ts=4:
420  */
SuspendReason
Definition: thread.hpp:65
Mutex * waitmutex
Definition: thread.hpp:113
void threads_start_thread(threadobject *thread, functionptr function)
bool thread_detach_current_external_thread(void)
Detaches the current external thread from the VM.
Definition: thread.cpp:603
bool park_permit
Definition: thread.hpp:121
void threads_thread_interrupt(threadobject *t)
bool threads_suspend_thread(threadobject *thread, SuspendReason reason)
Suspend the passed thread.
void threads_preinit(void)
Definition: thread.cpp:84
static struct stackframeinfo_t * threads_get_current_stackframeinfo(void)
Definition: thread.hpp:313
Mutex * suspendmutex
Definition: thread.hpp:116
s8 Cell
Definition: intrp.h:42
void threads_unpark(threadobject *t)
Unpark the specified thread.
intptr_t threads_get_tid(threadobject *t)
bool thread_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon)
Attaches the current thread to the VM.
Definition: thread.cpp:495
void thread_set_state_runnable(threadobject *t)
Definition: thread.cpp:754
static java_handle_t * thread_get_current_object(void)
Definition: thread.hpp:226
struct threadobject * flc_list
Definition: thread.hpp:105
void thread_set_state_timed_parked(threadobject *t)
Definition: thread.cpp:831
bool threads_thread_start_internal(Utf8String name, functionptr f)
Definition: thread.cpp:402
bool thread_handle_is_interrupted(java_handle_t *th)
Definition: thread.cpp:981
static bool thread_is_daemon(threadobject *t)
Definition: thread.hpp:285
static int cacaothread_get_state(threadobject *t)
Definition: thread.hpp:243
int thread_handle_get_state(java_handle_t *th)
Definition: thread.cpp:1012
Definition: tlh.hpp:34
Dummy implementation of a mutex.
Definition: mutex-none.hpp:33
uint8_t u1
Definition: types.hpp:40
void threads_impl_thread_clear(threadobject *t)
struct threadobject * flc_next
Definition: thread.hpp:107
JNIEnv jclass jobject const char * name
Definition: jvmti.h:312
void threads_set_thread_priority(threadobject *t, int priority)
Definition: thread-none.cpp:77
int64_t s8
Definition: types.hpp:48
static bool thread_current_is_attached(void)
Definition: thread.hpp:301
Mutex * flc_lock
Definition: thread.hpp:109
void(* functionptr)(void)
Definition: global.hpp:39
ptrint thinlock
Definition: thread.hpp:95
cacao::detail::threadobject impl
Definition: thread.hpp:91
void threads_thread_start(java_handle_t *object)
Definition: thread.cpp:445
Condition * waitcond
Definition: thread.hpp:114
u4 tracejavacallcount
Definition: thread.hpp:151
#define LLNI_WRAP(obj)
Definition: llni.hpp:51
java_object_t * _exceptionptr
Definition: thread.hpp:128
Condition * suspendcond
Definition: thread.hpp:117
bool suspended
Definition: thread.hpp:123
void thread_set_state_parked(threadobject *t)
Definition: thread.cpp:812
uint16_t u2
Definition: types.hpp:43
intptr_t threads_get_current_tid(void)
Definition: thread.cpp:722
void threads_impl_thread_start(threadobject *thread, functionptr f)
Definition: thread-none.cpp:64
localref_table * _localref_table
Definition: thread.hpp:130
bool thread_attach_current_external_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon)
Attaches the current external thread to the VM.
Definition: thread.cpp:584
void threads_wait_with_timeout_relative(threadobject *thread, s8 millis, s4 nanos)
bool interrupted
Definition: thread.hpp:119
JNIEnv jthread thread
Definition: jvmti.h:207
SuspendReason suspend_reason
Definition: thread.hpp:124
void thread_fprint_name(threadobject *t, FILE *stream)
Definition: thread.cpp:630
bool thread_is_interrupted(threadobject *t)
Definition: thread.cpp:926
void thread_free(threadobject *t)
Definition: thread.cpp:371
Dummy condition variable.
void threads_join_all_threads()
int32_t s4
Definition: types.hpp:45
ThreadState state
Definition: thread.hpp:97
struct threadobject * flc_tail
Definition: thread.hpp:106
void threads_suspend_ack()
static threadobject * thread_get_current(void)
void thread_set_state_terminated(threadobject *t)
Definition: thread.cpp:848
void thread_handle_set_priority(java_handle_t *th, int priority)
Definition: thread.cpp:964
bool thread_detach_current_thread(void)
Detaches the current thread from the VM.
Definition: thread-none.cpp:86
void threads_init(void)
Definition: thread.cpp:164
threadobject * thread_get_thread(java_handle_t *h)
Definition: thread.cpp:874
bool flc_bit
Definition: thread.hpp:104
java_handle_t * flc_object
Definition: thread.hpp:108
bool threads_thread_is_alive(threadobject *t)
Definition: thread.cpp:886
void threads_impl_preinit()
Definition: thread-none.cpp:52
s4 tracejavacallindent
Definition: thread.hpp:150
uint32_t u4
Definition: types.hpp:46
void thread_set_interrupted(threadobject *t, bool interrupted)
Definition: thread.cpp:949
u2 filterverbosecallctr[2]
Definition: thread.hpp:146
static bool thread_is_attached(threadobject *t)
Definition: thread.hpp:262
ThreadFlag
Definition: thread.hpp:58
void threads_impl_init()
Definition: thread-none.cpp:58
void thread_set_state_timed_waiting(threadobject *t)
Definition: thread.cpp:793
void threads_park(bool absolute, int64_t nanos)
Park the current thread for the specified amount of time or until a specified deadline.
void threads_impl_clear_heap_pointers(threadobject *t)
bool signaled
Definition: thread.hpp:120
void thread_print_info(threadobject *t)
Definition: thread.cpp:650
java_object_t * object
Definition: thread.hpp:93
void thread_set_state_waiting(threadobject *t)
Definition: thread.cpp:773
stackframeinfo_t * _stackframeinfo
Definition: thread.hpp:129
Condition * flc_cond
Definition: thread.hpp:110
void threads_sleep(int64_t millis, int32_t nanos)
DumpMemory * _dumpmemory
Dump memory structure.
Definition: thread.hpp:143
bool is_in_active_list
Definition: thread.hpp:100
uintptr_t ptrint
Definition: types.hpp:54
Thread-local dump memory structure.
Definition: dumpmemory.hpp:60
#define THREADOBJECT
Definition: thread-none.hpp:47
ThreadState
Definition: thread.hpp:47
void threads_impl_thread_reuse(threadobject *t)
void threads_yield(void)
static void threads_set_current_stackframeinfo(struct stackframeinfo_t *sfi)
Definition: thread.hpp:318
void thread_handle_interrupt(java_handle_t *th)
Definition: thread.cpp:995
bool threads_resume_thread(threadobject *thread, SuspendReason reason)
Resumes execution of the passed thread.
ThreadPriority
Definition: thread.hpp:75