9bb4293ea3074d264989c8e8cb5aadb02ebf4f84
[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
32 #include <assert.h>
33
34 #include "vm/types.h"
35
36 #include "native/jni.h"
37
38 #include "native/include/java_lang_Object.h"
39 #include "native/include/java_lang_String.h"
40 #include "native/include/java_lang_Thread.h"
41
42 #if defined(WITH_CLASSPATH_GNU)
43 # include "native/include/java_lang_VMThread.h"
44 #endif
45
46 #include "threads/threads-common.h"
47
48 #include "threads/native/threads.h"
49
50 #include "vm/builtin.h"
51 #include "vm/stringlocal.h"
52 #include "vm/vm.h"
53
54 #include "vm/jit/stacktrace.h"
55
56 #include "vmcore/class.h"
57
58 #if defined(ENABLE_STATISTICS)
59 # include "vmcore/options.h"
60 # include "vmcore/statistics.h"
61 #endif
62
63 #include "vmcore/utf8.h"
64
65
66 /* threads_create_thread *******************************************************
67
68    Creates a thread object with the given name.
69
70 *******************************************************************************/
71
72 threadobject *threads_create_thread(utf *name)
73 {
74         threadobject       *thread;
75         java_lang_Thread   *t;
76 #if defined(WITH_CLASSPATH_GNU)
77         java_lang_VMThread *vmt;
78 #endif
79
80         /* create the vm internal thread object */
81
82         thread = NEW(threadobject);
83
84         if (thread == NULL)
85                 return NULL;
86
87         /* create the java thread object */
88
89         t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
90
91         if (t == NULL)
92                 return NULL;
93
94 #if defined(WITH_CLASSPATH_GNU)
95         vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
96
97         if (vmt == NULL)
98                 return NULL;
99
100         vmt->thread = t;
101         vmt->vmdata = (java_lang_Object *) thread;
102
103         t->vmThread = vmt;
104 #elif defined(WITH_CLASSPATH_CLDC1_1)
105         t->vm_thread = (java_lang_Object *) thread;
106 #endif
107
108         thread->object     = t;
109         thread->flags      = THREAD_FLAG_DAEMON;
110
111         /* set java.lang.Thread fields */
112
113         t->name     = (java_lang_String *) javastring_new(name);
114 #if defined(ENABLE_JAVASE)
115         t->daemon   = true;
116 #endif
117         t->priority = NORM_PRIORITY;
118
119         /* return the thread object */
120
121         return thread;
122 }
123
124
125 /* threads_start_javathread ***************************************************
126
127    Start a thread in the JVM. Only the java thread object exists so far.
128
129    IN:
130       object.....the java thread object java.lang.Thread
131
132 ******************************************************************************/
133
134 void threads_start_javathread(java_lang_Thread *object)
135 {
136         threadobject *thread;
137
138         /* create the vm internal threadobject */
139
140         thread = NEW(threadobject);
141
142 #if defined(ENABLE_STATISTICS)
143         if (opt_stat)
144                 size_threadobject += sizeof(threadobject);
145 #endif
146
147         /* link the two objects together */
148
149         thread->object = object;
150
151 #if defined(ENABLE_JAVASE)
152         /* is this a daemon thread? */
153
154         if (object->daemon == true)
155                 thread->flags |= THREAD_FLAG_DAEMON;
156 #endif
157
158 #if defined(WITH_CLASSPATH_GNU)
159         assert(object->vmThread);
160         assert(object->vmThread->vmdata == NULL);
161
162         object->vmThread->vmdata = (java_lang_Object *) thread;
163 #elif defined(WITH_CLASSPATH_CLDC1_1)
164         object->vm_thread = (java_lang_Object *) thread;
165 #endif
166
167         /* Actually start the thread.  Don't pass a function pointer
168            (NULL) since we want Thread.run()V here. */
169
170         threads_start_thread(thread, NULL);
171 }
172
173
174 /* threads_get_current_tid *****************************************************
175
176    Return the tid of the current thread.
177    
178    RETURN VALUE:
179        the current tid
180
181 *******************************************************************************/
182
183 ptrint threads_get_current_tid(void)
184 {
185         threadobject *thread;
186
187         thread = THREADOBJECT;
188
189         /* this may happen during bootstrap */
190
191         if (thread == NULL)
192                 return 0;
193
194         return (ptrint) thread->tid;
195 }
196
197
198 /* threads_thread_get_state ****************************************************
199
200    Returns the current state of the given thread.
201
202 *******************************************************************************/
203
204 utf *threads_thread_get_state(threadobject *thread)
205 {
206         utf *u;
207
208         switch (thread->state) {
209         case THREAD_STATE_NEW:
210                 u = utf_new_char("NEW");
211                 break;
212         case THREAD_STATE_RUNNABLE:
213                 u = utf_new_char("RUNNABLE");
214                 break;
215         case THREAD_STATE_BLOCKED:
216                 u = utf_new_char("BLOCKED");
217                 break;
218         case THREAD_STATE_WAITING:
219                 u = utf_new_char("WAITING");
220                 break;
221         case THREAD_STATE_TIMED_WAITING:
222                 u = utf_new_char("TIMED_WAITING");
223                 break;
224         case THREAD_STATE_TERMINATED:
225                 u = utf_new_char("TERMINATED");
226                 break;
227         default:
228                 vm_abort("threads_get_state: unknown thread state %d", thread->state);
229         }
230
231         return u;
232 }
233
234
235 /* threads_thread_is_alive *****************************************************
236
237    Returns if the give thread is alive.
238
239 *******************************************************************************/
240
241 bool threads_thread_is_alive(threadobject *thread)
242 {
243         bool result;
244
245         switch (thread->state) {
246         case THREAD_STATE_NEW:
247         case THREAD_STATE_TERMINATED:
248                 result = false;
249                 break;
250
251         case THREAD_STATE_RUNNABLE:
252         case THREAD_STATE_BLOCKED:
253         case THREAD_STATE_WAITING:
254         case THREAD_STATE_TIMED_WAITING:
255                 result = true;
256                 break;
257
258         default:
259                 vm_abort("threads_is_alive: unknown thread state %d", thread->state);
260         }
261
262         return result;
263 }
264
265
266 /* threads_dump ****************************************************************
267
268    Dumps info for all threads running in the JVM.  This function is
269    called when SIGQUIT (<ctrl>-\) is sent to CACAO.
270
271 *******************************************************************************/
272
273 void threads_dump(void)
274 {
275         threadobject     *thread;
276         java_lang_Thread *t;
277         utf              *name;
278
279         /* XXX we should stop the world here */
280
281         printf("Full thread dump CACAO "VERSION":\n");
282
283         /* iterate over all started threads */
284
285         thread = mainthreadobj;
286
287         do {
288                 /* get thread object */
289
290                 t = thread->object;
291
292                 /* the thread may be currently in initalization, don't print it */
293
294                 if (t != NULL) {
295                         /* get thread name */
296
297 #if defined(ENABLE_JAVASE)
298                         name = javastring_toutf((java_objectheader *) t->name, false);
299 #elif defined(ENABLE_JAVAME_CLDC1_1)
300                         name = t->name;
301 #endif
302
303                         printf("\n\"");
304                         utf_display_printable_ascii(name);
305                         printf("\"");
306
307                         if (thread->flags & THREAD_FLAG_DAEMON)
308                                 printf(" daemon");
309
310                         printf(" prio=%d", t->priority);
311
312 #if SIZEOF_VOID_P == 8
313                         printf(" tid=0x%016lx", (ptrint) thread->tid);
314 #else
315                         printf(" tid=0x%08lx", (ptrint) thread->tid);
316 #endif
317
318                         /* print thread state */
319
320                         switch (thread->state) {
321                         case THREAD_STATE_NEW:
322                                 printf(" new");
323                                 break;
324                         case THREAD_STATE_RUNNABLE:
325                                 printf(" runnable");
326                                 break;
327                         case THREAD_STATE_BLOCKED:
328                                 printf(" blocked");
329                                 break;
330                         case THREAD_STATE_WAITING:
331                                 printf(" waiting");
332                                 break;
333                         case THREAD_STATE_TIMED_WAITING:
334                                 printf(" waiting on condition");
335                                 break;
336                         case THREAD_STATE_TERMINATED:
337                                 printf(" terminated");
338                                 break;
339                         default:
340                                 vm_abort("threads_dump: unknown thread state %d",
341                                                  thread->state);
342                         }
343
344                         printf("\n");
345
346                         /* print trace of thread */
347
348                         threads_thread_print_stacktrace(thread);
349                 }
350
351                 thread = thread->next;
352         } while ((thread != NULL) && (thread != mainthreadobj));
353 }
354
355
356 /* threads_thread_print_stacktrace *********************************************
357
358    Print the current stacktrace of the current thread.
359
360 *******************************************************************************/
361
362 void threads_thread_print_stacktrace(threadobject *thread)
363 {
364         stackframeinfo   *sfi;
365         stacktracebuffer *stb;
366         s4                dumpsize;
367
368         /* mark start of dump memory area */
369
370         dumpsize = dump_size();
371
372         /* create a stacktrace for the passed thread */
373
374         sfi = thread->_stackframeinfo;
375
376         stb = stacktrace_create(sfi);
377
378         /* print stacktrace */
379
380         if (stb != NULL)
381                 stacktrace_print_trace_from_buffer(stb);
382         else {
383                 puts("\t<<No stacktrace available>>");
384                 fflush(stdout);
385         }
386
387         dump_release(dumpsize);
388 }
389
390
391 /* threads_print_stacktrace ****************************************************
392
393    Print the current stacktrace of the current thread.
394
395 *******************************************************************************/
396
397 void threads_print_stacktrace(void)
398 {
399         threadobject *thread;
400
401         thread = THREADOBJECT;
402
403         threads_thread_print_stacktrace(thread);
404 }
405
406
407 /*
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  * ---------------------------------------------------------------------
412  * Local variables:
413  * mode: c
414  * indent-tabs-mode: t
415  * c-basic-offset: 4
416  * tab-width: 4
417  * End:
418  * vim:noexpandtab:sw=4:ts=4:
419  */