* src/threads/thread.cpp: Set thread to RUNNABLE during Thread.start.
[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         thread_set_state_runnable(t);
473
474         /* Start the thread.  Don't pass a function pointer (NULL) since
475            we want Thread.run()V here. */
476
477         threads_impl_thread_start(t, NULL);
478 }
479
480
481 /**
482  * Attaches the current thread to the VM.
483  *
484  * @param vm_aargs Attach arguments.
485  * @param isdaemon true if the attached thread should be a daemon
486  *                 thread.
487  *
488  * @return true on success, false otherwise.
489  */
490 bool thread_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon)
491 {
492         bool           result;
493         threadobject  *t;
494         utf           *u;
495         java_handle_t *name;
496         java_handle_t *group;
497
498     /* If the current thread has already been attached, this operation
499            is a no-op. */
500
501         result = thread_current_is_attached();
502
503         if (result == true)
504                 return true;
505
506         /* Create internal thread data structure. */
507
508         u4 flags = THREAD_FLAG_JAVA;
509         if (isdaemon)
510                 flags |= THREAD_FLAG_DAEMON;
511
512         t = thread_new(flags);
513
514         /* Store the internal thread data-structure in the TSD. */
515
516         thread_set_current(t);
517
518         /* The thread is flagged and (non-)daemon thread, we can leave the
519            mutex. */
520
521         /* Add the thread to the thread list. */
522
523         ThreadList::add_to_active_thread_list(t);
524
525         DEBUGTHREADS("attaching", t);
526
527         /* Get the thread name. */
528
529         if (vm_aargs != NULL) {
530                 u = utf_new_char(vm_aargs->name);
531         }
532         else {
533                 u = utf_null;
534         }
535
536         name = javastring_new(u);
537
538 #if defined(ENABLE_JAVASE)
539         /* Get the threadgroup. */
540
541         if (vm_aargs != NULL)
542                 group = (java_handle_t *) vm_aargs->group;
543         else
544                 group = NULL;
545
546         /* If no threadgroup was given, use the main threadgroup. */
547
548         if (group == NULL)
549                 group = threadgroup_main;
550 #endif
551
552 #if defined(ENABLE_INTRP)
553         /* create interpreter stack */
554
555         if (opt_intrp) {
556                 MSET(intrp_main_stack, 0, u1, opt_stacksize);
557                 thread->_global_sp = (Cell *) (intrp_main_stack + opt_stacksize);
558         }
559 #endif
560
561         /* Create the Java thread object. */
562
563         if (!thread_create_object(t, name, group)) {
564                 ThreadList::release_thread(t, true);
565                 return false;
566         }
567
568         /* The thread is completely initialized. */
569
570         thread_set_state_runnable(t);
571
572         return true;
573 }
574
575
576 /**
577  * Attaches the current external thread to the VM.  This function is
578  * called by JNI's AttachCurrentThread.
579  *
580  * @param vm_aargs Attach arguments.
581  * @param isdaemon true if the attached thread should be a daemon
582  *                 thread.
583  *
584  * @return true on success, false otherwise.
585  */
586 bool thread_attach_current_external_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon)
587 {
588         int result;
589
590 #if defined(ENABLE_GC_BOEHM)
591         struct GC_stack_base sb;
592
593         /* Register the thread with Boehm-GC.  This must happen before the
594            thread allocates any memory from the GC heap.*/
595
596         result = GC_get_stack_base(&sb);
597
598         if (result != GC_SUCCESS)
599                 vm_abort("threads_attach_current_thread: GC_get_stack_base failed");
600
601         GC_register_my_thread(&sb);
602 #endif
603
604         result = thread_attach_current_thread(vm_aargs, isdaemon);
605
606         if (result == false) {
607 #if defined(ENABLE_GC_BOEHM)
608                 /* Unregister the thread. */
609
610                 GC_unregister_my_thread();
611 #endif
612
613                 return false;
614         }
615
616         return true;
617 }
618
619
620 /**
621  * Detaches the current external thread from the VM.  This function is
622  * called by JNI's DetachCurrentThread.
623  *
624  * @return true on success, false otherwise.
625  */
626 bool thread_detach_current_external_thread(void)
627 {
628         int result;
629
630         result = thread_detach_current_thread();
631
632         if (result == false)
633                 return false;
634
635 #if defined(ENABLE_GC_BOEHM)
636         /* Unregister the thread with Boehm-GC.  This must happen after
637            the thread allocates any memory from the GC heap. */
638
639         /* Don't detach the main thread.  This is a workaround for
640            OpenJDK's java launcher. */
641         if (thread_get_current()->index != 1)
642                 GC_unregister_my_thread();
643 #endif
644
645         return true;
646 }
647
648
649 /* thread_fprint_name **********************************************************
650
651    Print the name of the given thread to the given stream.
652
653    ARGUMENTS:
654        t ........ thread data-structure
655        stream ... stream to print to
656
657 *******************************************************************************/
658
659 void thread_fprint_name(threadobject *t, FILE *stream)
660 {
661         if (LLNI_WRAP(t->object) == NULL)
662                 vm_abort("");
663
664         java_lang_Thread jlt(LLNI_WRAP(t->object));
665
666         ThreadRuntime::print_thread_name(jlt, stream);
667 }
668
669
670 /* thread_print_info ***********************************************************
671
672    Print information of the passed thread.
673
674    ARGUMENTS:
675        t ... thread data-structure.
676
677 *******************************************************************************/
678
679 void thread_print_info(threadobject *t)
680 {
681         java_lang_Thread jlt(LLNI_WRAP(t->object));
682
683         /* Print as much as we can when we are in state NEW. */
684
685         if (jlt.get_handle() != NULL) {
686                 /* Print thread name. */
687
688                 printf("\"");
689                 thread_fprint_name(t, stdout);
690                 printf("\"");
691         }
692         else {
693         }
694
695         if (thread_is_daemon(t))
696                 printf(" daemon");
697
698         if (jlt.get_handle() != NULL) {
699                 printf(" prio=%d", jlt.get_priority());
700         }
701
702 #if SIZEOF_VOID_P == 8
703         printf(" t=0x%016lx tid=0x%016lx (%ld)",
704                    (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
705 #else
706         printf(" t=0x%08x tid=0x%08x (%d)",
707                    (ptrint) t, (ptrint) t->tid, (ptrint) t->tid);
708 #endif
709
710         printf(" index=%d", t->index);
711
712         /* Print thread state. */
713
714         int state = cacaothread_get_state(t);
715
716         switch (state) {
717         case THREAD_STATE_NEW:
718                 printf(" new");
719                 break;
720         case THREAD_STATE_RUNNABLE:
721                 printf(" runnable");
722                 break;
723         case THREAD_STATE_BLOCKED:
724                 printf(" blocked");
725                 break;
726         case THREAD_STATE_WAITING:
727                 printf(" waiting");
728                 break;
729         case THREAD_STATE_TIMED_WAITING:
730                 printf(" waiting on condition");
731                 break;
732         case THREAD_STATE_PARKED:
733                 printf(" parked");
734                 break;
735         case THREAD_STATE_TIMED_PARKED:
736                 printf(" timed parked");
737                 break;
738         case THREAD_STATE_TERMINATED:
739                 printf(" terminated");
740                 break;
741         default:
742                 vm_abort("thread_print_info: unknown thread state %d", state);
743         }
744 }
745
746
747 /* threads_get_current_tid *****************************************************
748
749    Return the tid of the current thread.
750    
751    RETURN VALUE:
752        the current tid
753
754 *******************************************************************************/
755
756 intptr_t threads_get_current_tid(void)
757 {
758         threadobject *thread;
759
760         thread = THREADOBJECT;
761
762         /* this may happen during bootstrap */
763
764         if (thread == NULL)
765                 return 0;
766
767         return (intptr_t) thread->tid;
768 }
769
770
771 /**
772  * Set the current state of the given thread. This method should only
773  * be called while holding the threadlist-lock and after checking that
774  * the new state is valid. It is best to not call this method directly
775  * but call the specific setter methods below.
776  */
777 static inline void thread_set_state(threadobject *t, int state)
778 {
779         // Set the state of our internal threadobject.
780         t->state = state;
781
782         ThreadRuntime::set_javathread_state(t, state);
783 }
784
785
786 /* thread_set_state_runnable ***************************************************
787
788    Set the current state of the given thread to THREAD_STATE_RUNNABLE.
789
790    NOTE: If the thread has already terminated, don't set the state.
791          This is important for threads_detach_thread.
792
793 *******************************************************************************/
794
795 void thread_set_state_runnable(threadobject *t)
796 {
797         if (t->state != THREAD_STATE_TERMINATED) {
798                 thread_set_state(t, THREAD_STATE_RUNNABLE);
799
800                 DEBUGTHREADS("is RUNNABLE", t);
801         }
802 }
803
804
805 /* thread_set_state_waiting ****************************************************
806
807    Set the current state of the given thread to THREAD_STATE_WAITING.
808
809    NOTE: If the thread has already terminated, don't set the state.
810          This is important for threads_detach_thread.
811
812 *******************************************************************************/
813
814 void thread_set_state_waiting(threadobject *t)
815 {
816         if (t->state != THREAD_STATE_TERMINATED) {
817                 thread_set_state(t, THREAD_STATE_WAITING);
818
819                 DEBUGTHREADS("is WAITING", t);
820         }
821 }
822
823
824 /* thread_set_state_timed_waiting **********************************************
825
826    Set the current state of the given thread to
827    THREAD_STATE_TIMED_WAITING.
828
829    NOTE: If the thread has already terminated, don't set the state.
830          This is important for threads_detach_thread.
831
832 *******************************************************************************/
833
834 void thread_set_state_timed_waiting(threadobject *t)
835 {
836         if (t->state != THREAD_STATE_TERMINATED) {
837                 thread_set_state(t, THREAD_STATE_TIMED_WAITING);
838
839                 DEBUGTHREADS("is TIMED_WAITING", t);
840         }
841 }
842
843
844 /* thread_set_state_parked *****************************************************
845
846    Set the current state of the given thread to THREAD_STATE_PARKED.
847
848    NOTE: If the thread has already terminated, don't set the state.
849          This is important for threads_detach_thread.
850
851 *******************************************************************************/
852
853 void thread_set_state_parked(threadobject *t)
854 {
855         if (t->state != THREAD_STATE_TERMINATED) {
856                 thread_set_state(t, THREAD_STATE_PARKED);
857
858                 DEBUGTHREADS("is PARKED", t);
859         }
860 }
861
862
863 /* thread_set_state_timed_parked ***********************************************
864
865    Set the current state of the given thread to THREAD_STATE_TIMED_PARKED.
866
867    NOTE: If the thread has already terminated, don't set the state.
868          This is important for threads_detach_thread.
869
870 *******************************************************************************/
871
872 void thread_set_state_timed_parked(threadobject *t)
873 {
874         if (t->state != THREAD_STATE_TERMINATED) {
875                 thread_set_state(t, THREAD_STATE_TIMED_PARKED);
876
877                 DEBUGTHREADS("is TIMED_PARKED", t);
878         }
879 }
880
881
882 /* thread_set_state_terminated *************************************************
883
884    Set the current state of the given thread to
885    THREAD_STATE_TERMINATED.
886
887 *******************************************************************************/
888
889 void thread_set_state_terminated(threadobject *t)
890 {
891         /* Set the state inside a lock. */
892
893         thread_set_state(t, THREAD_STATE_TERMINATED);
894
895         DEBUGTHREADS("is TERMINATED", t);
896 }
897
898
899 /* thread_get_thread **********************************************************
900
901    Return the thread data structure of the given Java thread object.
902
903    ARGUMENTS:
904        h ... java.lang.{VM}Thread object
905
906    RETURN VALUE:
907        the thread object
908
909    NOTE:
910        Usage of this function without the thread list lock held is
911        almost certainly a bug.
912
913 *******************************************************************************/
914
915 threadobject *thread_get_thread(java_handle_t *h)
916 {
917         return ThreadRuntime::get_threadobject_from_thread(h);
918 }
919
920
921 /* threads_thread_is_alive *****************************************************
922
923    Returns if the give thread is alive.
924
925 *******************************************************************************/
926
927 bool threads_thread_is_alive(threadobject *t)
928 {
929         int state;
930
931         state = cacaothread_get_state(t);
932
933         switch (state) {
934         case THREAD_STATE_NEW:
935         case THREAD_STATE_TERMINATED:
936                 return false;
937
938         case THREAD_STATE_RUNNABLE:
939         case THREAD_STATE_BLOCKED:
940         case THREAD_STATE_WAITING:
941         case THREAD_STATE_TIMED_WAITING:
942         case THREAD_STATE_PARKED:
943         case THREAD_STATE_TIMED_PARKED:
944                 return true;
945
946         default:
947                 vm_abort("threads_thread_is_alive: unknown thread state %d", state);
948         }
949
950         /* keep compiler happy */
951
952         return false;
953 }
954
955 /* thread_is_interrupted *******************************************************
956
957    Check if the given thread has been interrupted.
958
959    ARGUMENTS:
960        t ... the thread to check
961
962    RETURN VALUE:
963       true, if the given thread had been interrupted
964
965 *******************************************************************************/
966
967 bool thread_is_interrupted(threadobject *t)
968 {
969         /* We need the mutex because classpath will call this function when
970            a blocking system call is interrupted. The mutex ensures that it will
971            see the correct value for the interrupted flag. */
972
973         t->waitmutex->lock();
974         bool interrupted = t->interrupted;
975         t->waitmutex->unlock();
976
977         return interrupted;
978 }
979
980
981 /* thread_set_interrupted ******************************************************
982
983    Set the interrupted flag to the given value.
984
985    ARGUMENTS:
986        interrupted ... value to set
987
988 *******************************************************************************/
989
990 void thread_set_interrupted(threadobject *t, bool interrupted)
991 {
992         t->waitmutex->lock();
993         t->interrupted = interrupted;
994         t->waitmutex->unlock();
995 }
996
997 /* thread_handle_set_priority **************************************************
998
999    Calls threads_set_thread_priority for the threadobject associated
1000    with the thread indicated by handle th, while holding the thread
1001    list lock.
1002
1003 *******************************************************************************/
1004
1005 void thread_handle_set_priority(java_handle_t *th, int priority)
1006 {
1007         threadobject *t = thread_get_thread(th);
1008         /* For GNU classpath, this should not happen, because both
1009            setPriority() and start() are synchronized. */
1010         assert(t != 0);
1011         threads_set_thread_priority(t->tid, priority);
1012 }
1013
1014 /* thread_handle_is_interrupted ************************************************
1015
1016    Calls thread_is_interrupted for the threadobject associated with
1017    the thread indicated by handle th, while holding the thread list
1018    lock.
1019
1020 *******************************************************************************/
1021
1022 bool thread_handle_is_interrupted(java_handle_t *th)
1023 {
1024         threadobject *t = thread_get_thread(th);
1025         return t ? thread_is_interrupted(t) : false;
1026 }
1027
1028 /* thread_handle_interrupt *****************************************************
1029
1030    Calls threads_thread_interrupt for the threadobject associated with
1031    the thread indicated by handle th, while holding the thread list
1032    lock.
1033
1034 *******************************************************************************/
1035
1036 void thread_handle_interrupt(java_handle_t *th)
1037 {
1038         threadobject *t = thread_get_thread(th);
1039         /* For GNU classpath, this should not happen, because both
1040            interrupt() and start() are synchronized. */
1041         assert(t != 0);
1042         threads_thread_interrupt(t);
1043 }
1044
1045 /* thread_handle_get_state *****************************************************
1046
1047    Calls cacaothread_get_state for the threadobject associated with
1048    the thread indicated by handle th, while holding the thread list
1049    lock.
1050
1051 *******************************************************************************/
1052
1053 int thread_handle_get_state(java_handle_t *th)
1054 {
1055         threadobject *t = thread_get_thread(th);
1056         return t ? cacaothread_get_state(t) : THREAD_STATE_NEW;
1057 }
1058
1059
1060 /*
1061  * These are local overrides for various environment variables in Emacs.
1062  * Please do not remove this and leave it at the end of the file, where
1063  * Emacs will automagically detect them.
1064  * ---------------------------------------------------------------------
1065  * Local variables:
1066  * mode: c++
1067  * indent-tabs-mode: t
1068  * c-basic-offset: 4
1069  * tab-width: 4
1070  * End:
1071  * vim:noexpandtab:sw=4:ts=4:
1072  */