1 /* src/threads/threads-common.c - machine independent thread functions
3 Copyright (C) 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
36 #include "mm/memory.h"
38 #include "native/jni.h"
39 #include "native/llni.h"
41 #include "native/include/java_lang_Object.h"
42 #include "native/include/java_lang_String.h"
43 #include "native/include/java_lang_Thread.h"
45 #if defined(WITH_CLASSPATH_GNU)
46 # include "native/include/java_lang_VMThread.h"
49 #include "threads/critical.h"
50 #include "threads/lock-common.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 /* global threads list */
74 static list_t *list_threads;
76 /* global threads free-list */
78 typedef struct thread_index_t {
83 static list_t *list_free_thread_index;
85 #if defined(__LINUX__)
86 /* XXX Remove for exact-GC. */
87 bool threads_pthreads_implementation_nptl;
91 /* threads_preinit *************************************************************
93 Do some early initialization of stuff required.
95 ATTENTION: Do NOT use any Java heap allocation here, as gc_init()
96 is called AFTER this function!
98 *******************************************************************************/
100 void threads_preinit(void)
102 threadobject *mainthread;
103 #if defined(__LINUX__) && defined(_CS_GNU_LIBPTHREAD_VERSION)
108 #if defined(__LINUX__)
109 /* XXX Remove for exact-GC. */
111 /* On Linux we need to check the pthread implementation. */
113 /* _CS_GNU_LIBPTHREAD_VERSION (GNU C library only; since glibc 2.3.2) */
114 /* If the glibc is a pre-2.3.2 version, we fall back to
117 # if defined(_CS_GNU_LIBPTHREAD_VERSION)
118 len = confstr(_CS_GNU_LIBPTHREAD_VERSION, NULL, (size_t) 0);
120 /* Some systems return as length 0 (maybe cross-compilation
121 related). In this case we also fall back to linuxthreads. */
124 pathbuf = MNEW(char, len);
126 (void) confstr(_CS_GNU_LIBPTHREAD_VERSION, pathbuf, len);
128 if (strstr(pathbuf, "NPTL") != NULL)
129 threads_pthreads_implementation_nptl = true;
131 threads_pthreads_implementation_nptl = false;
134 threads_pthreads_implementation_nptl = false;
136 threads_pthreads_implementation_nptl = false;
140 /* initialize the threads lists */
142 list_threads = list_create(OFFSET(threadobject, linkage));
143 list_free_thread_index = list_create(OFFSET(thread_index_t, linkage));
145 /* Initialize the threads implementation (sets the thinlock on the
148 threads_impl_preinit();
150 /* create internal thread data-structure for the main thread */
152 mainthread = threads_thread_new();
154 /* thread is a Java thread and running */
156 mainthread->flags |= THREAD_FLAG_JAVA;
157 mainthread->state = THREAD_STATE_RUNNABLE;
159 /* store the internal thread data-structure in the TSD */
161 threads_set_current_threadobject(mainthread);
163 /* initialize locking subsystems */
167 /* initialize the critical section */
173 /* threads_list_first **********************************************************
175 Return the first entry in the threads list.
177 NOTE: This function does not lock the lists.
179 *******************************************************************************/
181 threadobject *threads_list_first(void)
185 t = list_first_unsynced(list_threads);
191 /* threads_list_next ***********************************************************
193 Return the next entry in the threads list.
195 NOTE: This function does not lock the lists.
197 *******************************************************************************/
199 threadobject *threads_list_next(threadobject *t)
203 next = list_next_unsynced(list_threads, t);
209 /* threads_list_get_non_daemons ************************************************
211 Return the number of non-daemon threads.
213 NOTE: This function does a linear-search over the threads list,
214 because it's only used for joining the threads.
216 *******************************************************************************/
218 s4 threads_list_get_non_daemons(void)
223 /* lock the threads lists */
229 for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
230 if (!(t->flags & THREAD_FLAG_DAEMON))
234 /* unlock the threads lists */
236 threads_list_unlock();
242 /* threads_thread_new **********************************************************
244 Allocates and initializes an internal thread data-structure and
245 adds it to the threads list.
247 *******************************************************************************/
249 threadobject *threads_thread_new(void)
255 /* lock the threads-lists */
259 /* Try to get a thread index from the free-list. */
261 ti = list_first_unsynced(list_free_thread_index);
263 /* Is a free thread index available? */
266 /* Yes, remove it from the free list, get the index and free
269 list_remove_unsynced(list_free_thread_index, ti);
273 FREE(ti, thread_index_t);
275 #if defined(ENABLE_STATISTICS)
277 size_thread_index_t -= sizeof(thread_index_t);
281 /* Get a new the thread index. */
283 index = list_threads->size + 1;
286 /* Allocate a thread data structure. */
288 #if defined(ENABLE_GC_BOEHM)
289 t = GCNEW_UNCOLLECTABLE(threadobject, 1);
291 t = NEW(threadobject);
294 #if defined(ENABLE_STATISTICS)
296 size_threadobject += sizeof(threadobject);
301 MZERO(t, threadobject, 1);
303 #if defined(ENABLE_GC_CACAO)
304 /* Register reference to java.lang.Thread with the GC. */
306 gc_reference_register(&(t->object), GC_REFTYPE_THREADOBJECT);
307 gc_reference_register(&(t->_exceptionptr), GC_REFTYPE_THREADOBJECT);
310 /* Pre-compute the thinlock-word. */
315 t->thinlock = lock_pre_compute_thinlock(t->index);
317 t->state = THREAD_STATE_NEW;
319 #if defined(ENABLE_GC_CACAO)
320 t->flags |= THREAD_FLAG_IN_NATIVE;
323 /* Initialize the implementation-specific bits. */
325 threads_impl_thread_new(t);
327 /* Add the thread to the threads-list. */
329 list_add_last_unsynced(list_threads, t);
331 /* Unlock the threads-lists. */
333 threads_list_unlock();
339 /* threads_thread_free *********************************************************
341 Remove the thread from the threads-list and free the internal
342 thread data structure. The thread index is added to the
343 thread-index free-list.
346 t....thread data structure
348 *******************************************************************************/
350 void threads_thread_free(threadobject *t)
354 /* Lock the threads lists. */
358 /* Cleanup the implementation specific bits. */
360 threads_impl_thread_free(t);
362 /* Remove the thread from the threads-list. */
364 list_remove_unsynced(list_threads, t);
366 /* Add the thread index to the free list. */
368 ti = NEW(thread_index_t);
370 #if defined(ENABLE_STATISTICS)
372 size_thread_index_t += sizeof(thread_index_t);
375 ti->index = t->index;
377 list_add_last_unsynced(list_free_thread_index, ti);
379 /* Free the thread data structure. */
381 #if defined(ENABLE_GC_BOEHM)
384 FREE(t, threadobject);
387 #if defined(ENABLE_STATISTICS)
389 size_threadobject -= sizeof(threadobject);
392 /* Unlock the threads lists. */
394 threads_list_unlock();
398 /* threads_thread_start_internal ***********************************************
400 Start an internal thread in the JVM. No Java thread objects exists
404 name.......UTF-8 name of the thread
405 f..........function pointer to C function to start
407 *******************************************************************************/
409 bool threads_thread_start_internal(utf *name, functionptr f)
412 java_lang_Thread *object;
413 #if defined(WITH_CLASSPATH_GNU)
414 java_lang_VMThread *vmt;
417 /* Enter the join-mutex, so if the main-thread is currently
418 waiting to join all threads, the number of non-daemon threads
421 threads_mutex_join_lock();
423 /* create internal thread data-structure */
425 t = threads_thread_new();
427 t->flags |= THREAD_FLAG_INTERNAL | THREAD_FLAG_DAEMON;
429 /* The thread is flagged as (non-)daemon thread, we can leave the
432 threads_mutex_join_unlock();
434 /* create the java thread object */
436 object = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
438 /* XXX memory leak!!! */
442 #if defined(WITH_CLASSPATH_GNU)
443 vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
445 /* XXX memory leak!!! */
449 LLNI_field_set_ref(vmt, thread, object);
450 LLNI_field_set_val(vmt, vmdata, (java_lang_Object *) t);
452 LLNI_field_set_ref(object, vmThread, vmt);
453 #elif defined(WITH_CLASSPATH_CLDC1_1)
454 LLNI_field_set_val(object, vm_thread, (java_lang_Object *) t);
457 t->object = LLNI_DIRECT(object);
459 /* set java.lang.Thread fields */
461 #if defined(WITH_CLASSPATH_GNU)
462 LLNI_field_set_ref(object, name , (java_lang_String *) javastring_new(name));
463 #elif defined(WITH_CLASSPATH_CLDC1_1)
464 /* FIXME: In cldc the name is a char[] */
465 /* LLNI_field_set_ref(object, name , (java_chararray *) javastring_new(name)); */
466 LLNI_field_set_ref(object, name , NULL);
469 #if defined(ENABLE_JAVASE)
470 LLNI_field_set_val(object, daemon , true);
473 LLNI_field_set_val(object, priority, NORM_PRIORITY);
475 /* start the thread */
477 threads_impl_thread_start(t, f);
479 /* everything's ok */
485 /* threads_thread_start ********************************************************
487 Start a Java thread in the JVM. Only the java thread object exists
491 object.....the java thread object java.lang.Thread
493 *******************************************************************************/
495 void threads_thread_start(java_handle_t *object)
498 threadobject *thread;
499 #if defined(WITH_CLASSPATH_GNU)
500 java_lang_VMThread *vmt;
503 o = (java_lang_Thread *) object;
505 /* Enter the join-mutex, so if the main-thread is currently
506 waiting to join all threads, the number of non-daemon threads
509 threads_mutex_join_lock();
511 /* create internal thread data-structure */
513 thread = threads_thread_new();
515 /* this is a normal Java thread */
517 thread->flags |= THREAD_FLAG_JAVA;
519 #if defined(ENABLE_JAVASE)
520 /* is this a daemon thread? */
522 if (LLNI_field_direct(o, daemon) == true)
523 thread->flags |= THREAD_FLAG_DAEMON;
526 /* The thread is flagged and (non-)daemon thread, we can leave the
529 threads_mutex_join_unlock();
531 /* link the two objects together */
533 thread->object = LLNI_DIRECT(object);
535 #if defined(WITH_CLASSPATH_GNU)
536 LLNI_field_get_ref(o, vmThread, vmt);
539 assert(LLNI_field_direct(vmt, vmdata) == NULL);
541 LLNI_field_set_val(vmt, vmdata, (java_lang_Object *) thread);
542 #elif defined(WITH_CLASSPATH_CLDC1_1)
543 LLNI_field_set_val(o, vm_thread, (java_lang_Object *) thread);
546 /* Start the thread. Don't pass a function pointer (NULL) since
547 we want Thread.run()V here. */
549 threads_impl_thread_start(thread, NULL);
553 /* threads_thread_print_info ***************************************************
555 Print information of the passed thread.
557 *******************************************************************************/
559 void threads_thread_print_info(threadobject *t)
561 java_lang_Thread *object;
562 #if defined(WITH_CLASSPATH_GNU)
563 java_lang_String *namestring;
567 assert(t->state != THREAD_STATE_NEW);
569 /* the thread may be currently in initalization, don't print it */
571 object = (java_lang_Thread *) LLNI_WRAP(t->object);
573 if (object != NULL) {
574 /* get thread name */
576 #if defined(WITH_CLASSPATH_GNU)
577 LLNI_field_get_ref(object, name, namestring);
578 name = javastring_toutf((java_handle_t *) namestring, false);
579 #elif defined(WITH_CLASSPATH_SUN) || defined(WITH_CLASSPATH_CLDC1_1)
580 /* FIXME: In cldc the name is a char[] */
581 /* name = object->name; */
584 # error unknown classpath configuration
588 utf_display_printable_ascii(name);
591 if (t->flags & THREAD_FLAG_DAEMON)
594 printf(" prio=%d", LLNI_field_direct(object, priority));
596 #if SIZEOF_VOID_P == 8
597 printf(" t=0x%016lx tid=0x%016lx (%ld)",
598 (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
600 printf(" t=0x%08x tid=0x%08x (%d)",
601 (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
604 printf(" index=%d", t->index);
606 /* print thread state */
609 case THREAD_STATE_NEW:
612 case THREAD_STATE_RUNNABLE:
615 case THREAD_STATE_BLOCKED:
618 case THREAD_STATE_WAITING:
621 case THREAD_STATE_TIMED_WAITING:
622 printf(" waiting on condition");
624 case THREAD_STATE_TERMINATED:
625 printf(" terminated");
628 vm_abort("threads_thread_print_info: unknown thread state %d",
635 /* threads_get_current_tid *****************************************************
637 Return the tid of the current thread.
642 *******************************************************************************/
644 ptrint threads_get_current_tid(void)
646 threadobject *thread;
648 thread = THREADOBJECT;
650 /* this may happen during bootstrap */
655 return (ptrint) thread->tid;
659 /* threads_thread_state_runnable ***********************************************
661 Set the current state of the given thread to THREAD_STATE_RUNNABLE.
663 NOTE: If the thread has already terminated, don't set the state.
664 This is important for threads_detach_thread.
666 *******************************************************************************/
668 void threads_thread_state_runnable(threadobject *t)
670 /* Set the state inside a lock. */
674 if (t->state != THREAD_STATE_TERMINATED)
675 t->state = THREAD_STATE_RUNNABLE;
677 DEBUGTHREADS("is RUNNABLE", t);
679 threads_list_unlock();
683 /* threads_thread_state_waiting ************************************************
685 Set the current state of the given thread to THREAD_STATE_WAITING.
687 NOTE: If the thread has already terminated, don't set the state.
688 This is important for threads_detach_thread.
690 *******************************************************************************/
692 void threads_thread_state_waiting(threadobject *t)
694 /* Set the state inside a lock. */
698 if (t->state != THREAD_STATE_TERMINATED)
699 t->state = THREAD_STATE_WAITING;
701 DEBUGTHREADS("is WAITING", t);
703 threads_list_unlock();
707 /* threads_thread_state_timed_waiting ******************************************
709 Set the current state of the given thread to
710 THREAD_STATE_TIMED_WAITING.
712 NOTE: If the thread has already terminated, don't set the state.
713 This is important for threads_detach_thread.
715 *******************************************************************************/
717 void threads_thread_state_timed_waiting(threadobject *t)
719 /* Set the state inside a lock. */
723 if (t->state != THREAD_STATE_TERMINATED)
724 t->state = THREAD_STATE_TIMED_WAITING;
726 DEBUGTHREADS("is TIMED_WAITING", t);
728 threads_list_unlock();
732 /* threads_thread_state_terminated *********************************************
734 Set the current state of the given thread to
735 THREAD_STATE_TERMINATED.
737 *******************************************************************************/
739 void threads_thread_state_terminated(threadobject *t)
741 /* set the state in the lock */
745 t->state = THREAD_STATE_TERMINATED;
747 DEBUGTHREADS("is TERMINATED", t);
749 threads_list_unlock();
753 /* threads_thread_get_state ****************************************************
755 Returns the current state of the given thread.
757 *******************************************************************************/
759 utf *threads_thread_get_state(threadobject *t)
764 case THREAD_STATE_NEW:
765 u = utf_new_char("NEW");
767 case THREAD_STATE_RUNNABLE:
768 u = utf_new_char("RUNNABLE");
770 case THREAD_STATE_BLOCKED:
771 u = utf_new_char("BLOCKED");
773 case THREAD_STATE_WAITING:
774 u = utf_new_char("WAITING");
776 case THREAD_STATE_TIMED_WAITING:
777 u = utf_new_char("TIMED_WAITING");
779 case THREAD_STATE_TERMINATED:
780 u = utf_new_char("TERMINATED");
783 vm_abort("threads_get_state: unknown thread state %d", t->state);
785 /* keep compiler happy */
794 /* threads_thread_is_alive *****************************************************
796 Returns if the give thread is alive.
798 *******************************************************************************/
800 bool threads_thread_is_alive(threadobject *t)
803 case THREAD_STATE_NEW:
804 case THREAD_STATE_TERMINATED:
807 case THREAD_STATE_RUNNABLE:
808 case THREAD_STATE_BLOCKED:
809 case THREAD_STATE_WAITING:
810 case THREAD_STATE_TIMED_WAITING:
814 vm_abort("threads_thread_is_alive: unknown thread state %d", t->state);
817 /* keep compiler happy */
823 /* threads_dump ****************************************************************
825 Dumps info for all threads running in the JVM. This function is
826 called when SIGQUIT (<ctrl>-\) is sent to CACAO.
828 *******************************************************************************/
830 void threads_dump(void)
834 /* XXX we should stop the world here */
836 /* lock the threads lists */
840 printf("Full thread dump CACAO "VERSION":\n");
842 /* iterate over all started threads */
844 for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
845 /* print thread info */
848 threads_thread_print_info(t);
851 /* print trace of thread */
853 threads_thread_print_stacktrace(t);
856 /* unlock the threads lists */
858 threads_list_unlock();
862 /* threads_thread_print_stacktrace *********************************************
864 Print the current stacktrace of the current thread.
866 *******************************************************************************/
868 void threads_thread_print_stacktrace(threadobject *thread)
871 stacktracebuffer *stb;
874 /* mark start of dump memory area */
876 dumpsize = dump_size();
878 /* create a stacktrace for the passed thread */
880 sfi = thread->_stackframeinfo;
882 stb = stacktrace_create(sfi);
884 /* print stacktrace */
887 stacktrace_print_trace_from_buffer(stb);
889 puts("\t<<No stacktrace available>>");
893 dump_release(dumpsize);
897 /* threads_print_stacktrace ****************************************************
899 Print the current stacktrace of the current thread.
901 *******************************************************************************/
903 void threads_print_stacktrace(void)
905 threadobject *thread;
907 thread = THREADOBJECT;
909 threads_thread_print_stacktrace(thread);
914 * These are local overrides for various environment variables in Emacs.
915 * Please do not remove this and leave it at the end of the file, where
916 * Emacs will automagically detect them.
917 * ---------------------------------------------------------------------
920 * indent-tabs-mode: t
924 * vim:noexpandtab:sw=4:ts=4: