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 8272 2007-08-08 14:55:00Z twisti $
38 #include "mm/memory.h"
40 #include "native/jni.h"
42 #include "native/include/java_lang_Object.h"
43 #include "native/include/java_lang_String.h"
44 #include "native/include/java_lang_Thread.h"
46 #if defined(WITH_CLASSPATH_GNU)
47 # include "native/include/java_lang_VMThread.h"
50 #include "threads/critical.h"
51 #include "threads/lock-common.h"
52 #include "threads/threads-common.h"
54 #include "toolbox/list.h"
56 #include "vm/builtin.h"
57 #include "vm/stringlocal.h"
60 #include "vm/jit/stacktrace.h"
62 #include "vmcore/class.h"
64 #if defined(ENABLE_STATISTICS)
65 # include "vmcore/options.h"
66 # include "vmcore/statistics.h"
69 #include "vmcore/utf8.h"
72 /* global variables ***********************************************************/
74 /* global threads list */
75 static list_t *list_threads;
77 /* global threads free-list */
78 static list_t *list_threads_free;
80 #if defined(__LINUX__)
81 /* XXX Remove for exact-GC. */
82 bool threads_pthreads_implementation_nptl;
86 /* threads_preinit *************************************************************
88 Do some early initialization of stuff required.
90 ATTENTION: Do NOT use any Java heap allocation here, as gc_init()
91 is called AFTER this function!
93 *******************************************************************************/
95 void threads_preinit(void)
97 threadobject *mainthread;
98 #if defined(__LINUX__) && defined(_CS_GNU_LIBPTHREAD_VERSION)
103 #if defined(__LINUX__)
104 /* XXX Remove for exact-GC. */
106 /* On Linux we need to check the pthread implementation. */
108 /* _CS_GNU_LIBPTHREAD_VERSION (GNU C library only; since glibc 2.3.2) */
109 /* If the glibc is a pre-2.3.2 version, we fall back to
112 # if defined(_CS_GNU_LIBPTHREAD_VERSION)
113 len = confstr(_CS_GNU_LIBPTHREAD_VERSION, NULL, (size_t) 0);
115 /* Some systems return as length 0 (maybe cross-compilation
116 related). In this case we also fall back to linuxthreads. */
119 pathbuf = MNEW(char, len);
121 (void) confstr(_CS_GNU_LIBPTHREAD_VERSION, pathbuf, len);
123 if (strstr(pathbuf, "NPTL") != NULL)
124 threads_pthreads_implementation_nptl = true;
126 threads_pthreads_implementation_nptl = false;
129 threads_pthreads_implementation_nptl = false;
131 threads_pthreads_implementation_nptl = false;
135 /* initialize the threads lists */
137 list_threads = list_create(OFFSET(threadobject, linkage));
138 list_threads_free = list_create(OFFSET(threadobject, linkage));
140 /* Initialize the threads implementation (sets the thinlock on the
143 threads_impl_preinit();
145 /* create internal thread data-structure for the main thread */
147 mainthread = threads_thread_new();
149 /* thread is a Java thread and running */
151 mainthread->flags = THREAD_FLAG_JAVA;
152 mainthread->state = THREAD_STATE_RUNNABLE;
154 /* store the internal thread data-structure in the TSD */
156 threads_set_current_threadobject(mainthread);
158 /* initialize locking subsystems */
162 /* initialize the critical section */
168 /* threads_list_first **********************************************************
170 Return the first entry in the threads list.
172 NOTE: This function does not lock the lists.
174 *******************************************************************************/
176 threadobject *threads_list_first(void)
180 t = list_first_unsynced(list_threads);
186 /* threads_list_next ***********************************************************
188 Return the next entry in the threads list.
190 NOTE: This function does not lock the lists.
192 *******************************************************************************/
194 threadobject *threads_list_next(threadobject *t)
198 next = list_next_unsynced(list_threads, t);
204 /* threads_list_get_non_daemons ************************************************
206 Return the number of non-daemon threads.
208 NOTE: This function does a linear-search over the threads list,
209 because it's only used for joining the threads.
211 *******************************************************************************/
213 s4 threads_list_get_non_daemons(void)
218 /* lock the threads lists */
224 for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
225 if (!(t->flags & THREAD_FLAG_DAEMON))
229 /* unlock the threads lists */
231 threads_list_unlock();
237 /* threads_thread_new **********************************************************
239 Allocates and initializes an internal thread data-structure and
240 adds it to the threads list.
242 *******************************************************************************/
244 threadobject *threads_thread_new(void)
248 /* lock the threads-lists */
252 /* try to get a thread from the free-list */
254 t = list_first_unsynced(list_threads_free);
256 /* is a free thread available? */
259 /* yes, remove it from the free list */
261 list_remove_unsynced(list_threads_free, t);
264 /* no, allocate a new one */
266 #if defined(ENABLE_GC_BOEHM)
267 t = GCNEW_UNCOLLECTABLE(threadobject, 1);
269 t = NEW(threadobject);
272 #if defined(ENABLE_STATISTICS)
274 size_threadobject += sizeof(threadobject);
279 MZERO(t, threadobject, 1);
281 /* set the threads-index */
283 t->index = list_threads->size + 1;
286 /* pre-compute the thinlock-word */
288 assert(t->index != 0);
290 t->thinlock = lock_pre_compute_thinlock(t->index);
291 t->state = THREAD_STATE_NEW;
293 /* initialize the implementation-specific bits */
295 threads_impl_thread_new(t);
297 /* add the thread to the threads-list */
299 list_add_last_unsynced(list_threads, t);
301 /* unlock the threads-lists */
303 threads_list_unlock();
309 /* threads_thread_free *********************************************************
311 Frees an internal thread data-structure by removing it from the
312 threads-list and adding it to the free-list.
314 NOTE: The data-structure is NOT freed, the pointer keeps valid!
316 *******************************************************************************/
318 void threads_thread_free(threadobject *t)
323 /* lock the threads-lists */
327 /* cleanup the implementation-specific bits */
329 threads_impl_thread_free(t);
331 /* remove the thread from the threads-list */
333 list_remove_unsynced(list_threads, t);
335 /* Clear memory, but keep the thread-index and the
338 /* ATTENTION: Do this after list_remove, otherwise the linkage
339 pointers are invalid. */
344 MZERO(t, threadobject, 1);
349 /* add the thread to the free list */
351 list_add_first_unsynced(list_threads_free, t);
353 /* unlock the threads-lists */
355 threads_list_unlock();
359 /* threads_thread_start_internal ***********************************************
361 Start an internal thread in the JVM. No Java thread objects exists
365 name.......UTF-8 name of the thread
366 f..........function pointer to C function to start
368 *******************************************************************************/
370 bool threads_thread_start_internal(utf *name, functionptr f)
373 java_lang_Thread *object;
374 #if defined(WITH_CLASSPATH_GNU)
375 java_lang_VMThread *vmt;
378 /* Enter the join-mutex, so if the main-thread is currently
379 waiting to join all threads, the number of non-daemon threads
382 threads_mutex_join_lock();
384 /* create internal thread data-structure */
386 t = threads_thread_new();
388 t->flags = THREAD_FLAG_INTERNAL | THREAD_FLAG_DAEMON;
390 /* The thread is flagged as (non-)daemon thread, we can leave the
393 threads_mutex_join_unlock();
395 /* create the java thread object */
397 object = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
399 /* XXX memory leak!!! */
403 #if defined(WITH_CLASSPATH_GNU)
404 vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
406 /* XXX memory leak!!! */
410 vmt->thread = object;
411 vmt->vmdata = (java_lang_Object *) t;
413 object->vmThread = vmt;
414 #elif defined(WITH_CLASSPATH_CLDC1_1)
415 object->vm_thread = (java_lang_Object *) t;
420 /* set java.lang.Thread fields */
422 #if defined(WITH_CLASSPATH_GNU)
423 object->name = (java_lang_String *) javastring_new(name);
424 #elif defined(WITH_CLASSPATH_CLDC1_1)
425 /* FIXME: In cldc the name is a char[] */
426 /* object->name = (java_chararray *) javastring_new(name); */
430 #if defined(ENABLE_JAVASE)
431 object->daemon = true;
434 object->priority = NORM_PRIORITY;
436 /* start the thread */
438 threads_impl_thread_start(t, f);
440 /* everything's ok */
446 /* threads_thread_start ********************************************************
448 Start a Java thread in the JVM. Only the java thread object exists
452 object.....the java thread object java.lang.Thread
454 *******************************************************************************/
456 void threads_thread_start(java_lang_Thread *object)
458 threadobject *thread;
460 /* Enter the join-mutex, so if the main-thread is currently
461 waiting to join all threads, the number of non-daemon threads
464 threads_mutex_join_lock();
466 /* create internal thread data-structure */
468 thread = threads_thread_new();
470 /* this is a normal Java thread */
472 thread->flags = THREAD_FLAG_JAVA;
474 #if defined(ENABLE_JAVASE)
475 /* is this a daemon thread? */
477 if (object->daemon == true)
478 thread->flags |= THREAD_FLAG_DAEMON;
481 /* The thread is flagged and (non-)daemon thread, we can leave the
484 threads_mutex_join_unlock();
486 /* link the two objects together */
488 thread->object = object;
490 #if defined(WITH_CLASSPATH_GNU)
491 assert(object->vmThread);
492 assert(object->vmThread->vmdata == NULL);
494 object->vmThread->vmdata = (java_lang_Object *) thread;
495 #elif defined(WITH_CLASSPATH_CLDC1_1)
496 object->vm_thread = (java_lang_Object *) thread;
499 /* Start the thread. Don't pass a function pointer (NULL) since
500 we want Thread.run()V here. */
502 threads_impl_thread_start(thread, NULL);
506 /* threads_thread_print_info ***************************************************
508 Print information of the passed thread.
510 *******************************************************************************/
512 void threads_thread_print_info(threadobject *t)
514 java_lang_Thread *object;
517 assert(t->state != THREAD_STATE_NEW);
519 /* the thread may be currently in initalization, don't print it */
523 if (object != NULL) {
524 /* get thread name */
526 #if defined(WITH_CLASSPATH_GNU)
527 name = javastring_toutf((java_objectheader *) object->name, false);
528 #elif defined(WITH_CLASSPATH_SUN) || defined(WITH_CLASSPATH_CLDC1_1)
529 /* FIXME: In cldc the name is a char[] */
530 /* name = object->name; */
533 # error unknown classpath configuration
537 utf_display_printable_ascii(name);
540 if (t->flags & THREAD_FLAG_DAEMON)
543 printf(" prio=%d", object->priority);
545 #if SIZEOF_VOID_P == 8
546 printf(" t=0x%016lx tid=0x%016lx (%ld)",
547 (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
549 printf(" t=0x%08x tid=0x%08x (%d)",
550 (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
553 printf(" index=%d", t->index);
555 /* print thread state */
558 case THREAD_STATE_NEW:
561 case THREAD_STATE_RUNNABLE:
564 case THREAD_STATE_BLOCKED:
567 case THREAD_STATE_WAITING:
570 case THREAD_STATE_TIMED_WAITING:
571 printf(" waiting on condition");
573 case THREAD_STATE_TERMINATED:
574 printf(" terminated");
577 vm_abort("threads_thread_print_info: unknown thread state %d",
584 /* threads_get_current_tid *****************************************************
586 Return the tid of the current thread.
591 *******************************************************************************/
593 ptrint threads_get_current_tid(void)
595 threadobject *thread;
597 thread = THREADOBJECT;
599 /* this may happen during bootstrap */
604 return (ptrint) thread->tid;
608 /* threads_thread_state_runnable ***********************************************
610 Set the current state of the given thread to THREAD_STATE_RUNNABLE.
612 *******************************************************************************/
614 void threads_thread_state_runnable(threadobject *t)
616 /* set the state inside the lock */
620 t->state = THREAD_STATE_RUNNABLE;
622 threads_list_unlock();
626 /* threads_thread_state_waiting ************************************************
628 Set the current state of the given thread to THREAD_STATE_WAITING.
630 *******************************************************************************/
632 void threads_thread_state_waiting(threadobject *t)
634 /* set the state in the lock */
638 t->state = THREAD_STATE_WAITING;
640 threads_list_unlock();
644 /* threads_thread_state_timed_waiting ******************************************
646 Set the current state of the given thread to
647 THREAD_STATE_TIMED_WAITING.
649 *******************************************************************************/
651 void threads_thread_state_timed_waiting(threadobject *t)
653 /* set the state in the lock */
657 t->state = THREAD_STATE_TIMED_WAITING;
659 threads_list_unlock();
663 /* threads_thread_state_terminated *********************************************
665 Set the current state of the given thread to
666 THREAD_STATE_TERMINATED.
668 *******************************************************************************/
670 void threads_thread_state_terminated(threadobject *t)
672 /* set the state in the lock */
676 t->state = THREAD_STATE_TERMINATED;
678 threads_list_unlock();
682 /* threads_thread_get_state ****************************************************
684 Returns the current state of the given thread.
686 *******************************************************************************/
688 utf *threads_thread_get_state(threadobject *t)
693 case THREAD_STATE_NEW:
694 u = utf_new_char("NEW");
696 case THREAD_STATE_RUNNABLE:
697 u = utf_new_char("RUNNABLE");
699 case THREAD_STATE_BLOCKED:
700 u = utf_new_char("BLOCKED");
702 case THREAD_STATE_WAITING:
703 u = utf_new_char("WAITING");
705 case THREAD_STATE_TIMED_WAITING:
706 u = utf_new_char("TIMED_WAITING");
708 case THREAD_STATE_TERMINATED:
709 u = utf_new_char("TERMINATED");
712 vm_abort("threads_get_state: unknown thread state %d", t->state);
714 /* keep compiler happy */
723 /* threads_thread_is_alive *****************************************************
725 Returns if the give thread is alive.
727 *******************************************************************************/
729 bool threads_thread_is_alive(threadobject *thread)
733 switch (thread->state) {
734 case THREAD_STATE_NEW:
735 case THREAD_STATE_TERMINATED:
739 case THREAD_STATE_RUNNABLE:
740 case THREAD_STATE_BLOCKED:
741 case THREAD_STATE_WAITING:
742 case THREAD_STATE_TIMED_WAITING:
747 vm_abort("threads_is_alive: unknown thread state %d", thread->state);
749 /* keep compiler happy */
758 /* threads_dump ****************************************************************
760 Dumps info for all threads running in the JVM. This function is
761 called when SIGQUIT (<ctrl>-\) is sent to CACAO.
763 *******************************************************************************/
765 void threads_dump(void)
769 /* XXX we should stop the world here */
771 /* lock the threads lists */
775 printf("Full thread dump CACAO "VERSION":\n");
777 /* iterate over all started threads */
779 for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
780 /* print thread info */
783 threads_thread_print_info(t);
786 /* print trace of thread */
788 threads_thread_print_stacktrace(t);
791 /* unlock the threads lists */
793 threads_list_unlock();
797 /* threads_thread_print_stacktrace *********************************************
799 Print the current stacktrace of the current thread.
801 *******************************************************************************/
803 void threads_thread_print_stacktrace(threadobject *thread)
806 stacktracebuffer *stb;
809 /* mark start of dump memory area */
811 dumpsize = dump_size();
813 /* create a stacktrace for the passed thread */
815 sfi = thread->_stackframeinfo;
817 stb = stacktrace_create(sfi);
819 /* print stacktrace */
822 stacktrace_print_trace_from_buffer(stb);
824 puts("\t<<No stacktrace available>>");
828 dump_release(dumpsize);
832 /* threads_print_stacktrace ****************************************************
834 Print the current stacktrace of the current thread.
836 *******************************************************************************/
838 void threads_print_stacktrace(void)
840 threadobject *thread;
842 thread = THREADOBJECT;
844 threads_thread_print_stacktrace(thread);
849 * These are local overrides for various environment variables in Emacs.
850 * Please do not remove this and leave it at the end of the file, where
851 * Emacs will automagically detect them.
852 * ---------------------------------------------------------------------
855 * indent-tabs-mode: t
859 * vim:noexpandtab:sw=4:ts=4: