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 7813 2007-04-25 19:20:13Z 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/lock-common.h"
47 #include "threads/threads-common.h"
49 #include "threads/native/threads.h"
51 #include "vm/builtin.h"
52 #include "vm/stringlocal.h"
55 #include "vm/jit/stacktrace.h"
57 #include "vmcore/class.h"
59 #if defined(ENABLE_STATISTICS)
60 # include "vmcore/options.h"
61 # include "vmcore/statistics.h"
64 #include "vmcore/utf8.h"
67 /* threads_create_thread *******************************************************
69 Creates and initializes an internal thread data-structure.
71 *******************************************************************************/
73 threadobject *threads_create_thread(void)
77 /* allocate internal thread data-structure */
79 #if defined(ENABLE_GC_BOEHM)
80 thread = GCNEW_UNCOLLECTABLE(threadobject, 1);
82 thread = NEW(threadobject);
85 #if defined(ENABLE_STATISTICS)
87 size_threadobject += sizeof(threadobject);
90 /* initialize thread data structure */
92 threads_init_threadobject(thread);
93 lock_init_execution_env(thread);
99 /* threads_thread_create_internal **********************************************
101 Creates an internal thread data-structure with the given name, plus
102 necessary Java objects for the VM (e.g. finalizer-thread,
105 *******************************************************************************/
107 threadobject *threads_thread_create_internal(utf *name)
109 threadobject *thread;
111 #if defined(WITH_CLASSPATH_GNU)
112 java_lang_VMThread *vmt;
115 /* create internal thread data-structure */
117 thread = threads_create_thread();
119 /* create the java thread object */
121 t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
126 #if defined(WITH_CLASSPATH_GNU)
127 vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
133 vmt->vmdata = (java_lang_Object *) thread;
136 #elif defined(WITH_CLASSPATH_CLDC1_1)
137 t->vm_thread = (java_lang_Object *) thread;
141 thread->flags = THREAD_FLAG_DAEMON;
143 /* set java.lang.Thread fields */
145 t->name = (java_lang_String *) javastring_new(name);
146 #if defined(ENABLE_JAVASE)
149 t->priority = NORM_PRIORITY;
151 /* return the thread data-structure */
157 /* threads_start_javathread ****************************************************
159 Start a thread in the JVM. Only the java thread object exists so far.
162 object.....the java thread object java.lang.Thread
164 *******************************************************************************/
166 void threads_start_javathread(java_lang_Thread *object)
168 threadobject *thread;
170 /* create internal thread data-structure */
172 thread = threads_create_thread();
174 /* link the two objects together */
176 thread->object = object;
178 #if defined(ENABLE_JAVASE)
179 /* is this a daemon thread? */
181 if (object->daemon == true)
182 thread->flags |= THREAD_FLAG_DAEMON;
185 #if defined(WITH_CLASSPATH_GNU)
186 assert(object->vmThread);
187 assert(object->vmThread->vmdata == NULL);
189 object->vmThread->vmdata = (java_lang_Object *) thread;
190 #elif defined(WITH_CLASSPATH_CLDC1_1)
191 object->vm_thread = (java_lang_Object *) thread;
194 /* Actually start the thread. Don't pass a function pointer
195 (NULL) since we want Thread.run()V here. */
197 threads_start_thread(thread, NULL);
201 /* threads_get_current_tid *****************************************************
203 Return the tid of the current thread.
208 *******************************************************************************/
210 ptrint threads_get_current_tid(void)
212 threadobject *thread;
214 thread = THREADOBJECT;
216 /* this may happen during bootstrap */
221 return (ptrint) thread->tid;
225 /* threads_thread_get_state ****************************************************
227 Returns the current state of the given thread.
229 *******************************************************************************/
231 utf *threads_thread_get_state(threadobject *thread)
235 switch (thread->state) {
236 case THREAD_STATE_NEW:
237 u = utf_new_char("NEW");
239 case THREAD_STATE_RUNNABLE:
240 u = utf_new_char("RUNNABLE");
242 case THREAD_STATE_BLOCKED:
243 u = utf_new_char("BLOCKED");
245 case THREAD_STATE_WAITING:
246 u = utf_new_char("WAITING");
248 case THREAD_STATE_TIMED_WAITING:
249 u = utf_new_char("TIMED_WAITING");
251 case THREAD_STATE_TERMINATED:
252 u = utf_new_char("TERMINATED");
255 vm_abort("threads_get_state: unknown thread state %d", thread->state);
262 /* threads_thread_is_alive *****************************************************
264 Returns if the give thread is alive.
266 *******************************************************************************/
268 bool threads_thread_is_alive(threadobject *thread)
272 switch (thread->state) {
273 case THREAD_STATE_NEW:
274 case THREAD_STATE_TERMINATED:
278 case THREAD_STATE_RUNNABLE:
279 case THREAD_STATE_BLOCKED:
280 case THREAD_STATE_WAITING:
281 case THREAD_STATE_TIMED_WAITING:
286 vm_abort("threads_is_alive: unknown thread state %d", thread->state);
293 /* threads_dump ****************************************************************
295 Dumps info for all threads running in the JVM. This function is
296 called when SIGQUIT (<ctrl>-\) is sent to CACAO.
298 *******************************************************************************/
300 void threads_dump(void)
302 threadobject *thread;
306 /* XXX we should stop the world here */
308 printf("Full thread dump CACAO "VERSION":\n");
310 /* iterate over all started threads */
312 thread = mainthreadobj;
315 /* get thread object */
319 /* the thread may be currently in initalization, don't print it */
322 /* get thread name */
324 #if defined(ENABLE_JAVASE)
325 name = javastring_toutf((java_objectheader *) t->name, false);
326 #elif defined(ENABLE_JAVAME_CLDC1_1)
331 utf_display_printable_ascii(name);
334 if (thread->flags & THREAD_FLAG_DAEMON)
337 printf(" prio=%d", t->priority);
339 #if SIZEOF_VOID_P == 8
340 printf(" tid=0x%016lx (%ld)",
341 (ptrint) thread->tid, (ptrint) thread->tid);
343 printf(" tid=0x%08lx (%ld)",
344 (ptrint) thread->tid, (ptrint) thread->tid);
347 /* print thread state */
349 switch (thread->state) {
350 case THREAD_STATE_NEW:
353 case THREAD_STATE_RUNNABLE:
356 case THREAD_STATE_BLOCKED:
359 case THREAD_STATE_WAITING:
362 case THREAD_STATE_TIMED_WAITING:
363 printf(" waiting on condition");
365 case THREAD_STATE_TERMINATED:
366 printf(" terminated");
369 vm_abort("threads_dump: unknown thread state %d",
375 /* print trace of thread */
377 threads_thread_print_stacktrace(thread);
380 thread = thread->next;
381 } while ((thread != NULL) && (thread != mainthreadobj));
385 /* threads_thread_print_stacktrace *********************************************
387 Print the current stacktrace of the current thread.
389 *******************************************************************************/
391 void threads_thread_print_stacktrace(threadobject *thread)
394 stacktracebuffer *stb;
397 /* mark start of dump memory area */
399 dumpsize = dump_size();
401 /* create a stacktrace for the passed thread */
403 sfi = thread->_stackframeinfo;
405 stb = stacktrace_create(sfi);
407 /* print stacktrace */
410 stacktrace_print_trace_from_buffer(stb);
412 puts("\t<<No stacktrace available>>");
416 dump_release(dumpsize);
420 /* threads_print_stacktrace ****************************************************
422 Print the current stacktrace of the current thread.
424 *******************************************************************************/
426 void threads_print_stacktrace(void)
428 threadobject *thread;
430 thread = THREADOBJECT;
432 threads_thread_print_stacktrace(thread);
437 * These are local overrides for various environment variables in Emacs.
438 * Please do not remove this and leave it at the end of the file, where
439 * Emacs will automagically detect them.
440 * ---------------------------------------------------------------------
443 * indent-tabs-mode: t
447 * vim:noexpandtab:sw=4:ts=4: