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