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: signal.c 7246 2007-01-29 18:49:05Z twisti $
33 #include "native/jni.h"
35 #include "native/include/java_lang_Object.h"
36 #include "native/include/java_lang_String.h"
37 #include "native/include/java_lang_Thread.h"
39 #if defined(WITH_CLASSPATH_GNU)
40 # include "native/include/java_lang_VMThread.h"
43 #include "threads/threads-common.h"
45 #include "threads/native/threads.h"
47 #include "vm/builtin.h"
48 #include "vm/stringlocal.h"
51 #include "vm/jit/stacktrace.h"
53 #include "vmcore/class.h"
54 #include "vmcore/utf8.h"
57 /* threads_create_thread *******************************************************
59 Creates a thread object with the given name.
61 *******************************************************************************/
63 threadobject *threads_create_thread(utf *name)
67 #if defined(WITH_CLASSPATH_GNU)
68 java_lang_VMThread *vmt;
71 /* create the vm internal thread object */
73 thread = NEW(threadobject);
78 /* create the java thread object */
80 t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
85 #if defined(WITH_CLASSPATH_GNU)
86 vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
92 vmt->vmdata = (java_lang_Object *) thread;
95 #elif defined(WITH_CLASSPATH_CLDC1_1)
96 t->vm_thread = (java_lang_Object *) thread;
100 thread->flags = THREAD_FLAG_DAEMON;
102 /* set java.lang.Thread fields */
104 t->name = (java_lang_String *) javastring_new(name);
105 #if defined(ENABLE_JAVASE)
108 t->priority = NORM_PRIORITY;
110 /* return the thread object */
116 /* threads_get_current_tid *****************************************************
118 Return the tid of the current thread.
123 *******************************************************************************/
125 ptrint threads_get_current_tid(void)
127 threadobject *thread;
129 thread = THREADOBJECT;
131 /* this may happen during bootstrap */
136 return (ptrint) thread->tid;
140 /* threads_thread_get_state ****************************************************
142 Returns the current state of the given thread.
144 *******************************************************************************/
146 utf *threads_thread_get_state(threadobject *thread)
150 switch (thread->state) {
151 case THREAD_STATE_NEW:
152 u = utf_new_char("NEW");
154 case THREAD_STATE_RUNNABLE:
155 u = utf_new_char("RUNNABLE");
157 case THREAD_STATE_BLOCKED:
158 u = utf_new_char("BLOCKED");
160 case THREAD_STATE_WAITING:
161 u = utf_new_char("WAITING");
163 case THREAD_STATE_TIMED_WAITING:
164 u = utf_new_char("TIMED_WAITING");
166 case THREAD_STATE_TERMINATED:
167 u = utf_new_char("TERMINATED");
170 vm_abort("threads_get_state: unknown thread state %d", thread->state);
177 /* threads_thread_is_alive *****************************************************
179 Returns if the give thread is alive.
181 *******************************************************************************/
183 bool threads_thread_is_alive(threadobject *thread)
187 switch (thread->state) {
188 case THREAD_STATE_NEW:
189 case THREAD_STATE_TERMINATED:
193 case THREAD_STATE_RUNNABLE:
194 case THREAD_STATE_BLOCKED:
195 case THREAD_STATE_WAITING:
196 case THREAD_STATE_TIMED_WAITING:
201 vm_abort("threads_is_alive: unknown thread state %d", thread->state);
208 /* threads_dump ****************************************************************
210 Dumps info for all threads running in the JVM. This function is
211 called when SIGQUIT (<ctrl>-\) is sent to CACAO.
213 *******************************************************************************/
215 void threads_dump(void)
217 threadobject *thread;
221 thread = mainthreadobj;
223 /* XXX we should stop the world here */
225 printf("Full thread dump CACAO "VERSION":\n");
227 /* iterate over all started threads */
230 /* get thread object */
234 /* the thread may be currently in initalization, don't print it */
237 /* get thread name */
239 #if defined(ENABLE_JAVASE)
240 name = javastring_toutf((java_objectheader *) t->name, false);
241 #elif defined(ENABLE_JAVAME_CLDC1_1)
246 utf_display_printable_ascii(name);
249 if (thread->flags & THREAD_FLAG_DAEMON)
252 printf(" prio=%d", t->priority);
254 #if SIZEOF_VOID_P == 8
255 printf(" tid=0x%016lx", (ptrint) thread->tid);
257 printf(" tid=0x%08lx", (ptrint) thread->tid);
260 /* print thread state */
262 switch (thread->state) {
263 case THREAD_STATE_NEW:
266 case THREAD_STATE_RUNNABLE:
269 case THREAD_STATE_BLOCKED:
272 case THREAD_STATE_WAITING:
275 case THREAD_STATE_TIMED_WAITING:
276 printf(" waiting on condition");
278 case THREAD_STATE_TERMINATED:
279 printf(" terminated");
282 vm_abort("threads_dump: unknown thread state %d",
288 /* print trace of thread */
290 threads_print_stacktrace(thread);
293 thread = thread->next;
294 } while ((thread != NULL) && (thread != mainthreadobj));
298 /* threads_print_stacktrace ****************************************************
300 Print the current stacktrace of the given thread.
302 *******************************************************************************/
304 void threads_print_stacktrace(threadobject *thread)
307 stacktracebuffer *stb;
310 /* mark start of dump memory area */
312 dumpsize = dump_size();
314 /* create a stacktrace for the passed thread */
316 sfi = thread->_stackframeinfo;
318 stb = stacktrace_create(sfi);
320 /* print stacktrace */
323 stacktrace_print_trace_from_buffer(stb);
325 puts("\t<<No stacktrace available>>");
329 dump_release(dumpsize);
334 * These are local overrides for various environment variables in Emacs.
335 * Please do not remove this and leave it at the end of the file, where
336 * Emacs will automagically detect them.
337 * ---------------------------------------------------------------------
340 * indent-tabs-mode: t
344 * vim:noexpandtab:sw=4:ts=4: