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 $
36 #include "native/jni.h"
38 #include "native/include/java_lang_Object.h"
39 #include "native/include/java_lang_String.h"
40 #include "native/include/java_lang_Thread.h"
42 #if defined(WITH_CLASSPATH_GNU)
43 # include "native/include/java_lang_VMThread.h"
46 #include "threads/threads-common.h"
48 #include "threads/native/threads.h"
50 #include "vm/builtin.h"
51 #include "vm/stringlocal.h"
54 #include "vm/jit/stacktrace.h"
56 #include "vmcore/class.h"
58 #if defined(ENABLE_STATISTICS)
59 # include "vmcore/options.h"
60 # include "vmcore/statistics.h"
63 #include "vmcore/utf8.h"
66 /* threads_create_thread *******************************************************
68 Creates a thread object with the given name.
70 *******************************************************************************/
72 threadobject *threads_create_thread(utf *name)
76 #if defined(WITH_CLASSPATH_GNU)
77 java_lang_VMThread *vmt;
80 /* create the vm internal thread object */
82 thread = NEW(threadobject);
87 /* create the java thread object */
89 t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
94 #if defined(WITH_CLASSPATH_GNU)
95 vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
101 vmt->vmdata = (java_lang_Object *) thread;
104 #elif defined(WITH_CLASSPATH_CLDC1_1)
105 t->vm_thread = (java_lang_Object *) thread;
109 thread->flags = THREAD_FLAG_DAEMON;
111 /* set java.lang.Thread fields */
113 t->name = (java_lang_String *) javastring_new(name);
114 #if defined(ENABLE_JAVASE)
117 t->priority = NORM_PRIORITY;
119 /* return the thread object */
125 /* threads_start_javathread ***************************************************
127 Start a thread in the JVM. Only the java thread object exists so far.
130 object.....the java thread object java.lang.Thread
132 ******************************************************************************/
134 void threads_start_javathread(java_lang_Thread *object)
136 threadobject *thread;
138 /* create the vm internal threadobject */
140 thread = NEW(threadobject);
142 #if defined(ENABLE_STATISTICS)
144 size_threadobject += sizeof(threadobject);
147 /* link the two objects together */
149 thread->object = object;
151 #if defined(ENABLE_JAVASE)
152 /* is this a daemon thread? */
154 if (object->daemon == true)
155 thread->flags |= THREAD_FLAG_DAEMON;
158 #if defined(WITH_CLASSPATH_GNU)
159 assert(object->vmThread);
160 assert(object->vmThread->vmdata == NULL);
162 object->vmThread->vmdata = (java_lang_Object *) thread;
163 #elif defined(WITH_CLASSPATH_CLDC1_1)
164 object->vm_thread = (java_lang_Object *) thread;
167 /* Actually start the thread. Don't pass a function pointer
168 (NULL) since we want Thread.run()V here. */
170 threads_start_thread(thread, NULL);
174 /* threads_get_current_tid *****************************************************
176 Return the tid of the current thread.
181 *******************************************************************************/
183 ptrint threads_get_current_tid(void)
185 threadobject *thread;
187 thread = THREADOBJECT;
189 /* this may happen during bootstrap */
194 return (ptrint) thread->tid;
198 /* threads_thread_get_state ****************************************************
200 Returns the current state of the given thread.
202 *******************************************************************************/
204 utf *threads_thread_get_state(threadobject *thread)
208 switch (thread->state) {
209 case THREAD_STATE_NEW:
210 u = utf_new_char("NEW");
212 case THREAD_STATE_RUNNABLE:
213 u = utf_new_char("RUNNABLE");
215 case THREAD_STATE_BLOCKED:
216 u = utf_new_char("BLOCKED");
218 case THREAD_STATE_WAITING:
219 u = utf_new_char("WAITING");
221 case THREAD_STATE_TIMED_WAITING:
222 u = utf_new_char("TIMED_WAITING");
224 case THREAD_STATE_TERMINATED:
225 u = utf_new_char("TERMINATED");
228 vm_abort("threads_get_state: unknown thread state %d", thread->state);
235 /* threads_thread_is_alive *****************************************************
237 Returns if the give thread is alive.
239 *******************************************************************************/
241 bool threads_thread_is_alive(threadobject *thread)
245 switch (thread->state) {
246 case THREAD_STATE_NEW:
247 case THREAD_STATE_TERMINATED:
251 case THREAD_STATE_RUNNABLE:
252 case THREAD_STATE_BLOCKED:
253 case THREAD_STATE_WAITING:
254 case THREAD_STATE_TIMED_WAITING:
259 vm_abort("threads_is_alive: unknown thread state %d", thread->state);
266 /* threads_dump ****************************************************************
268 Dumps info for all threads running in the JVM. This function is
269 called when SIGQUIT (<ctrl>-\) is sent to CACAO.
271 *******************************************************************************/
273 void threads_dump(void)
275 threadobject *thread;
279 /* XXX we should stop the world here */
281 printf("Full thread dump CACAO "VERSION":\n");
283 /* iterate over all started threads */
285 thread = mainthreadobj;
288 /* get thread object */
292 /* the thread may be currently in initalization, don't print it */
295 /* get thread name */
297 #if defined(ENABLE_JAVASE)
298 name = javastring_toutf((java_objectheader *) t->name, false);
299 #elif defined(ENABLE_JAVAME_CLDC1_1)
304 utf_display_printable_ascii(name);
307 if (thread->flags & THREAD_FLAG_DAEMON)
310 printf(" prio=%d", t->priority);
312 #if SIZEOF_VOID_P == 8
313 printf(" tid=0x%016lx", (ptrint) thread->tid);
315 printf(" tid=0x%08lx", (ptrint) thread->tid);
318 /* print thread state */
320 switch (thread->state) {
321 case THREAD_STATE_NEW:
324 case THREAD_STATE_RUNNABLE:
327 case THREAD_STATE_BLOCKED:
330 case THREAD_STATE_WAITING:
333 case THREAD_STATE_TIMED_WAITING:
334 printf(" waiting on condition");
336 case THREAD_STATE_TERMINATED:
337 printf(" terminated");
340 vm_abort("threads_dump: unknown thread state %d",
346 /* print trace of thread */
348 threads_thread_print_stacktrace(thread);
351 thread = thread->next;
352 } while ((thread != NULL) && (thread != mainthreadobj));
356 /* threads_thread_print_stacktrace *********************************************
358 Print the current stacktrace of the current thread.
360 *******************************************************************************/
362 void threads_thread_print_stacktrace(threadobject *thread)
365 stacktracebuffer *stb;
368 /* mark start of dump memory area */
370 dumpsize = dump_size();
372 /* create a stacktrace for the passed thread */
374 sfi = thread->_stackframeinfo;
376 stb = stacktrace_create(sfi);
378 /* print stacktrace */
381 stacktrace_print_trace_from_buffer(stb);
383 puts("\t<<No stacktrace available>>");
387 dump_release(dumpsize);
391 /* threads_print_stacktrace ****************************************************
393 Print the current stacktrace of the current thread.
395 *******************************************************************************/
397 void threads_print_stacktrace(void)
399 threadobject *thread;
401 thread = THREADOBJECT;
403 threads_thread_print_stacktrace(thread);
408 * These are local overrides for various environment variables in Emacs.
409 * Please do not remove this and leave it at the end of the file, where
410 * Emacs will automagically detect them.
411 * ---------------------------------------------------------------------
414 * indent-tabs-mode: t
418 * vim:noexpandtab:sw=4:ts=4: