* src/mm/gc-boehm.cpp: Define GC_SOLARIS_THREADS on Solaris.
[cacao.git] / src / threads / posix / thread-posix.cpp
1 /* src/threads/posix/thread-posix.cpp - POSIX thread functions
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008, 2010
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 Mutex* mutex_join;
213 static Condition* cond_join;
214
215 #if defined(ENABLE_GC_CACAO)
216 /* semaphore used for acknowleding thread suspension                          */
217 static sem_t suspend_ack;
218 #endif
219
220
221 /* threads_sem_init ************************************************************
222  
223    Initialize a semaphore. Checks against errors and interruptions.
224
225    IN:
226        sem..............the semaphore to initialize
227            shared...........true if this semaphore will be shared between processes
228            value............the initial value for the semaphore
229    
230 *******************************************************************************/
231
232 void threads_sem_init(sem_t *sem, bool shared, int value)
233 {
234         int r;
235
236         assert(sem);
237
238         do {
239                 r = sem_init(sem, shared, value);
240                 if (r == 0)
241                         return;
242         } while (errno == EINTR);
243
244         vm_abort("sem_init failed: %s", strerror(errno));
245 }
246
247
248 /* threads_sem_wait ************************************************************
249  
250    Wait for a semaphore, non-interruptible.
251
252    IMPORTANT: Always use this function instead of `sem_wait` directly, as
253               `sem_wait` may be interrupted by signals!
254   
255    IN:
256        sem..............the semaphore to wait on
257    
258 *******************************************************************************/
259
260 void threads_sem_wait(sem_t *sem)
261 {
262         int r;
263
264         assert(sem);
265
266         do {
267                 r = sem_wait(sem);
268                 if (r == 0)
269                         return;
270         } while (errno == EINTR);
271
272         vm_abort("sem_wait failed: %s", strerror(errno));
273 }
274
275
276 /* threads_sem_post ************************************************************
277  
278    Increase the count of a semaphore. Checks for errors.
279
280    IN:
281        sem..............the semaphore to increase the count of
282    
283 *******************************************************************************/
284
285 void threads_sem_post(sem_t *sem)
286 {
287         int r;
288
289         assert(sem);
290
291         /* unlike sem_wait, sem_post is not interruptible */
292
293         r = sem_post(sem);
294         if (r == 0)
295                 return;
296
297         vm_abort("sem_post failed: %s", strerror(errno));
298 }
299
300
301 /* threads_stopworld ***********************************************************
302
303    Stops the world from turning. All threads except the calling one
304    are suspended. The function returns as soon as all threads have
305    acknowledged their suspension.
306
307 *******************************************************************************/
308
309 #if defined(ENABLE_GC_CACAO)
310 void threads_stopworld(void)
311 {
312 #if !defined(__DARWIN__) && !defined(__CYGWIN__)
313         threadobject *t;
314         threadobject *self;
315         bool result;
316         s4 count, i;
317 #endif
318
319         stopworldlock->lock();
320
321         /* lock the threads lists */
322
323         threadlist_lock();
324
325 #if defined(__DARWIN__)
326         /*threads_cast_darwinstop();*/
327         assert(0);
328 #elif defined(__CYGWIN__)
329         /* TODO */
330         assert(0);
331 #else
332         self = THREADOBJECT;
333
334         DEBUGTHREADS("stops World", self);
335
336         count = 0;
337
338         /* suspend all running threads */
339         for (t = threadlist_first(); t != NULL; t = threadlist_next(t)) {
340                 /* don't send the signal to ourself */
341
342                 if (t == self)
343                         continue;
344
345                 /* don't send the signal to NEW threads (because they are not
346                    completely initialized) */
347
348                 if (t->state == THREAD_STATE_NEW)
349                         continue;
350
351                 /* send the signal */
352
353                 result = threads_suspend_thread(t, SUSPEND_REASON_STOPWORLD);
354                 assert(result);
355
356                 /* increase threads count */
357
358                 count++;
359         }
360
361         /* wait for all threads signaled to suspend */
362         for (i = 0; i < count; i++)
363                 threads_sem_wait(&suspend_ack);
364 #endif
365
366         /* ATTENTION: Don't unlock the threads-lists here so that
367            non-signaled NEW threads can't change their state and execute
368            code. */
369 }
370 #endif
371
372
373 /* threads_startworld **********************************************************
374
375    Starts the world again after it has previously been stopped. 
376
377 *******************************************************************************/
378
379 #if defined(ENABLE_GC_CACAO)
380 void threads_startworld(void)
381 {
382 #if !defined(__DARWIN__) && !defined(__CYGWIN__)
383         threadobject *t;
384         threadobject *self;
385         bool result;
386         s4 count, i;
387 #endif
388
389 #if defined(__DARWIN__)
390         /*threads_cast_darwinresume();*/
391         assert(0);
392 #elif defined(__IRIX__)
393         threads_cast_irixresume();
394 #elif defined(__CYGWIN__)
395         /* TODO */
396         assert(0);
397 #else
398         self = THREADOBJECT;
399
400         DEBUGTHREADS("starts World", self);
401
402         count = 0;
403
404         /* resume all thread we haltet */
405         for (t = threadlist_first(); t != NULL; t = threadlist_next(t)) {
406                 /* don't send the signal to ourself */
407
408                 if (t == self)
409                         continue;
410
411                 /* don't send the signal to NEW threads (because they are not
412                    completely initialized) */
413
414                 if (t->state == THREAD_STATE_NEW)
415                         continue;
416
417                 /* send the signal */
418
419                 result = threads_resume_thread(t);
420                 assert(result);
421
422                 /* increase threads count */
423
424                 count++;
425         }
426
427         /* wait for all threads signaled to suspend */
428         for (i = 0; i < count; i++)
429                 threads_sem_wait(&suspend_ack);
430
431 #endif
432
433         /* unlock the threads lists */
434
435         threadlist_unlock();
436
437         stopworldlock->unlock();
438 }
439 #endif
440
441
442 /* threads_impl_thread_clear ***************************************************
443
444    Clears all fields in threadobject the way an MZERO would have
445    done. MZERO cannot be used anymore because it would mess up the
446    pthread_* bits.
447
448    IN:
449       t....the threadobject
450
451 *******************************************************************************/
452
453 void threads_impl_thread_clear(threadobject *t)
454 {
455         t->object = NULL;
456
457         t->thinlock = 0;
458
459         t->index = 0;
460         t->flags = 0;
461         t->state = 0;
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
536 /* threads_impl_thread_free ****************************************************
537
538    Cleanup thread stuff.
539
540    IN:
541       t....the threadobject
542
543 *******************************************************************************/
544
545 #if 0
546 /* never used */
547 void threads_impl_thread_free(threadobject *t)
548 {
549         int result;
550
551         /* Destroy the mutex and the condition. */
552
553         delete t->flc_lock;
554
555         result = pthread_cond_destroy(&(t->flc_cond));
556
557         if (result != 0)
558                 os::abort_errnum(result, "threads_impl_thread_free: pthread_cond_destroy failed");
559
560         delete t->waitmutex;
561
562         result = pthread_cond_destroy(&(t->waitcond));
563
564         if (result != 0)
565                 os::abort_errnum(result, "threads_impl_thread_free: pthread_cond_destroy failed");
566
567         delete t->suspendmutex;
568
569         result = pthread_cond_destroy(&(t->suspendcond));
570
571         if (result != 0)
572                 os::abort_errnum(result, "threads_impl_thread_free: pthread_cond_destroy failed");
573 }
574 #endif
575
576
577 /* threads_impl_preinit ********************************************************
578
579    Do some early initialization of stuff required.
580
581    ATTENTION: Do NOT use any Java heap allocation here, as gc_init()
582    is called AFTER this function!
583
584 *******************************************************************************/
585
586 void threads_impl_preinit(void)
587 {
588         stopworldlock = new Mutex();
589
590         /* initialize exit mutex and condition (on exit we join all
591            threads) */
592
593         mutex_join = new Mutex();
594         cond_join = new Condition();
595
596 #if defined(ENABLE_GC_CACAO)
597         /* initialize the GC mutex & suspend semaphore */
598
599         mutex_gc = new Mutex();
600         threads_sem_init(&suspend_ack, 0, 0);
601 #endif
602
603 #if !defined(HAVE___THREAD)
604         int result = pthread_key_create(&thread_current_key, NULL);
605         if (result != 0)
606                 os::abort_errnum(result, "threads_impl_preinit: pthread_key_create failed");
607 #endif
608 }
609
610
611 /* threads_mutex_gc_lock *******************************************************
612
613    Enter the global GC mutex.
614
615 *******************************************************************************/
616
617 #if defined(ENABLE_GC_CACAO)
618 void threads_mutex_gc_lock(void)
619 {
620         mutex_gc->lock();
621 }
622 #endif
623
624
625 /* threads_mutex_gc_unlock *****************************************************
626
627    Leave the global GC mutex.
628
629 *******************************************************************************/
630
631 #if defined(ENABLE_GC_CACAO)
632 void threads_mutex_gc_unlock(void)
633 {
634         mutex_gc->unlock();
635 }
636 #endif
637
638 /* threads_mutex_join_lock *****************************************************
639
640    Enter the join mutex.
641
642 *******************************************************************************/
643
644 void threads_mutex_join_lock(void)
645 {
646         mutex_join->lock();
647 }
648
649
650 /* threads_mutex_join_unlock ***************************************************
651
652    Leave the join mutex.
653
654 *******************************************************************************/
655
656 void threads_mutex_join_unlock(void)
657 {
658         mutex_join->unlock();
659 }
660
661
662 /* threads_impl_init ***********************************************************
663
664    Initializes the implementation specific bits.
665
666 *******************************************************************************/
667
668 void threads_impl_init(void)
669 {
670         pthread_attr_t attr;
671         int            result;
672
673         threads_set_thread_priority(pthread_self(), NORM_PRIORITY);
674
675         /* Initialize the thread attribute object. */
676
677         result = pthread_attr_init(&attr);
678
679         if (result != 0)
680                 os::abort_errnum(result, "threads_impl_init: pthread_attr_init failed");
681
682         result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
683
684         if (result != 0)
685                 os::abort_errnum(result, "threads_impl_init: pthread_attr_setdetachstate failed");
686 }
687
688
689 /* threads_startup_thread ******************************************************
690
691    Thread startup function called by pthread_create.
692
693    Thread which have a startup.function != NULL are marked as internal
694    threads. All other threads are threated as normal Java threads.
695
696    NOTE: This function is not called directly by pthread_create. The Boehm GC
697          inserts its own GC_start_routine in between, which then calls
698                  threads_startup.
699
700    IN:
701       arg..........the argument passed to pthread_create, ie. a pointer to
702                        a startupinfo struct. CAUTION: When the `psem` semaphore
703                                    is posted, the startupinfo struct becomes invalid! (It
704                                    is allocated on the stack of threads_start_thread.)
705
706 ******************************************************************************/
707
708 static void *threads_startup_thread(void *arg)
709 {
710         startupinfo  *startup;
711         threadobject *t;
712         sem_t        *psem;
713         classinfo    *c;
714         methodinfo   *m;
715         functionptr   function;
716
717 #if defined(ENABLE_GC_BOEHM)
718 # if !defined(__DARWIN__)
719         struct GC_stack_base sb;
720         int result;
721 # endif
722 #endif
723
724 #if defined(ENABLE_INTRP)
725         u1 *intrp_thread_stack;
726 #endif
727
728 #if defined(ENABLE_INTRP)
729         /* create interpreter stack */
730
731         if (opt_intrp) {
732                 intrp_thread_stack = GCMNEW(u1, opt_stacksize);
733                 MSET(intrp_thread_stack, 0, u1, opt_stacksize);
734         }
735         else
736                 intrp_thread_stack = NULL;
737 #endif
738
739         /* get passed startupinfo structure and the values in there */
740
741         startup = (startupinfo*) arg;
742
743         t        = startup->thread;
744         function = startup->function;
745         psem     = startup->psem;
746
747         /* Seems like we've encountered a situation where thread->tid was
748            not set by pthread_create. We alleviate this problem by waiting
749            for pthread_create to return. */
750
751         threads_sem_wait(startup->psem_first);
752
753 #if defined(__DARWIN__)
754         t->mach_thread = mach_thread_self();
755 #endif
756
757         /* Now that we are in the new thread, we can store the internal
758            thread data-structure in the TSD. */
759
760         thread_set_current(t);
761
762 #if defined(ENABLE_GC_BOEHM)
763 # if defined(__DARWIN__)
764         // This is currently not implemented in Boehm-GC.  Just fail silently.
765 # else
766         /* Register the thread with Boehm-GC.  This must happen before the
767            thread allocates any memory from the GC heap.*/
768
769         result = GC_get_stack_base(&sb);
770
771         if (result != 0)
772                 vm_abort("threads_startup_thread: GC_get_stack_base failed: result=%d", result);
773
774         GC_register_my_thread(&sb);
775 # endif
776 #endif
777
778         // Get the java.lang.Thread object for this thread.
779         java_handle_t* object = thread_get_object(t);
780         java_lang_Thread jlt(object);
781
782         /* set our priority */
783
784         threads_set_thread_priority(t->tid, jlt.get_priority());
785
786         /* Thread is completely initialized. */
787
788         thread_set_state_runnable(t);
789
790         /* tell threads_startup_thread that we registered ourselves */
791         /* CAUTION: *startup becomes invalid with this!             */
792
793         startup = NULL;
794         threads_sem_post(psem);
795
796 #if defined(ENABLE_INTRP)
797         /* set interpreter stack */
798
799         if (opt_intrp)
800                 thread->_global_sp = (Cell *) (intrp_thread_stack + opt_stacksize);
801 #endif
802
803         // Hook point just before the threads initial method is executed.
804         Hook::thread_start(t);
805
806         DEBUGTHREADS("starting", t);
807
808         /* find and run the Thread.run()V method if no other function was passed */
809
810         if (function == NULL) {
811 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
812                 /* We need to start the run method of
813                    java.lang.VMThread. Since this is a final class, we can use
814                    the class object directly. */
815
816                 c = class_java_lang_VMThread;
817 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK) || defined(WITH_JAVA_RUNTIME_LIBRARY_CLDC1_1)
818                 LLNI_class_get(object, c);
819 #else
820 # error unknown classpath configuration
821 #endif
822
823                 m = class_resolveclassmethod(c, utf_run, utf_void__void, c, true);
824
825                 if (m == NULL)
826                         vm_abort("threads_startup_thread: run() method not found in class");
827
828 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
829
830                 // We need to start the run method of java.lang.VMThread.
831                 java_lang_VMThread jlvmt(jlt.get_vmThread());
832                 java_handle_t* h = jlvmt.get_handle();
833
834 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK) || defined(WITH_JAVA_RUNTIME_LIBRARY_CLDC1_1)
835
836                 java_handle_t* h = jlt.get_handle();
837
838 #else
839 # error unknown classpath configuration
840 #endif
841
842                 /* Run the thread. */
843
844                 (void) vm_call_method(m, h);
845         }
846         else {
847                 /* call passed function, e.g. finalizer_thread */
848
849                 (function)();
850         }
851
852         DEBUGTHREADS("stopping", t);
853
854         // Hook point just after the threads initial method returned.
855         Hook::thread_end(t);
856
857         /* We ignore the return value. */
858
859         (void) thread_detach_current_thread();
860
861         return NULL;
862 }
863
864
865 /* threads_impl_thread_start ***************************************************
866
867    Start a thread in the JVM.  Both (vm internal and java) thread
868    objects exist.
869
870    IN:
871       thread....the thread object
872           f.........function to run in the new thread. NULL means that the
873                     "run" method of the object `t` should be called
874
875 ******************************************************************************/
876
877 void threads_impl_thread_start(threadobject *thread, functionptr f)
878 {
879         sem_t          sem;
880         sem_t          sem_first;
881         pthread_attr_t attr;
882         startupinfo    startup;
883         int            result;
884
885         /* fill startupinfo structure passed by pthread_create to
886          * threads_startup_thread */
887
888         startup.thread     = thread;
889         startup.function   = f;              /* maybe we don't call Thread.run()V */
890         startup.psem       = &sem;
891         startup.psem_first = &sem_first;
892
893         threads_sem_init(&sem, 0, 0);
894         threads_sem_init(&sem_first, 0, 0);
895
896         /* Initialize thread attributes. */
897
898         result = pthread_attr_init(&attr);
899
900         if (result != 0)
901                 os::abort_errnum(result, "threads_impl_thread_start: pthread_attr_init failed");
902
903     result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
904
905     if (result != 0)
906                 os::abort_errnum(result, "threads_impl_thread_start: pthread_attr_setdetachstate failed");
907
908         /* initialize thread stacksize */
909
910         result = pthread_attr_setstacksize(&attr, opt_stacksize);
911
912         if (result != 0)
913                 os::abort_errnum(result, "threads_impl_thread_start: pthread_attr_setstacksize failed");
914
915         /* create the thread */
916
917         result = pthread_create(&(thread->tid), &attr, threads_startup_thread, &startup);
918
919         if (result != 0)
920                 os::abort_errnum(result, "threads_impl_thread_start: pthread_create failed");
921
922         /* destroy the thread attributes */
923
924         result = pthread_attr_destroy(&attr);
925
926         if (result != 0)
927                 os::abort_errnum(result, "threads_impl_thread_start: pthread_attr_destroy failed");
928
929         /* signal that pthread_create has returned, so thread->tid is valid */
930
931         threads_sem_post(&sem_first);
932
933         /* wait here until the thread has entered itself into the thread list */
934
935         threads_sem_wait(&sem);
936
937         /* cleanup */
938
939         sem_destroy(&sem);
940         sem_destroy(&sem_first);
941 }
942
943
944 /* threads_set_thread_priority *************************************************
945
946    Set the priority of the given thread.
947
948    IN:
949       tid..........thread id
950           priority.....priority to set
951
952 ******************************************************************************/
953
954 void threads_set_thread_priority(pthread_t tid, int priority)
955 {
956         struct sched_param schedp;
957         int policy;
958
959         pthread_getschedparam(tid, &policy, &schedp);
960         schedp.sched_priority = priority;
961         pthread_setschedparam(tid, policy, &schedp);
962 }
963
964
965 /**
966  * Detaches the current thread from the VM.
967  *
968  * @return true on success, false otherwise
969  */
970 bool thread_detach_current_thread(void)
971 {
972         threadobject* t = thread_get_current();
973
974         /* Sanity check. */
975
976         assert(t != NULL);
977
978     /* If the given thread has already been detached, this operation
979            is a no-op. */
980
981         if (thread_is_attached(t) == false)
982                 return true;
983
984         DEBUGTHREADS("detaching", t);
985
986         java_handle_t* object = thread_get_object(t);
987         java_lang_Thread jlt(object);
988
989 #if defined(ENABLE_JAVASE)
990         java_handle_t* group = jlt.get_group();
991
992     /* If there's an uncaught exception, call uncaughtException on the
993        thread's exception handler, or the thread's group if this is
994        unset. */
995
996         java_handle_t* e = exceptions_get_and_clear_exception();
997
998     if (e != NULL) {
999                 /* We use the type void* for handler here, as it's not trivial
1000                    to build the java_lang_Thread_UncaughtExceptionHandler
1001                    header file with cacaoh. */
1002
1003 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
1004
1005                 java_handle_t* handler = jlt.get_exceptionHandler();
1006
1007 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
1008
1009                 java_handle_t* handler = jlt.get_uncaughtExceptionHandler();
1010
1011 # endif
1012
1013                 classinfo*     c;
1014                 java_handle_t* h;
1015
1016                 if (handler != NULL) {
1017                         LLNI_class_get(handler, c);
1018                         h = (java_handle_t *) handler;
1019                 }
1020                 else {
1021                         LLNI_class_get(group, c);
1022                         h = (java_handle_t *) group;
1023                 }
1024
1025                 methodinfo* m = class_resolveclassmethod(c,
1026                                                                                                  utf_uncaughtException,
1027                                                                                                  utf_java_lang_Thread_java_lang_Throwable__V,
1028                                                                                                  NULL,
1029                                                                                                  true);
1030
1031                 if (m == NULL)
1032                         return false;
1033
1034                 (void) vm_call_method(m, h, object, e);
1035
1036                 if (exceptions_get_exception())
1037                         return false;
1038     }
1039
1040         /* XXX TWISTI: should all threads be in a ThreadGroup? */
1041
1042         /* Remove thread from the thread group. */
1043
1044         if (group != NULL) {
1045                 classinfo* c;
1046                 LLNI_class_get(group, c);
1047
1048 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
1049                 methodinfo* m = class_resolveclassmethod(c,
1050                                                                                                  utf_removeThread,
1051                                                                                                  utf_java_lang_Thread__V,
1052                                                                                                  class_java_lang_ThreadGroup,
1053                                                                                                  true);
1054 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
1055                 methodinfo* m = class_resolveclassmethod(c,
1056                                                                                                  utf_remove,
1057                                                                                                  utf_java_lang_Thread__V,
1058                                                                                                  class_java_lang_ThreadGroup,
1059                                                                                                  true);
1060 # else
1061 #  error unknown classpath configuration
1062 # endif
1063
1064                 if (m == NULL)
1065                         return false;
1066
1067                 (void) vm_call_method(m, group, object);
1068
1069                 if (exceptions_get_exception())
1070                         return false;
1071
1072                 // Clear the ThreadGroup in the Java thread object (Mauve
1073                 // test: gnu/testlet/java/lang/Thread/getThreadGroup).
1074                 jlt.set_group(NULL);
1075         }
1076 #endif
1077
1078         /* Thread has terminated. */
1079
1080         thread_set_state_terminated(t);
1081
1082         /* Notify all threads waiting on this thread.  These are joining
1083            this thread. */
1084
1085         /* XXX Care about exceptions? */
1086         (void) lock_monitor_enter(jlt.get_handle());
1087         
1088         lock_notify_all_object(jlt.get_handle());
1089
1090         /* XXX Care about exceptions? */
1091         (void) lock_monitor_exit(jlt.get_handle());
1092
1093         /* Enter the join-mutex before calling thread_free, so
1094            threads_join_all_threads gets the correct number of non-daemon
1095            threads. */
1096
1097         threads_mutex_join_lock();
1098
1099         /* Free the internal thread data-structure. */
1100
1101         thread_free(t);
1102
1103         /* Signal that this thread has finished and leave the mutex. */
1104
1105         cond_join->signal();
1106         threads_mutex_join_unlock();
1107
1108         return true;
1109 }
1110
1111
1112 /**
1113  * Internal helper function which suspends the current thread. This is
1114  * the core method of the suspension mechanism actually blocking the
1115  * execution until the suspension reason is cleared again. Note that
1116  * the current thread needs to hold the suspension mutex while calling
1117  * this function.
1118  */
1119 static void threads_suspend_self()
1120 {
1121         threadobject* thread = THREADOBJECT;
1122
1123         DEBUGTHREADS("suspending", thread);
1124
1125         // Mark thread as suspended.
1126         assert(!thread->suspended);
1127         assert(thread->suspend_reason != SUSPEND_REASON_NONE);
1128         thread->suspended = true;
1129
1130         // Acknowledge the suspension.
1131         thread->suspendcond->broadcast();
1132
1133 #if defined(ENABLE_GC_CACAO)
1134         // If we are stopping the world, we should send a global ack.
1135         if (thread->suspend_reason == SUSPEND_REASON_STOPWORLD)
1136                 threads_sem_post(&suspend_ack);
1137 #endif
1138
1139         // Release the suspension mutex and wait till we are resumed.
1140         thread->suspendcond->wait(thread->suspendmutex);
1141
1142 #if defined(ENABLE_GC_CACAO)
1143         // XXX This is propably not ok!
1144         // If we are starting the world, we should send a global ack.
1145         if (thread->suspend_reason == SUSPEND_REASON_STOPWORLD)
1146                 threads_sem_post(&suspend_ack);
1147 #endif
1148
1149         // Mark thread as not suspended.
1150         assert(thread->suspended);
1151         assert(thread->suspend_reason == SUSPEND_REASON_NONE);
1152         thread->suspended = false;
1153
1154         DEBUGTHREADS("resuming", thread);
1155 }
1156
1157
1158 /**
1159  * Suspend the passed thread. Execution of that thread stops until the thread
1160  * is explicitly resumend again.
1161  *
1162  * @param thread The thread to be suspended.
1163  * @param reason Reason for suspending the given thread.
1164  * @return True of operation was successful, false otherwise.
1165  */
1166 bool threads_suspend_thread(threadobject *thread, int32_t reason)
1167 {
1168         // Sanity check.
1169         assert(reason != SUSPEND_REASON_NONE);
1170
1171         // Guard this with the suspension mutex.
1172         MutexLocker ml(*thread->suspendmutex);
1173
1174         // Check if thread is already suspended.
1175         if (thread->suspended)
1176                 return false;
1177
1178         // Check if thread is in the process of suspending.
1179         if (thread->suspend_reason != SUSPEND_REASON_NONE)
1180                 return false;
1181
1182         // Set the reason for suspending the thread.
1183         thread->suspend_reason = reason;
1184
1185         if (thread == THREADOBJECT) {
1186                 // We already hold the suspension mutex and can suspend ourselves
1187                 // immediately without using signals at all.
1188                 threads_suspend_self();
1189         }
1190         else {
1191                 // Send the suspend signal to the other thread.
1192                 if (pthread_kill(thread->tid, SIGUSR1) != 0)
1193                         os::abort_errno("threads_suspend_thread: pthread_kill failed");
1194
1195                 // Wait for the thread to acknowledge the suspension.
1196                 // XXX A possible optimization would be to not wait here, but you
1197                 //     better think this through twice before trying it!
1198                 thread->suspendcond->wait(thread->suspendmutex);
1199         }
1200
1201         return true;
1202 }
1203
1204
1205 /**
1206  * Resumes execution of the passed thread.
1207  *
1208  * @param thread The thread to be resumed.
1209  * @param reason Reason for suspending the given thread.
1210  * @return True of operation was successful, false otherwise.
1211  */
1212 bool threads_resume_thread(threadobject *thread, int32_t reason)
1213 {
1214         // Sanity check.
1215         assert(thread != THREADOBJECT);
1216         assert(reason != SUSPEND_REASON_NONE);
1217
1218         // Guard this with the suspension mutex.
1219         MutexLocker ml(*thread->suspendmutex);
1220
1221         // Check if thread really is suspended.
1222         if (!thread->suspended)
1223                 return false;
1224
1225         // Threads can only be resumed for the same reason they were suspended.
1226         if (thread->suspend_reason != reason)
1227                 return false;
1228
1229         // Clear the reason for suspending the thread.
1230         thread->suspend_reason = SUSPEND_REASON_NONE;
1231
1232         // Tell everyone that the thread should resume.
1233         thread->suspendcond->broadcast();
1234
1235         return true;
1236 }
1237
1238
1239 /**
1240  * Acknowledges the suspension of the current thread.
1241  */
1242 void threads_suspend_ack()
1243 {
1244         threadobject* thread = THREADOBJECT;
1245
1246         // Guard this with the suspension mutex.
1247         MutexLocker ml(*thread->suspendmutex);
1248
1249         // Suspend ourselves while holding the suspension mutex.
1250         threads_suspend_self();
1251 }
1252
1253
1254 /* threads_join_all_threads ****************************************************
1255
1256    Join all non-daemon threads.
1257
1258 *******************************************************************************/
1259
1260 void threads_join_all_threads(void)
1261 {
1262         threadobject *t;
1263
1264         /* get current thread */
1265
1266         t = THREADOBJECT;
1267
1268         /* This thread is waiting for all non-daemon threads to exit. */
1269
1270         thread_set_state_waiting(t);
1271
1272         /* enter join mutex */
1273
1274         threads_mutex_join_lock();
1275
1276         /* Wait for condition as long as we have non-daemon threads.  We
1277            compare against 1 because the current (main thread) is also a
1278            non-daemon thread. */
1279
1280         while (ThreadList::get_number_of_non_daemon_threads() > 1)
1281                 cond_join->wait(mutex_join);
1282
1283         /* leave join mutex */
1284
1285         threads_mutex_join_unlock();
1286 }
1287
1288
1289 /* threads_timespec_earlier ****************************************************
1290
1291    Return true if timespec tv1 is earlier than timespec tv2.
1292
1293    IN:
1294       tv1..........first timespec
1295           tv2..........second timespec
1296
1297    RETURN VALUE:
1298       true, if the first timespec is earlier
1299
1300 *******************************************************************************/
1301
1302 static inline bool threads_timespec_earlier(const struct timespec *tv1,
1303                                                                                         const struct timespec *tv2)
1304 {
1305         return (tv1->tv_sec < tv2->tv_sec)
1306                                 ||
1307                 (tv1->tv_sec == tv2->tv_sec && tv1->tv_nsec < tv2->tv_nsec);
1308 }
1309
1310
1311 /* threads_current_time_is_earlier_than ****************************************
1312
1313    Check if the current time is earlier than the given timespec.
1314
1315    IN:
1316       tv...........the timespec to compare against
1317
1318    RETURN VALUE:
1319       true, if the current time is earlier
1320
1321 *******************************************************************************/
1322
1323 static bool threads_current_time_is_earlier_than(const struct timespec *tv)
1324 {
1325         struct timeval tvnow;
1326         struct timespec tsnow;
1327
1328         /* get current time */
1329
1330         if (gettimeofday(&tvnow, NULL) != 0)
1331                 vm_abort("gettimeofday failed: %s\n", strerror(errno));
1332
1333         /* convert it to a timespec */
1334
1335         tsnow.tv_sec = tvnow.tv_sec;
1336         tsnow.tv_nsec = tvnow.tv_usec * 1000;
1337
1338         /* compare current time with the given timespec */
1339
1340         return threads_timespec_earlier(&tsnow, tv);
1341 }
1342
1343
1344 /* threads_wait_with_timeout ***************************************************
1345
1346    Wait until the given point in time on a monitor until either
1347    we are notified, we are interrupted, or the time is up.
1348
1349    IN:
1350       t............the current thread
1351           wakeupTime...absolute (latest) wakeup time
1352                            If both tv_sec and tv_nsec are zero, this function
1353                                            waits for an unlimited amount of time.
1354
1355 *******************************************************************************/
1356
1357 static void threads_wait_with_timeout(threadobject *t, struct timespec *wakeupTime, bool parking)
1358 {
1359         // Acquire the waitmutex.
1360         t->waitmutex->lock();
1361
1362         /* wait on waitcond */
1363
1364         if (wakeupTime->tv_sec || wakeupTime->tv_nsec) {
1365                 /* with timeout */
1366                 while (!t->interrupted && !(parking ? t->park_permit : t->signaled)
1367                            && threads_current_time_is_earlier_than(wakeupTime))
1368                 {
1369                         if (parking)
1370                                 thread_set_state_timed_parked(t);
1371                         else
1372                                 thread_set_state_timed_waiting(t);
1373
1374                         t->waitcond->timedwait(t->waitmutex, wakeupTime);
1375
1376                         thread_set_state_runnable(t);
1377                 }
1378         }
1379         else {
1380                 /* no timeout */
1381                 while (!t->interrupted && !(parking ? t->park_permit : t->signaled)) {
1382                         if (parking)
1383                                 thread_set_state_parked(t);
1384                         else
1385                                 thread_set_state_waiting(t);
1386
1387                         t->waitcond->wait(t->waitmutex);
1388
1389                         thread_set_state_runnable(t);
1390                 }
1391         }
1392
1393         if (parking)
1394                 t->park_permit = false;
1395
1396         // Release the waitmutex.
1397         t->waitmutex->unlock();
1398 }
1399
1400
1401 /* threads_wait_with_timeout_relative ******************************************
1402
1403    Wait for the given maximum amount of time on a monitor until either
1404    we are notified, we are interrupted, or the time is up.
1405
1406    IN:
1407       t............the current thread
1408           millis.......milliseconds to wait
1409           nanos........nanoseconds to wait
1410
1411 *******************************************************************************/
1412
1413 void threads_wait_with_timeout_relative(threadobject *thread, s8 millis,
1414                                                                                 s4 nanos)
1415 {
1416         struct timespec wakeupTime;
1417
1418         /* calculate the the (latest) wakeup time */
1419
1420         threads_calc_absolute_time(&wakeupTime, millis, nanos);
1421
1422         /* wait */
1423
1424         threads_wait_with_timeout(thread, &wakeupTime, false);
1425 }
1426
1427
1428 /* threads_calc_absolute_time **************************************************
1429
1430    Calculate the absolute point in time a given number of ms and ns from now.
1431
1432    IN:
1433       millis............milliseconds from now
1434           nanos.............nanoseconds from now
1435
1436    OUT:
1437       *tm...............receives the timespec of the absolute point in time
1438
1439 *******************************************************************************/
1440
1441 static void threads_calc_absolute_time(struct timespec *tm, s8 millis, s4 nanos)
1442 {
1443         // (at least with GNU classpath) we know that 0 <= nanos <= 999999
1444         do {
1445                 if (!millis && !nanos)
1446                         break;
1447                 struct timeval tv;
1448                 gettimeofday(&tv, NULL);
1449                 s8 secs = tv.tv_sec + millis / 1000;
1450                 if (secs > INT32_MAX)   // integer overflow
1451                         break;
1452                 tv.tv_sec = secs;
1453                 millis %= 1000;
1454                 long nsec = tv.tv_usec * 1000 + (s4) millis * 1000000 + nanos;
1455                 tm->tv_sec = tv.tv_sec + nsec / 1000000000;
1456                 if (tm->tv_sec < tv.tv_sec) // integer overflow
1457                         break;
1458                 tm->tv_nsec = nsec % 1000000000;
1459                 return;
1460         } while (0);
1461         tm->tv_sec = 0;
1462         tm->tv_nsec = 0;
1463 }
1464
1465
1466 /* threads_thread_interrupt ****************************************************
1467
1468    Interrupt the given thread.
1469
1470    The thread gets the "waitcond" signal and 
1471    its interrupted flag is set to true.
1472
1473    IN:
1474       thread............the thread to interrupt
1475
1476 *******************************************************************************/
1477
1478 void threads_thread_interrupt(threadobject *t)
1479 {
1480         /* Signal the thread a "waitcond" and tell it that it has been
1481            interrupted. */
1482
1483         t->waitmutex->lock();
1484
1485         DEBUGTHREADS("interrupted", t);
1486
1487         /* Interrupt blocking system call using a signal. */
1488
1489         pthread_kill(t->tid, Signal_INTERRUPT_SYSTEM_CALL);
1490
1491         t->waitcond->signal();
1492
1493         t->interrupted = true;
1494
1495         t->waitmutex->unlock();
1496 }
1497
1498
1499 /**
1500  * Sleep the current thread for the specified amount of time.
1501  *
1502  * @param millis Milliseconds to sleep.
1503  * @param nanos  Nanoseconds to sleep.
1504  */
1505 void threads_sleep(int64_t millis, int32_t nanos)
1506 {
1507         threadobject    *t;
1508         struct timespec  wakeupTime;
1509         bool             interrupted;
1510
1511         if (millis < 0) {
1512 /*              exceptions_throw_illegalargumentexception("timeout value is negative"); */
1513                 exceptions_throw_illegalargumentexception();
1514                 return;
1515         }
1516
1517         t = thread_get_current();
1518
1519         if (thread_is_interrupted(t) && !exceptions_get_exception()) {
1520                 /* Clear interrupted flag (Mauve test:
1521                    gnu/testlet/java/lang/Thread/interrupt). */
1522
1523                 thread_set_interrupted(t, false);
1524
1525 /*              exceptions_throw_interruptedexception("sleep interrupted"); */
1526                 exceptions_throw_interruptedexception();
1527                 return;
1528         }
1529
1530         // (Note taken from classpath/vm/reference/java/lang/VMThread.java (sleep))
1531         // Note: JDK treats a zero length sleep is like Thread.yield(),
1532         // without checking the interrupted status of the thread.  It's
1533         // unclear if this is a bug in the implementation or the spec.
1534         // See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6213203 */
1535         if (millis == 0 && nanos == 0) {
1536                 threads_yield();
1537         }
1538         else {
1539                 threads_calc_absolute_time(&wakeupTime, millis, nanos);
1540
1541                 threads_wait_with_timeout(t, &wakeupTime, false);
1542
1543                 interrupted = thread_is_interrupted(t);
1544
1545                 if (interrupted) {
1546                         thread_set_interrupted(t, false);
1547
1548                         // An other exception could have been thrown
1549                         // (e.g. ThreadDeathException).
1550                         if (!exceptions_get_exception())
1551                                 exceptions_throw_interruptedexception();
1552                 }
1553         }
1554 }
1555
1556 /**
1557  * Park the current thread for the specified amount of time or until a
1558  * specified deadline.
1559  *
1560  * @param absolute Is the time in nanos a deadline or a duration?
1561  * @param nanos    Nanoseconds to park (absolute=false)
1562  *                 or deadline in milliseconds (absolute=true)
1563  */
1564 void threads_park(bool absolute, int64_t nanos)
1565 {
1566         threadobject    *t;
1567         struct timespec  wakeupTime;
1568
1569         t = thread_get_current();
1570
1571         if (absolute) {
1572                 wakeupTime.tv_nsec = 0;
1573                 wakeupTime.tv_sec = nanos / 1000; /* milliseconds */
1574         }
1575         else
1576                 threads_calc_absolute_time(&wakeupTime, nanos / 1000000, nanos % 1000000);
1577
1578         threads_wait_with_timeout(t, &wakeupTime, true);
1579 }
1580
1581 /**
1582  * Unpark the specified thread.
1583  *
1584  * @param t The thread to unpark.
1585  */
1586 void threads_unpark(threadobject *t)
1587 {
1588         t->waitmutex->lock();
1589
1590         t->waitcond->signal();
1591
1592         t->park_permit = true;
1593
1594         t->waitmutex->unlock();
1595 }
1596
1597
1598 /* threads_yield ***************************************************************
1599
1600    Yield to the scheduler.
1601
1602 *******************************************************************************/
1603
1604 void threads_yield(void)
1605 {
1606         sched_yield();
1607 }
1608
1609 #if defined(ENABLE_TLH)
1610
1611 void threads_tlh_add_frame() {
1612         tlh_add_frame(&(THREADOBJECT->tlh));
1613 }
1614
1615 void threads_tlh_remove_frame() {
1616         tlh_remove_frame(&(THREADOBJECT->tlh));
1617 }
1618
1619 #endif
1620
1621
1622 /*
1623  * These are local overrides for various environment variables in Emacs.
1624  * Please do not remove this and leave it at the end of the file, where
1625  * Emacs will automagically detect them.
1626  * ---------------------------------------------------------------------
1627  * Local variables:
1628  * mode: c++
1629  * indent-tabs-mode: t
1630  * c-basic-offset: 4
1631  * tab-width: 4
1632  * End:
1633  * vim:noexpandtab:sw=4:ts=4:
1634  */