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 8137 2007-06-22 16:41:36Z michi $
37 #include "mm/memory.h"
39 #include "native/jni.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"
63 #if defined(ENABLE_STATISTICS)
64 # include "vmcore/options.h"
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 */
77 static list_t *list_threads_free;
79 #if defined(__LINUX__)
80 /* XXX Remove for exact-GC. */
81 bool threads_pthreads_implementation_nptl;
85 /* threads_preinit *************************************************************
87 Do some early initialization of stuff required.
89 ATTENTION: Do NOT use any Java heap allocation here, as gc_init()
90 is called AFTER this function!
92 *******************************************************************************/
94 void threads_preinit(void)
96 threadobject *mainthread;
97 #if defined(__LINUX__)
102 #if defined(__LINUX__)
103 /* XXX Remove for exact-GC. */
105 /* On Linux we need to check the pthread implementation. */
107 /* _CS_GNU_LIBPTHREAD_VERSION (GNU C library only; since glibc 2.3.2) */
108 /* If the glibc is a pre-2.3.2 version, we fall back to
111 # if defined(_CS_GNU_LIBPTHREAD_VERSION)
112 len = confstr(_CS_GNU_LIBPTHREAD_VERSION, NULL, (size_t) 0);
114 /* Some systems return as length 0 (maybe cross-compilation
115 related). In this case we also fall back to linuxthreads. */
118 pathbuf = MNEW(char, len);
120 (void) confstr(_CS_GNU_LIBPTHREAD_VERSION, pathbuf, len);
122 if (strstr(pathbuf, "NPTL") != NULL)
123 threads_pthreads_implementation_nptl = true;
125 threads_pthreads_implementation_nptl = false;
128 threads_pthreads_implementation_nptl = false;
130 threads_pthreads_implementation_nptl = false;
134 /* initialize the threads lists */
136 list_threads = list_create(OFFSET(threadobject, linkage));
137 list_threads_free = list_create(OFFSET(threadobject, linkage));
139 /* Initialize the threads implementation (sets the thinlock on the
142 threads_impl_preinit();
144 /* create internal thread data-structure for the main thread */
146 mainthread = threads_thread_new();
148 /* thread is a Java thread and running */
150 mainthread->flags = THREAD_FLAG_JAVA;
151 mainthread->state = THREAD_STATE_RUNNABLE;
153 /* store the internal thread data-structure in the TSD */
155 threads_set_current_threadobject(mainthread);
157 /* initialize locking subsystems */
161 /* initialize the critical section */
167 /* threads_list_first **********************************************************
169 Return the first entry in the threads list.
171 NOTE: This function does not lock the lists.
173 *******************************************************************************/
175 threadobject *threads_list_first(void)
179 t = list_first_unsynced(list_threads);
185 /* threads_list_next ***********************************************************
187 Return the next entry in the threads list.
189 NOTE: This function does not lock the lists.
191 *******************************************************************************/
193 threadobject *threads_list_next(threadobject *t)
197 next = list_next_unsynced(list_threads, t);
203 /* threads_list_get_non_daemons ************************************************
205 Return the number of non-daemon threads.
207 NOTE: This function does a linear-search over the threads list,
208 because it's only used for joining the threads.
210 *******************************************************************************/
212 s4 threads_list_get_non_daemons(void)
217 /* lock the threads lists */
223 for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
224 if (!(t->flags & THREAD_FLAG_DAEMON))
228 /* unlock the threads lists */
230 threads_list_unlock();
236 /* threads_thread_new **********************************************************
238 Allocates and initializes an internal thread data-structure and
239 adds it to the threads list.
241 *******************************************************************************/
243 threadobject *threads_thread_new(void)
247 /* lock the threads-lists */
251 /* try to get a thread from the free-list */
253 t = list_first_unsynced(list_threads_free);
255 /* is a free thread available? */
258 /* yes, remove it from the free list */
260 list_remove_unsynced(list_threads_free, t);
263 /* no, allocate a new one */
265 #if defined(ENABLE_GC_BOEHM)
266 t = GCNEW_UNCOLLECTABLE(threadobject, 1);
268 t = NEW(threadobject);
271 #if defined(ENABLE_STATISTICS)
273 size_threadobject += sizeof(threadobject);
278 MZERO(t, threadobject, 1);
280 /* set the threads-index */
282 t->index = list_threads->size + 1;
284 #if defined(ENABLE_GC_CACAO)
285 /* register reference to java.lang.Thread with the GC */
287 gc_reference_register((java_objectheader **) &(t->object));
291 /* pre-compute the thinlock-word */
293 assert(t->index != 0);
295 t->thinlock = lock_pre_compute_thinlock(t->index);
297 t->state = THREAD_STATE_NEW;
299 #if defined(ENABLE_GC_CACAO)
300 t->flags |= THREAD_FLAG_IN_NATIVE;
303 /* initialize the implementation-specific bits */
305 threads_impl_thread_new(t);
307 /* add the thread to the threads-list */
309 list_add_last_unsynced(list_threads, t);
311 /* unlock the threads-lists */
313 threads_list_unlock();
319 /* threads_thread_free *********************************************************
321 Frees an internal thread data-structure by removing it from the
322 threads-list and adding it to the free-list.
324 NOTE: The data-structure is NOT freed, the pointer keeps valid!
326 *******************************************************************************/
328 void threads_thread_free(threadobject *t)
332 /* lock the threads-lists */
336 /* cleanup the implementation-specific bits */
338 threads_impl_thread_free(t);
340 /* remove the thread from the threads-list */
342 list_remove_unsynced(list_threads, t);
344 /* Clear memory, but keep the thread-index. */
345 /* ATTENTION: Do this after list_remove, otherwise the linkage
346 pointers are invalid. */
350 MZERO(t, threadobject, 1);
354 /* add the thread to the free list */
356 list_add_first_unsynced(list_threads_free, t);
358 /* unlock the threads-lists */
360 threads_list_unlock();
364 /* threads_thread_start_internal ***********************************************
366 Start an internal thread in the JVM. No Java thread objects exists
370 name.......UTF-8 name of the thread
371 f..........function pointer to C function to start
373 *******************************************************************************/
375 bool threads_thread_start_internal(utf *name, functionptr f)
378 java_lang_Thread *object;
379 #if defined(WITH_CLASSPATH_GNU)
380 java_lang_VMThread *vmt;
383 /* Enter the join-mutex, so if the main-thread is currently
384 waiting to join all threads, the number of non-daemon threads
387 threads_mutex_join_lock();
389 /* create internal thread data-structure */
391 t = threads_thread_new();
393 t->flags |= THREAD_FLAG_INTERNAL | THREAD_FLAG_DAEMON;
395 /* The thread is flagged as (non-)daemon thread, we can leave the
398 threads_mutex_join_unlock();
400 /* create the java thread object */
402 object = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
404 /* XXX memory leak!!! */
408 #if defined(WITH_CLASSPATH_GNU)
409 vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
411 /* XXX memory leak!!! */
415 vmt->thread = object;
416 vmt->vmdata = (java_lang_Object *) t;
418 object->vmThread = vmt;
419 #elif defined(WITH_CLASSPATH_CLDC1_1)
420 object->vm_thread = (java_lang_Object *) t;
425 /* set java.lang.Thread fields */
427 #if defined(WITH_CLASSPATH_GNU)
428 object->name = (java_lang_String *) javastring_new(name);
429 #elif defined(WITH_CLASSPATH_CLDC1_1)
430 /* FIXME: In cldc the name is a char[] */
431 /* object->name = (java_chararray *) javastring_new(name); */
435 #if defined(ENABLE_JAVASE)
436 object->daemon = true;
439 object->priority = NORM_PRIORITY;
441 /* start the thread */
443 threads_impl_thread_start(t, f);
445 /* everything's ok */
451 /* threads_thread_start ********************************************************
453 Start a Java thread in the JVM. Only the java thread object exists
457 object.....the java thread object java.lang.Thread
459 *******************************************************************************/
461 void threads_thread_start(java_lang_Thread *object)
463 threadobject *thread;
465 /* Enter the join-mutex, so if the main-thread is currently
466 waiting to join all threads, the number of non-daemon threads
469 threads_mutex_join_lock();
471 /* create internal thread data-structure */
473 thread = threads_thread_new();
475 /* this is a normal Java thread */
477 thread->flags |= THREAD_FLAG_JAVA;
479 #if defined(ENABLE_JAVASE)
480 /* is this a daemon thread? */
482 if (object->daemon == true)
483 thread->flags |= THREAD_FLAG_DAEMON;
486 /* The thread is flagged and (non-)daemon thread, we can leave the
489 threads_mutex_join_unlock();
491 /* link the two objects together */
493 thread->object = object;
495 #if defined(WITH_CLASSPATH_GNU)
496 assert(object->vmThread);
497 assert(object->vmThread->vmdata == NULL);
499 object->vmThread->vmdata = (java_lang_Object *) thread;
500 #elif defined(WITH_CLASSPATH_CLDC1_1)
501 object->vm_thread = (java_lang_Object *) thread;
504 /* Start the thread. Don't pass a function pointer (NULL) since
505 we want Thread.run()V here. */
507 threads_impl_thread_start(thread, NULL);
511 /* threads_thread_print_info ***************************************************
513 Print information of the passed thread.
515 *******************************************************************************/
517 void threads_thread_print_info(threadobject *t)
519 java_lang_Thread *object;
522 assert(t->state != THREAD_STATE_NEW);
524 /* the thread may be currently in initalization, don't print it */
528 if (object != NULL) {
529 /* get thread name */
531 #if defined(WITH_CLASSPATH_GNU)
532 name = javastring_toutf((java_objectheader *) object->name, false);
533 #elif defined(WITH_CLASSPATH_SUN) || defined(WITH_CLASSPATH_CLDC1_1)
534 /* FIXME: In cldc the name is a char[] */
535 /* name = object->name; */
538 # error unknown classpath configuration
542 utf_display_printable_ascii(name);
545 if (t->flags & THREAD_FLAG_DAEMON)
548 printf(" prio=%d", object->priority);
550 #if SIZEOF_VOID_P == 8
551 printf(" t=0x%016lx tid=0x%016lx (%ld)",
552 (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
554 printf(" t=0x%08x tid=0x%08x (%d)",
555 (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
558 printf(" index=%d", t->index);
560 /* print thread state */
563 case THREAD_STATE_NEW:
566 case THREAD_STATE_RUNNABLE:
569 case THREAD_STATE_BLOCKED:
572 case THREAD_STATE_WAITING:
575 case THREAD_STATE_TIMED_WAITING:
576 printf(" waiting on condition");
578 case THREAD_STATE_TERMINATED:
579 printf(" terminated");
582 vm_abort("threads_thread_print_info: unknown thread state %d",
589 /* threads_get_current_tid *****************************************************
591 Return the tid of the current thread.
596 *******************************************************************************/
598 ptrint threads_get_current_tid(void)
600 threadobject *thread;
602 thread = THREADOBJECT;
604 /* this may happen during bootstrap */
609 return (ptrint) thread->tid;
613 /* threads_thread_state_runnable ***********************************************
615 Set the current state of the given thread to THREAD_STATE_RUNNABLE.
617 *******************************************************************************/
619 void threads_thread_state_runnable(threadobject *t)
621 /* set the state inside the lock */
625 t->state = THREAD_STATE_RUNNABLE;
627 threads_list_unlock();
631 /* threads_thread_state_waiting ************************************************
633 Set the current state of the given thread to THREAD_STATE_WAITING.
635 *******************************************************************************/
637 void threads_thread_state_waiting(threadobject *t)
639 /* set the state in the lock */
643 t->state = THREAD_STATE_WAITING;
645 threads_list_unlock();
649 /* threads_thread_state_timed_waiting ******************************************
651 Set the current state of the given thread to
652 THREAD_STATE_TIMED_WAITING.
654 *******************************************************************************/
656 void threads_thread_state_timed_waiting(threadobject *t)
658 /* set the state in the lock */
662 t->state = THREAD_STATE_TIMED_WAITING;
664 threads_list_unlock();
668 /* threads_thread_state_terminated *********************************************
670 Set the current state of the given thread to
671 THREAD_STATE_TERMINATED.
673 *******************************************************************************/
675 void threads_thread_state_terminated(threadobject *t)
677 /* set the state in the lock */
681 t->state = THREAD_STATE_TERMINATED;
683 threads_list_unlock();
687 /* threads_thread_get_state ****************************************************
689 Returns the current state of the given thread.
691 *******************************************************************************/
693 utf *threads_thread_get_state(threadobject *t)
698 case THREAD_STATE_NEW:
699 u = utf_new_char("NEW");
701 case THREAD_STATE_RUNNABLE:
702 u = utf_new_char("RUNNABLE");
704 case THREAD_STATE_BLOCKED:
705 u = utf_new_char("BLOCKED");
707 case THREAD_STATE_WAITING:
708 u = utf_new_char("WAITING");
710 case THREAD_STATE_TIMED_WAITING:
711 u = utf_new_char("TIMED_WAITING");
713 case THREAD_STATE_TERMINATED:
714 u = utf_new_char("TERMINATED");
717 vm_abort("threads_get_state: unknown thread state %d", t->state);
719 /* keep compiler happy */
728 /* threads_thread_is_alive *****************************************************
730 Returns if the give thread is alive.
732 *******************************************************************************/
734 bool threads_thread_is_alive(threadobject *thread)
738 switch (thread->state) {
739 case THREAD_STATE_NEW:
740 case THREAD_STATE_TERMINATED:
744 case THREAD_STATE_RUNNABLE:
745 case THREAD_STATE_BLOCKED:
746 case THREAD_STATE_WAITING:
747 case THREAD_STATE_TIMED_WAITING:
752 vm_abort("threads_is_alive: unknown thread state %d", thread->state);
754 /* keep compiler happy */
763 /* threads_dump ****************************************************************
765 Dumps info for all threads running in the JVM. This function is
766 called when SIGQUIT (<ctrl>-\) is sent to CACAO.
768 *******************************************************************************/
770 void threads_dump(void)
774 /* XXX we should stop the world here */
776 /* lock the threads lists */
780 printf("Full thread dump CACAO "VERSION":\n");
782 /* iterate over all started threads */
784 for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
785 /* print thread info */
788 threads_thread_print_info(t);
791 /* print trace of thread */
793 threads_thread_print_stacktrace(t);
796 /* unlock the threads lists */
798 threads_list_unlock();
802 /* threads_thread_print_stacktrace *********************************************
804 Print the current stacktrace of the current thread.
806 *******************************************************************************/
808 void threads_thread_print_stacktrace(threadobject *thread)
811 stacktracebuffer *stb;
814 /* mark start of dump memory area */
816 dumpsize = dump_size();
818 /* create a stacktrace for the passed thread */
820 sfi = thread->_stackframeinfo;
822 stb = stacktrace_create(sfi);
824 /* print stacktrace */
827 stacktrace_print_trace_from_buffer(stb);
829 puts("\t<<No stacktrace available>>");
833 dump_release(dumpsize);
837 /* threads_print_stacktrace ****************************************************
839 Print the current stacktrace of the current thread.
841 *******************************************************************************/
843 void threads_print_stacktrace(void)
845 threadobject *thread;
847 thread = THREADOBJECT;
849 threads_thread_print_stacktrace(thread);
854 * These are local overrides for various environment variables in Emacs.
855 * Please do not remove this and leave it at the end of the file, where
856 * Emacs will automagically detect them.
857 * ---------------------------------------------------------------------
860 * indent-tabs-mode: t
864 * vim:noexpandtab:sw=4:ts=4: