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_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 */
78 typedef struct thread_index_t {
83 static list_t *list_free_thread_index;
85 #if defined(__LINUX__)
86 /* XXX Remove for exact-GC. */
87 bool threads_pthreads_implementation_nptl;
91 /* threads_preinit *************************************************************
93 Do some early initialization of stuff required.
95 ATTENTION: Do NOT use any Java heap allocation here, as gc_init()
96 is called AFTER this function!
98 *******************************************************************************/
100 void threads_preinit(void)
102 threadobject *mainthread;
103 #if defined(__LINUX__) && defined(_CS_GNU_LIBPTHREAD_VERSION)
108 #if defined(__LINUX__)
109 /* XXX Remove for exact-GC. */
111 /* On Linux we need to check the pthread implementation. */
113 /* _CS_GNU_LIBPTHREAD_VERSION (GNU C library only; since glibc 2.3.2) */
114 /* If the glibc is a pre-2.3.2 version, we fall back to
117 # if defined(_CS_GNU_LIBPTHREAD_VERSION)
118 len = confstr(_CS_GNU_LIBPTHREAD_VERSION, NULL, (size_t) 0);
120 /* Some systems return as length 0 (maybe cross-compilation
121 related). In this case we also fall back to linuxthreads. */
124 pathbuf = MNEW(char, len);
126 (void) confstr(_CS_GNU_LIBPTHREAD_VERSION, pathbuf, len);
128 if (strstr(pathbuf, "NPTL") != NULL)
129 threads_pthreads_implementation_nptl = true;
131 threads_pthreads_implementation_nptl = false;
134 threads_pthreads_implementation_nptl = false;
136 threads_pthreads_implementation_nptl = false;
140 /* initialize the threads lists */
142 list_threads = list_create(OFFSET(threadobject, linkage));
143 list_free_thread_index = list_create(OFFSET(thread_index_t, linkage));
145 /* Initialize the threads implementation (sets the thinlock on the
148 threads_impl_preinit();
150 /* create internal thread data-structure for the main thread */
152 mainthread = threads_thread_new();
154 /* thread is a Java thread and running */
156 mainthread->flags |= THREAD_FLAG_JAVA;
157 mainthread->state = THREAD_STATE_RUNNABLE;
159 /* store the internal thread data-structure in the TSD */
161 threads_set_current_threadobject(mainthread);
163 /* initialize locking subsystems */
167 /* initialize the critical section */
173 /* threads_list_first **********************************************************
175 Return the first entry in the threads list.
177 NOTE: This function does not lock the lists.
179 *******************************************************************************/
181 threadobject *threads_list_first(void)
185 t = list_first_unsynced(list_threads);
191 /* threads_list_next ***********************************************************
193 Return the next entry in the threads list.
195 NOTE: This function does not lock the lists.
197 *******************************************************************************/
199 threadobject *threads_list_next(threadobject *t)
203 next = list_next_unsynced(list_threads, t);
209 /* threads_list_get_non_daemons ************************************************
211 Return the number of non-daemon threads.
213 NOTE: This function does a linear-search over the threads list,
214 because it's only used for joining the threads.
216 *******************************************************************************/
218 s4 threads_list_get_non_daemons(void)
223 /* lock the threads lists */
229 for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
230 if (!(t->flags & THREAD_FLAG_DAEMON))
234 /* unlock the threads lists */
236 threads_list_unlock();
242 /* threads_thread_new **********************************************************
244 Allocates and initializes an internal thread data-structure and
245 adds it to the threads list.
247 *******************************************************************************/
249 threadobject *threads_thread_new(void)
255 /* lock the threads-lists */
259 /* Try to get a thread index from the free-list. */
261 ti = list_first_unsynced(list_free_thread_index);
263 /* Is a free thread index available? */
266 /* Yes, remove it from the free list, get the index and free
269 list_remove_unsynced(list_free_thread_index, ti);
273 FREE(ti, thread_index_t);
275 #if defined(ENABLE_STATISTICS)
277 size_thread_index_t -= sizeof(thread_index_t);
281 /* Get a new the thread index. */
283 index = list_threads->size + 1;
286 /* Allocate a thread data structure. */
288 #if defined(ENABLE_GC_BOEHM)
289 t = GCNEW_UNCOLLECTABLE(threadobject, 1);
291 t = NEW(threadobject);
294 #if defined(ENABLE_STATISTICS)
296 size_threadobject += sizeof(threadobject);
301 MZERO(t, threadobject, 1);
303 #if defined(ENABLE_GC_CACAO)
304 /* Register reference to java.lang.Thread with the GC. */
306 gc_reference_register((java_object_t **) &(t->object), GC_REFTYPE_THREADOBJECT);
309 /* Pre-compute the thinlock-word. */
314 t->thinlock = lock_pre_compute_thinlock(t->index);
316 t->state = THREAD_STATE_NEW;
318 #if defined(ENABLE_GC_CACAO)
319 t->flags |= THREAD_FLAG_IN_NATIVE;
322 /* Initialize the implementation-specific bits. */
324 threads_impl_thread_new(t);
326 /* Add the thread to the threads-list. */
328 list_add_last_unsynced(list_threads, t);
330 /* Unlock the threads-lists. */
332 threads_list_unlock();
338 /* threads_thread_free *********************************************************
340 Remove the thread from the threads-list and free the internal
341 thread data structure. The thread index is added to the
342 thread-index free-list.
345 t....thread data structure
347 *******************************************************************************/
349 void threads_thread_free(threadobject *t)
353 /* Lock the threads lists. */
357 /* Cleanup the implementation specific bits. */
359 threads_impl_thread_free(t);
361 /* Remove the thread from the threads-list. */
363 list_remove_unsynced(list_threads, t);
365 /* Add the thread index to the free list. */
367 ti = NEW(thread_index_t);
369 #if defined(ENABLE_STATISTICS)
371 size_thread_index_t += sizeof(thread_index_t);
374 ti->index = t->index;
376 list_add_last_unsynced(list_free_thread_index, ti);
378 /* Free the thread data structure. */
380 #if defined(ENABLE_GC_BOEHM)
383 FREE(t, threadobject);
386 #if defined(ENABLE_STATISTICS)
388 size_threadobject -= sizeof(threadobject);
391 /* Unlock the threads lists. */
393 threads_list_unlock();
397 /* threads_thread_start_internal ***********************************************
399 Start an internal thread in the JVM. No Java thread objects exists
403 name.......UTF-8 name of the thread
404 f..........function pointer to C function to start
406 *******************************************************************************/
408 bool threads_thread_start_internal(utf *name, functionptr f)
411 java_lang_Thread *object;
412 #if defined(WITH_CLASSPATH_GNU)
413 java_lang_VMThread *vmt;
416 /* Enter the join-mutex, so if the main-thread is currently
417 waiting to join all threads, the number of non-daemon threads
420 threads_mutex_join_lock();
422 /* create internal thread data-structure */
424 t = threads_thread_new();
426 t->flags |= THREAD_FLAG_INTERNAL | THREAD_FLAG_DAEMON;
428 /* The thread is flagged as (non-)daemon thread, we can leave the
431 threads_mutex_join_unlock();
433 /* create the java thread object */
435 object = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
437 /* XXX memory leak!!! */
441 #if defined(WITH_CLASSPATH_GNU)
442 vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
444 /* XXX memory leak!!! */
448 LLNI_field_set_ref(vmt, thread, object);
449 LLNI_field_set_val(vmt, vmdata, (java_lang_Object *) t);
451 LLNI_field_set_ref(object, vmThread, vmt);
452 #elif defined(WITH_CLASSPATH_CLDC1_1)
453 LLNI_field_set_val(object, vm_thread, (java_lang_Object *) t);
456 t->object = LLNI_DIRECT(object);
458 /* set java.lang.Thread fields */
460 #if defined(WITH_CLASSPATH_GNU)
461 LLNI_field_set_ref(object, name , (java_lang_String *) javastring_new(name));
462 #elif defined(WITH_CLASSPATH_CLDC1_1)
463 /* FIXME: In cldc the name is a char[] */
464 /* LLNI_field_set_ref(object, name , (java_chararray *) javastring_new(name)); */
465 LLNI_field_set_ref(object, name , NULL);
468 #if defined(ENABLE_JAVASE)
469 LLNI_field_set_val(object, daemon , true);
472 LLNI_field_set_val(object, priority, NORM_PRIORITY);
474 /* start the thread */
476 threads_impl_thread_start(t, f);
478 /* everything's ok */
484 /* threads_thread_start ********************************************************
486 Start a Java thread in the JVM. Only the java thread object exists
490 object.....the java thread object java.lang.Thread
492 *******************************************************************************/
494 void threads_thread_start(java_handle_t *object)
497 threadobject *thread;
498 #if defined(WITH_CLASSPATH_GNU)
499 java_lang_VMThread *vmt;
502 o = (java_lang_Thread *) object;
504 /* Enter the join-mutex, so if the main-thread is currently
505 waiting to join all threads, the number of non-daemon threads
508 threads_mutex_join_lock();
510 /* create internal thread data-structure */
512 thread = threads_thread_new();
514 /* this is a normal Java thread */
516 thread->flags |= THREAD_FLAG_JAVA;
518 #if defined(ENABLE_JAVASE)
519 /* is this a daemon thread? */
521 if (LLNI_field_direct(o, daemon) == true)
522 thread->flags |= THREAD_FLAG_DAEMON;
525 /* The thread is flagged and (non-)daemon thread, we can leave the
528 threads_mutex_join_unlock();
530 /* link the two objects together */
532 thread->object = LLNI_DIRECT(object);
534 #if defined(WITH_CLASSPATH_GNU)
535 LLNI_field_get_ref(o, vmThread, vmt);
538 assert(LLNI_field_direct(vmt, vmdata) == NULL);
540 LLNI_field_set_val(vmt, vmdata, (java_lang_Object *) thread);
541 #elif defined(WITH_CLASSPATH_CLDC1_1)
542 LLNI_field_set_val(o, vm_thread, (java_lang_Object *) thread);
545 /* Start the thread. Don't pass a function pointer (NULL) since
546 we want Thread.run()V here. */
548 threads_impl_thread_start(thread, NULL);
552 /* threads_thread_print_info ***************************************************
554 Print information of the passed thread.
556 *******************************************************************************/
558 void threads_thread_print_info(threadobject *t)
560 java_lang_Thread *object;
561 #if defined(WITH_CLASSPATH_GNU)
562 java_lang_String *namestring;
566 assert(t->state != THREAD_STATE_NEW);
568 /* the thread may be currently in initalization, don't print it */
570 object = (java_lang_Thread *) LLNI_WRAP(t->object);
572 if (object != NULL) {
573 /* get thread name */
575 #if defined(WITH_CLASSPATH_GNU)
576 LLNI_field_get_ref(object, name, namestring);
577 name = javastring_toutf((java_handle_t *) namestring, false);
578 #elif defined(WITH_CLASSPATH_SUN) || defined(WITH_CLASSPATH_CLDC1_1)
579 /* FIXME: In cldc the name is a char[] */
580 /* name = object->name; */
583 # error unknown classpath configuration
587 utf_display_printable_ascii(name);
590 if (t->flags & THREAD_FLAG_DAEMON)
593 printf(" prio=%d", LLNI_field_direct(object, priority));
595 #if SIZEOF_VOID_P == 8
596 printf(" t=0x%016lx tid=0x%016lx (%ld)",
597 (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
599 printf(" t=0x%08x tid=0x%08x (%d)",
600 (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
603 printf(" index=%d", t->index);
605 /* print thread state */
608 case THREAD_STATE_NEW:
611 case THREAD_STATE_RUNNABLE:
614 case THREAD_STATE_BLOCKED:
617 case THREAD_STATE_WAITING:
620 case THREAD_STATE_TIMED_WAITING:
621 printf(" waiting on condition");
623 case THREAD_STATE_TERMINATED:
624 printf(" terminated");
627 vm_abort("threads_thread_print_info: unknown thread state %d",
634 /* threads_get_current_tid *****************************************************
636 Return the tid of the current thread.
641 *******************************************************************************/
643 ptrint threads_get_current_tid(void)
645 threadobject *thread;
647 thread = THREADOBJECT;
649 /* this may happen during bootstrap */
654 return (ptrint) thread->tid;
658 /* threads_thread_state_runnable ***********************************************
660 Set the current state of the given thread to THREAD_STATE_RUNNABLE.
662 NOTE: If the thread has already terminated, don't set the state.
663 This is important for threads_detach_thread.
665 *******************************************************************************/
667 void threads_thread_state_runnable(threadobject *t)
669 /* Set the state inside a lock. */
673 if (t->state != THREAD_STATE_TERMINATED)
674 t->state = THREAD_STATE_RUNNABLE;
676 threads_list_unlock();
680 /* threads_thread_state_waiting ************************************************
682 Set the current state of the given thread to THREAD_STATE_WAITING.
684 NOTE: If the thread has already terminated, don't set the state.
685 This is important for threads_detach_thread.
687 *******************************************************************************/
689 void threads_thread_state_waiting(threadobject *t)
691 /* Set the state inside a lock. */
695 if (t->state != THREAD_STATE_TERMINATED)
696 t->state = THREAD_STATE_WAITING;
698 threads_list_unlock();
702 /* threads_thread_state_timed_waiting ******************************************
704 Set the current state of the given thread to
705 THREAD_STATE_TIMED_WAITING.
707 NOTE: If the thread has already terminated, don't set the state.
708 This is important for threads_detach_thread.
710 *******************************************************************************/
712 void threads_thread_state_timed_waiting(threadobject *t)
714 /* Set the state inside a lock. */
718 if (t->state != THREAD_STATE_TERMINATED)
719 t->state = THREAD_STATE_TIMED_WAITING;
721 threads_list_unlock();
725 /* threads_thread_state_terminated *********************************************
727 Set the current state of the given thread to
728 THREAD_STATE_TERMINATED.
730 *******************************************************************************/
732 void threads_thread_state_terminated(threadobject *t)
734 /* set the state in the lock */
738 t->state = THREAD_STATE_TERMINATED;
740 threads_list_unlock();
744 /* threads_thread_get_state ****************************************************
746 Returns the current state of the given thread.
748 *******************************************************************************/
750 utf *threads_thread_get_state(threadobject *t)
755 case THREAD_STATE_NEW:
756 u = utf_new_char("NEW");
758 case THREAD_STATE_RUNNABLE:
759 u = utf_new_char("RUNNABLE");
761 case THREAD_STATE_BLOCKED:
762 u = utf_new_char("BLOCKED");
764 case THREAD_STATE_WAITING:
765 u = utf_new_char("WAITING");
767 case THREAD_STATE_TIMED_WAITING:
768 u = utf_new_char("TIMED_WAITING");
770 case THREAD_STATE_TERMINATED:
771 u = utf_new_char("TERMINATED");
774 vm_abort("threads_get_state: unknown thread state %d", t->state);
776 /* keep compiler happy */
785 /* threads_thread_is_alive *****************************************************
787 Returns if the give thread is alive.
789 *******************************************************************************/
791 bool threads_thread_is_alive(threadobject *t)
794 case THREAD_STATE_NEW:
795 case THREAD_STATE_TERMINATED:
798 case THREAD_STATE_RUNNABLE:
799 case THREAD_STATE_BLOCKED:
800 case THREAD_STATE_WAITING:
801 case THREAD_STATE_TIMED_WAITING:
805 vm_abort("threads_thread_is_alive: unknown thread state %d", t->state);
808 /* keep compiler happy */
814 /* threads_dump ****************************************************************
816 Dumps info for all threads running in the JVM. This function is
817 called when SIGQUIT (<ctrl>-\) is sent to CACAO.
819 *******************************************************************************/
821 void threads_dump(void)
825 /* XXX we should stop the world here */
827 /* lock the threads lists */
831 printf("Full thread dump CACAO "VERSION":\n");
833 /* iterate over all started threads */
835 for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
836 /* print thread info */
839 threads_thread_print_info(t);
842 /* print trace of thread */
844 threads_thread_print_stacktrace(t);
847 /* unlock the threads lists */
849 threads_list_unlock();
853 /* threads_thread_print_stacktrace *********************************************
855 Print the current stacktrace of the current thread.
857 *******************************************************************************/
859 void threads_thread_print_stacktrace(threadobject *thread)
862 stacktracebuffer *stb;
865 /* mark start of dump memory area */
867 dumpsize = dump_size();
869 /* create a stacktrace for the passed thread */
871 sfi = thread->_stackframeinfo;
873 stb = stacktrace_create(sfi);
875 /* print stacktrace */
878 stacktrace_print_trace_from_buffer(stb);
880 puts("\t<<No stacktrace available>>");
884 dump_release(dumpsize);
888 /* threads_print_stacktrace ****************************************************
890 Print the current stacktrace of the current thread.
892 *******************************************************************************/
894 void threads_print_stacktrace(void)
896 threadobject *thread;
898 thread = THREADOBJECT;
900 threads_thread_print_stacktrace(thread);
905 * These are local overrides for various environment variables in Emacs.
906 * Please do not remove this and leave it at the end of the file, where
907 * Emacs will automagically detect them.
908 * ---------------------------------------------------------------------
911 * indent-tabs-mode: t
915 * vim:noexpandtab:sw=4:ts=4: