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_Throwable.h"
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"
63 #include "vmcore/options.h"
65 #if defined(ENABLE_STATISTICS)
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 */
79 typedef struct thread_index_t {
84 static list_t *list_free_thread_index;
86 #if defined(__LINUX__)
87 /* XXX Remove for exact-GC. */
88 bool threads_pthreads_implementation_nptl;
92 /* threads_preinit *************************************************************
94 Do some early initialization of stuff required.
96 ATTENTION: Do NOT use any Java heap allocation here, as gc_init()
97 is called AFTER this function!
99 *******************************************************************************/
101 void threads_preinit(void)
103 threadobject *mainthread;
104 #if defined(__LINUX__) && defined(_CS_GNU_LIBPTHREAD_VERSION)
109 #if defined(__LINUX__)
110 /* XXX Remove for exact-GC. */
112 /* On Linux we need to check the pthread implementation. */
114 /* _CS_GNU_LIBPTHREAD_VERSION (GNU C library only; since glibc 2.3.2) */
115 /* If the glibc is a pre-2.3.2 version, we fall back to
118 # if defined(_CS_GNU_LIBPTHREAD_VERSION)
119 len = confstr(_CS_GNU_LIBPTHREAD_VERSION, NULL, (size_t) 0);
121 /* Some systems return as length 0 (maybe cross-compilation
122 related). In this case we also fall back to linuxthreads. */
125 pathbuf = MNEW(char, len);
127 (void) confstr(_CS_GNU_LIBPTHREAD_VERSION, pathbuf, len);
129 if (strstr(pathbuf, "NPTL") != NULL)
130 threads_pthreads_implementation_nptl = true;
132 threads_pthreads_implementation_nptl = false;
135 threads_pthreads_implementation_nptl = false;
137 threads_pthreads_implementation_nptl = false;
141 /* initialize the threads lists */
143 list_threads = list_create(OFFSET(threadobject, linkage));
144 list_free_thread_index = list_create(OFFSET(thread_index_t, linkage));
146 /* Initialize the threads implementation (sets the thinlock on the
149 threads_impl_preinit();
151 /* create internal thread data-structure for the main thread */
153 mainthread = threads_thread_new();
155 /* thread is a Java thread and running */
157 mainthread->flags |= THREAD_FLAG_JAVA;
158 mainthread->state = THREAD_STATE_RUNNABLE;
160 /* store the internal thread data-structure in the TSD */
162 threads_set_current_threadobject(mainthread);
164 /* initialize locking subsystems */
168 /* initialize the critical section */
174 /* threads_list_first **********************************************************
176 Return the first entry in the threads list.
178 NOTE: This function does not lock the lists.
180 *******************************************************************************/
182 threadobject *threads_list_first(void)
186 t = list_first_unsynced(list_threads);
192 /* threads_list_next ***********************************************************
194 Return the next entry in the threads list.
196 NOTE: This function does not lock the lists.
198 *******************************************************************************/
200 threadobject *threads_list_next(threadobject *t)
204 next = list_next_unsynced(list_threads, t);
210 /* threads_list_get_non_daemons ************************************************
212 Return the number of non-daemon threads.
214 NOTE: This function does a linear-search over the threads list,
215 because it's only used for joining the threads.
217 *******************************************************************************/
219 s4 threads_list_get_non_daemons(void)
224 /* lock the threads lists */
230 for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
231 if (!(t->flags & THREAD_FLAG_DAEMON))
235 /* unlock the threads lists */
237 threads_list_unlock();
243 /* threads_thread_new **********************************************************
245 Allocates and initializes an internal thread data-structure and
246 adds it to the threads list.
248 *******************************************************************************/
250 threadobject *threads_thread_new(void)
256 /* lock the threads-lists */
260 /* Try to get a thread index from the free-list. */
262 ti = list_first_unsynced(list_free_thread_index);
264 /* Is a free thread index available? */
267 /* Yes, remove it from the free list, get the index and free
270 list_remove_unsynced(list_free_thread_index, ti);
274 FREE(ti, thread_index_t);
276 #if defined(ENABLE_STATISTICS)
278 size_thread_index_t -= sizeof(thread_index_t);
282 /* Get a new the thread index. */
284 index = list_threads->size + 1;
287 /* Allocate a thread data structure. */
289 #if defined(ENABLE_GC_BOEHM)
290 t = GCNEW_UNCOLLECTABLE(threadobject, 1);
292 t = NEW(threadobject);
295 #if defined(ENABLE_STATISTICS)
297 size_threadobject += sizeof(threadobject);
302 MZERO(t, threadobject, 1);
304 #if defined(ENABLE_GC_CACAO)
305 /* Register reference to java.lang.Thread with the GC. */
307 gc_reference_register(&(t->object), GC_REFTYPE_THREADOBJECT);
308 gc_reference_register(&(t->_exceptionptr), GC_REFTYPE_THREADOBJECT);
311 /* Pre-compute the thinlock-word. */
316 t->thinlock = lock_pre_compute_thinlock(t->index);
318 t->state = THREAD_STATE_NEW;
320 #if defined(ENABLE_GC_CACAO)
321 t->flags |= THREAD_FLAG_IN_NATIVE;
324 /* Initialize the implementation-specific bits. */
326 threads_impl_thread_new(t);
328 /* Add the thread to the threads-list. */
330 list_add_last_unsynced(list_threads, t);
332 /* Unlock the threads-lists. */
334 threads_list_unlock();
340 /* threads_thread_free *********************************************************
342 Remove the thread from the threads-list and free the internal
343 thread data structure. The thread index is added to the
344 thread-index free-list.
347 t....thread data structure
349 *******************************************************************************/
351 void threads_thread_free(threadobject *t)
355 /* Lock the threads lists. */
359 /* Cleanup the implementation specific bits. */
361 threads_impl_thread_free(t);
363 /* Remove the thread from the threads-list. */
365 list_remove_unsynced(list_threads, t);
367 /* Add the thread index to the free list. */
369 ti = NEW(thread_index_t);
371 #if defined(ENABLE_STATISTICS)
373 size_thread_index_t += sizeof(thread_index_t);
376 ti->index = t->index;
378 list_add_last_unsynced(list_free_thread_index, ti);
380 /* Free the thread data structure. */
382 #if defined(ENABLE_GC_BOEHM)
385 FREE(t, threadobject);
388 #if defined(ENABLE_STATISTICS)
390 size_threadobject -= sizeof(threadobject);
393 /* Unlock the threads lists. */
395 threads_list_unlock();
399 /* threads_thread_start_internal ***********************************************
401 Start an internal thread in the JVM. No Java thread objects exists
405 name.......UTF-8 name of the thread
406 f..........function pointer to C function to start
408 *******************************************************************************/
410 bool threads_thread_start_internal(utf *name, functionptr f)
413 java_lang_Thread *object;
414 #if defined(WITH_CLASSPATH_GNU)
415 java_lang_VMThread *vmt;
418 /* Enter the join-mutex, so if the main-thread is currently
419 waiting to join all threads, the number of non-daemon threads
422 threads_mutex_join_lock();
424 /* create internal thread data-structure */
426 t = threads_thread_new();
428 t->flags |= THREAD_FLAG_INTERNAL | THREAD_FLAG_DAEMON;
430 /* The thread is flagged as (non-)daemon thread, we can leave the
433 threads_mutex_join_unlock();
435 /* create the java thread object */
437 object = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
439 /* XXX memory leak!!! */
443 #if defined(WITH_CLASSPATH_GNU)
444 vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
446 /* XXX memory leak!!! */
450 LLNI_field_set_ref(vmt, thread, object);
451 LLNI_field_set_val(vmt, vmdata, (java_lang_Object *) t);
453 LLNI_field_set_ref(object, vmThread, vmt);
454 #elif defined(WITH_CLASSPATH_CLDC1_1)
455 LLNI_field_set_val(object, vm_thread, (java_lang_Object *) t);
458 threads_thread_set_object(t, (java_handle_t *) object);
460 /* set java.lang.Thread fields */
462 #if defined(WITH_CLASSPATH_GNU)
463 LLNI_field_set_ref(object, name , (java_lang_String *) javastring_new(name));
464 #elif defined(WITH_CLASSPATH_CLDC1_1)
465 /* FIXME: In cldc the name is a char[] */
466 /* LLNI_field_set_ref(object, name , (java_chararray *) javastring_new(name)); */
467 LLNI_field_set_ref(object, name , NULL);
470 #if defined(ENABLE_JAVASE)
471 LLNI_field_set_val(object, daemon , true);
474 LLNI_field_set_val(object, priority, NORM_PRIORITY);
476 /* start the thread */
478 threads_impl_thread_start(t, f);
480 /* everything's ok */
486 /* threads_thread_start ********************************************************
488 Start a Java thread in the JVM. Only the java thread object exists
492 object.....the java thread object java.lang.Thread
494 *******************************************************************************/
496 void threads_thread_start(java_handle_t *object)
499 threadobject *thread;
500 #if defined(WITH_CLASSPATH_GNU)
501 java_lang_VMThread *vmt;
504 o = (java_lang_Thread *) object;
506 /* Enter the join-mutex, so if the main-thread is currently
507 waiting to join all threads, the number of non-daemon threads
510 threads_mutex_join_lock();
512 /* create internal thread data-structure */
514 thread = threads_thread_new();
516 /* this is a normal Java thread */
518 thread->flags |= THREAD_FLAG_JAVA;
520 #if defined(ENABLE_JAVASE)
521 /* is this a daemon thread? */
523 if (LLNI_field_direct(o, daemon) == true)
524 thread->flags |= THREAD_FLAG_DAEMON;
527 /* The thread is flagged and (non-)daemon thread, we can leave the
530 threads_mutex_join_unlock();
532 /* link the two objects together */
534 threads_thread_set_object(thread, object);
536 #if defined(WITH_CLASSPATH_GNU)
537 LLNI_field_get_ref(o, vmThread, vmt);
540 assert(LLNI_field_direct(vmt, vmdata) == NULL);
542 LLNI_field_set_val(vmt, vmdata, (java_lang_Object *) thread);
543 #elif defined(WITH_CLASSPATH_CLDC1_1)
544 LLNI_field_set_val(o, vm_thread, (java_lang_Object *) thread);
547 /* Start the thread. Don't pass a function pointer (NULL) since
548 we want Thread.run()V here. */
550 threads_impl_thread_start(thread, NULL);
554 /* threads_thread_print_info ***************************************************
556 Print information of the passed thread.
558 *******************************************************************************/
560 void threads_thread_print_info(threadobject *t)
562 java_lang_Thread *object;
563 #if defined(WITH_CLASSPATH_GNU)
564 java_lang_String *namestring;
568 assert(t->state != THREAD_STATE_NEW);
570 /* the thread may be currently in initalization, don't print it */
572 object = (java_lang_Thread *) threads_thread_get_object(t);
574 if (object != NULL) {
575 /* get thread name */
577 #if defined(WITH_CLASSPATH_GNU)
578 LLNI_field_get_ref(object, name, namestring);
579 name = javastring_toutf((java_handle_t *) namestring, false);
580 #elif defined(WITH_CLASSPATH_SUN) || defined(WITH_CLASSPATH_CLDC1_1)
581 /* FIXME: In cldc the name is a char[] */
582 /* name = object->name; */
585 # error unknown classpath configuration
589 utf_display_printable_ascii(name);
592 if (t->flags & THREAD_FLAG_DAEMON)
595 printf(" prio=%d", LLNI_field_direct(object, priority));
597 #if SIZEOF_VOID_P == 8
598 printf(" t=0x%016lx tid=0x%016lx (%ld)",
599 (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
601 printf(" t=0x%08x tid=0x%08x (%d)",
602 (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
605 printf(" index=%d", t->index);
607 /* print thread state */
610 case THREAD_STATE_NEW:
613 case THREAD_STATE_RUNNABLE:
616 case THREAD_STATE_BLOCKED:
619 case THREAD_STATE_WAITING:
622 case THREAD_STATE_TIMED_WAITING:
623 printf(" waiting on condition");
625 case THREAD_STATE_TERMINATED:
626 printf(" terminated");
629 vm_abort("threads_thread_print_info: unknown thread state %d",
636 /* threads_get_current_tid *****************************************************
638 Return the tid of the current thread.
643 *******************************************************************************/
645 ptrint threads_get_current_tid(void)
647 threadobject *thread;
649 thread = THREADOBJECT;
651 /* this may happen during bootstrap */
656 return (ptrint) thread->tid;
660 /* threads_thread_state_runnable ***********************************************
662 Set the current state of the given thread to THREAD_STATE_RUNNABLE.
664 NOTE: If the thread has already terminated, don't set the state.
665 This is important for threads_detach_thread.
667 *******************************************************************************/
669 void threads_thread_state_runnable(threadobject *t)
671 /* Set the state inside a lock. */
675 if (t->state != THREAD_STATE_TERMINATED)
676 t->state = THREAD_STATE_RUNNABLE;
678 DEBUGTHREADS("is RUNNABLE", t);
680 threads_list_unlock();
684 /* threads_thread_state_waiting ************************************************
686 Set the current state of the given thread to THREAD_STATE_WAITING.
688 NOTE: If the thread has already terminated, don't set the state.
689 This is important for threads_detach_thread.
691 *******************************************************************************/
693 void threads_thread_state_waiting(threadobject *t)
695 /* Set the state inside a lock. */
699 if (t->state != THREAD_STATE_TERMINATED)
700 t->state = THREAD_STATE_WAITING;
702 DEBUGTHREADS("is WAITING", t);
704 threads_list_unlock();
708 /* threads_thread_state_timed_waiting ******************************************
710 Set the current state of the given thread to
711 THREAD_STATE_TIMED_WAITING.
713 NOTE: If the thread has already terminated, don't set the state.
714 This is important for threads_detach_thread.
716 *******************************************************************************/
718 void threads_thread_state_timed_waiting(threadobject *t)
720 /* Set the state inside a lock. */
724 if (t->state != THREAD_STATE_TERMINATED)
725 t->state = THREAD_STATE_TIMED_WAITING;
727 DEBUGTHREADS("is TIMED_WAITING", t);
729 threads_list_unlock();
733 /* threads_thread_state_terminated *********************************************
735 Set the current state of the given thread to
736 THREAD_STATE_TERMINATED.
738 *******************************************************************************/
740 void threads_thread_state_terminated(threadobject *t)
742 /* set the state in the lock */
746 t->state = THREAD_STATE_TERMINATED;
748 DEBUGTHREADS("is TERMINATED", t);
750 threads_list_unlock();
754 /* threads_thread_get_state ****************************************************
756 Returns the current state of the given thread.
758 *******************************************************************************/
760 utf *threads_thread_get_state(threadobject *t)
765 case THREAD_STATE_NEW:
766 u = utf_new_char("NEW");
768 case THREAD_STATE_RUNNABLE:
769 u = utf_new_char("RUNNABLE");
771 case THREAD_STATE_BLOCKED:
772 u = utf_new_char("BLOCKED");
774 case THREAD_STATE_WAITING:
775 u = utf_new_char("WAITING");
777 case THREAD_STATE_TIMED_WAITING:
778 u = utf_new_char("TIMED_WAITING");
780 case THREAD_STATE_TERMINATED:
781 u = utf_new_char("TERMINATED");
784 vm_abort("threads_get_state: unknown thread state %d", t->state);
786 /* keep compiler happy */
795 /* threads_thread_is_alive *****************************************************
797 Returns if the give thread is alive.
799 *******************************************************************************/
801 bool threads_thread_is_alive(threadobject *t)
804 case THREAD_STATE_NEW:
805 case THREAD_STATE_TERMINATED:
808 case THREAD_STATE_RUNNABLE:
809 case THREAD_STATE_BLOCKED:
810 case THREAD_STATE_WAITING:
811 case THREAD_STATE_TIMED_WAITING:
815 vm_abort("threads_thread_is_alive: unknown thread state %d", t->state);
818 /* keep compiler happy */
824 /* threads_dump ****************************************************************
826 Dumps info for all threads running in the JVM. This function is
827 called when SIGQUIT (<ctrl>-\) is sent to CACAO.
829 *******************************************************************************/
831 void threads_dump(void)
835 /* XXX we should stop the world here */
837 /* lock the threads lists */
841 printf("Full thread dump CACAO "VERSION":\n");
843 /* iterate over all started threads */
845 for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
846 /* print thread info */
849 threads_thread_print_info(t);
852 /* print trace of thread */
854 threads_thread_print_stacktrace(t);
857 /* unlock the threads lists */
859 threads_list_unlock();
863 /* threads_thread_print_stacktrace *********************************************
865 Print the current stacktrace of the current thread.
867 *******************************************************************************/
869 void threads_thread_print_stacktrace(threadobject *thread)
872 stacktracebuffer *stb;
875 /* mark start of dump memory area */
877 dumpsize = dump_size();
879 /* create a stacktrace for the passed thread */
881 sfi = thread->_stackframeinfo;
883 stb = stacktrace_create(sfi);
885 /* print stacktrace */
888 stacktrace_print_trace_from_buffer(stb);
890 puts("\t<<No stacktrace available>>");
894 dump_release(dumpsize);
898 /* threads_print_stacktrace ****************************************************
900 Print the current stacktrace of the current thread.
902 *******************************************************************************/
904 void threads_print_stacktrace(void)
906 threadobject *thread;
908 thread = THREADOBJECT;
910 threads_thread_print_stacktrace(thread);
915 * These are local overrides for various environment variables in Emacs.
916 * Please do not remove this and leave it at the end of the file, where
917 * Emacs will automagically detect them.
918 * ---------------------------------------------------------------------
921 * indent-tabs-mode: t
925 * vim:noexpandtab:sw=4:ts=4: