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 8295 2007-08-11 17:57:24Z 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;
287 /* pre-compute the thinlock-word */
289 assert(t->index != 0);
291 t->thinlock = lock_pre_compute_thinlock(t->index);
292 t->state = THREAD_STATE_NEW;
294 /* initialize the implementation-specific bits */
296 threads_impl_thread_new(t);
298 /* add the thread to the threads-list */
300 list_add_last_unsynced(list_threads, t);
302 /* unlock the threads-lists */
304 threads_list_unlock();
310 /* threads_thread_free *********************************************************
312 Frees an internal thread data-structure by removing it from the
313 threads-list and adding it to the free-list.
315 NOTE: The data-structure is NOT freed, the pointer keeps valid!
317 *******************************************************************************/
319 void threads_thread_free(threadobject *t)
324 /* lock the threads-lists */
328 /* cleanup the implementation-specific bits */
330 threads_impl_thread_free(t);
332 /* remove the thread from the threads-list */
334 list_remove_unsynced(list_threads, t);
336 /* Clear memory, but keep the thread-index and the
339 /* ATTENTION: Do this after list_remove, otherwise the linkage
340 pointers are invalid. */
345 MZERO(t, threadobject, 1);
350 /* add the thread to the free list */
352 list_add_first_unsynced(list_threads_free, t);
354 /* unlock the threads-lists */
356 threads_list_unlock();
360 /* threads_thread_start_internal ***********************************************
362 Start an internal thread in the JVM. No Java thread objects exists
366 name.......UTF-8 name of the thread
367 f..........function pointer to C function to start
369 *******************************************************************************/
371 bool threads_thread_start_internal(utf *name, functionptr f)
374 java_lang_Thread *object;
375 #if defined(WITH_CLASSPATH_GNU)
376 java_lang_VMThread *vmt;
379 /* Enter the join-mutex, so if the main-thread is currently
380 waiting to join all threads, the number of non-daemon threads
383 threads_mutex_join_lock();
385 /* create internal thread data-structure */
387 t = threads_thread_new();
389 t->flags = THREAD_FLAG_INTERNAL | THREAD_FLAG_DAEMON;
391 /* The thread is flagged as (non-)daemon thread, we can leave the
394 threads_mutex_join_unlock();
396 /* create the java thread object */
398 object = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
400 /* XXX memory leak!!! */
404 #if defined(WITH_CLASSPATH_GNU)
405 vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
407 /* XXX memory leak!!! */
411 LLNI_field_set_ref(vmt, thread, object);
412 LLNI_field_set_val(vmt, vmdata, (java_lang_Object *) t);
414 LLNI_field_set_ref(object, vmThread, vmt);
415 #elif defined(WITH_CLASSPATH_CLDC1_1)
416 LLNI_field_set_val(object, vm_thread, (java_lang_Object *) t);
421 /* set java.lang.Thread fields */
423 #if defined(WITH_CLASSPATH_GNU)
424 LLNI_field_set_ref(object, name , (java_lang_String *) javastring_new(name));
425 #elif defined(WITH_CLASSPATH_CLDC1_1)
426 /* FIXME: In cldc the name is a char[] */
427 /* LLNI_field_set_ref(object, name , (java_chararray *) javastring_new(name)); */
428 LLNI_field_set_ref(object, name , NULL);
431 #if defined(ENABLE_JAVASE)
432 LLNI_field_set_val(object, daemon , true);
435 LLNI_field_set_val(object, priority, NORM_PRIORITY);
437 /* start the thread */
439 threads_impl_thread_start(t, f);
441 /* everything's ok */
447 /* threads_thread_start ********************************************************
449 Start a Java thread in the JVM. Only the java thread object exists
453 object.....the java thread object java.lang.Thread
455 *******************************************************************************/
457 void threads_thread_start(java_lang_Thread *object)
459 threadobject *thread;
460 #if defined(WITH_CLASSPATH_GNU)
461 java_lang_VMThread *vmt;
464 /* Enter the join-mutex, so if the main-thread is currently
465 waiting to join all threads, the number of non-daemon threads
468 threads_mutex_join_lock();
470 /* create internal thread data-structure */
472 thread = threads_thread_new();
474 /* this is a normal Java thread */
476 thread->flags = THREAD_FLAG_JAVA;
478 #if defined(ENABLE_JAVASE)
479 /* is this a daemon thread? */
481 if (LLNI_field_direct(object, daemon) == true)
482 thread->flags |= THREAD_FLAG_DAEMON;
485 /* The thread is flagged and (non-)daemon thread, we can leave the
488 threads_mutex_join_unlock();
490 /* link the two objects together */
492 thread->object = object;
494 #if defined(WITH_CLASSPATH_GNU)
495 LLNI_field_get_ref(object, vmThread, vmt);
498 assert(LLNI_field_direct(vmt, vmdata) == NULL);
500 LLNI_field_set_val(vmt, vmdata, (java_lang_Object *) thread);
501 #elif defined(WITH_CLASSPATH_CLDC1_1)
502 LLNI_field_set_val(object, vm_thread, (java_lang_Object *) thread);
505 /* Start the thread. Don't pass a function pointer (NULL) since
506 we want Thread.run()V here. */
508 threads_impl_thread_start(thread, NULL);
512 /* threads_thread_print_info ***************************************************
514 Print information of the passed thread.
516 *******************************************************************************/
518 void threads_thread_print_info(threadobject *t)
520 java_lang_Thread *object;
523 assert(t->state != THREAD_STATE_NEW);
525 /* the thread may be currently in initalization, don't print it */
529 if (object != NULL) {
530 /* get thread name */
532 #if defined(WITH_CLASSPATH_GNU)
533 name = javastring_toutf((java_handle_t *) LLNI_field_direct(object, name), false);
534 #elif defined(WITH_CLASSPATH_SUN) || defined(WITH_CLASSPATH_CLDC1_1)
535 /* FIXME: In cldc the name is a char[] */
536 /* name = object->name; */
539 # error unknown classpath configuration
543 utf_display_printable_ascii(name);
546 if (t->flags & THREAD_FLAG_DAEMON)
549 printf(" prio=%d", LLNI_field_direct(object, priority));
551 #if SIZEOF_VOID_P == 8
552 printf(" t=0x%016lx tid=0x%016lx (%ld)",
553 (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
555 printf(" t=0x%08x tid=0x%08x (%d)",
556 (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
559 printf(" index=%d", t->index);
561 /* print thread state */
564 case THREAD_STATE_NEW:
567 case THREAD_STATE_RUNNABLE:
570 case THREAD_STATE_BLOCKED:
573 case THREAD_STATE_WAITING:
576 case THREAD_STATE_TIMED_WAITING:
577 printf(" waiting on condition");
579 case THREAD_STATE_TERMINATED:
580 printf(" terminated");
583 vm_abort("threads_thread_print_info: unknown thread state %d",
590 /* threads_get_current_tid *****************************************************
592 Return the tid of the current thread.
597 *******************************************************************************/
599 ptrint threads_get_current_tid(void)
601 threadobject *thread;
603 thread = THREADOBJECT;
605 /* this may happen during bootstrap */
610 return (ptrint) thread->tid;
614 /* threads_thread_state_runnable ***********************************************
616 Set the current state of the given thread to THREAD_STATE_RUNNABLE.
618 *******************************************************************************/
620 void threads_thread_state_runnable(threadobject *t)
622 /* set the state inside the lock */
626 t->state = THREAD_STATE_RUNNABLE;
628 threads_list_unlock();
632 /* threads_thread_state_waiting ************************************************
634 Set the current state of the given thread to THREAD_STATE_WAITING.
636 *******************************************************************************/
638 void threads_thread_state_waiting(threadobject *t)
640 /* set the state in the lock */
644 t->state = THREAD_STATE_WAITING;
646 threads_list_unlock();
650 /* threads_thread_state_timed_waiting ******************************************
652 Set the current state of the given thread to
653 THREAD_STATE_TIMED_WAITING.
655 *******************************************************************************/
657 void threads_thread_state_timed_waiting(threadobject *t)
659 /* set the state in the lock */
663 t->state = THREAD_STATE_TIMED_WAITING;
665 threads_list_unlock();
669 /* threads_thread_state_terminated *********************************************
671 Set the current state of the given thread to
672 THREAD_STATE_TERMINATED.
674 *******************************************************************************/
676 void threads_thread_state_terminated(threadobject *t)
678 /* set the state in the lock */
682 t->state = THREAD_STATE_TERMINATED;
684 threads_list_unlock();
688 /* threads_thread_get_state ****************************************************
690 Returns the current state of the given thread.
692 *******************************************************************************/
694 utf *threads_thread_get_state(threadobject *t)
699 case THREAD_STATE_NEW:
700 u = utf_new_char("NEW");
702 case THREAD_STATE_RUNNABLE:
703 u = utf_new_char("RUNNABLE");
705 case THREAD_STATE_BLOCKED:
706 u = utf_new_char("BLOCKED");
708 case THREAD_STATE_WAITING:
709 u = utf_new_char("WAITING");
711 case THREAD_STATE_TIMED_WAITING:
712 u = utf_new_char("TIMED_WAITING");
714 case THREAD_STATE_TERMINATED:
715 u = utf_new_char("TERMINATED");
718 vm_abort("threads_get_state: unknown thread state %d", t->state);
720 /* keep compiler happy */
729 /* threads_thread_is_alive *****************************************************
731 Returns if the give thread is alive.
733 *******************************************************************************/
735 bool threads_thread_is_alive(threadobject *thread)
739 switch (thread->state) {
740 case THREAD_STATE_NEW:
741 case THREAD_STATE_TERMINATED:
745 case THREAD_STATE_RUNNABLE:
746 case THREAD_STATE_BLOCKED:
747 case THREAD_STATE_WAITING:
748 case THREAD_STATE_TIMED_WAITING:
753 vm_abort("threads_is_alive: unknown thread state %d", thread->state);
755 /* keep compiler happy */
764 /* threads_dump ****************************************************************
766 Dumps info for all threads running in the JVM. This function is
767 called when SIGQUIT (<ctrl>-\) is sent to CACAO.
769 *******************************************************************************/
771 void threads_dump(void)
775 /* XXX we should stop the world here */
777 /* lock the threads lists */
781 printf("Full thread dump CACAO "VERSION":\n");
783 /* iterate over all started threads */
785 for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
786 /* print thread info */
789 threads_thread_print_info(t);
792 /* print trace of thread */
794 threads_thread_print_stacktrace(t);
797 /* unlock the threads lists */
799 threads_list_unlock();
803 /* threads_thread_print_stacktrace *********************************************
805 Print the current stacktrace of the current thread.
807 *******************************************************************************/
809 void threads_thread_print_stacktrace(threadobject *thread)
812 stacktracebuffer *stb;
815 /* mark start of dump memory area */
817 dumpsize = dump_size();
819 /* create a stacktrace for the passed thread */
821 sfi = thread->_stackframeinfo;
823 stb = stacktrace_create(sfi);
825 /* print stacktrace */
828 stacktrace_print_trace_from_buffer(stb);
830 puts("\t<<No stacktrace available>>");
834 dump_release(dumpsize);
838 /* threads_print_stacktrace ****************************************************
840 Print the current stacktrace of the current thread.
842 *******************************************************************************/
844 void threads_print_stacktrace(void)
846 threadobject *thread;
848 thread = THREADOBJECT;
850 threads_thread_print_stacktrace(thread);
855 * These are local overrides for various environment variables in Emacs.
856 * Please do not remove this and leave it at the end of the file, where
857 * Emacs will automagically detect them.
858 * ---------------------------------------------------------------------
861 * indent-tabs-mode: t
865 * vim:noexpandtab:sw=4:ts=4: