* src/threads/thread.cpp: Set thread to RUNNABLE during Thread.start.
[cacao.git] / src / threads / posix / thread-posix.cpp
1 /* src/threads/posix/thread-posix.cpp - POSIX 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 /* XXX cleanup these includes */
29
30 #define __STDC_LIMIT_MACROS
31
32 #include <stdlib.h>
33 #include <string.h>
34 #include <assert.h>
35 #include <sys/types.h>
36 #include <unistd.h>
37 #include <sys/time.h>
38 #include <time.h>
39 #include <errno.h>
40
41 #include <pthread.h>
42
43 #include "vm/types.h"
44
45 #include "arch.h"
46
47 #include "mm/gc.hpp"
48 #include "mm/memory.hpp"
49
50 #if defined(ENABLE_GC_CACAO)
51 # include "mm/cacao-gc/gc.h"
52 #endif
53
54 #include "native/llni.h"
55 #include "native/native.hpp"
56
57 #include "threads/condition.hpp"
58 #include "threads/lock.hpp"
59 #include "threads/mutex.hpp"
60 #include "threads/threadlist.hpp"
61 #include "threads/thread.hpp"
62
63 #include "toolbox/logging.hpp"
64
65 #include "vm/jit/builtin.hpp"
66 #include "vm/exceptions.hpp"
67 #include "vm/global.h"
68 #include "vm/globals.hpp"
69 #include "vm/hook.hpp"
70 #include "vm/javaobjects.hpp"
71 #include "vm/options.h"
72 #include "vm/os.hpp"
73 #include "vm/signallocal.hpp"
74 #include "vm/string.hpp"
75 #include "vm/vm.hpp"
76
77 #if defined(ENABLE_STATISTICS)
78 # include "vm/statistics.h"
79 #endif
80
81 #include "vm/jit/asmpart.h"
82
83 #if defined(__DARWIN__)
84
85 typedef struct {
86         Mutex* mutex;
87         Condition* cond;
88         int value;
89 } sem_t;
90
91 #else
92 # include <semaphore.h>
93 #endif
94
95 #if defined(__LINUX__)
96 # define GC_LINUX_THREADS
97 #elif defined(__IRIX__)
98 # define GC_IRIX_THREADS
99 #elif defined(__DARWIN__)
100 # define GC_DARWIN_THREADS
101 #elif defined(__SOLARIS__)
102 # define GC_SOLARIS_THREADS
103 #endif
104
105 #if defined(ENABLE_GC_BOEHM)
106 /* We need to include Boehm's gc.h here because it overrides
107    pthread_create and friends. */
108 # include "mm/boehm-gc/include/gc.h"
109 #endif
110
111
112 #if defined(__DARWIN__)
113 /* Darwin has no working semaphore implementation.  This one is taken
114    from Boehm-GC. */
115
116 /*
117    This is a very simple semaphore implementation for Darwin. It
118    is implemented in terms of pthreads calls so it isn't async signal
119    safe. This isn't a problem because signals aren't used to
120    suspend threads on Darwin.
121 */
122    
123 static int sem_init(sem_t *sem, int pshared, int value)
124 {
125         if (pshared)
126                 assert(0);
127
128         sem->mutex = new Mutex();
129         sem->cond  = new Condition();
130         sem->value = value;
131
132         return 0;
133 }
134
135 static int sem_post(sem_t *sem)
136 {
137         sem->mutex->lock();
138         sem->value++;
139         sem->cond->signal();
140         sem->mutex->unlock();
141
142         return 0;
143 }
144
145 static int sem_wait(sem_t *sem)
146 {
147         sem->mutex->lock();
148
149         while (sem->value == 0) {
150                 sem->cond->wait(sem->mutex);
151         }
152
153         sem->value--;
154         sem->mutex->unlock();
155
156         return 0;
157 }
158
159 static int sem_destroy(sem_t *sem)
160 {
161         delete sem->cond;
162         delete sem->mutex;
163
164         return 0;
165 }
166 #endif /* defined(__DARWIN__) */
167
168
169 /* startupinfo *****************************************************************
170
171    Struct used to pass info from threads_start_thread to 
172    threads_startup_thread.
173
174 ******************************************************************************/
175
176 typedef struct {
177         threadobject *thread;      /* threadobject for this thread             */
178         functionptr   function;    /* function to run in the new thread        */
179         sem_t        *psem;        /* signals when thread has been entered     */
180                                    /* in the thread list                       */
181         sem_t        *psem_first;  /* signals when pthread_create has returned */
182 } startupinfo;
183
184
185 /* prototypes *****************************************************************/
186
187 static void threads_calc_absolute_time(struct timespec *tm, s8 millis, s4 nanos);
188
189
190 /******************************************************************************/
191 /* GLOBAL VARIABLES                                                           */
192 /******************************************************************************/
193
194 /* the thread object of the current thread                                    */
195 /* This is either a thread-local variable defined with __thread, or           */
196 /* a thread-specific value stored with key threads_current_threadobject_key.  */
197 #if defined(HAVE___THREAD)
198 __thread threadobject *thread_current;
199 #else
200 pthread_key_t thread_current_key;
201 #endif
202
203 /* global mutex for stop-the-world                                            */
204 static Mutex* stopworldlock;
205
206 #if defined(ENABLE_GC_CACAO)
207 /* global mutex for the GC */
208 static Mutex* mutex_gc;
209 #endif
210
211 /* global mutex and condition for joining threads on exit */
212 static Condition* cond_join;
213
214 #if defined(ENABLE_GC_CACAO)
215 /* semaphore used for acknowleding thread suspension                          */
216 static sem_t suspend_ack;
217 #endif
218
219
220 /* threads_sem_init ************************************************************
221  
222    Initialize a semaphore. Checks against errors and interruptions.
223
224    IN:
225        sem..............the semaphore to initialize
226            shared...........true if this semaphore will be shared between processes
227            value............the initial value for the semaphore
228    
229 *******************************************************************************/
230
231 void threads_sem_init(sem_t *sem, bool shared, int value)
232 {
233         int r;
234
235         assert(sem);
236
237         do {
238                 r = sem_init(sem, shared, value);
239                 if (r == 0)
240                         return;
241         } while (errno == EINTR);
242
243         vm_abort("sem_init failed: %s", strerror(errno));
244 }
245
246
247 /* threads_sem_wait ************************************************************
248  
249    Wait for a semaphore, non-interruptible.
250
251    IMPORTANT: Always use this function instead of `sem_wait` directly, as
252               `sem_wait` may be interrupted by signals!
253   
254    IN:
255        sem..............the semaphore to wait on
256    
257 *******************************************************************************/
258
259 void threads_sem_wait(sem_t *sem)
260 {
261         int r;
262
263         assert(sem);
264
265         do {
266                 r = sem_wait(sem);
267                 if (r == 0)
268                         return;
269         } while (errno == EINTR);
270
271         vm_abort("sem_wait failed: %s", strerror(errno));
272 }
273
274
275 /* threads_sem_post ************************************************************
276  
277    Increase the count of a semaphore. Checks for errors.
278
279    IN:
280        sem..............the semaphore to increase the count of
281    
282 *******************************************************************************/
283
284 void threads_sem_post(sem_t *sem)
285 {
286         int r;
287
288         assert(sem);
289
290         /* unlike sem_wait, sem_post is not interruptible */
291
292         r = sem_post(sem);
293         if (r == 0)
294                 return;
295
296         vm_abort("sem_post failed: %s", strerror(errno));
297 }
298
299
300 /* threads_stopworld ***********************************************************
301
302    Stops the world from turning. All threads except the calling one
303    are suspended. The function returns as soon as all threads have
304    acknowledged their suspension.
305
306 *******************************************************************************/
307
308 #if defined(ENABLE_GC_CACAO)
309 void threads_stopworld(void)
310 {
311 #if !defined(__DARWIN__) && !defined(__CYGWIN__)
312         threadobject *t;
313         threadobject *self;
314         bool result;
315         s4 count, i;
316 #endif
317
318         stopworldlock->lock();
319
320         /* lock the threads lists */
321
322         threadlist_lock();
323
324 #if defined(__DARWIN__)
325         /*threads_cast_darwinstop();*/
326         assert(0);
327 #elif defined(__CYGWIN__)
328         /* TODO */
329         assert(0);
330 #else
331         self = THREADOBJECT;
332
333         DEBUGTHREADS("stops World", self);
334
335         count = 0;
336
337         /* suspend all running threads */
338         for (t = threadlist_first(); t != NULL; t = threadlist_next(t)) {
339                 /* don't send the signal to ourself */
340
341                 if (t == self)
342                         continue;
343
344                 /* don't send the signal to NEW threads (because they are not
345                    completely initialized) */
346
347                 if (t->state == THREAD_STATE_NEW)
348                         continue;
349
350                 /* send the signal */
351
352                 result = threads_suspend_thread(t, SUSPEND_REASON_STOPWORLD);
353                 assert(result);
354
355                 /* increase threads count */
356
357                 count++;
358         }
359
360         /* wait for all threads signaled to suspend */
361         for (i = 0; i < count; i++)
362                 threads_sem_wait(&suspend_ack);
363 #endif
364
365         /* ATTENTION: Don't unlock the threads-lists here so that
366            non-signaled NEW threads can't change their state and execute
367            code. */
368 }
369 #endif
370
371
372 /* threads_startworld **********************************************************
373
374    Starts the world again after it has previously been stopped. 
375
376 *******************************************************************************/
377
378 #if defined(ENABLE_GC_CACAO)
379 void threads_startworld(void)
380 {
381 #if !defined(__DARWIN__) && !defined(__CYGWIN__)
382         threadobject *t;
383         threadobject *self;
384         bool result;
385         s4 count, i;
386 #endif
387
388 #if defined(__DARWIN__)
389         /*threads_cast_darwinresume();*/
390         assert(0);
391 #elif defined(__IRIX__)
392         threads_cast_irixresume();
393 #elif defined(__CYGWIN__)
394         /* TODO */
395         assert(0);
396 #else
397         self = THREADOBJECT;
398
399         DEBUGTHREADS("starts World", self);
400
401         count = 0;
402
403         /* resume all thread we haltet */
404         for (t = threadlist_first(); t != NULL; t = threadlist_next(t)) {
405                 /* don't send the signal to ourself */
406
407                 if (t == self)
408                         continue;
409
410                 /* don't send the signal to NEW threads (because they are not
411                    completely initialized) */
412
413                 if (t->state == THREAD_STATE_NEW)
414                         continue;
415
416                 /* send the signal */
417
418                 result = threads_resume_thread(t);
419                 assert(result);
420
421                 /* increase threads count */
422
423                 count++;
424         }
425
426         /* wait for all threads signaled to suspend */
427         for (i = 0; i < count; i++)
428                 threads_sem_wait(&suspend_ack);
429
430 #endif
431
432         /* unlock the threads lists */
433
434         threadlist_unlock();
435
436         stopworldlock->unlock();
437 }
438 #endif
439
440
441 /* threads_impl_thread_clear ***************************************************
442
443    Clears all fields in threadobject the way an MZERO would have
444    done. MZERO cannot be used anymore because it would mess up the
445    pthread_* bits.
446
447    IN:
448       t....the threadobject
449
450 *******************************************************************************/
451
452 void threads_impl_thread_clear(threadobject *t)
453 {
454         t->object = NULL;
455
456         t->thinlock = 0;
457
458         t->index = 0;
459         t->flags = 0;
460         t->state = 0;
461         t->is_in_active_list = false;
462
463         t->tid = 0;
464
465 #if defined(__DARWIN__)
466         t->mach_thread = 0;
467 #endif
468
469         t->interrupted = false;
470         t->signaled = false;
471
472         t->suspended = false;
473         t->suspend_reason = 0;
474
475         t->pc = NULL;
476
477         t->_exceptionptr = NULL;
478         t->_stackframeinfo = NULL;
479         t->_localref_table = NULL;
480
481 #if defined(ENABLE_INTRP)
482         t->_global_sp = NULL;
483 #endif
484
485 #if defined(ENABLE_GC_CACAO)
486         t->gc_critical = false;
487
488         t->ss = NULL;
489         t->es = NULL;
490 #endif
491
492         // Simply reuse the existing dump memory.
493 }
494
495 /* threads_impl_thread_reuse ***************************************************
496
497    Resets some implementation fields in threadobject. This was
498    previously done in threads_impl_thread_new.
499
500    IN:
501       t....the threadobject
502
503 *******************************************************************************/
504
505 void threads_impl_thread_reuse(threadobject *t)
506 {
507         /* get the pthread id */
508
509         t->tid = pthread_self();
510
511 #if defined(ENABLE_DEBUG_FILTER)
512         /* Initialize filter counters */
513         t->filterverbosecallctr[0] = 0;
514         t->filterverbosecallctr[1] = 0;
515 #endif
516
517 #if !defined(NDEBUG)
518         t->tracejavacallindent = 0;
519         t->tracejavacallcount = 0;
520 #endif
521
522         t->flc_bit = false;
523         t->flc_next = NULL;
524         t->flc_list = NULL;
525
526 /*      not really needed */
527         t->flc_object = NULL;
528
529 #if defined(ENABLE_TLH)
530         tlh_destroy(&(t->tlh));
531         tlh_init(&(t->tlh));
532 #endif
533 }
534
535 void threads_impl_clear_heap_pointers(threadobject *t)
536 {
537         t->object = 0;
538         t->flc_object = 0;
539 }
540
541 /* threads_impl_preinit ********************************************************
542
543    Do some early initialization of stuff required.
544
545    ATTENTION: Do NOT use any Java heap allocation here, as gc_init()
546    is called AFTER this function!
547
548 *******************************************************************************/
549
550 void threads_impl_preinit(void)
551 {
552         stopworldlock = new Mutex();
553
554         /* initialize exit mutex and condition (on exit we join all
555            threads) */
556
557         cond_join = new Condition();
558
559 #if defined(ENABLE_GC_CACAO)
560         /* initialize the GC mutex & suspend semaphore */
561
562         mutex_gc = new Mutex();
563         threads_sem_init(&suspend_ack, 0, 0);
564 #endif
565
566 #if !defined(HAVE___THREAD)
567         int result = pthread_key_create(&thread_current_key, NULL);
568         if (result != 0)
569                 os::abort_errnum(result, "threads_impl_preinit: pthread_key_create failed");
570 #endif
571 }
572
573
574 /* threads_mutex_gc_lock *******************************************************
575
576    Enter the global GC mutex.
577
578 *******************************************************************************/
579
580 #if defined(ENABLE_GC_CACAO)
581 void threads_mutex_gc_lock(void)
582 {
583         mutex_gc->lock();
584 }
585 #endif
586
587
588 /* threads_mutex_gc_unlock *****************************************************
589
590    Leave the global GC mutex.
591
592 *******************************************************************************/
593
594 #if defined(ENABLE_GC_CACAO)
595 void threads_mutex_gc_unlock(void)
596 {
597         mutex_gc->unlock();
598 }
599 #endif
600
601 /* threads_impl_init ***********************************************************
602
603    Initializes the implementation specific bits.
604
605 *******************************************************************************/
606
607 void threads_impl_init(void)
608 {
609         pthread_attr_t attr;
610         int            result;
611
612         threads_set_thread_priority(pthread_self(), NORM_PRIORITY);
613
614         /* Initialize the thread attribute object. */
615
616         result = pthread_attr_init(&attr);
617
618         if (result != 0)
619                 os::abort_errnum(result, "threads_impl_init: pthread_attr_init failed");
620
621         result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
622
623         if (result != 0)
624                 os::abort_errnum(result, "threads_impl_init: pthread_attr_setdetachstate failed");
625 }
626
627
628 /* threads_startup_thread ******************************************************
629
630    Thread startup function called by pthread_create.
631
632    Thread which have a startup.function != NULL are marked as internal
633    threads. All other threads are threated as normal Java threads.
634
635    NOTE: This function is not called directly by pthread_create. The Boehm GC
636          inserts its own GC_start_routine in between, which then calls
637                  threads_startup.
638
639    IN:
640       arg..........the argument passed to pthread_create, ie. a pointer to
641                        a startupinfo struct. CAUTION: When the `psem` semaphore
642                                    is posted, the startupinfo struct becomes invalid! (It
643                                    is allocated on the stack of threads_start_thread.)
644
645 ******************************************************************************/
646
647 static void *threads_startup_thread(void *arg)
648 {
649         startupinfo  *startup;
650         threadobject *t;
651         sem_t        *psem;
652         classinfo    *c;
653         methodinfo   *m;
654         functionptr   function;
655
656 #if defined(ENABLE_GC_BOEHM)
657 # if !defined(__DARWIN__)
658         struct GC_stack_base sb;
659         int result;
660 # endif
661 #endif
662
663 #if defined(ENABLE_INTRP)
664         u1 *intrp_thread_stack;
665 #endif
666
667 #if defined(ENABLE_INTRP)
668         /* create interpreter stack */
669
670         if (opt_intrp) {
671                 intrp_thread_stack = GCMNEW(u1, opt_stacksize);
672                 MSET(intrp_thread_stack, 0, u1, opt_stacksize);
673         }
674         else
675                 intrp_thread_stack = NULL;
676 #endif
677
678         /* get passed startupinfo structure and the values in there */
679
680         startup = (startupinfo*) arg;
681
682         t        = startup->thread;
683         function = startup->function;
684         psem     = startup->psem;
685
686         /* Seems like we've encountered a situation where thread->tid was
687            not set by pthread_create. We alleviate this problem by waiting
688            for pthread_create to return. */
689
690         threads_sem_wait(startup->psem_first);
691
692 #if defined(__DARWIN__)
693         t->mach_thread = mach_thread_self();
694 #endif
695
696         /* Now that we are in the new thread, we can store the internal
697            thread data-structure in the TSD. */
698
699         thread_set_current(t);
700
701 #if defined(ENABLE_GC_BOEHM)
702 # if defined(__DARWIN__)
703         // This is currently not implemented in Boehm-GC.  Just fail silently.
704 # else
705         /* Register the thread with Boehm-GC.  This must happen before the
706            thread allocates any memory from the GC heap.*/
707
708         result = GC_get_stack_base(&sb);
709
710         if (result != 0)
711                 vm_abort("threads_startup_thread: GC_get_stack_base failed: result=%d", result);
712
713         GC_register_my_thread(&sb);
714 # endif
715 #endif
716
717         // Get the java.lang.Thread object for this thread.
718         java_handle_t* object = LLNI_WRAP(t->object);
719         java_lang_Thread jlt(object);
720
721         /* set our priority */
722
723         threads_set_thread_priority(t->tid, jlt.get_priority());
724
725         /* tell threads_startup_thread that we registered ourselves */
726         /* CAUTION: *startup becomes invalid with this!             */
727
728         startup = NULL;
729         threads_sem_post(psem);
730
731 #if defined(ENABLE_INTRP)
732         /* set interpreter stack */
733
734         if (opt_intrp)
735                 thread->_global_sp = (Cell *) (intrp_thread_stack + opt_stacksize);
736 #endif
737
738         // Hook point just before the threads initial method is executed.
739         Hook::thread_start(t);
740
741         DEBUGTHREADS("starting", t);
742
743         /* find and run the Thread.run()V method if no other function was passed */
744
745         if (function == NULL) {
746                 c = ThreadRuntime::get_thread_class_from_object(object);
747
748                 m = class_resolveclassmethod(c, utf_run, utf_void__void, c, true);
749
750                 if (m == NULL)
751                         vm_abort("threads_startup_thread: run() method not found in class");
752
753                 java_handle_t *h = ThreadRuntime::get_vmthread_handle(jlt);
754
755                 /* Run the thread. */
756
757                 (void) vm_call_method(m, h);
758         }
759         else {
760                 /* call passed function, e.g. finalizer_thread */
761
762                 (function)();
763         }
764
765         DEBUGTHREADS("stopping", t);
766
767         // Hook point just after the threads initial method returned.
768         Hook::thread_end(t);
769
770         /* We ignore the return value. */
771
772         (void) thread_detach_current_thread();
773
774         return NULL;
775 }
776
777
778 /* threads_impl_thread_start ***************************************************
779
780    Start a thread in the JVM.  Both (vm internal and java) thread
781    objects exist.
782
783    IN:
784       thread....the thread object
785           f.........function to run in the new thread. NULL means that the
786                     "run" method of the object `t` should be called
787
788 ******************************************************************************/
789
790 void threads_impl_thread_start(threadobject *thread, functionptr f)
791 {
792         sem_t          sem;
793         sem_t          sem_first;
794         pthread_attr_t attr;
795         startupinfo    startup;
796         int            result;
797
798         /* fill startupinfo structure passed by pthread_create to
799          * threads_startup_thread */
800
801         startup.thread     = thread;
802         startup.function   = f;              /* maybe we don't call Thread.run()V */
803         startup.psem       = &sem;
804         startup.psem_first = &sem_first;
805
806         threads_sem_init(&sem, 0, 0);
807         threads_sem_init(&sem_first, 0, 0);
808
809         /* Initialize thread attributes. */
810
811         result = pthread_attr_init(&attr);
812
813         if (result != 0)
814                 os::abort_errnum(result, "threads_impl_thread_start: pthread_attr_init failed");
815
816     result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
817
818     if (result != 0)
819                 os::abort_errnum(result, "threads_impl_thread_start: pthread_attr_setdetachstate failed");
820
821         /* initialize thread stacksize */
822
823         result = pthread_attr_setstacksize(&attr, opt_stacksize);
824
825         if (result != 0)
826                 os::abort_errnum(result, "threads_impl_thread_start: pthread_attr_setstacksize failed");
827
828         /* create the thread */
829
830         result = pthread_create(&(thread->tid), &attr, threads_startup_thread, &startup);
831
832         if (result != 0)
833                 os::abort_errnum(result, "threads_impl_thread_start: pthread_create failed");
834
835         /* destroy the thread attributes */
836
837         result = pthread_attr_destroy(&attr);
838
839         if (result != 0)
840                 os::abort_errnum(result, "threads_impl_thread_start: pthread_attr_destroy failed");
841
842         /* signal that pthread_create has returned, so thread->tid is valid */
843
844         threads_sem_post(&sem_first);
845
846         /* wait here until the thread has entered itself into the thread list */
847
848         threads_sem_wait(&sem);
849
850         /* cleanup */
851
852         sem_destroy(&sem);
853         sem_destroy(&sem_first);
854 }
855
856
857 /* threads_set_thread_priority *************************************************
858
859    Set the priority of the given thread.
860
861    IN:
862       tid..........thread id
863           priority.....priority to set
864
865 ******************************************************************************/
866
867 void threads_set_thread_priority(pthread_t tid, int priority)
868 {
869         struct sched_param schedp;
870         int policy;
871
872         pthread_getschedparam(tid, &policy, &schedp);
873         schedp.sched_priority = priority;
874         pthread_setschedparam(tid, policy, &schedp);
875 }
876
877
878 /**
879  * Detaches the current thread from the VM.
880  *
881  * @return true on success, false otherwise
882  */
883 bool thread_detach_current_thread(void)
884 {
885         threadobject* t = thread_get_current();
886
887         /* Sanity check. */
888
889         assert(t != NULL);
890
891     /* If the given thread has already been detached, this operation
892            is a no-op. */
893
894         if (thread_is_attached(t) == false)
895                 return true;
896
897         DEBUGTHREADS("detaching", t);
898
899         java_handle_t* object = LLNI_WRAP(t->object);
900         java_lang_Thread jlt(object);
901
902 #if defined(ENABLE_JAVASE)
903         java_handle_t* group = jlt.get_group();
904
905     /* If there's an uncaught exception, call uncaughtException on the
906        thread's exception handler, or the thread's group if this is
907        unset. */
908
909         java_handle_t* e = exceptions_get_and_clear_exception();
910
911     if (e != NULL) {
912                 /* We use the type void* for handler here, as it's not trivial
913                    to build the java_lang_Thread_UncaughtExceptionHandler
914                    header file with cacaoh. */
915
916                 java_handle_t *handler = ThreadRuntime::get_thread_exception_handler(jlt);
917
918                 classinfo*     c;
919                 java_handle_t* h;
920
921                 if (handler != NULL) {
922                         LLNI_class_get(handler, c);
923                         h = (java_handle_t *) handler;
924                 }
925                 else {
926                         LLNI_class_get(group, c);
927                         h = (java_handle_t *) group;
928                 }
929
930                 methodinfo* m = class_resolveclassmethod(c,
931                                                                                                  utf_uncaughtException,
932                                                                                                  utf_java_lang_Thread_java_lang_Throwable__V,
933                                                                                                  NULL,
934                                                                                                  true);
935
936                 if (m == NULL)
937                         return false;
938
939                 (void) vm_call_method(m, h, object, e);
940
941                 if (exceptions_get_exception())
942                         return false;
943     }
944
945         /* XXX TWISTI: should all threads be in a ThreadGroup? */
946
947         /* Remove thread from the thread group. */
948
949         if (group != NULL) {
950                 classinfo* c;
951                 LLNI_class_get(group, c);
952
953                 methodinfo *m = ThreadRuntime::get_threadgroup_remove_method(c);
954
955                 if (m == NULL)
956                         return false;
957
958                 (void) vm_call_method(m, group, object);
959
960                 if (exceptions_get_exception())
961                         return false;
962
963                 // Clear the ThreadGroup in the Java thread object (Mauve
964                 // test: gnu/testlet/java/lang/Thread/getThreadGroup).
965                 jlt.set_group(NULL);
966         }
967 #endif
968
969         /* Thread has terminated. */
970
971         thread_set_state_terminated(t);
972
973         /* Notify all threads waiting on this thread.  These are joining
974            this thread. */
975
976         /* XXX Care about exceptions? */
977         (void) lock_monitor_enter(jlt.get_handle());
978         
979         lock_notify_all_object(jlt.get_handle());
980
981         /* XXX Care about exceptions? */
982         (void) lock_monitor_exit(jlt.get_handle());
983
984         t->waitmutex->lock();
985         t->tid = 0;
986         t->waitmutex->unlock();
987
988         ThreadList::lock();
989
990         /* Free the internal thread data-structure. */
991
992         thread_free(t);
993
994         /* Signal that this thread has finished and leave the mutex. */
995
996         cond_join->signal();
997         ThreadList::unlock();
998
999         t->suspendmutex->lock();
1000         t->suspendmutex->unlock();
1001
1002         return true;
1003 }
1004
1005
1006 /**
1007  * Internal helper function which suspends the current thread. This is
1008  * the core method of the suspension mechanism actually blocking the
1009  * execution until the suspension reason is cleared again. Note that
1010  * the current thread needs to hold the suspension mutex while calling
1011  * this function.
1012  */
1013 static void threads_suspend_self()
1014 {
1015         threadobject* thread = THREADOBJECT;
1016
1017         DEBUGTHREADS("suspending", thread);
1018
1019         // Mark thread as suspended.
1020         assert(!thread->suspended);
1021         assert(thread->suspend_reason != SUSPEND_REASON_NONE);
1022         thread->suspended = true;
1023
1024         // Acknowledge the suspension.
1025         thread->suspendcond->broadcast();
1026
1027 #if defined(ENABLE_GC_CACAO)
1028         // If we are stopping the world, we should send a global ack.
1029         if (thread->suspend_reason == SUSPEND_REASON_STOPWORLD)
1030                 threads_sem_post(&suspend_ack);
1031 #endif
1032
1033         // Release the suspension mutex and wait till we are resumed.
1034         while (thread->suspend_reason != SUSPEND_REASON_NONE)
1035                 thread->suspendcond->wait(thread->suspendmutex);
1036
1037 #if defined(ENABLE_GC_CACAO)
1038         // XXX This is propably not ok!
1039         // If we are starting the world, we should send a global ack.
1040         if (thread->suspend_reason == SUSPEND_REASON_STOPWORLD)
1041                 threads_sem_post(&suspend_ack);
1042 #endif
1043
1044         // Mark thread as not suspended.
1045         assert(thread->suspended);
1046         assert(thread->suspend_reason == SUSPEND_REASON_NONE);
1047         thread->suspended = false;
1048
1049         DEBUGTHREADS("resuming", thread);
1050 }
1051
1052
1053 /**
1054  * Suspend the passed thread. Execution of that thread stops until the thread
1055  * is explicitly resumed again.
1056  *
1057  * @param thread The thread to be suspended.
1058  * @param reason Reason for suspending the given thread.
1059  * @return True of operation was successful, false otherwise.
1060  */
1061 bool threads_suspend_thread(threadobject *thread, int32_t reason)
1062 {
1063         // Sanity check.
1064         assert(reason != SUSPEND_REASON_NONE);
1065
1066         // Guard this with the suspension mutex.
1067         MutexLocker ml(*thread->suspendmutex);
1068
1069         // Check if thread is already suspended.
1070         if (thread->suspended)
1071                 return false;
1072
1073         // Check if thread is in the process of suspending.
1074         if (thread->suspend_reason != SUSPEND_REASON_NONE)
1075                 return false;
1076
1077         // Set the reason for suspending the thread.
1078         thread->suspend_reason = reason;
1079
1080         if (thread == THREADOBJECT) {
1081                 // We already hold the suspension mutex and can suspend ourselves
1082                 // immediately without using signals at all.
1083                 threads_suspend_self();
1084         }
1085         else {
1086                 // Send the suspend signal to the other thread.
1087                 if (!thread->tid)
1088                         return false;
1089                 if (pthread_kill(thread->tid, SIGUSR1) != 0)
1090                         os::abort_errno("threads_suspend_thread: pthread_kill failed");
1091
1092                 // Wait for the thread to acknowledge the suspension.
1093                 while (!thread->suspended)
1094                         thread->suspendcond->wait(thread->suspendmutex);
1095         }
1096
1097         return true;
1098 }
1099
1100
1101 /**
1102  * Resumes execution of the passed thread.
1103  *
1104  * @param thread The thread to be resumed.
1105  * @param reason Reason for suspending the given thread.
1106  * @return True of operation was successful, false otherwise.
1107  */
1108 bool threads_resume_thread(threadobject *thread, int32_t reason)
1109 {
1110         // Sanity check.
1111         assert(thread != THREADOBJECT);
1112         assert(reason != SUSPEND_REASON_NONE);
1113
1114         // Guard this with the suspension mutex.
1115         MutexLocker ml(*thread->suspendmutex);
1116
1117         // Check if thread really is suspended.
1118         if (!thread->suspended)
1119                 return false;
1120
1121         // Threads can only be resumed for the same reason they were suspended.
1122         if (thread->suspend_reason != reason)
1123                 return false;
1124
1125         // Clear the reason for suspending the thread.
1126         thread->suspend_reason = SUSPEND_REASON_NONE;
1127
1128         // Tell everyone that the thread should resume.
1129         thread->suspendcond->broadcast();
1130
1131         return true;
1132 }
1133
1134
1135 /**
1136  * Acknowledges the suspension of the current thread.
1137  */
1138 void threads_suspend_ack()
1139 {
1140         threadobject* thread = THREADOBJECT;
1141
1142         // Guard this with the suspension mutex.
1143         MutexLocker ml(*thread->suspendmutex);
1144
1145         // Suspend ourselves while holding the suspension mutex.
1146         threads_suspend_self();
1147 }
1148
1149
1150 /* threads_join_all_threads ****************************************************
1151
1152    Join all non-daemon threads.
1153
1154 *******************************************************************************/
1155
1156 void threads_join_all_threads(void)
1157 {
1158         threadobject *t;
1159
1160         /* get current thread */
1161
1162         t = THREADOBJECT;
1163
1164         /* This thread is waiting for all non-daemon threads to exit. */
1165
1166         thread_set_state_waiting(t);
1167
1168         /* enter join mutex */
1169
1170         ThreadList::lock();
1171
1172         /* Wait for condition as long as we have non-daemon threads.  We
1173            compare against 1 because the current (main thread) is also a
1174            non-daemon thread. */
1175
1176         while (ThreadList::get_number_of_non_daemon_threads() > 1)
1177                 ThreadList::wait_cond(cond_join);
1178
1179         /* leave join mutex */
1180
1181         ThreadList::unlock();
1182 }
1183
1184
1185 /* threads_timespec_earlier ****************************************************
1186
1187    Return true if timespec tv1 is earlier than timespec tv2.
1188
1189    IN:
1190       tv1..........first timespec
1191           tv2..........second timespec
1192
1193    RETURN VALUE:
1194       true, if the first timespec is earlier
1195
1196 *******************************************************************************/
1197
1198 static inline bool threads_timespec_earlier(const struct timespec *tv1,
1199                                                                                         const struct timespec *tv2)
1200 {
1201         return (tv1->tv_sec < tv2->tv_sec)
1202                                 ||
1203                 (tv1->tv_sec == tv2->tv_sec && tv1->tv_nsec < tv2->tv_nsec);
1204 }
1205
1206
1207 /* threads_current_time_is_earlier_than ****************************************
1208
1209    Check if the current time is earlier than the given timespec.
1210
1211    IN:
1212       tv...........the timespec to compare against
1213
1214    RETURN VALUE:
1215       true, if the current time is earlier
1216
1217 *******************************************************************************/
1218
1219 static bool threads_current_time_is_earlier_than(const struct timespec *tv)
1220 {
1221         struct timeval tvnow;
1222         struct timespec tsnow;
1223
1224         /* get current time */
1225
1226         if (gettimeofday(&tvnow, NULL) != 0)
1227                 vm_abort("gettimeofday failed: %s\n", strerror(errno));
1228
1229         /* convert it to a timespec */
1230
1231         tsnow.tv_sec = tvnow.tv_sec;
1232         tsnow.tv_nsec = tvnow.tv_usec * 1000;
1233
1234         /* compare current time with the given timespec */
1235
1236         return threads_timespec_earlier(&tsnow, tv);
1237 }
1238
1239
1240 /* threads_wait_with_timeout ***************************************************
1241
1242    Wait until the given point in time on a monitor until either
1243    we are notified, we are interrupted, or the time is up.
1244
1245    IN:
1246       t............the current thread
1247           wakeupTime...absolute (latest) wakeup time
1248                            If both tv_sec and tv_nsec are zero, this function
1249                                            waits for an unlimited amount of time.
1250
1251 *******************************************************************************/
1252
1253 static void threads_wait_with_timeout(threadobject *t, struct timespec *wakeupTime, bool parking)
1254 {
1255         // Acquire the waitmutex.
1256         t->waitmutex->lock();
1257
1258         /* wait on waitcond */
1259
1260         if (wakeupTime->tv_sec || wakeupTime->tv_nsec) {
1261                 /* with timeout */
1262                 while (!t->interrupted && !(parking ? t->park_permit : t->signaled)
1263                            && threads_current_time_is_earlier_than(wakeupTime))
1264                 {
1265                         if (parking)
1266                                 thread_set_state_timed_parked(t);
1267                         else
1268                                 thread_set_state_timed_waiting(t);
1269
1270                         t->waitcond->timedwait(t->waitmutex, wakeupTime);
1271
1272                         thread_set_state_runnable(t);
1273                 }
1274         }
1275         else {
1276                 /* no timeout */
1277                 while (!t->interrupted && !(parking ? t->park_permit : t->signaled)) {
1278                         if (parking)
1279                                 thread_set_state_parked(t);
1280                         else
1281                                 thread_set_state_waiting(t);
1282
1283                         t->waitcond->wait(t->waitmutex);
1284
1285                         thread_set_state_runnable(t);
1286                 }
1287         }
1288
1289         if (parking)
1290                 t->park_permit = false;
1291
1292         // Release the waitmutex.
1293         t->waitmutex->unlock();
1294 }
1295
1296
1297 /* threads_wait_with_timeout_relative ******************************************
1298
1299    Wait for the given maximum amount of time on a monitor until either
1300    we are notified, we are interrupted, or the time is up.
1301
1302    IN:
1303       t............the current thread
1304           millis.......milliseconds to wait
1305           nanos........nanoseconds to wait
1306
1307 *******************************************************************************/
1308
1309 void threads_wait_with_timeout_relative(threadobject *thread, s8 millis,
1310                                                                                 s4 nanos)
1311 {
1312         struct timespec wakeupTime;
1313
1314         /* calculate the the (latest) wakeup time */
1315
1316         threads_calc_absolute_time(&wakeupTime, millis, nanos);
1317
1318         /* wait */
1319
1320         threads_wait_with_timeout(thread, &wakeupTime, false);
1321 }
1322
1323
1324 /* threads_calc_absolute_time **************************************************
1325
1326    Calculate the absolute point in time a given number of ms and ns from now.
1327
1328    IN:
1329       millis............milliseconds from now
1330           nanos.............nanoseconds from now
1331
1332    OUT:
1333       *tm...............receives the timespec of the absolute point in time
1334
1335 *******************************************************************************/
1336
1337 static void threads_calc_absolute_time(struct timespec *tm, s8 millis, s4 nanos)
1338 {
1339         // (at least with GNU classpath) we know that 0 <= nanos <= 999999
1340         do {
1341                 if (!millis && !nanos)
1342                         break;
1343                 struct timeval tv;
1344                 gettimeofday(&tv, NULL);
1345                 s8 secs = tv.tv_sec + millis / 1000;
1346                 if (secs > INT32_MAX)   // integer overflow
1347                         break;
1348                 tv.tv_sec = secs;
1349                 millis %= 1000;
1350                 long nsec = tv.tv_usec * 1000 + (s4) millis * 1000000 + nanos;
1351                 tm->tv_sec = tv.tv_sec + nsec / 1000000000;
1352                 if (tm->tv_sec < tv.tv_sec) // integer overflow
1353                         break;
1354                 tm->tv_nsec = nsec % 1000000000;
1355                 return;
1356         } while (0);
1357         tm->tv_sec = 0;
1358         tm->tv_nsec = 0;
1359 }
1360
1361
1362 /* threads_thread_interrupt ****************************************************
1363
1364    Interrupt the given thread.
1365
1366    The thread gets the "waitcond" signal and 
1367    its interrupted flag is set to true.
1368
1369    IN:
1370       thread............the thread to interrupt
1371
1372 *******************************************************************************/
1373
1374 void threads_thread_interrupt(threadobject *t)
1375 {
1376         /* Signal the thread a "waitcond" and tell it that it has been
1377            interrupted. */
1378
1379         t->waitmutex->lock();
1380
1381         DEBUGTHREADS("interrupted", t);
1382
1383         /* Interrupt blocking system call using a signal. */
1384
1385         if (t->tid)
1386                 pthread_kill(t->tid, Signal_INTERRUPT_SYSTEM_CALL);
1387
1388         t->waitcond->signal();
1389
1390         t->interrupted = true;
1391
1392         t->waitmutex->unlock();
1393 }
1394
1395
1396 /**
1397  * Sleep the current thread for the specified amount of time.
1398  *
1399  * @param millis Milliseconds to sleep.
1400  * @param nanos  Nanoseconds to sleep.
1401  */
1402 void threads_sleep(int64_t millis, int32_t nanos)
1403 {
1404         threadobject    *t;
1405         struct timespec  wakeupTime;
1406         bool             interrupted;
1407
1408         if (millis < 0) {
1409 /*              exceptions_throw_illegalargumentexception("timeout value is negative"); */
1410                 exceptions_throw_illegalargumentexception();
1411                 return;
1412         }
1413
1414         t = thread_get_current();
1415
1416         if (thread_is_interrupted(t) && !exceptions_get_exception()) {
1417                 /* Clear interrupted flag (Mauve test:
1418                    gnu/testlet/java/lang/Thread/interrupt). */
1419
1420                 thread_set_interrupted(t, false);
1421
1422 /*              exceptions_throw_interruptedexception("sleep interrupted"); */
1423                 exceptions_throw_interruptedexception();
1424                 return;
1425         }
1426
1427         // (Note taken from classpath/vm/reference/java/lang/VMThread.java (sleep))
1428         // Note: JDK treats a zero length sleep is like Thread.yield(),
1429         // without checking the interrupted status of the thread.  It's
1430         // unclear if this is a bug in the implementation or the spec.
1431         // See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6213203 */
1432         if (millis == 0 && nanos == 0) {
1433                 threads_yield();
1434         }
1435         else {
1436                 threads_calc_absolute_time(&wakeupTime, millis, nanos);
1437
1438                 threads_wait_with_timeout(t, &wakeupTime, false);
1439
1440                 interrupted = thread_is_interrupted(t);
1441
1442                 if (interrupted) {
1443                         thread_set_interrupted(t, false);
1444
1445                         // An other exception could have been thrown
1446                         // (e.g. ThreadDeathException).
1447                         if (!exceptions_get_exception())
1448                                 exceptions_throw_interruptedexception();
1449                 }
1450         }
1451 }
1452
1453 /**
1454  * Park the current thread for the specified amount of time or until a
1455  * specified deadline.
1456  *
1457  * @param absolute Is the time in nanos a deadline or a duration?
1458  * @param nanos    Nanoseconds to park (absolute=false)
1459  *                 or deadline in milliseconds (absolute=true)
1460  */
1461 void threads_park(bool absolute, int64_t nanos)
1462 {
1463         threadobject    *t;
1464         struct timespec  wakeupTime;
1465
1466         t = thread_get_current();
1467
1468         if (absolute) {
1469                 wakeupTime.tv_nsec = 0;
1470                 wakeupTime.tv_sec = nanos / 1000; /* milliseconds */
1471         }
1472         else
1473                 threads_calc_absolute_time(&wakeupTime, nanos / 1000000, nanos % 1000000);
1474
1475         threads_wait_with_timeout(t, &wakeupTime, true);
1476 }
1477
1478 /**
1479  * Unpark the specified thread.
1480  *
1481  * @param t The thread to unpark.
1482  */
1483 void threads_unpark(threadobject *t)
1484 {
1485         t->waitmutex->lock();
1486
1487         t->waitcond->signal();
1488
1489         t->park_permit = true;
1490
1491         t->waitmutex->unlock();
1492 }
1493
1494
1495 /* threads_yield ***************************************************************
1496
1497    Yield to the scheduler.
1498
1499 *******************************************************************************/
1500
1501 void threads_yield(void)
1502 {
1503         sched_yield();
1504 }
1505
1506 #if defined(ENABLE_TLH)
1507
1508 void threads_tlh_add_frame() {
1509         tlh_add_frame(&(THREADOBJECT->tlh));
1510 }
1511
1512 void threads_tlh_remove_frame() {
1513         tlh_remove_frame(&(THREADOBJECT->tlh));
1514 }
1515
1516 #endif
1517
1518
1519 /*
1520  * These are local overrides for various environment variables in Emacs.
1521  * Please do not remove this and leave it at the end of the file, where
1522  * Emacs will automagically detect them.
1523  * ---------------------------------------------------------------------
1524  * Local variables:
1525  * mode: c++
1526  * indent-tabs-mode: t
1527  * c-basic-offset: 4
1528  * tab-width: 4
1529  * End:
1530  * vim:noexpandtab:sw=4:ts=4:
1531  */