1 /* src/threads/threads-common.c - machine independent thread functions
3 Copyright (C) 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
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.
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.
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
34 #include "mm/memory.h"
36 #include "native/jni.h"
37 #include "native/llni.h"
39 #include "native/include/java_lang_Object.h"
40 #include "native/include/java_lang_String.h"
41 #include "native/include/java_lang_Thread.h"
43 #if defined(WITH_CLASSPATH_GNU)
44 # include "native/include/java_lang_Throwable.h"
45 # include "native/include/java_lang_VMThread.h"
48 #include "threads/critical.h"
49 #include "threads/lock-common.h"
50 #include "threads/threadlist.h"
51 #include "threads/threads-common.h"
53 #include "toolbox/list.h"
55 #include "vm/builtin.h"
56 #include "vm/stringlocal.h"
59 #include "vm/jit/stacktrace.h"
61 #include "vmcore/class.h"
62 #include "vmcore/options.h"
64 #if defined(ENABLE_STATISTICS)
65 # include "vmcore/statistics.h"
68 #include "vmcore/utf8.h"
71 /* global variables ***********************************************************/
73 #if defined(__LINUX__)
74 /* XXX Remove for exact-GC. */
75 bool threads_pthreads_implementation_nptl;
79 /* threads_preinit *************************************************************
81 Do some early initialization of stuff required.
83 *******************************************************************************/
85 void threads_preinit(void)
87 threadobject *mainthread;
88 #if defined(__LINUX__) && defined(_CS_GNU_LIBPTHREAD_VERSION)
93 TRACESUBSYSTEMINITIALIZATION("threads_preinit");
95 #if defined(__LINUX__)
96 /* XXX Remove for exact-GC. */
98 /* On Linux we need to check the pthread implementation. */
100 /* _CS_GNU_LIBPTHREAD_VERSION (GNU C library only; since glibc 2.3.2) */
101 /* If the glibc is a pre-2.3.2 version, we fall back to
104 # if defined(_CS_GNU_LIBPTHREAD_VERSION)
105 len = confstr(_CS_GNU_LIBPTHREAD_VERSION, NULL, (size_t) 0);
107 /* Some systems return as length 0 (maybe cross-compilation
108 related). In this case we also fall back to linuxthreads. */
111 pathbuf = MNEW(char, len);
113 (void) confstr(_CS_GNU_LIBPTHREAD_VERSION, pathbuf, len);
115 if (strstr(pathbuf, "NPTL") != NULL)
116 threads_pthreads_implementation_nptl = true;
118 threads_pthreads_implementation_nptl = false;
121 threads_pthreads_implementation_nptl = false;
123 threads_pthreads_implementation_nptl = false;
127 /* Initialize the threads implementation (sets the thinlock on the
130 threads_impl_preinit();
132 /* create internal thread data-structure for the main thread */
134 mainthread = threads_thread_new();
136 /* thread is a Java thread and running */
138 mainthread->flags |= THREAD_FLAG_JAVA;
139 mainthread->state = THREAD_STATE_RUNNABLE;
141 /* store the internal thread data-structure in the TSD */
143 threads_set_current_threadobject(mainthread);
147 /* threads_thread_new **********************************************************
149 Allocates and initializes an internal thread data-structure and
150 adds it to the threads list.
152 *******************************************************************************/
154 threadobject *threads_thread_new(void)
159 /* lock the threads-lists */
163 index = threadlist_get_free_index();
165 /* Allocate a thread data structure. */
167 /* First, try to get one from the free-list. */
169 t = threadlist_free_first();
172 /* Remove from free list. */
174 threadlist_free_remove(t);
176 /* Equivalent of MZERO on the else path */
178 threads_impl_thread_clear(t);
181 #if defined(ENABLE_GC_BOEHM)
182 t = GCNEW_UNCOLLECTABLE(threadobject, 1);
184 t = NEW(threadobject);
187 #if defined(ENABLE_STATISTICS)
189 size_threadobject += sizeof(threadobject);
194 MZERO(t, threadobject, 1);
196 #if defined(ENABLE_GC_CACAO)
197 /* Register reference to java.lang.Thread with the GC. */
198 /* FIXME is it ok to do this only once? */
200 gc_reference_register(&(t->object), GC_REFTYPE_THREADOBJECT);
201 gc_reference_register(&(t->_exceptionptr), GC_REFTYPE_THREADOBJECT);
204 /* Initialize the implementation-specific bits. */
206 threads_impl_thread_init(t);
209 /* Pre-compute the thinlock-word. */
214 t->thinlock = lock_pre_compute_thinlock(t->index);
216 t->state = THREAD_STATE_NEW;
218 #if defined(ENABLE_GC_CACAO)
219 t->flags |= THREAD_FLAG_IN_NATIVE;
222 /* Initialize the implementation-specific bits. */
224 threads_impl_thread_reuse(t);
226 /* Add the thread to the thread list. */
230 /* Unlock the threads-lists. */
232 threads_list_unlock();
238 /* threads_thread_free *********************************************************
240 Remove the thread from the threads-list and free the internal
241 thread data structure. The thread index is added to the
242 thread-index free-list.
245 t....thread data structure
247 *******************************************************************************/
249 void threads_thread_free(threadobject *t)
251 /* Lock the threads lists. */
255 /* Remove the thread from the thread-list. */
257 threadlist_remove(t);
259 /* Add the thread index to the free list. */
261 threadlist_index_add(t->index);
263 /* Add the thread data structure to the free list. */
265 threads_thread_set_object(t, NULL);
267 threadlist_free_add(t);
269 /* Unlock the threads lists. */
271 threads_list_unlock();
275 /* threads_thread_start_internal ***********************************************
277 Start an internal thread in the JVM. No Java thread objects exists
281 name.......UTF-8 name of the thread
282 f..........function pointer to C function to start
284 *******************************************************************************/
286 bool threads_thread_start_internal(utf *name, functionptr f)
289 java_lang_Thread *object;
290 #if defined(WITH_CLASSPATH_GNU)
291 java_lang_VMThread *vmt;
294 /* Enter the join-mutex, so if the main-thread is currently
295 waiting to join all threads, the number of non-daemon threads
298 threads_mutex_join_lock();
300 /* create internal thread data-structure */
302 t = threads_thread_new();
304 t->flags |= THREAD_FLAG_INTERNAL | THREAD_FLAG_DAEMON;
306 /* The thread is flagged as (non-)daemon thread, we can leave the
309 threads_mutex_join_unlock();
311 /* create the java thread object */
313 object = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
315 /* XXX memory leak!!! */
319 #if defined(WITH_CLASSPATH_GNU)
320 vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
322 /* XXX memory leak!!! */
326 LLNI_field_set_ref(vmt, thread, object);
327 LLNI_field_set_val(vmt, vmdata, (java_lang_Object *) t);
329 LLNI_field_set_ref(object, vmThread, vmt);
330 #elif defined(WITH_CLASSPATH_CLDC1_1)
331 LLNI_field_set_val(object, vm_thread, (java_lang_Object *) t);
334 threads_thread_set_object(t, (java_handle_t *) object);
336 /* set java.lang.Thread fields */
338 #if defined(WITH_CLASSPATH_GNU)
339 LLNI_field_set_ref(object, name , (java_lang_String *) javastring_new(name));
340 #elif defined(WITH_CLASSPATH_CLDC1_1)
341 /* FIXME: In cldc the name is a char[] */
342 /* LLNI_field_set_ref(object, name , (java_chararray *) javastring_new(name)); */
343 LLNI_field_set_ref(object, name , NULL);
346 #if defined(ENABLE_JAVASE)
347 LLNI_field_set_val(object, daemon , true);
350 LLNI_field_set_val(object, priority, NORM_PRIORITY);
352 /* start the thread */
354 threads_impl_thread_start(t, f);
356 /* everything's ok */
362 /* threads_thread_start ********************************************************
364 Start a Java thread in the JVM. Only the java thread object exists
368 object.....the java thread object java.lang.Thread
370 *******************************************************************************/
372 void threads_thread_start(java_handle_t *object)
375 threadobject *thread;
376 #if defined(WITH_CLASSPATH_GNU)
377 java_lang_VMThread *vmt;
380 o = (java_lang_Thread *) object;
382 /* Enter the join-mutex, so if the main-thread is currently
383 waiting to join all threads, the number of non-daemon threads
386 threads_mutex_join_lock();
388 /* create internal thread data-structure */
390 thread = threads_thread_new();
392 /* this is a normal Java thread */
394 thread->flags |= THREAD_FLAG_JAVA;
396 #if defined(ENABLE_JAVASE)
397 /* is this a daemon thread? */
399 if (LLNI_field_direct(o, daemon) == true)
400 thread->flags |= THREAD_FLAG_DAEMON;
403 /* The thread is flagged and (non-)daemon thread, we can leave the
406 threads_mutex_join_unlock();
408 /* link the two objects together */
410 threads_thread_set_object(thread, object);
412 #if defined(WITH_CLASSPATH_GNU)
413 LLNI_field_get_ref(o, vmThread, vmt);
416 assert(LLNI_field_direct(vmt, vmdata) == NULL);
418 LLNI_field_set_val(vmt, vmdata, (java_lang_Object *) thread);
419 #elif defined(WITH_CLASSPATH_CLDC1_1)
420 LLNI_field_set_val(o, vm_thread, (java_lang_Object *) thread);
423 /* Start the thread. Don't pass a function pointer (NULL) since
424 we want Thread.run()V here. */
426 threads_impl_thread_start(thread, NULL);
430 /* threads_thread_print_info ***************************************************
432 Print information of the passed thread.
434 *******************************************************************************/
436 void threads_thread_print_info(threadobject *t)
438 java_lang_Thread *object;
439 #if defined(WITH_CLASSPATH_GNU)
440 java_lang_String *namestring;
444 assert(t->state != THREAD_STATE_NEW);
446 /* the thread may be currently in initalization, don't print it */
448 object = (java_lang_Thread *) threads_thread_get_object(t);
450 if (object != NULL) {
451 /* get thread name */
453 #if defined(WITH_CLASSPATH_GNU)
454 LLNI_field_get_ref(object, name, namestring);
455 name = javastring_toutf((java_handle_t *) namestring, false);
456 #elif defined(WITH_CLASSPATH_SUN) || defined(WITH_CLASSPATH_CLDC1_1)
457 /* FIXME: In cldc the name is a char[] */
458 /* name = object->name; */
461 # error unknown classpath configuration
465 utf_display_printable_ascii(name);
468 if (t->flags & THREAD_FLAG_DAEMON)
471 printf(" prio=%d", LLNI_field_direct(object, priority));
473 #if SIZEOF_VOID_P == 8
474 printf(" t=0x%016lx tid=0x%016lx (%ld)",
475 (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
477 printf(" t=0x%08x tid=0x%08x (%d)",
478 (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
481 printf(" index=%d", t->index);
483 /* print thread state */
486 case THREAD_STATE_NEW:
489 case THREAD_STATE_RUNNABLE:
492 case THREAD_STATE_BLOCKED:
495 case THREAD_STATE_WAITING:
498 case THREAD_STATE_TIMED_WAITING:
499 printf(" waiting on condition");
501 case THREAD_STATE_TERMINATED:
502 printf(" terminated");
505 vm_abort("threads_thread_print_info: unknown thread state %d",
512 /* threads_get_current_tid *****************************************************
514 Return the tid of the current thread.
519 *******************************************************************************/
521 ptrint threads_get_current_tid(void)
523 threadobject *thread;
525 thread = THREADOBJECT;
527 /* this may happen during bootstrap */
532 return (ptrint) thread->tid;
536 /* threads_thread_state_runnable ***********************************************
538 Set the current state of the given thread to THREAD_STATE_RUNNABLE.
540 NOTE: If the thread has already terminated, don't set the state.
541 This is important for threads_detach_thread.
543 *******************************************************************************/
545 void threads_thread_state_runnable(threadobject *t)
547 /* Set the state inside a lock. */
551 if (t->state != THREAD_STATE_TERMINATED)
552 t->state = THREAD_STATE_RUNNABLE;
554 DEBUGTHREADS("is RUNNABLE", t);
556 threads_list_unlock();
560 /* threads_thread_state_waiting ************************************************
562 Set the current state of the given thread to THREAD_STATE_WAITING.
564 NOTE: If the thread has already terminated, don't set the state.
565 This is important for threads_detach_thread.
567 *******************************************************************************/
569 void threads_thread_state_waiting(threadobject *t)
571 /* Set the state inside a lock. */
575 if (t->state != THREAD_STATE_TERMINATED)
576 t->state = THREAD_STATE_WAITING;
578 DEBUGTHREADS("is WAITING", t);
580 threads_list_unlock();
584 /* threads_thread_state_timed_waiting ******************************************
586 Set the current state of the given thread to
587 THREAD_STATE_TIMED_WAITING.
589 NOTE: If the thread has already terminated, don't set the state.
590 This is important for threads_detach_thread.
592 *******************************************************************************/
594 void threads_thread_state_timed_waiting(threadobject *t)
596 /* Set the state inside a lock. */
600 if (t->state != THREAD_STATE_TERMINATED)
601 t->state = THREAD_STATE_TIMED_WAITING;
603 DEBUGTHREADS("is TIMED_WAITING", t);
605 threads_list_unlock();
609 /* threads_thread_state_terminated *********************************************
611 Set the current state of the given thread to
612 THREAD_STATE_TERMINATED.
614 *******************************************************************************/
616 void threads_thread_state_terminated(threadobject *t)
618 /* set the state in the lock */
622 t->state = THREAD_STATE_TERMINATED;
624 DEBUGTHREADS("is TERMINATED", t);
626 threads_list_unlock();
630 /* threads_thread_get_state ****************************************************
632 Returns the current state of the given thread.
634 *******************************************************************************/
636 utf *threads_thread_get_state(threadobject *t)
641 case THREAD_STATE_NEW:
642 u = utf_new_char("NEW");
644 case THREAD_STATE_RUNNABLE:
645 u = utf_new_char("RUNNABLE");
647 case THREAD_STATE_BLOCKED:
648 u = utf_new_char("BLOCKED");
650 case THREAD_STATE_WAITING:
651 u = utf_new_char("WAITING");
653 case THREAD_STATE_TIMED_WAITING:
654 u = utf_new_char("TIMED_WAITING");
656 case THREAD_STATE_TERMINATED:
657 u = utf_new_char("TERMINATED");
660 vm_abort("threads_get_state: unknown thread state %d", t->state);
662 /* keep compiler happy */
671 /* threads_thread_is_alive *****************************************************
673 Returns if the give thread is alive.
675 *******************************************************************************/
677 bool threads_thread_is_alive(threadobject *t)
680 case THREAD_STATE_NEW:
681 case THREAD_STATE_TERMINATED:
684 case THREAD_STATE_RUNNABLE:
685 case THREAD_STATE_BLOCKED:
686 case THREAD_STATE_WAITING:
687 case THREAD_STATE_TIMED_WAITING:
691 vm_abort("threads_thread_is_alive: unknown thread state %d", t->state);
694 /* keep compiler happy */
700 /* threads_dump ****************************************************************
702 Dumps info for all threads running in the JVM. This function is
703 called when SIGQUIT (<ctrl>-\) is sent to CACAO.
705 *******************************************************************************/
707 void threads_dump(void)
711 /* XXX we should stop the world here */
713 /* lock the threads lists */
717 printf("Full thread dump CACAO "VERSION":\n");
719 /* iterate over all started threads */
721 for (t = threadlist_first(); t != NULL; t = threadlist_next(t)) {
722 /* ignore threads which are in state NEW */
723 if (t->state == THREAD_STATE_NEW)
726 #if defined(ENABLE_GC_CACAO)
727 /* Suspend the thread. */
728 /* XXX Is the suspend reason correct? */
730 if (threads_suspend_thread(t, SUSPEND_REASON_JNI) == false)
731 vm_abort("threads_dump: threads_suspend_thread failed");
734 /* Print thread info. */
737 threads_thread_print_info(t);
740 /* Print trace of thread. */
742 threads_thread_print_stacktrace(t);
744 #if defined(ENABLE_GC_CACAO)
745 /* Resume the thread. */
747 if (threads_resume_thread(t) == false)
748 vm_abort("threads_dump: threads_resume_thread failed");
752 /* unlock the threads lists */
754 threads_list_unlock();
758 /* threads_thread_print_stacktrace *********************************************
760 Print the current stacktrace of the given thread.
762 *******************************************************************************/
764 void threads_thread_print_stacktrace(threadobject *thread)
766 stackframeinfo_t *sfi;
767 java_handle_bytearray_t *ba;
770 /* Build a stacktrace for the passed thread. */
772 sfi = thread->_stackframeinfo;
773 ba = stacktrace_get(sfi);
776 /* We need a critical section here as we use the byte-array
777 data pointer directly. */
781 st = (stacktrace_t *) LLNI_array_data(ba);
783 /* Print stacktrace. */
785 stacktrace_print(st);
790 puts("\t<<No stacktrace available>>");
796 /* threads_print_stacktrace ****************************************************
798 Print the current stacktrace of the current thread.
800 *******************************************************************************/
802 void threads_print_stacktrace(void)
804 threadobject *thread;
806 thread = THREADOBJECT;
808 threads_thread_print_stacktrace(thread);
813 * These are local overrides for various environment variables in Emacs.
814 * Please do not remove this and leave it at the end of the file, where
815 * Emacs will automagically detect them.
816 * ---------------------------------------------------------------------
819 * indent-tabs-mode: t
823 * vim:noexpandtab:sw=4:ts=4: