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
25 $Id: threads-common.c 8299 2007-08-13 08:41:18Z michi $
38 #include "mm/memory.h"
40 #include "native/jni.h"
41 #include "native/llni.h"
43 #include "native/include/java_lang_Object.h"
44 #include "native/include/java_lang_String.h"
45 #include "native/include/java_lang_Thread.h"
47 #if defined(WITH_CLASSPATH_GNU)
48 # include "native/include/java_lang_VMThread.h"
51 #include "threads/critical.h"
52 #include "threads/lock-common.h"
53 #include "threads/threads-common.h"
55 #include "toolbox/list.h"
57 #include "vm/builtin.h"
58 #include "vm/stringlocal.h"
61 #include "vm/jit/stacktrace.h"
63 #include "vmcore/class.h"
65 #if defined(ENABLE_STATISTICS)
66 # include "vmcore/options.h"
67 # include "vmcore/statistics.h"
70 #include "vmcore/utf8.h"
73 /* global variables ***********************************************************/
75 /* global threads list */
76 static list_t *list_threads;
78 /* global threads free-list */
79 static list_t *list_threads_free;
81 #if defined(__LINUX__)
82 /* XXX Remove for exact-GC. */
83 bool threads_pthreads_implementation_nptl;
87 /* threads_preinit *************************************************************
89 Do some early initialization of stuff required.
91 ATTENTION: Do NOT use any Java heap allocation here, as gc_init()
92 is called AFTER this function!
94 *******************************************************************************/
96 void threads_preinit(void)
98 threadobject *mainthread;
99 #if defined(__LINUX__) && defined(_CS_GNU_LIBPTHREAD_VERSION)
104 #if defined(__LINUX__)
105 /* XXX Remove for exact-GC. */
107 /* On Linux we need to check the pthread implementation. */
109 /* _CS_GNU_LIBPTHREAD_VERSION (GNU C library only; since glibc 2.3.2) */
110 /* If the glibc is a pre-2.3.2 version, we fall back to
113 # if defined(_CS_GNU_LIBPTHREAD_VERSION)
114 len = confstr(_CS_GNU_LIBPTHREAD_VERSION, NULL, (size_t) 0);
116 /* Some systems return as length 0 (maybe cross-compilation
117 related). In this case we also fall back to linuxthreads. */
120 pathbuf = MNEW(char, len);
122 (void) confstr(_CS_GNU_LIBPTHREAD_VERSION, pathbuf, len);
124 if (strstr(pathbuf, "NPTL") != NULL)
125 threads_pthreads_implementation_nptl = true;
127 threads_pthreads_implementation_nptl = false;
130 threads_pthreads_implementation_nptl = false;
132 threads_pthreads_implementation_nptl = false;
136 /* initialize the threads lists */
138 list_threads = list_create(OFFSET(threadobject, linkage));
139 list_threads_free = list_create(OFFSET(threadobject, linkage));
141 /* Initialize the threads implementation (sets the thinlock on the
144 threads_impl_preinit();
146 /* create internal thread data-structure for the main thread */
148 mainthread = threads_thread_new();
150 /* thread is a Java thread and running */
152 mainthread->flags = THREAD_FLAG_JAVA;
153 mainthread->state = THREAD_STATE_RUNNABLE;
155 /* store the internal thread data-structure in the TSD */
157 threads_set_current_threadobject(mainthread);
159 /* initialize locking subsystems */
163 /* initialize the critical section */
169 /* threads_list_first **********************************************************
171 Return the first entry in the threads list.
173 NOTE: This function does not lock the lists.
175 *******************************************************************************/
177 threadobject *threads_list_first(void)
181 t = list_first_unsynced(list_threads);
187 /* threads_list_next ***********************************************************
189 Return the next entry in the threads list.
191 NOTE: This function does not lock the lists.
193 *******************************************************************************/
195 threadobject *threads_list_next(threadobject *t)
199 next = list_next_unsynced(list_threads, t);
205 /* threads_list_get_non_daemons ************************************************
207 Return the number of non-daemon threads.
209 NOTE: This function does a linear-search over the threads list,
210 because it's only used for joining the threads.
212 *******************************************************************************/
214 s4 threads_list_get_non_daemons(void)
219 /* lock the threads lists */
225 for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
226 if (!(t->flags & THREAD_FLAG_DAEMON))
230 /* unlock the threads lists */
232 threads_list_unlock();
238 /* threads_thread_new **********************************************************
240 Allocates and initializes an internal thread data-structure and
241 adds it to the threads list.
243 *******************************************************************************/
245 threadobject *threads_thread_new(void)
249 /* lock the threads-lists */
253 /* try to get a thread from the free-list */
255 t = list_first_unsynced(list_threads_free);
257 /* is a free thread available? */
260 /* yes, remove it from the free list */
262 list_remove_unsynced(list_threads_free, t);
265 /* no, allocate a new one */
267 #if defined(ENABLE_GC_BOEHM)
268 t = GCNEW_UNCOLLECTABLE(threadobject, 1);
270 t = NEW(threadobject);
273 #if defined(ENABLE_STATISTICS)
275 size_threadobject += sizeof(threadobject);
280 MZERO(t, threadobject, 1);
282 /* set the threads-index */
284 t->index = list_threads->size + 1;
286 #if defined(ENABLE_GC_CACAO)
287 /* register reference to java.lang.Thread with the GC */
289 gc_reference_register((java_objectheader **) &(t->object));
293 /* pre-compute the thinlock-word */
295 assert(t->index != 0);
297 t->thinlock = lock_pre_compute_thinlock(t->index);
299 t->state = THREAD_STATE_NEW;
301 #if defined(ENABLE_GC_CACAO)
302 t->flags |= THREAD_FLAG_IN_NATIVE;
305 /* initialize the implementation-specific bits */
307 threads_impl_thread_new(t);
309 /* add the thread to the threads-list */
311 list_add_last_unsynced(list_threads, t);
313 /* unlock the threads-lists */
315 threads_list_unlock();
321 /* threads_thread_free *********************************************************
323 Frees an internal thread data-structure by removing it from the
324 threads-list and adding it to the free-list.
326 NOTE: The data-structure is NOT freed, the pointer keeps valid!
328 *******************************************************************************/
330 void threads_thread_free(threadobject *t)
335 /* lock the threads-lists */
339 /* cleanup the implementation-specific bits */
341 threads_impl_thread_free(t);
343 /* remove the thread from the threads-list */
345 list_remove_unsynced(list_threads, t);
347 /* Clear memory, but keep the thread-index and the
350 /* ATTENTION: Do this after list_remove, otherwise the linkage
351 pointers are invalid. */
356 MZERO(t, threadobject, 1);
361 /* add the thread to the free list */
363 list_add_first_unsynced(list_threads_free, t);
365 /* unlock the threads-lists */
367 threads_list_unlock();
371 /* threads_thread_start_internal ***********************************************
373 Start an internal thread in the JVM. No Java thread objects exists
377 name.......UTF-8 name of the thread
378 f..........function pointer to C function to start
380 *******************************************************************************/
382 bool threads_thread_start_internal(utf *name, functionptr f)
385 java_lang_Thread *object;
386 #if defined(WITH_CLASSPATH_GNU)
387 java_lang_VMThread *vmt;
390 /* Enter the join-mutex, so if the main-thread is currently
391 waiting to join all threads, the number of non-daemon threads
394 threads_mutex_join_lock();
396 /* create internal thread data-structure */
398 t = threads_thread_new();
400 t->flags |= THREAD_FLAG_INTERNAL | THREAD_FLAG_DAEMON;
402 /* The thread is flagged as (non-)daemon thread, we can leave the
405 threads_mutex_join_unlock();
407 /* create the java thread object */
409 object = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
411 /* XXX memory leak!!! */
415 #if defined(WITH_CLASSPATH_GNU)
416 vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
418 /* XXX memory leak!!! */
422 LLNI_field_set_ref(vmt, thread, object);
423 LLNI_field_set_val(vmt, vmdata, (java_lang_Object *) t);
425 LLNI_field_set_ref(object, vmThread, vmt);
426 #elif defined(WITH_CLASSPATH_CLDC1_1)
427 LLNI_field_set_val(object, vm_thread, (java_lang_Object *) t);
432 /* set java.lang.Thread fields */
434 #if defined(WITH_CLASSPATH_GNU)
435 LLNI_field_set_ref(object, name , (java_lang_String *) javastring_new(name));
436 #elif defined(WITH_CLASSPATH_CLDC1_1)
437 /* FIXME: In cldc the name is a char[] */
438 /* LLNI_field_set_ref(object, name , (java_chararray *) javastring_new(name)); */
439 LLNI_field_set_ref(object, name , NULL);
442 #if defined(ENABLE_JAVASE)
443 LLNI_field_set_val(object, daemon , true);
446 LLNI_field_set_val(object, priority, NORM_PRIORITY);
448 /* start the thread */
450 threads_impl_thread_start(t, f);
452 /* everything's ok */
458 /* threads_thread_start ********************************************************
460 Start a Java thread in the JVM. Only the java thread object exists
464 object.....the java thread object java.lang.Thread
466 *******************************************************************************/
468 void threads_thread_start(java_lang_Thread *object)
470 threadobject *thread;
471 #if defined(WITH_CLASSPATH_GNU)
472 java_lang_VMThread *vmt;
475 /* Enter the join-mutex, so if the main-thread is currently
476 waiting to join all threads, the number of non-daemon threads
479 threads_mutex_join_lock();
481 /* create internal thread data-structure */
483 thread = threads_thread_new();
485 /* this is a normal Java thread */
487 thread->flags |= THREAD_FLAG_JAVA;
489 #if defined(ENABLE_JAVASE)
490 /* is this a daemon thread? */
492 if (LLNI_field_direct(object, daemon) == true)
493 thread->flags |= THREAD_FLAG_DAEMON;
496 /* The thread is flagged and (non-)daemon thread, we can leave the
499 threads_mutex_join_unlock();
501 /* link the two objects together */
503 thread->object = object;
505 #if defined(WITH_CLASSPATH_GNU)
506 LLNI_field_get_ref(object, vmThread, vmt);
509 assert(LLNI_field_direct(vmt, vmdata) == NULL);
511 LLNI_field_set_val(vmt, vmdata, (java_lang_Object *) thread);
512 #elif defined(WITH_CLASSPATH_CLDC1_1)
513 LLNI_field_set_val(object, vm_thread, (java_lang_Object *) thread);
516 /* Start the thread. Don't pass a function pointer (NULL) since
517 we want Thread.run()V here. */
519 threads_impl_thread_start(thread, NULL);
523 /* threads_thread_print_info ***************************************************
525 Print information of the passed thread.
527 *******************************************************************************/
529 void threads_thread_print_info(threadobject *t)
531 java_lang_Thread *object;
534 assert(t->state != THREAD_STATE_NEW);
536 /* the thread may be currently in initalization, don't print it */
540 if (object != NULL) {
541 /* get thread name */
543 #if defined(WITH_CLASSPATH_GNU)
544 name = javastring_toutf((java_handle_t *) LLNI_field_direct(object, name), false);
545 #elif defined(WITH_CLASSPATH_SUN) || defined(WITH_CLASSPATH_CLDC1_1)
546 /* FIXME: In cldc the name is a char[] */
547 /* name = object->name; */
550 # error unknown classpath configuration
554 utf_display_printable_ascii(name);
557 if (t->flags & THREAD_FLAG_DAEMON)
560 printf(" prio=%d", LLNI_field_direct(object, priority));
562 #if SIZEOF_VOID_P == 8
563 printf(" t=0x%016lx tid=0x%016lx (%ld)",
564 (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
566 printf(" t=0x%08x tid=0x%08x (%d)",
567 (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
570 printf(" index=%d", t->index);
572 /* print thread state */
575 case THREAD_STATE_NEW:
578 case THREAD_STATE_RUNNABLE:
581 case THREAD_STATE_BLOCKED:
584 case THREAD_STATE_WAITING:
587 case THREAD_STATE_TIMED_WAITING:
588 printf(" waiting on condition");
590 case THREAD_STATE_TERMINATED:
591 printf(" terminated");
594 vm_abort("threads_thread_print_info: unknown thread state %d",
601 /* threads_get_current_tid *****************************************************
603 Return the tid of the current thread.
608 *******************************************************************************/
610 ptrint threads_get_current_tid(void)
612 threadobject *thread;
614 thread = THREADOBJECT;
616 /* this may happen during bootstrap */
621 return (ptrint) thread->tid;
625 /* threads_thread_state_runnable ***********************************************
627 Set the current state of the given thread to THREAD_STATE_RUNNABLE.
629 *******************************************************************************/
631 void threads_thread_state_runnable(threadobject *t)
633 /* set the state inside the lock */
637 t->state = THREAD_STATE_RUNNABLE;
639 threads_list_unlock();
643 /* threads_thread_state_waiting ************************************************
645 Set the current state of the given thread to THREAD_STATE_WAITING.
647 *******************************************************************************/
649 void threads_thread_state_waiting(threadobject *t)
651 /* set the state in the lock */
655 t->state = THREAD_STATE_WAITING;
657 threads_list_unlock();
661 /* threads_thread_state_timed_waiting ******************************************
663 Set the current state of the given thread to
664 THREAD_STATE_TIMED_WAITING.
666 *******************************************************************************/
668 void threads_thread_state_timed_waiting(threadobject *t)
670 /* set the state in the lock */
674 t->state = THREAD_STATE_TIMED_WAITING;
676 threads_list_unlock();
680 /* threads_thread_state_terminated *********************************************
682 Set the current state of the given thread to
683 THREAD_STATE_TERMINATED.
685 *******************************************************************************/
687 void threads_thread_state_terminated(threadobject *t)
689 /* set the state in the lock */
693 t->state = THREAD_STATE_TERMINATED;
695 threads_list_unlock();
699 /* threads_thread_get_state ****************************************************
701 Returns the current state of the given thread.
703 *******************************************************************************/
705 utf *threads_thread_get_state(threadobject *t)
710 case THREAD_STATE_NEW:
711 u = utf_new_char("NEW");
713 case THREAD_STATE_RUNNABLE:
714 u = utf_new_char("RUNNABLE");
716 case THREAD_STATE_BLOCKED:
717 u = utf_new_char("BLOCKED");
719 case THREAD_STATE_WAITING:
720 u = utf_new_char("WAITING");
722 case THREAD_STATE_TIMED_WAITING:
723 u = utf_new_char("TIMED_WAITING");
725 case THREAD_STATE_TERMINATED:
726 u = utf_new_char("TERMINATED");
729 vm_abort("threads_get_state: unknown thread state %d", t->state);
731 /* keep compiler happy */
740 /* threads_thread_is_alive *****************************************************
742 Returns if the give thread is alive.
744 *******************************************************************************/
746 bool threads_thread_is_alive(threadobject *thread)
750 switch (thread->state) {
751 case THREAD_STATE_NEW:
752 case THREAD_STATE_TERMINATED:
756 case THREAD_STATE_RUNNABLE:
757 case THREAD_STATE_BLOCKED:
758 case THREAD_STATE_WAITING:
759 case THREAD_STATE_TIMED_WAITING:
764 vm_abort("threads_is_alive: unknown thread state %d", thread->state);
766 /* keep compiler happy */
775 /* threads_dump ****************************************************************
777 Dumps info for all threads running in the JVM. This function is
778 called when SIGQUIT (<ctrl>-\) is sent to CACAO.
780 *******************************************************************************/
782 void threads_dump(void)
786 /* XXX we should stop the world here */
788 /* lock the threads lists */
792 printf("Full thread dump CACAO "VERSION":\n");
794 /* iterate over all started threads */
796 for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
797 /* print thread info */
800 threads_thread_print_info(t);
803 /* print trace of thread */
805 threads_thread_print_stacktrace(t);
808 /* unlock the threads lists */
810 threads_list_unlock();
814 /* threads_thread_print_stacktrace *********************************************
816 Print the current stacktrace of the current thread.
818 *******************************************************************************/
820 void threads_thread_print_stacktrace(threadobject *thread)
823 stacktracebuffer *stb;
826 /* mark start of dump memory area */
828 dumpsize = dump_size();
830 /* create a stacktrace for the passed thread */
832 sfi = thread->_stackframeinfo;
834 stb = stacktrace_create(sfi);
836 /* print stacktrace */
839 stacktrace_print_trace_from_buffer(stb);
841 puts("\t<<No stacktrace available>>");
845 dump_release(dumpsize);
849 /* threads_print_stacktrace ****************************************************
851 Print the current stacktrace of the current thread.
853 *******************************************************************************/
855 void threads_print_stacktrace(void)
857 threadobject *thread;
859 thread = THREADOBJECT;
861 threads_thread_print_stacktrace(thread);
866 * These are local overrides for various environment variables in Emacs.
867 * Please do not remove this and leave it at the end of the file, where
868 * Emacs will automagically detect them.
869 * ---------------------------------------------------------------------
872 * indent-tabs-mode: t
876 * vim:noexpandtab:sw=4:ts=4: