* src/threads/thread.cpp: Break a reference cycle.
[cacao.git] / src / threads / thread.cpp
1 /* src/threads/thread.cpp - machine independent thread functions
2
3    Copyright (C) 1996-2011
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <stdint.h>
30 #include <unistd.h>
31
32 #include "vm/types.h"
33
34 #include "mm/memory.hpp"
35
36 #if defined(ENABLE_GC_BOEHM)
37 /* We need to include Boehm's gc.h here for GC_register_my_thread and
38    friends. */
39 # include "mm/boehm-gc/include/gc.h"
40 #endif
41
42 #include "native/llni.h"
43 #include "native/native.hpp"
44
45 #include "threads/lock.hpp"
46 #include "threads/threadlist.hpp"
47 #include "threads/thread.hpp"
48
49 #include "vm/jit/builtin.hpp"
50 #include "vm/class.hpp"
51 #include "vm/exceptions.hpp"
52 #include "vm/finalizer.hpp"
53 #include "vm/globals.hpp"
54 #include "vm/javaobjects.hpp"
55 #include "vm/method.hpp"
56 #include "vm/options.h"
57 #include "vm/string.hpp"
58 #include "vm/utf8.h"
59 #include "vm/vm.hpp"
60
61 #if defined(ENABLE_STATISTICS)
62 # include "vm/statistics.h"
63 #endif
64
65 #include "vm/jit/stacktrace.hpp"
66
67
68 /* global variables ***********************************************************/
69
70 static methodinfo    *thread_method_init;
71 static java_handle_t *threadgroup_system;
72 static java_handle_t *threadgroup_main;
73
74 #if defined(__LINUX__)
75 /* XXX Remove for exact-GC. */
76 bool threads_pthreads_implementation_nptl;
77 #endif
78
79
80 /* static functions ***********************************************************/
81
82 static void          thread_create_initial_thread(void);
83 static threadobject *thread_new(int32_t flags);
84
85
86 /* threads_preinit *************************************************************
87
88    Do some early initialization of stuff required.
89
90 *******************************************************************************/
91
92 void threads_preinit(void)
93 {
94         threadobject *mainthread;
95 #if defined(__LINUX__) && defined(_CS_GNU_LIBPTHREAD_VERSION)
96         char         *pathbuf;
97         size_t        len;
98 #endif
99
100         TRACESUBSYSTEMINITIALIZATION("threads_preinit");
101
102 #if defined(__LINUX__)
103         /* XXX Remove for exact-GC. */
104
105         /* On Linux we need to check the pthread implementation. */
106
107         /* _CS_GNU_LIBPTHREAD_VERSION (GNU C library only; since glibc 2.3.2) */
108         /* If the glibc is a pre-2.3.2 version, we fall back to
109            linuxthreads. */
110
111 # if defined(_CS_GNU_LIBPTHREAD_VERSION)
112         len = confstr(_CS_GNU_LIBPTHREAD_VERSION, NULL, (size_t) 0);
113
114         /* Some systems return as length 0 (maybe cross-compilation
115            related).  In this case we also fall back to linuxthreads. */
116
117         if (len > 0) {
118                 pathbuf = MNEW(char, len);
119
120                 (void) confstr(_CS_GNU_LIBPTHREAD_VERSION, pathbuf, len);
121
122                 if (strstr(pathbuf, "NPTL") != NULL)
123                         threads_pthreads_implementation_nptl = true;
124                 else
125                         threads_pthreads_implementation_nptl = false;
126         }
127         else
128                 threads_pthreads_implementation_nptl = false;
129 # else
130         threads_pthreads_implementation_nptl = false;
131 # endif
132 #endif
133
134         /* Initialize the threads implementation (sets the thinlock on the
135            main thread). */
136
137         threads_impl_preinit();
138
139         /* Create internal thread data-structure for the main thread. */
140
141         mainthread = thread_new(THREAD_FLAG_JAVA);
142
143         /* Add the thread to the thread list. */
144
145         ThreadList::add_to_active_thread_list(mainthread);
146
147         /* The main thread should always have index 1. */
148
149         if (mainthread->index != 1)
150                 vm_abort("threads_preinit: main thread index not 1: %d != 1",
151                                  mainthread->index);
152
153         /* Thread is already running. */
154
155         mainthread->state = THREAD_STATE_RUNNABLE;
156
157         /* Store the internal thread data-structure in the TSD. */
158
159         thread_set_current(mainthread);
160 }
161
162
163 /* threads_init ****************************************************************
164
165    Initialize the main thread.
166
167 *******************************************************************************/
168
169 void threads_init(void)
170 {
171         TRACESUBSYSTEMINITIALIZATION("threads_init");
172
173         /* Create the system and main thread groups. */
174
175         ThreadRuntime::thread_create_initial_threadgroups(&threadgroup_main, &threadgroup_system);
176
177         /* Cache the java.lang.Thread initialization method. */
178
179         thread_method_init = ThreadRuntime::get_thread_init_method();
180
181         if (thread_method_init == NULL)
182                 vm_abort("threads_init: failed to resolve thread init method");
183
184         thread_create_initial_thread();
185 }
186
187
188 /* thread_create_object ********************************************************
189
190    Create a Java thread object for the given thread data-structure,
191    initializes it and adds the thread to the threadgroup.
192
193    ARGUMENTS:
194
195        t ....... thread
196        name .... thread name
197        group ... threadgroup
198
199    RETURN:
200
201 *******************************************************************************/
202
203 static bool thread_create_object(threadobject *t, java_handle_t *name, java_handle_t *group)
204 {
205         /* Create a java.lang.Thread Java object. */
206
207         java_handle_t* h = builtin_new(class_java_lang_Thread);
208
209         if (h == NULL)
210                 return false;
211
212         java_lang_Thread jlt(h);
213
214         // Set the Java object in the thread data-structure.  This
215         // indicates that the thread is attached to the VM.
216         t->object = LLNI_DIRECT(jlt.get_handle());
217
218         return ThreadRuntime::invoke_thread_initializer(jlt, t, thread_method_init, name, group);
219 }
220
221
222 /* thread_create_initial_thread ***********************************************
223
224    Create the initial thread: main
225
226 *******************************************************************************/
227
228 static void thread_create_initial_thread(void)
229 {
230         threadobject  *t;
231         java_handle_t *name;
232
233         /* Get the main-thread (NOTE: The main thread is always the first
234            thread in the list). */
235
236         t = ThreadList::get_main_thread();
237
238         /* The thread name. */
239
240         name = javastring_new(utf_main);
241
242 #if defined(ENABLE_INTRP)
243         /* create interpreter stack */
244
245         if (opt_intrp) {
246                 MSET(intrp_main_stack, 0, u1, opt_stacksize);
247                 mainthread->_global_sp = (Cell*) (intrp_main_stack + opt_stacksize);
248         }
249 #endif
250
251         /* Create the Java thread object. */
252
253         if (!thread_create_object(t, name, threadgroup_main))
254                 vm_abort("thread_create_initial_thread: failed to create Java object");
255
256         /* Initialize the implementation specific bits. */
257
258         threads_impl_init();
259
260         DEBUGTHREADS("starting (main)", t);
261 }
262
263
264 /* thread_new ******************************************************************
265
266    Allocates and initializes an internal thread data-structure and
267    adds it to the threads list.
268
269 *******************************************************************************/
270
271 static threadobject *thread_new(int32_t flags)
272 {
273         int32_t       index;
274         threadobject *t;
275         
276         /* Lock the thread lists */
277
278         ThreadList::lock();
279
280         index = ThreadList::get_free_thread_index();
281
282         /* Allocate a thread data structure. */
283
284         /* First, try to get one from the free-list. */
285
286         t = ThreadList::get_free_thread();
287
288         /* Unlock the thread lists. */
289
290         ThreadList::unlock();
291
292         if (t != NULL) {
293                 /* Equivalent of MZERO on the else path */
294
295                 threads_impl_thread_clear(t);
296         }
297         else {
298 #if defined(ENABLE_GC_BOEHM)
299                 t = GCNEW_UNCOLLECTABLE(threadobject, 1);
300 #else
301                 t = NEW(threadobject);
302 #endif
303
304 #if defined(ENABLE_STATISTICS)
305                 if (opt_stat)
306                         size_threadobject += sizeof(threadobject);
307 #endif
308
309                 /* Clear memory. */
310
311                 MZERO(t, threadobject, 1);
312
313                 // Initialize the mutex and the condition.
314                 t->flc_lock = new Mutex();
315                 t->flc_cond = new Condition();
316
317                 t->waitmutex = new Mutex();
318                 t->waitcond = new Condition();
319
320                 t->suspendmutex = new Mutex();
321                 t->suspendcond = new Condition();
322
323 #if defined(ENABLE_TLH)
324                 tlh_init(&(t->tlh));
325 #endif
326
327 #if defined(ENABLE_GC_CACAO)
328                 /* Register reference to java.lang.Thread with the GC. */
329                 /* FIXME is it ok to do this only once? */
330
331                 gc_reference_register(&(t->object), GC_REFTYPE_THREADOBJECT);
332                 gc_reference_register(&(t->_exceptionptr), GC_REFTYPE_THREADOBJECT);
333 #endif
334
335                 t->_dumpmemory = new DumpMemory();
336         }
337
338         /* Pre-compute the thinlock-word. */
339
340         assert(index != 0);
341
342         t->index     = index;
343         t->thinlock  = Lockword::pre_compute_thinlock(t->index);
344         t->flags     = flags;
345         t->state     = THREAD_STATE_NEW;
346
347 #if defined(ENABLE_GC_CACAO)
348         t->flags    |= THREAD_FLAG_IN_NATIVE; 
349 #endif
350
351         /* Initialize the implementation-specific bits. */
352
353         threads_impl_thread_reuse(t);
354
355         return t;
356 }
357
358
359 /* thread_free *****************************************************************
360
361    Remove the thread from the threads-list and free the internal
362    thread data structure.  The thread index is added to the
363    thread-index free-list.
364
365    IN:
366        t ... thread data structure
367
368 *******************************************************************************/
369
370 void thread_free(threadobject *t)
371 {
372         java_handle_t *h = LLNI_WRAP(t->object);
373         java_lang_Thread jlt(h);
374         ThreadRuntime::clear_heap_reference(jlt);
375
376         /* Set the reference to the Java object to NULL. */
377
378         t->object = 0;
379
380         ThreadList::deactivate_thread(t);
381 }
382
383
384 /* threads_thread_start_internal ***********************************************
385
386    Start an internal thread in the JVM.  No Java thread objects exists
387    so far.
388
389    IN:
390       name.......UTF-8 name of the thread
391       f..........function pointer to C function to start
392
393 *******************************************************************************/
394
395 static void thread_cleanup_finalizer(java_handle_t *h, void *data)
396 {
397         threadobject *t = reinterpret_cast<threadobject*>(data);
398         ThreadList::release_thread(t, false);
399 }
400
401 bool threads_thread_start_internal(utf *name, functionptr f)
402 {
403         threadobject *t;
404
405         /* Create internal thread data-structure. */
406
407         t = thread_new(THREAD_FLAG_INTERNAL | THREAD_FLAG_DAEMON);
408
409         /* Add the thread to the thread list. */
410
411         ThreadList::add_to_active_thread_list(t);
412
413         /* Create the Java thread object. */
414
415         if (!thread_create_object(t, javastring_new(name), threadgroup_system)) {
416                 ThreadList::release_thread(t, true);
417                 return false;
418         }
419
420         Finalizer::attach_custom_finalizer(LLNI_WRAP(t->object), thread_cleanup_finalizer, t);
421
422         /* Start the thread. */
423
424         threads_impl_thread_start(t, f);
425
426         /* everything's ok */
427
428         return true;
429 }
430
431
432 /* threads_thread_start ********************************************************
433
434    Start a Java thread in the JVM.  Only the java thread object exists
435    so far.
436
437    IN:
438       object.....the java thread object java.lang.Thread
439
440 *******************************************************************************/
441
442 void threads_thread_start(java_handle_t *object)
443 {
444         java_lang_Thread jlt(object);
445
446         /* Create internal thread data-structure. */
447
448         u4 flags = THREAD_FLAG_JAVA;
449 #if defined(ENABLE_JAVASE)
450         /* Is this a daemon thread? */
451
452         if (jlt.get_daemon())
453                 flags |= THREAD_FLAG_DAEMON;
454 #endif
455
456         threadobject* t = thread_new(flags);
457
458         /* Link the two objects together. */
459
460         t->object = LLNI_DIRECT(object);
461
462         /* Add the thread to the thread list. */
463
464         ThreadList::add_to_active_thread_list(t);
465
466         Atomic::write_memory_barrier();
467
468         ThreadRuntime::setup_thread_vmdata(jlt, t);
469
470         Finalizer::attach_custom_finalizer(LLNI_WRAP(t->object), thread_cleanup_finalizer, t);
471
472         /* Start the thread.  Don't pass a function pointer (NULL) since
473            we want Thread.run()V here. */
474
475         threads_impl_thread_start(t, NULL);
476 }
477
478
479 /**
480  * Attaches the current thread to the VM.
481  *
482  * @param vm_aargs Attach arguments.
483  * @param isdaemon true if the attached thread should be a daemon
484  *                 thread.
485  *
486  * @return true on success, false otherwise.
487  */
488 bool thread_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon)
489 {
490         bool           result;
491         threadobject  *t;
492         utf           *u;
493         java_handle_t *name;
494         java_handle_t *group;
495
496     /* If the current thread has already been attached, this operation
497            is a no-op. */
498
499         result = thread_current_is_attached();
500
501         if (result == true)
502                 return true;
503
504         /* Create internal thread data structure. */
505
506         u4 flags = THREAD_FLAG_JAVA;
507         if (isdaemon)
508                 flags |= THREAD_FLAG_DAEMON;
509
510         t = thread_new(flags);
511
512         /* Store the internal thread data-structure in the TSD. */
513
514         thread_set_current(t);
515
516         /* The thread is flagged and (non-)daemon thread, we can leave the
517            mutex. */
518
519         /* Add the thread to the thread list. */
520
521         ThreadList::add_to_active_thread_list(t);
522
523         DEBUGTHREADS("attaching", t);
524
525         /* Get the thread name. */
526
527         if (vm_aargs != NULL) {
528                 u = utf_new_char(vm_aargs->name);
529         }
530         else {
531                 u = utf_null;
532         }
533
534         name = javastring_new(u);
535
536 #if defined(ENABLE_JAVASE)
537         /* Get the threadgroup. */
538
539         if (vm_aargs != NULL)
540                 group = (java_handle_t *) vm_aargs->group;
541         else
542                 group = NULL;
543
544         /* If no threadgroup was given, use the main threadgroup. */
545
546         if (group == NULL)
547                 group = threadgroup_main;
548 #endif
549
550 #if defined(ENABLE_INTRP)
551         /* create interpreter stack */
552
553         if (opt_intrp) {
554                 MSET(intrp_main_stack, 0, u1, opt_stacksize);
555                 thread->_global_sp = (Cell *) (intrp_main_stack + opt_stacksize);
556         }
557 #endif
558
559         /* Create the Java thread object. */
560
561         if (!thread_create_object(t, name, group)) {
562                 ThreadList::release_thread(t, true);
563                 return false;
564         }
565
566         /* The thread is completely initialized. */
567
568         thread_set_state_runnable(t);
569
570         return true;
571 }
572
573
574 /**
575  * Attaches the current external thread to the VM.  This function is
576  * called by JNI's AttachCurrentThread.
577  *
578  * @param vm_aargs Attach arguments.
579  * @param isdaemon true if the attached thread should be a daemon
580  *                 thread.
581  *
582  * @return true on success, false otherwise.
583  */
584 bool thread_attach_current_external_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon)
585 {
586         int result;
587
588 #if defined(ENABLE_GC_BOEHM)
589         struct GC_stack_base sb;
590
591         /* Register the thread with Boehm-GC.  This must happen before the
592            thread allocates any memory from the GC heap.*/
593
594         result = GC_get_stack_base(&sb);
595
596         if (result != GC_SUCCESS)
597                 vm_abort("threads_attach_current_thread: GC_get_stack_base failed");
598
599         GC_register_my_thread(&sb);
600 #endif
601
602         result = thread_attach_current_thread(vm_aargs, isdaemon);
603
604         if (result == false) {
605 #if defined(ENABLE_GC_BOEHM)
606                 /* Unregister the thread. */
607
608                 GC_unregister_my_thread();
609 #endif
610
611                 return false;
612         }
613
614         return true;
615 }
616
617
618 /**
619  * Detaches the current external thread from the VM.  This function is
620  * called by JNI's DetachCurrentThread.
621  *
622  * @return true on success, false otherwise.
623  */
624 bool thread_detach_current_external_thread(void)
625 {
626         int result;
627
628         result = thread_detach_current_thread();
629
630         if (result == false)
631                 return false;
632
633 #if defined(ENABLE_GC_BOEHM)
634         /* Unregister the thread with Boehm-GC.  This must happen after
635            the thread allocates any memory from the GC heap. */
636
637         /* Don't detach the main thread.  This is a workaround for
638            OpenJDK's java launcher. */
639         if (thread_get_current()->index != 1)
640                 GC_unregister_my_thread();
641 #endif
642
643         return true;
644 }
645
646
647 /* thread_fprint_name **********************************************************
648
649    Print the name of the given thread to the given stream.
650
651    ARGUMENTS:
652        t ........ thread data-structure
653        stream ... stream to print to
654
655 *******************************************************************************/
656
657 void thread_fprint_name(threadobject *t, FILE *stream)
658 {
659         if (LLNI_WRAP(t->object) == NULL)
660                 vm_abort("");
661
662         java_lang_Thread jlt(LLNI_WRAP(t->object));
663
664         ThreadRuntime::print_thread_name(jlt, stream);
665 }
666
667
668 /* thread_print_info ***********************************************************
669
670    Print information of the passed thread.
671
672    ARGUMENTS:
673        t ... thread data-structure.
674
675 *******************************************************************************/
676
677 void thread_print_info(threadobject *t)
678 {
679         java_lang_Thread jlt(LLNI_WRAP(t->object));
680
681         /* Print as much as we can when we are in state NEW. */
682
683         if (jlt.get_handle() != NULL) {
684                 /* Print thread name. */
685
686                 printf("\"");
687                 thread_fprint_name(t, stdout);
688                 printf("\"");
689         }
690         else {
691         }
692
693         if (thread_is_daemon(t))
694                 printf(" daemon");
695
696         if (jlt.get_handle() != NULL) {
697                 printf(" prio=%d", jlt.get_priority());
698         }
699
700 #if SIZEOF_VOID_P == 8
701         printf(" t=0x%016lx tid=0x%016lx (%ld)",
702                    (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
703 #else
704         printf(" t=0x%08x tid=0x%08x (%d)",
705                    (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
706 #endif
707
708         printf(" index=%d", t->index);
709
710         /* Print thread state. */
711
712         int state = cacaothread_get_state(t);
713
714         switch (state) {
715         case THREAD_STATE_NEW:
716                 printf(" new");
717                 break;
718         case THREAD_STATE_RUNNABLE:
719                 printf(" runnable");
720                 break;
721         case THREAD_STATE_BLOCKED:
722                 printf(" blocked");
723                 break;
724         case THREAD_STATE_WAITING:
725                 printf(" waiting");
726                 break;
727         case THREAD_STATE_TIMED_WAITING:
728                 printf(" waiting on condition");
729                 break;
730         case THREAD_STATE_PARKED:
731                 printf(" parked");
732                 break;
733         case THREAD_STATE_TIMED_PARKED:
734                 printf(" timed parked");
735                 break;
736         case THREAD_STATE_TERMINATED:
737                 printf(" terminated");
738                 break;
739         default:
740                 vm_abort("thread_print_info: unknown thread state %d", state);
741         }
742 }
743
744
745 /* threads_get_current_tid *****************************************************
746
747    Return the tid of the current thread.
748    
749    RETURN VALUE:
750        the current tid
751
752 *******************************************************************************/
753
754 intptr_t threads_get_current_tid(void)
755 {
756         threadobject *thread;
757
758         thread = THREADOBJECT;
759
760         /* this may happen during bootstrap */
761
762         if (thread == NULL)
763                 return 0;
764
765         return (intptr_t) thread->tid;
766 }
767
768
769 /**
770  * Set the current state of the given thread. This method should only
771  * be called while holding the threadlist-lock and after checking that
772  * the new state is valid. It is best to not call this method directly
773  * but call the specific setter methods below.
774  */
775 static inline void thread_set_state(threadobject *t, int state)
776 {
777         // Set the state of our internal threadobject.
778         t->state = state;
779
780         ThreadRuntime::set_javathread_state(t, state);
781 }
782
783
784 /* thread_set_state_runnable ***************************************************
785
786    Set the current state of the given thread to THREAD_STATE_RUNNABLE.
787
788    NOTE: If the thread has already terminated, don't set the state.
789          This is important for threads_detach_thread.
790
791 *******************************************************************************/
792
793 void thread_set_state_runnable(threadobject *t)
794 {
795         if (t->state != THREAD_STATE_TERMINATED) {
796                 thread_set_state(t, THREAD_STATE_RUNNABLE);
797
798                 DEBUGTHREADS("is RUNNABLE", t);
799         }
800 }
801
802
803 /* thread_set_state_waiting ****************************************************
804
805    Set the current state of the given thread to THREAD_STATE_WAITING.
806
807    NOTE: If the thread has already terminated, don't set the state.
808          This is important for threads_detach_thread.
809
810 *******************************************************************************/
811
812 void thread_set_state_waiting(threadobject *t)
813 {
814         if (t->state != THREAD_STATE_TERMINATED) {
815                 thread_set_state(t, THREAD_STATE_WAITING);
816
817                 DEBUGTHREADS("is WAITING", t);
818         }
819 }
820
821
822 /* thread_set_state_timed_waiting **********************************************
823
824    Set the current state of the given thread to
825    THREAD_STATE_TIMED_WAITING.
826
827    NOTE: If the thread has already terminated, don't set the state.
828          This is important for threads_detach_thread.
829
830 *******************************************************************************/
831
832 void thread_set_state_timed_waiting(threadobject *t)
833 {
834         if (t->state != THREAD_STATE_TERMINATED) {
835                 thread_set_state(t, THREAD_STATE_TIMED_WAITING);
836
837                 DEBUGTHREADS("is TIMED_WAITING", t);
838         }
839 }
840
841
842 /* thread_set_state_parked *****************************************************
843
844    Set the current state of the given thread to THREAD_STATE_PARKED.
845
846    NOTE: If the thread has already terminated, don't set the state.
847          This is important for threads_detach_thread.
848
849 *******************************************************************************/
850
851 void thread_set_state_parked(threadobject *t)
852 {
853         if (t->state != THREAD_STATE_TERMINATED) {
854                 thread_set_state(t, THREAD_STATE_PARKED);
855
856                 DEBUGTHREADS("is PARKED", t);
857         }
858 }
859
860
861 /* thread_set_state_timed_parked ***********************************************
862
863    Set the current state of the given thread to THREAD_STATE_TIMED_PARKED.
864
865    NOTE: If the thread has already terminated, don't set the state.
866          This is important for threads_detach_thread.
867
868 *******************************************************************************/
869
870 void thread_set_state_timed_parked(threadobject *t)
871 {
872         if (t->state != THREAD_STATE_TERMINATED) {
873                 thread_set_state(t, THREAD_STATE_TIMED_PARKED);
874
875                 DEBUGTHREADS("is TIMED_PARKED", t);
876         }
877 }
878
879
880 /* thread_set_state_terminated *************************************************
881
882    Set the current state of the given thread to
883    THREAD_STATE_TERMINATED.
884
885 *******************************************************************************/
886
887 void thread_set_state_terminated(threadobject *t)
888 {
889         /* Set the state inside a lock. */
890
891         thread_set_state(t, THREAD_STATE_TERMINATED);
892
893         DEBUGTHREADS("is TERMINATED", t);
894 }
895
896
897 /* thread_get_thread **********************************************************
898
899    Return the thread data structure of the given Java thread object.
900
901    ARGUMENTS:
902        h ... java.lang.{VM}Thread object
903
904    RETURN VALUE:
905        the thread object
906
907    NOTE:
908        Usage of this function without the thread list lock held is
909        almost certainly a bug.
910
911 *******************************************************************************/
912
913 threadobject *thread_get_thread(java_handle_t *h)
914 {
915         return ThreadRuntime::get_threadobject_from_thread(h);
916 }
917
918
919 /* threads_thread_is_alive *****************************************************
920
921    Returns if the give thread is alive.
922
923 *******************************************************************************/
924
925 bool threads_thread_is_alive(threadobject *t)
926 {
927         int state;
928
929         state = cacaothread_get_state(t);
930
931         switch (state) {
932         case THREAD_STATE_NEW:
933         case THREAD_STATE_TERMINATED:
934                 return false;
935
936         case THREAD_STATE_RUNNABLE:
937         case THREAD_STATE_BLOCKED:
938         case THREAD_STATE_WAITING:
939         case THREAD_STATE_TIMED_WAITING:
940         case THREAD_STATE_PARKED:
941         case THREAD_STATE_TIMED_PARKED:
942                 return true;
943
944         default:
945                 vm_abort("threads_thread_is_alive: unknown thread state %d", state);
946         }
947
948         /* keep compiler happy */
949
950         return false;
951 }
952
953 /* thread_is_interrupted *******************************************************
954
955    Check if the given thread has been interrupted.
956
957    ARGUMENTS:
958        t ... the thread to check
959
960    RETURN VALUE:
961       true, if the given thread had been interrupted
962
963 *******************************************************************************/
964
965 bool thread_is_interrupted(threadobject *t)
966 {
967         /* We need the mutex because classpath will call this function when
968            a blocking system call is interrupted. The mutex ensures that it will
969            see the correct value for the interrupted flag. */
970
971         t->waitmutex->lock();
972         bool interrupted = t->interrupted;
973         t->waitmutex->unlock();
974
975         return interrupted;
976 }
977
978
979 /* thread_set_interrupted ******************************************************
980
981    Set the interrupted flag to the given value.
982
983    ARGUMENTS:
984        interrupted ... value to set
985
986 *******************************************************************************/
987
988 void thread_set_interrupted(threadobject *t, bool interrupted)
989 {
990         t->waitmutex->lock();
991         t->interrupted = interrupted;
992         t->waitmutex->unlock();
993 }
994
995 /* thread_handle_set_priority **************************************************
996
997    Calls threads_set_thread_priority for the threadobject associated
998    with the thread indicated by handle th, while holding the thread
999    list lock.
1000
1001 *******************************************************************************/
1002
1003 void thread_handle_set_priority(java_handle_t *th, int priority)
1004 {
1005         threadobject *t = thread_get_thread(th);
1006         /* For GNU classpath, this should not happen, because both
1007            setPriority() and start() are synchronized. */
1008         assert(t != 0);
1009         threads_set_thread_priority(t->tid, priority);
1010 }
1011
1012 /* thread_handle_is_interrupted ************************************************
1013
1014    Calls thread_is_interrupted for the threadobject associated with
1015    the thread indicated by handle th, while holding the thread list
1016    lock.
1017
1018 *******************************************************************************/
1019
1020 bool thread_handle_is_interrupted(java_handle_t *th)
1021 {
1022         threadobject *t = thread_get_thread(th);
1023         return t ? thread_is_interrupted(t) : false;
1024 }
1025
1026 /* thread_handle_interrupt *****************************************************
1027
1028    Calls threads_thread_interrupt for the threadobject associated with
1029    the thread indicated by handle th, while holding the thread list
1030    lock.
1031
1032 *******************************************************************************/
1033
1034 void thread_handle_interrupt(java_handle_t *th)
1035 {
1036         threadobject *t = thread_get_thread(th);
1037         /* For GNU classpath, this should not happen, because both
1038            interrupt() and start() are synchronized. */
1039         assert(t != 0);
1040         threads_thread_interrupt(t);
1041 }
1042
1043 /* thread_handle_get_state *****************************************************
1044
1045    Calls cacaothread_get_state for the threadobject associated with
1046    the thread indicated by handle th, while holding the thread list
1047    lock.
1048
1049 *******************************************************************************/
1050
1051 int thread_handle_get_state(java_handle_t *th)
1052 {
1053         threadobject *t = thread_get_thread(th);
1054         return t ? cacaothread_get_state(t) : THREAD_STATE_NEW;
1055 }
1056
1057
1058 /*
1059  * These are local overrides for various environment variables in Emacs.
1060  * Please do not remove this and leave it at the end of the file, where
1061  * Emacs will automagically detect them.
1062  * ---------------------------------------------------------------------
1063  * Local variables:
1064  * mode: c++
1065  * indent-tabs-mode: t
1066  * c-basic-offset: 4
1067  * tab-width: 4
1068  * End:
1069  * vim:noexpandtab:sw=4:ts=4:
1070  */