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