Merged revisions 7707-7722 via svnmerge from
[cacao.git] / src / threads / threads-common.c
1 /* src/threads/threads-common.c - machine independent thread functions
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02110-1301, USA.
24
25    $Id: signal.c 7246 2007-01-29 18:49:05Z twisti $
26
27 */
28
29
30 #include "config.h"
31 #include "vm/types.h"
32
33 #include "native/jni.h"
34
35 #include "native/include/java_lang_Object.h"
36 #include "native/include/java_lang_String.h"
37 #include "native/include/java_lang_Thread.h"
38
39 #if defined(WITH_CLASSPATH_GNU)
40 # include "native/include/java_lang_VMThread.h"
41 #endif
42
43 #include "threads/threads-common.h"
44
45 #include "threads/native/threads.h"
46
47 #include "vm/builtin.h"
48 #include "vm/stringlocal.h"
49 #include "vm/vm.h"
50
51 #include "vm/jit/stacktrace.h"
52
53 #include "vmcore/class.h"
54 #include "vmcore/utf8.h"
55
56
57 /* threads_create_thread *******************************************************
58
59    Creates a thread object with the given name.
60
61 *******************************************************************************/
62
63 threadobject *threads_create_thread(utf *name)
64 {
65         threadobject       *thread;
66         java_lang_Thread   *t;
67 #if defined(WITH_CLASSPATH_GNU)
68         java_lang_VMThread *vmt;
69 #endif
70
71         /* create the vm internal thread object */
72
73         thread = NEW(threadobject);
74
75         if (thread == NULL)
76                 return NULL;
77
78         /* create the java thread object */
79
80         t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
81
82         if (t == NULL)
83                 return NULL;
84
85 #if defined(WITH_CLASSPATH_GNU)
86         vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
87
88         if (vmt == NULL)
89                 return NULL;
90
91         vmt->thread = t;
92         vmt->vmdata = (java_lang_Object *) thread;
93
94         t->vmThread = vmt;
95 #elif defined(WITH_CLASSPATH_CLDC1_1)
96         t->vm_thread = (java_lang_Object *) thread;
97 #endif
98
99         thread->object     = t;
100         thread->flags      = THREAD_FLAG_DAEMON;
101
102         /* set java.lang.Thread fields */
103
104         t->name     = (java_lang_String *) javastring_new(name);
105 #if defined(ENABLE_JAVASE)
106         t->daemon   = true;
107 #endif
108         t->priority = NORM_PRIORITY;
109
110         /* return the thread object */
111
112         return thread;
113 }
114
115
116 /* threads_get_current_tid *****************************************************
117
118    Return the tid of the current thread.
119    
120    RETURN VALUE:
121        the current tid
122
123 *******************************************************************************/
124
125 ptrint threads_get_current_tid(void)
126 {
127         threadobject *thread;
128
129         thread = THREADOBJECT;
130
131         /* this may happen during bootstrap */
132
133         if (thread == NULL)
134                 return 0;
135
136         return (ptrint) thread->tid;
137 }
138
139
140 /* threads_thread_get_state ****************************************************
141
142    Returns the current state of the given thread.
143
144 *******************************************************************************/
145
146 utf *threads_thread_get_state(threadobject *thread)
147 {
148         utf *u;
149
150         switch (thread->state) {
151         case THREAD_STATE_NEW:
152                 u = utf_new_char("NEW");
153                 break;
154         case THREAD_STATE_RUNNABLE:
155                 u = utf_new_char("RUNNABLE");
156                 break;
157         case THREAD_STATE_BLOCKED:
158                 u = utf_new_char("BLOCKED");
159                 break;
160         case THREAD_STATE_WAITING:
161                 u = utf_new_char("WAITING");
162                 break;
163         case THREAD_STATE_TIMED_WAITING:
164                 u = utf_new_char("TIMED_WAITING");
165                 break;
166         case THREAD_STATE_TERMINATED:
167                 u = utf_new_char("TERMINATED");
168                 break;
169         default:
170                 vm_abort("threads_get_state: unknown thread state %d", thread->state);
171         }
172
173         return u;
174 }
175
176
177 /* threads_thread_is_alive *****************************************************
178
179    Returns if the give thread is alive.
180
181 *******************************************************************************/
182
183 bool threads_thread_is_alive(threadobject *thread)
184 {
185         bool result;
186
187         switch (thread->state) {
188         case THREAD_STATE_NEW:
189         case THREAD_STATE_TERMINATED:
190                 result = false;
191                 break;
192
193         case THREAD_STATE_RUNNABLE:
194         case THREAD_STATE_BLOCKED:
195         case THREAD_STATE_WAITING:
196         case THREAD_STATE_TIMED_WAITING:
197                 result = true;
198                 break;
199
200         default:
201                 vm_abort("threads_is_alive: unknown thread state %d", thread->state);
202         }
203
204         return result;
205 }
206
207
208 /* threads_dump ****************************************************************
209
210    Dumps info for all threads running in the JVM.  This function is
211    called when SIGQUIT (<ctrl>-\) is sent to CACAO.
212
213 *******************************************************************************/
214
215 void threads_dump(void)
216 {
217         threadobject     *thread;
218         java_lang_Thread *t;
219         utf              *name;
220
221         thread = mainthreadobj;
222
223         /* XXX we should stop the world here */
224
225         printf("Full thread dump CACAO "VERSION":\n");
226
227         /* iterate over all started threads */
228
229         do {
230                 /* get thread object */
231
232                 t = thread->object;
233
234                 /* the thread may be currently in initalization, don't print it */
235
236                 if (t != NULL) {
237                         /* get thread name */
238
239 #if defined(ENABLE_JAVASE)
240                         name = javastring_toutf((java_objectheader *) t->name, false);
241 #elif defined(ENABLE_JAVAME_CLDC1_1)
242                         name = t->name;
243 #endif
244
245                         printf("\n\"");
246                         utf_display_printable_ascii(name);
247                         printf("\"");
248
249                         if (thread->flags & THREAD_FLAG_DAEMON)
250                                 printf(" daemon");
251
252                         printf(" prio=%d", t->priority);
253
254 #if SIZEOF_VOID_P == 8
255                         printf(" tid=0x%016lx", (ptrint) thread->tid);
256 #else
257                         printf(" tid=0x%08lx", (ptrint) thread->tid);
258 #endif
259
260                         /* print thread state */
261
262                         switch (thread->state) {
263                         case THREAD_STATE_NEW:
264                                 printf(" new");
265                                 break;
266                         case THREAD_STATE_RUNNABLE:
267                                 printf(" runnable");
268                                 break;
269                         case THREAD_STATE_BLOCKED:
270                                 printf(" blocked");
271                                 break;
272                         case THREAD_STATE_WAITING:
273                                 printf(" waiting");
274                                 break;
275                         case THREAD_STATE_TIMED_WAITING:
276                                 printf(" waiting on condition");
277                                 break;
278                         case THREAD_STATE_TERMINATED:
279                                 printf(" terminated");
280                                 break;
281                         default:
282                                 vm_abort("threads_dump: unknown thread state %d",
283                                                  thread->state);
284                         }
285
286                         printf("\n");
287
288                         /* print trace of thread */
289
290                         threads_print_stacktrace(thread);
291                 }
292
293                 thread = thread->next;
294         } while ((thread != NULL) && (thread != mainthreadobj));
295 }
296
297
298 /* threads_print_stacktrace ****************************************************
299
300    Print the current stacktrace of the given thread.
301
302 *******************************************************************************/
303
304 void threads_print_stacktrace(threadobject *thread)
305 {
306         stackframeinfo   *sfi;
307         stacktracebuffer *stb;
308         s4                dumpsize;
309
310         /* mark start of dump memory area */
311
312         dumpsize = dump_size();
313
314         /* create a stacktrace for the passed thread */
315
316         sfi = thread->_stackframeinfo;
317
318         stb = stacktrace_create(sfi);
319
320         /* print stacktrace */
321
322         if (stb != NULL)
323                 stacktrace_print_trace_from_buffer(stb);
324         else {
325                 puts("\t<<No stacktrace available>>");
326                 fflush(stdout);
327         }
328
329         dump_release(dumpsize);
330 }
331
332
333 /*
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  * ---------------------------------------------------------------------
338  * Local variables:
339  * mode: c
340  * indent-tabs-mode: t
341  * c-basic-offset: 4
342  * tab-width: 4
343  * End:
344  * vim:noexpandtab:sw=4:ts=4:
345  */