* src/threads/threads-common.c (threads/critical.h): Added.
[cacao.git] / src / threads / native / threads.c
1 /* src/threads/native/threads.c - native threads support
2
3    Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    $Id: threads.c 7830 2007-04-26 11:14:39Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 /* XXX cleanup these includes */
33
34 #include <stdlib.h>
35 #include <string.h>
36 #include <assert.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39 #include <signal.h>
40 #include <sys/time.h>
41 #include <time.h>
42 #include <errno.h>
43
44 #include <pthread.h>
45
46 #include "vm/types.h"
47
48 #include "arch.h"
49
50 #if !defined(USE_FAKE_ATOMIC_INSTRUCTIONS)
51 # include "machine-instr.h"
52 #else
53 # include "threads/native/generic-primitives.h"
54 #endif
55
56 #include "mm/gc-common.h"
57 #include "mm/memory.h"
58
59 #include "native/jni.h"
60 #include "native/native.h"
61 #include "native/include/java_lang_Object.h"
62 #include "native/include/java_lang_String.h"
63 #include "native/include/java_lang_Throwable.h"
64 #include "native/include/java_lang_Thread.h"
65
66 #if defined(ENABLE_JAVASE)
67 # include "native/include/java_lang_ThreadGroup.h"
68 #endif
69
70 #if defined(WITH_CLASSPATH_GNU)
71 # include "native/include/java_lang_VMThread.h"
72 #endif
73
74 #include "threads/lock-common.h"
75 #include "threads/threads-common.h"
76
77 #include "threads/native/threads.h"
78
79 #include "toolbox/avl.h"
80 #include "toolbox/logging.h"
81
82 #include "vm/builtin.h"
83 #include "vm/exceptions.h"
84 #include "vm/global.h"
85 #include "vm/stringlocal.h"
86 #include "vm/vm.h"
87
88 #include "vm/jit/asmpart.h"
89
90 #include "vmcore/options.h"
91
92 #if defined(ENABLE_STATISTICS)
93 # include "vmcore/statistics.h"
94 #endif
95
96 #if !defined(__DARWIN__)
97 # if defined(__LINUX__)
98 #  define GC_LINUX_THREADS
99 # elif defined(__MIPS__)
100 #  define GC_IRIX_THREADS
101 # endif
102 # include <semaphore.h>
103 # if defined(ENABLE_GC_BOEHM)
104 #  include "mm/boehm-gc/include/gc.h"
105 # endif
106 #endif
107
108 #if defined(ENABLE_JVMTI)
109 #include "native/jvmti/cacaodbg.h"
110 #endif
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->value = value;
129     
130         if (pthread_mutex_init(&sem->mutex, NULL) < 0)
131                 return -1;
132
133         if (pthread_cond_init(&sem->cond, NULL) < 0)
134                 return -1;
135
136         return 0;
137 }
138
139 static int sem_post(sem_t *sem)
140 {
141         if (pthread_mutex_lock(&sem->mutex) < 0)
142                 return -1;
143
144         sem->value++;
145
146         if (pthread_cond_signal(&sem->cond) < 0) {
147                 pthread_mutex_unlock(&sem->mutex);
148                 return -1;
149         }
150
151         if (pthread_mutex_unlock(&sem->mutex) < 0)
152                 return -1;
153
154         return 0;
155 }
156
157 static int sem_wait(sem_t *sem)
158 {
159         if (pthread_mutex_lock(&sem->mutex) < 0)
160                 return -1;
161
162         while (sem->value == 0) {
163                 pthread_cond_wait(&sem->cond, &sem->mutex);
164         }
165
166         sem->value--;
167
168         if (pthread_mutex_unlock(&sem->mutex) < 0)
169                 return -1;    
170
171         return 0;
172 }
173
174 static int sem_destroy(sem_t *sem)
175 {
176         if (pthread_cond_destroy(&sem->cond) < 0)
177                 return -1;
178
179         if (pthread_mutex_destroy(&sem->mutex) < 0)
180                 return -1;
181
182         return 0;
183 }
184 #endif /* defined(__DARWIN__) */
185
186
187 /* internally used constants **************************************************/
188
189 /* CAUTION: Do not change these values. Boehm GC code depends on them.        */
190 #define STOPWORLD_FROM_GC               1
191 #define STOPWORLD_FROM_CLASS_NUMBERING  2
192
193
194 /* startupinfo *****************************************************************
195
196    Struct used to pass info from threads_start_thread to 
197    threads_startup_thread.
198
199 ******************************************************************************/
200
201 typedef struct {
202         threadobject *thread;      /* threadobject for this thread             */
203         functionptr   function;    /* function to run in the new thread        */
204         sem_t        *psem;        /* signals when thread has been entered     */
205                                    /* in the thread list                       */
206         sem_t        *psem_first;  /* signals when pthread_create has returned */
207 } startupinfo;
208
209
210 /* prototypes *****************************************************************/
211
212 static void threads_calc_absolute_time(struct timespec *tm, s8 millis, s4 nanos);
213
214
215 /******************************************************************************/
216 /* GLOBAL VARIABLES                                                           */
217 /******************************************************************************/
218
219 /* the main thread                                                            */
220 threadobject *mainthreadobj;
221
222 static methodinfo *method_thread_init;
223
224 /* the thread object of the current thread                                    */
225 /* This is either a thread-local variable defined with __thread, or           */
226 /* a thread-specific value stored with key threads_current_threadobject_key.  */
227 #if defined(HAVE___THREAD)
228 __thread threadobject *threads_current_threadobject;
229 #else
230 pthread_key_t threads_current_threadobject_key;
231 #endif
232
233 /* global mutex for changing the thread list                                  */
234 static pthread_mutex_t threadlistlock;
235
236 /* global mutex for stop-the-world                                            */
237 static pthread_mutex_t stopworldlock;
238
239 /* global mutex and condition for joining threads on exit */
240 static pthread_mutex_t mutex_join;
241 static pthread_cond_t  cond_join;
242
243 /* this is one of the STOPWORLD_FROM_ constants, telling why the world is     */
244 /* being stopped                                                              */
245 static volatile int stopworldwhere;
246
247 /* semaphore used for acknowleding thread suspension                          */
248 static sem_t suspend_ack;
249 #if defined(__MIPS__)
250 static pthread_mutex_t suspend_ack_lock = PTHREAD_MUTEX_INITIALIZER;
251 static pthread_cond_t suspend_cond = PTHREAD_COND_INITIALIZER;
252 #endif
253
254 static pthread_attr_t threadattr;
255
256 /* mutexes used by the fake atomic instructions                               */
257 #if defined(USE_FAKE_ATOMIC_INSTRUCTIONS)
258 pthread_mutex_t _atomic_add_lock = PTHREAD_MUTEX_INITIALIZER;
259 pthread_mutex_t _cas_lock = PTHREAD_MUTEX_INITIALIZER;
260 pthread_mutex_t _mb_lock = PTHREAD_MUTEX_INITIALIZER;
261 #endif
262
263
264 /* threads_sem_init ************************************************************
265  
266    Initialize a semaphore. Checks against errors and interruptions.
267
268    IN:
269        sem..............the semaphore to initialize
270            shared...........true if this semaphore will be shared between processes
271            value............the initial value for the semaphore
272    
273 *******************************************************************************/
274
275 void threads_sem_init(sem_t *sem, bool shared, int value)
276 {
277         int r;
278
279         assert(sem);
280
281         do {
282                 r = sem_init(sem, shared, value);
283                 if (r == 0)
284                         return;
285         } while (errno == EINTR);
286
287         vm_abort("sem_init failed: %s", strerror(errno));
288 }
289
290
291 /* threads_sem_wait ************************************************************
292  
293    Wait for a semaphore, non-interruptible.
294
295    IMPORTANT: Always use this function instead of `sem_wait` directly, as
296               `sem_wait` may be interrupted by signals!
297   
298    IN:
299        sem..............the semaphore to wait on
300    
301 *******************************************************************************/
302
303 void threads_sem_wait(sem_t *sem)
304 {
305         int r;
306
307         assert(sem);
308
309         do {
310                 r = sem_wait(sem);
311                 if (r == 0)
312                         return;
313         } while (errno == EINTR);
314
315         vm_abort("sem_wait failed: %s", strerror(errno));
316 }
317
318
319 /* threads_sem_post ************************************************************
320  
321    Increase the count of a semaphore. Checks for errors.
322
323    IN:
324        sem..............the semaphore to increase the count of
325    
326 *******************************************************************************/
327
328 void threads_sem_post(sem_t *sem)
329 {
330         int r;
331
332         assert(sem);
333
334         /* unlike sem_wait, sem_post is not interruptible */
335
336         r = sem_post(sem);
337         if (r == 0)
338                 return;
339
340         vm_abort("sem_post failed: %s", strerror(errno));
341 }
342
343
344 /* lock_stopworld **************************************************************
345
346    Enter the stopworld lock, specifying why the world shall be stopped.
347
348    IN:
349       where........ STOPWORLD_FROM_GC              (1) from within GC
350                     STOPWORLD_FROM_CLASS_NUMBERING (2) class numbering
351
352 ******************************************************************************/
353
354 void lock_stopworld(int where)
355 {
356         pthread_mutex_lock(&stopworldlock);
357         stopworldwhere = where;
358 }
359
360
361 /* unlock_stopworld ************************************************************
362
363    Release the stopworld lock.
364
365 ******************************************************************************/
366
367 void unlock_stopworld(void)
368 {
369         stopworldwhere = 0;
370         pthread_mutex_unlock(&stopworldlock);
371 }
372
373 #if !defined(__DARWIN__)
374 /* Caller must hold threadlistlock */
375 static int threads_cast_sendsignals(int sig, int count)
376 {
377         /* Count threads */
378         threadobject *tobj = mainthreadobj;
379         threadobject *self = THREADOBJECT;
380
381         if (count == 0) {
382                 do {
383                         count++;
384                         tobj = tobj->next;
385                 } while (tobj != mainthreadobj);
386         }
387
388         assert(tobj == mainthreadobj);
389
390         do {
391                 if (tobj != self)
392                         pthread_kill(tobj->tid, sig);
393                 tobj = tobj->next;
394         } while (tobj != mainthreadobj);
395
396         return count - 1;
397 }
398
399 #else
400
401 static void threads_cast_darwinstop(void)
402 {
403         threadobject *tobj = mainthreadobj;
404         threadobject *self = THREADOBJECT;
405
406         do {
407                 if (tobj != self)
408                 {
409                         thread_state_flavor_t flavor = MACHINE_THREAD_STATE;
410                         mach_msg_type_number_t thread_state_count = MACHINE_THREAD_STATE_COUNT;
411 #if defined(__I386__)
412                         i386_thread_state_t thread_state;
413 #else
414                         ppc_thread_state_t thread_state;
415 #endif
416                         mach_port_t thread = tobj->mach_thread;
417                         kern_return_t r;
418
419                         r = thread_suspend(thread);
420
421                         if (r != KERN_SUCCESS)
422                                 vm_abort("thread_suspend failed");
423
424                         r = thread_get_state(thread, flavor, (natural_t *) &thread_state,
425                                                                  &thread_state_count);
426
427                         if (r != KERN_SUCCESS)
428                                 vm_abort("thread_get_state failed");
429
430                         thread_restartcriticalsection((ucontext_t *) &thread_state);
431
432                         r = thread_set_state(thread, flavor, (natural_t *) &thread_state,
433                                                                  thread_state_count);
434
435                         if (r != KERN_SUCCESS)
436                                 vm_abort("thread_set_state failed");
437                 }
438
439                 tobj = tobj->next;
440         } while (tobj != mainthreadobj);
441 }
442
443 static void threads_cast_darwinresume(void)
444 {
445         threadobject *tobj = mainthreadobj;
446         threadobject *self = THREADOBJECT;
447
448         do {
449                 if (tobj != self)
450                 {
451                         mach_port_t thread = tobj->mach_thread;
452                         kern_return_t r;
453
454                         r = thread_resume(thread);
455
456                         if (r != KERN_SUCCESS)
457                                 vm_abort("thread_resume failed");
458                 }
459
460                 tobj = tobj->next;
461         } while (tobj != mainthreadobj);
462 }
463
464 #endif
465
466 #if defined(__MIPS__)
467 static void threads_cast_irixresume(void)
468 {
469         pthread_mutex_lock(&suspend_ack_lock);
470         pthread_cond_broadcast(&suspend_cond);
471         pthread_mutex_unlock(&suspend_ack_lock);
472 }
473 #endif
474
475 #if !defined(DISABLE_GC)
476
477 void threads_cast_stopworld(void)
478 {
479 #if !defined(__DARWIN__) && !defined(__CYGWIN__)
480         int count, i;
481 #endif
482
483         lock_stopworld(STOPWORLD_FROM_CLASS_NUMBERING);
484         pthread_mutex_lock(&threadlistlock);
485
486 #if defined(__DARWIN__)
487         threads_cast_darwinstop();
488 #elif defined(__CYGWIN__)
489         /* TODO */
490         assert(0);
491 #else
492         count = threads_cast_sendsignals(GC_signum1(), 0);
493         for (i = 0; i < count; i++)
494                 threads_sem_wait(&suspend_ack);
495 #endif
496
497         pthread_mutex_unlock(&threadlistlock);
498 }
499
500 void threads_cast_startworld(void)
501 {
502         pthread_mutex_lock(&threadlistlock);
503 #if defined(__DARWIN__)
504         threads_cast_darwinresume();
505 #elif defined(__MIPS__)
506         threads_cast_irixresume();
507 #elif defined(__CYGWIN__)
508         /* TODO */
509         assert(0);
510 #else
511         threads_cast_sendsignals(GC_signum2(), -1);
512 #endif
513         pthread_mutex_unlock(&threadlistlock);
514         unlock_stopworld();
515 }
516
517
518 #if !defined(__DARWIN__)
519 static void threads_sigsuspend_handler(ucontext_t *ctx)
520 {
521         int sig;
522         sigset_t sigs;
523
524         /* XXX TWISTI: this is just a quick hack */
525 #if defined(ENABLE_JIT)
526         thread_restartcriticalsection(ctx);
527 #endif
528
529         /* Do as Boehm does. On IRIX a condition variable is used for wake-up
530            (not POSIX async-safe). */
531 #if defined(__IRIX__)
532         pthread_mutex_lock(&suspend_ack_lock);
533         threads_sem_post(&suspend_ack);
534         pthread_cond_wait(&suspend_cond, &suspend_ack_lock);
535         pthread_mutex_unlock(&suspend_ack_lock);
536 #elif defined(__CYGWIN__)
537         /* TODO */
538         assert(0);
539 #else
540         threads_sem_post(&suspend_ack);
541
542         sig = GC_signum2();
543         sigfillset(&sigs);
544         sigdelset(&sigs, sig);
545         sigsuspend(&sigs);
546 #endif
547 }
548
549 /* This function is called from Boehm GC code. */
550
551 int cacao_suspendhandler(ucontext_t *ctx)
552 {
553         if (stopworldwhere != STOPWORLD_FROM_CLASS_NUMBERING)
554                 return 0;
555
556         threads_sigsuspend_handler(ctx);
557         return 1;
558 }
559 #endif
560
561 #endif /* DISABLE_GC */
562
563
564 /* threads_set_current_threadobject ********************************************
565
566    Set the current thread object.
567    
568    IN:
569       thread.......the thread object to set
570
571 *******************************************************************************/
572
573 void threads_set_current_threadobject(threadobject *thread)
574 {
575 #if !defined(HAVE___THREAD)
576         if (pthread_setspecific(threads_current_threadobject_key, thread) != 0)
577                 vm_abort("threads_set_current_threadobject: pthread_setspecific failed: %s", strerror(errno));
578 #else
579         threads_current_threadobject = thread;
580 #endif
581 }
582
583
584 /* threads_init_threadobject **************************************************
585
586    Initialize implementation fields of a threadobject.
587
588    IN:
589       thread............the threadobject
590
591 ******************************************************************************/
592
593 void threads_init_threadobject(threadobject *thread)
594 {
595         /* get the pthread id */
596
597         thread->tid = pthread_self();
598
599         thread->index = 0;
600
601         /* TODO destroy all those things */
602
603         pthread_mutex_init(&(thread->waitmutex), NULL);
604         pthread_cond_init(&(thread->waitcond), NULL);
605
606         thread->interrupted = false;
607         thread->signaled    = false;
608         thread->sleeping    = false;
609 }
610
611
612 /* threads_get_current_threadobject ********************************************
613
614    Return the threadobject of the current thread.
615    
616    RETURN VALUE:
617        the current threadobject * (an instance of java.lang.Thread)
618
619 *******************************************************************************/
620
621 threadobject *threads_get_current_threadobject(void)
622 {
623         return THREADOBJECT;
624 }
625
626
627 /* threads_impl_preinit ********************************************************
628
629    Do some early initialization of stuff required.
630
631    ATTENTION: Do NOT use any Java heap allocation here, as gc_init()
632    is called AFTER this function!
633
634 *******************************************************************************/
635
636 void threads_impl_preinit(void)
637 {
638         pthread_mutex_init(&threadlistlock, NULL);
639         pthread_mutex_init(&stopworldlock, NULL);
640
641         /* initialize exit mutex and condition (on exit we join all
642            threads) */
643
644         pthread_mutex_init(&mutex_join, NULL);
645         pthread_cond_init(&cond_join, NULL);
646
647 #if !defined(HAVE___THREAD)
648         pthread_key_create(&threads_current_threadobject_key, NULL);
649 #endif
650
651         /* create internal thread data-structure */
652
653         mainthreadobj = threads_create_thread();
654
655         mainthreadobj->object   = NULL;
656         mainthreadobj->index    = 1;
657         mainthreadobj->thinlock = lock_pre_compute_thinlock(mainthreadobj->index);
658
659         /* store the internal thread data-structure in the TSD */
660
661         threads_set_current_threadobject(mainthreadobj);
662         
663         threads_sem_init(&suspend_ack, 0, 0);
664 }
665
666
667 /* threads_init ****************************************************************
668
669    Initializes the threads required by the JVM: main, finalizer.
670
671 *******************************************************************************/
672
673 bool threads_init(void)
674 {
675         java_objectheader     *threadname;
676         java_lang_Thread      *t;
677         java_objectheader     *o;
678
679 #if defined(ENABLE_JAVASE)
680         java_lang_ThreadGroup *threadgroup;
681         methodinfo            *m;
682 #endif
683
684 #if defined(WITH_CLASSPATH_GNU)
685         java_lang_VMThread    *vmt;
686 #endif
687
688         /* get methods we need in this file */
689
690 #if defined(WITH_CLASSPATH_GNU)
691         method_thread_init =
692                 class_resolveclassmethod(class_java_lang_Thread,
693                                                                  utf_init,
694                                                                  utf_new_char("(Ljava/lang/VMThread;Ljava/lang/String;IZ)V"),
695                                                                  class_java_lang_Thread,
696                                                                  true);
697 #else
698         method_thread_init =
699                 class_resolveclassmethod(class_java_lang_Thread,
700                                                                  utf_init,
701                                                                  utf_new_char("(Ljava/lang/String;)V"),
702                                                                  class_java_lang_Thread,
703                                                                  true);
704 #endif
705
706         if (method_thread_init == NULL)
707                 return false;
708
709         /* create a java.lang.Thread for the main thread */
710
711         t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
712
713         if (t == NULL)
714                 return false;
715
716         /* set the object in the internal data structure */
717
718         mainthreadobj->object = t;
719
720         /* thread is running */
721
722         mainthreadobj->state = THREAD_STATE_RUNNABLE;
723
724         mainthreadobj->next = mainthreadobj;
725         mainthreadobj->prev = mainthreadobj;
726
727         threads_table_add(mainthreadobj);
728
729         /* mark main thread as Java thread */
730
731         mainthreadobj->flags = THREAD_FLAG_JAVA;
732
733 #if defined(ENABLE_INTRP)
734         /* create interpreter stack */
735
736         if (opt_intrp) {
737                 MSET(intrp_main_stack, 0, u1, opt_stacksize);
738                 mainthreadobj->_global_sp = (Cell*) (intrp_main_stack + opt_stacksize);
739         }
740 #endif
741
742         threadname = javastring_new(utf_new_char("main"));
743
744 #if defined(ENABLE_JAVASE)
745         /* allocate and init ThreadGroup */
746
747         threadgroup = (java_lang_ThreadGroup *)
748                 native_new_and_init(class_java_lang_ThreadGroup);
749
750         if (threadgroup == NULL)
751                 return false;
752 #endif
753
754 #if defined(WITH_CLASSPATH_GNU)
755         /* create a java.lang.VMThread for the main thread */
756
757         vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
758
759         if (vmt == NULL)
760                 return false;
761
762         /* set the thread */
763
764         vmt->thread = t;
765         vmt->vmdata = (java_lang_Object *) mainthreadobj;
766
767         /* call java.lang.Thread.<init>(Ljava/lang/VMThread;Ljava/lang/String;IZ)V */
768         o = (java_objectheader *) t;
769
770         (void) vm_call_method(method_thread_init, o, vmt, threadname, NORM_PRIORITY,
771                                                   false);
772 #elif defined(WITH_CLASSPATH_CLDC1_1)
773         /* set the thread */
774
775         t->vm_thread = (java_lang_Object *) mainthreadobj;
776
777         /* call public Thread(String name) */
778
779         o = (java_objectheader *) t;
780
781         (void) vm_call_method(method_thread_init, o, threadname);
782 #endif
783
784         if (*exceptionptr)
785                 return false;
786
787 #if defined(ENABLE_JAVASE)
788         t->group = threadgroup;
789
790         /* add main thread to java.lang.ThreadGroup */
791
792         m = class_resolveclassmethod(class_java_lang_ThreadGroup,
793                                                                  utf_addThread,
794                                                                  utf_java_lang_Thread__V,
795                                                                  class_java_lang_ThreadGroup,
796                                                                  true);
797
798         o = (java_objectheader *) threadgroup;
799
800         (void) vm_call_method(m, o, t);
801
802         if (*exceptionptr)
803                 return false;
804 #endif
805
806         threads_set_thread_priority(pthread_self(), NORM_PRIORITY);
807
808         /* initialize the thread attribute object */
809
810         if (pthread_attr_init(&threadattr)) {
811                 log_println("pthread_attr_init failed: %s", strerror(errno));
812                 return false;
813         }
814
815         pthread_attr_setdetachstate(&threadattr, PTHREAD_CREATE_DETACHED);
816
817         /* everything's ok */
818
819         return true;
820 }
821
822
823 /* threads_startup_thread ******************************************************
824
825    Thread startup function called by pthread_create.
826
827    Thread which have a startup.function != NULL are marked as internal
828    threads. All other threads are threated as normal Java threads.
829
830    NOTE: This function is not called directly by pthread_create. The Boehm GC
831          inserts its own GC_start_routine in between, which then calls
832                  threads_startup.
833
834    IN:
835       t............the argument passed to pthread_create, ie. a pointer to
836                        a startupinfo struct. CAUTION: When the `psem` semaphore
837                                    is posted, the startupinfo struct becomes invalid! (It
838                                    is allocated on the stack of threads_start_thread.)
839
840 ******************************************************************************/
841
842 static void *threads_startup_thread(void *t)
843 {
844         startupinfo        *startup;
845         threadobject       *thread;
846 #if defined(WITH_CLASSPATH_GNU)
847         java_lang_VMThread *vmt;
848 #endif
849         sem_t              *psem;
850         threadobject       *tnext;
851         classinfo          *c;
852         methodinfo         *m;
853         java_objectheader  *o;
854         functionptr         function;
855
856 #if defined(ENABLE_INTRP)
857         u1 *intrp_thread_stack;
858
859         /* create interpreter stack */
860
861         if (opt_intrp) {
862                 intrp_thread_stack = GCMNEW(u1, opt_stacksize);
863                 MSET(intrp_thread_stack, 0, u1, opt_stacksize);
864         }
865         else
866                 intrp_thread_stack = NULL;
867 #endif
868
869         /* get passed startupinfo structure and the values in there */
870
871         startup = t;
872         t = NULL; /* make sure it's not used wrongly */
873
874         thread   = startup->thread;
875         function = startup->function;
876         psem     = startup->psem;
877
878         /* Seems like we've encountered a situation where thread->tid was not set by
879          * pthread_create. We alleviate this problem by waiting for pthread_create
880          * to return. */
881         threads_sem_wait(startup->psem_first);
882
883 #if defined(__DARWIN__)
884         thread->mach_thread = mach_thread_self();
885 #endif
886
887         /* store the internal thread data-structure in the TSD */
888
889         threads_set_current_threadobject(thread);
890
891         /* thread is running */
892
893         thread->state = THREAD_STATE_RUNNABLE;
894
895         /* insert the thread into the threadlist and the threads table */
896
897         pthread_mutex_lock(&threadlistlock);
898
899         thread->prev = mainthreadobj;
900         thread->next = tnext = mainthreadobj->next;
901         mainthreadobj->next = thread;
902         tnext->prev = thread;
903
904         threads_table_add(thread);
905
906         pthread_mutex_unlock(&threadlistlock);
907
908         /* tell threads_startup_thread that we registered ourselves */
909         /* CAUTION: *startup becomes invalid with this!             */
910
911         startup = NULL;
912         threads_sem_post(psem);
913
914         /* set our priority */
915
916         threads_set_thread_priority(thread->tid, thread->object->priority);
917
918 #if defined(ENABLE_INTRP)
919         /* set interpreter stack */
920
921         if (opt_intrp)
922                 thread->_global_sp = (Cell *) (intrp_thread_stack + opt_stacksize);
923 #endif
924
925 #if defined(ENABLE_JVMTI)
926         /* fire thread start event */
927
928         if (jvmti) 
929                 jvmti_ThreadStartEnd(JVMTI_EVENT_THREAD_START);
930 #endif
931
932         /* find and run the Thread.run()V method if no other function was passed */
933
934         if (function == NULL) {
935                 /* this is a normal Java thread */
936
937                 thread->flags |= THREAD_FLAG_JAVA;
938
939 #if defined(WITH_CLASSPATH_GNU)
940                 /* We need to start the run method of
941                    java.lang.VMThread. Since this is a final class, we can use
942                    the class object directly. */
943
944                 c   = class_java_lang_VMThread;
945 #elif defined(WITH_CLASSPATH_CLDC1_1)
946                 c   = thread->object->header.vftbl->class;
947 #endif
948
949                 m = class_resolveclassmethod(c, utf_run, utf_void__void, c, true);
950
951                 if (m == NULL)
952                         vm_abort("threads_startup_thread: run() method not found in class");
953
954                 /* set ThreadMXBean variables */
955
956                 _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount++;
957                 _Jv_jvm->java_lang_management_ThreadMXBean_TotalStartedThreadCount++;
958
959                 if (_Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount >
960                         _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount)
961                         _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount =
962                                 _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount;
963
964 #if defined(WITH_CLASSPATH_GNU)
965                 /* we need to start the run method of java.lang.VMThread */
966
967                 vmt = (java_lang_VMThread *) thread->object->vmThread;
968                 o   = (java_objectheader *) vmt;
969
970 #elif defined(WITH_CLASSPATH_CLDC1_1)
971                 o   = (java_objectheader *) thread->object;
972 #endif
973
974                 /* run the thread */
975
976                 (void) vm_call_method(m, o);
977         }
978         else {
979                 /* this is an internal thread */
980
981                 thread->flags |= THREAD_FLAG_INTERNAL;
982
983                 /* set ThreadMXBean variables */
984
985                 _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount++;
986                 _Jv_jvm->java_lang_management_ThreadMXBean_TotalStartedThreadCount++;
987
988                 if (_Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount >
989                         _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount)
990                         _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount =
991                                 _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount;
992
993                 /* call passed function, e.g. finalizer_thread */
994
995                 (function)();
996         }
997
998 #if defined(ENABLE_JVMTI)
999         /* fire thread end event */
1000
1001         if (jvmti)
1002                 jvmti_ThreadStartEnd(JVMTI_EVENT_THREAD_END);
1003 #endif
1004
1005         if (!threads_detach_thread(thread))
1006                 vm_abort("threads_startup_thread: threads_detach_thread failed");
1007
1008         /* set ThreadMXBean variables */
1009
1010         _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount--;
1011
1012         return NULL;
1013 }
1014
1015
1016 /* threads_start_thread ********************************************************
1017
1018    Start a thread in the JVM. Both (vm internal and java) thread objects exist.
1019
1020    IN:
1021       thread.......the thread object
1022           function.....function to run in the new thread. NULL means that the
1023                        "run" method of the object `t` should be called
1024
1025 ******************************************************************************/
1026
1027 void threads_start_thread(threadobject *thread, functionptr function)
1028 {
1029         sem_t          sem;
1030         sem_t          sem_first;
1031         pthread_attr_t attr;
1032         startupinfo    startup;
1033
1034         /* fill startupinfo structure passed by pthread_create to
1035          * threads_startup_thread */
1036
1037         startup.thread     = thread;
1038         startup.function   = function;       /* maybe we don't call Thread.run()V */
1039         startup.psem       = &sem;
1040         startup.psem_first = &sem_first;
1041
1042         threads_sem_init(&sem, 0, 0);
1043         threads_sem_init(&sem_first, 0, 0);
1044
1045         /* initialize thread attribute object */
1046
1047         if (pthread_attr_init(&attr))
1048                 vm_abort("pthread_attr_init failed: %s", strerror(errno));
1049
1050         /* initialize thread stacksize */
1051
1052         if (pthread_attr_setstacksize(&attr, opt_stacksize))
1053                 vm_abort("pthread_attr_setstacksize failed: %s", strerror(errno));
1054
1055         /* create the thread */
1056
1057         if (pthread_create(&(thread->tid), &attr, threads_startup_thread, &startup))
1058                 vm_abort("pthread_create failed: %s", strerror(errno));
1059
1060         /* signal that pthread_create has returned, so thread->tid is valid */
1061
1062         threads_sem_post(&sem_first);
1063
1064         /* wait here until the thread has entered itself into the thread list */
1065
1066         threads_sem_wait(&sem);
1067
1068         /* cleanup */
1069
1070         sem_destroy(&sem);
1071         sem_destroy(&sem_first);
1072 }
1073
1074
1075 /* threads_set_thread_priority *************************************************
1076
1077    Set the priority of the given thread.
1078
1079    IN:
1080       tid..........thread id
1081           priority.....priority to set
1082
1083 ******************************************************************************/
1084
1085 void threads_set_thread_priority(pthread_t tid, int priority)
1086 {
1087         struct sched_param schedp;
1088         int policy;
1089
1090         pthread_getschedparam(tid, &policy, &schedp);
1091         schedp.sched_priority = priority;
1092         pthread_setschedparam(tid, policy, &schedp);
1093 }
1094
1095
1096 /* threads_attach_current_thread ***********************************************
1097
1098    Attaches the current thread to the VM.  Used in JNI.
1099
1100 *******************************************************************************/
1101
1102 bool threads_attach_current_thread(JavaVMAttachArgs *vm_aargs, bool isdaemon)
1103 {
1104         threadobject          *thread;
1105         utf                   *u;
1106         java_objectheader     *s;
1107         java_objectheader     *o;
1108         java_lang_Thread      *t;
1109
1110 #if defined(ENABLE_JAVASE)
1111         java_lang_ThreadGroup *group;
1112         methodinfo            *m;
1113 #endif
1114
1115 #if defined(WITH_CLASSPATH_GNU)
1116         java_lang_VMThread    *vmt;
1117 #endif
1118
1119         /* create internal thread data-structure */
1120
1121         thread = threads_create_thread();
1122
1123         /* create a java.lang.Thread object */
1124
1125         t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
1126
1127         if (t == NULL)
1128                 return false;
1129
1130         thread->object = t;
1131
1132         /* thread is running */
1133
1134         thread->state = THREAD_STATE_RUNNABLE;
1135
1136         /* insert the thread into the threadlist and the threads table */
1137
1138         pthread_mutex_lock(&threadlistlock);
1139
1140         thread->prev        = mainthreadobj;
1141         thread->next        = mainthreadobj->next;
1142         mainthreadobj->next = thread;
1143         thread->next->prev  = thread;
1144
1145         threads_table_add(thread);
1146
1147         pthread_mutex_unlock(&threadlistlock);
1148
1149         /* mark thread as Java thread */
1150
1151         thread->flags = THREAD_FLAG_JAVA;
1152
1153         if (isdaemon)
1154                 thread->flags |= THREAD_FLAG_DAEMON;
1155
1156 #if defined(ENABLE_INTRP)
1157         /* create interpreter stack */
1158
1159         if (opt_intrp) {
1160                 MSET(intrp_main_stack, 0, u1, opt_stacksize);
1161                 thread->_global_sp = (Cell *) (intrp_main_stack + opt_stacksize);
1162         }
1163 #endif
1164
1165 #if defined(WITH_CLASSPATH_GNU)
1166         /* create a java.lang.VMThread object */
1167
1168         vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
1169
1170         if (vmt == NULL)
1171                 return false;
1172
1173         /* set the thread */
1174
1175         vmt->thread = t;
1176         vmt->vmdata = (java_lang_Object *) thread;
1177 #elif defined(WITH_CLASSPATH_CLDC1_1)
1178         t->vm_thread = (java_lang_Object *) thread;
1179 #endif
1180
1181         if (vm_aargs != NULL) {
1182                 u     = utf_new_char(vm_aargs->name);
1183 #if defined(ENABLE_JAVASE)
1184                 group = (java_lang_ThreadGroup *) vm_aargs->group;
1185 #endif
1186         }
1187         else {
1188                 u     = utf_null;
1189 #if defined(ENABLE_JAVASE)
1190                 group = mainthreadobj->object->group;
1191 #endif
1192         }
1193
1194         /* the the thread name */
1195
1196         s = javastring_new(u);
1197
1198         /* for convenience */
1199
1200         o = (java_objectheader *) thread->object;
1201
1202 #if defined(WITH_CLASSPATH_GNU)
1203         (void) vm_call_method(method_thread_init, o, vmt, s, NORM_PRIORITY,
1204                                                   isdaemon);
1205 #elif defined(WITH_CLASSPATH_CLDC1_1)
1206         (void) vm_call_method(method_thread_init, o, s);
1207 #endif
1208
1209         if (*exceptionptr)
1210                 return false;
1211
1212 #if defined(ENABLE_JAVASE)
1213         /* store the thread group in the object */
1214
1215         thread->object->group = group;
1216
1217         /* add thread to given thread-group */
1218
1219         m = class_resolveclassmethod(group->header.vftbl->class,
1220                                                                  utf_addThread,
1221                                                                  utf_java_lang_Thread__V,
1222                                                                  class_java_lang_ThreadGroup,
1223                                                                  true);
1224
1225         o = (java_objectheader *) group;
1226
1227         (void) vm_call_method(m, o, t);
1228
1229         if (*exceptionptr)
1230                 return false;
1231 #endif
1232
1233         return true;
1234 }
1235
1236
1237 /* threads_detach_thread *******************************************************
1238
1239    Detaches the passed thread from the VM.  Used in JNI.
1240
1241 *******************************************************************************/
1242
1243 bool threads_detach_thread(threadobject *thread)
1244 {
1245 #if defined(ENABLE_JAVASE)
1246         java_lang_ThreadGroup *group;
1247         methodinfo            *m;
1248         java_objectheader     *o;
1249         java_lang_Thread      *t;
1250 #endif
1251
1252         /* Allow lock record pools to be used by other threads. They
1253            cannot be deleted so we'd better not waste them. */
1254
1255         /* XXX We have to find a new way to free lock records */
1256         /*     with the new locking algorithm.                */
1257         /* lock_record_free_pools(thread->ee.lockrecordpools); */
1258
1259         /* XXX implement uncaught exception stuff (like JamVM does) */
1260
1261 #if defined(ENABLE_JAVASE)
1262         /* remove thread from the thread group */
1263
1264         group = thread->object->group;
1265
1266         /* XXX TWISTI: should all threads be in a ThreadGroup? */
1267
1268         if (group != NULL) {
1269                 m = class_resolveclassmethod(group->header.vftbl->class,
1270                                                                          utf_removeThread,
1271                                                                          utf_java_lang_Thread__V,
1272                                                                          class_java_lang_ThreadGroup,
1273                                                                          true);
1274
1275                 if (m == NULL)
1276                         return false;
1277
1278                 o = (java_objectheader *) group;
1279                 t = thread->object;
1280
1281                 (void) vm_call_method(m, o, t);
1282
1283                 if (*exceptionptr)
1284                         return false;
1285         }
1286 #endif
1287
1288         /* thread is terminated */
1289
1290         thread->state = THREAD_STATE_TERMINATED;
1291
1292         /* lock thread list */
1293
1294         pthread_mutex_lock(&threadlistlock);
1295
1296         /* remove thread from thread list and threads table */
1297
1298         thread->next->prev = thread->prev;
1299         thread->prev->next = thread->next;
1300
1301         threads_table_remove(thread);
1302
1303         /* unlock thread list */
1304
1305         pthread_mutex_unlock(&threadlistlock);
1306
1307         /* signal that this thread has finished */
1308
1309         pthread_mutex_lock(&mutex_join);
1310         pthread_cond_signal(&cond_join);
1311         pthread_mutex_unlock(&mutex_join);
1312
1313         /* free the vm internal thread object */
1314
1315 #if defined(ENABLE_GC_BOEHM)
1316         GCFREE(thread);
1317 #else
1318         FREE(thread, threadobject);
1319 #endif
1320
1321 #if defined(ENABLE_STATISTICS)
1322         if (opt_stat)
1323                 size_threadobject -= sizeof(threadobject);
1324 #endif
1325
1326         return true;
1327 }
1328
1329
1330 /* threads_find_non_daemon_thread **********************************************
1331
1332    Helper function used by threads_join_all_threads for finding
1333    non-daemon threads that are still running.
1334
1335 *******************************************************************************/
1336
1337 static threadobject *threads_find_non_daemon_thread(void)
1338 {
1339         threadobject *thread;
1340
1341         /* lock the thread list */
1342
1343         pthread_mutex_lock(&threadlistlock);
1344
1345         /* iterate over all threads */
1346
1347         thread = mainthreadobj->next;
1348
1349         while (thread != mainthreadobj) {
1350                 if (!(thread->flags & THREAD_FLAG_DAEMON)) {
1351                         /* unlock thread list */
1352
1353                         pthread_mutex_unlock(&threadlistlock);
1354
1355                         return thread;
1356                 }
1357
1358                 thread = thread->next;
1359         }
1360
1361         /* unlock thread list */
1362
1363         pthread_mutex_unlock(&threadlistlock);
1364
1365         return NULL;
1366 }
1367
1368
1369 /* threads_join_all_threads ****************************************************
1370
1371    Join all non-daemon threads.
1372
1373 *******************************************************************************/
1374
1375 void threads_join_all_threads(void)
1376 {
1377         threadobject *thread;
1378
1379         /* get current thread */
1380
1381         thread = THREADOBJECT;
1382
1383         /* this thread is waiting for all non-daemon threads to exit */
1384
1385         thread->state = THREAD_STATE_WAITING;
1386
1387         /* enter join mutex */
1388
1389         pthread_mutex_lock(&mutex_join);
1390
1391         /* wait for condition as long as we have non-daemon threads */
1392
1393         while (threads_find_non_daemon_thread() != NULL)
1394                 pthread_cond_wait(&cond_join, &mutex_join);
1395
1396         /* leave join mutex */
1397
1398         pthread_mutex_unlock(&mutex_join);
1399 }
1400
1401
1402 /* threads_timespec_earlier ****************************************************
1403
1404    Return true if timespec tv1 is earlier than timespec tv2.
1405
1406    IN:
1407       tv1..........first timespec
1408           tv2..........second timespec
1409
1410    RETURN VALUE:
1411       true, if the first timespec is earlier
1412
1413 *******************************************************************************/
1414
1415 static inline bool threads_timespec_earlier(const struct timespec *tv1,
1416                                                                                         const struct timespec *tv2)
1417 {
1418         return (tv1->tv_sec < tv2->tv_sec)
1419                                 ||
1420                 (tv1->tv_sec == tv2->tv_sec && tv1->tv_nsec < tv2->tv_nsec);
1421 }
1422
1423
1424 /* threads_current_time_is_earlier_than ****************************************
1425
1426    Check if the current time is earlier than the given timespec.
1427
1428    IN:
1429       tv...........the timespec to compare against
1430
1431    RETURN VALUE:
1432       true, if the current time is earlier
1433
1434 *******************************************************************************/
1435
1436 static bool threads_current_time_is_earlier_than(const struct timespec *tv)
1437 {
1438         struct timeval tvnow;
1439         struct timespec tsnow;
1440
1441         /* get current time */
1442
1443         if (gettimeofday(&tvnow, NULL) != 0)
1444                 vm_abort("gettimeofday failed: %s\n", strerror(errno));
1445
1446         /* convert it to a timespec */
1447
1448         tsnow.tv_sec = tvnow.tv_sec;
1449         tsnow.tv_nsec = tvnow.tv_usec * 1000;
1450
1451         /* compare current time with the given timespec */
1452
1453         return threads_timespec_earlier(&tsnow, tv);
1454 }
1455
1456
1457 /* threads_wait_with_timeout ***************************************************
1458
1459    Wait until the given point in time on a monitor until either
1460    we are notified, we are interrupted, or the time is up.
1461
1462    IN:
1463       t............the current thread
1464           wakeupTime...absolute (latest) wakeup time
1465                            If both tv_sec and tv_nsec are zero, this function
1466                                            waits for an unlimited amount of time.
1467
1468    RETURN VALUE:
1469       true.........if the wait has been interrupted,
1470           false........if the wait was ended by notification or timeout
1471
1472 *******************************************************************************/
1473
1474 static bool threads_wait_with_timeout(threadobject *thread,
1475                                                                           struct timespec *wakeupTime)
1476 {
1477         bool wasinterrupted;
1478
1479         /* acquire the waitmutex */
1480
1481         pthread_mutex_lock(&thread->waitmutex);
1482
1483         /* mark us as sleeping */
1484
1485         thread->sleeping = true;
1486
1487         /* wait on waitcond */
1488
1489         if (wakeupTime->tv_sec || wakeupTime->tv_nsec) {
1490                 /* with timeout */
1491                 while (!thread->interrupted && !thread->signaled
1492                            && threads_current_time_is_earlier_than(wakeupTime))
1493                 {
1494                         thread->state = THREAD_STATE_TIMED_WAITING;
1495
1496                         pthread_cond_timedwait(&thread->waitcond, &thread->waitmutex,
1497                                                                    wakeupTime);
1498
1499                         thread->state = THREAD_STATE_RUNNABLE;
1500                 }
1501         }
1502         else {
1503                 /* no timeout */
1504                 while (!thread->interrupted && !thread->signaled) {
1505                         thread->state = THREAD_STATE_WAITING;
1506
1507                         pthread_cond_wait(&thread->waitcond, &thread->waitmutex);
1508
1509                         thread->state = THREAD_STATE_RUNNABLE;
1510                 }
1511         }
1512
1513         /* check if we were interrupted */
1514
1515         wasinterrupted = thread->interrupted;
1516
1517         /* reset all flags */
1518
1519         thread->interrupted = false;
1520         thread->signaled    = false;
1521         thread->sleeping    = false;
1522
1523         /* release the waitmutex */
1524
1525         pthread_mutex_unlock(&thread->waitmutex);
1526
1527         return wasinterrupted;
1528 }
1529
1530
1531 /* threads_wait_with_timeout_relative ******************************************
1532
1533    Wait for the given maximum amount of time on a monitor until either
1534    we are notified, we are interrupted, or the time is up.
1535
1536    IN:
1537       t............the current thread
1538           millis.......milliseconds to wait
1539           nanos........nanoseconds to wait
1540
1541    RETURN VALUE:
1542       true.........if the wait has been interrupted,
1543           false........if the wait was ended by notification or timeout
1544
1545 *******************************************************************************/
1546
1547 bool threads_wait_with_timeout_relative(threadobject *thread, s8 millis,
1548                                                                                 s4 nanos)
1549 {
1550         struct timespec wakeupTime;
1551
1552         /* calculate the the (latest) wakeup time */
1553
1554         threads_calc_absolute_time(&wakeupTime, millis, nanos);
1555
1556         /* wait */
1557
1558         return threads_wait_with_timeout(thread, &wakeupTime);
1559 }
1560
1561
1562 /* threads_calc_absolute_time **************************************************
1563
1564    Calculate the absolute point in time a given number of ms and ns from now.
1565
1566    IN:
1567       millis............milliseconds from now
1568           nanos.............nanoseconds from now
1569
1570    OUT:
1571       *tm...............receives the timespec of the absolute point in time
1572
1573 *******************************************************************************/
1574
1575 static void threads_calc_absolute_time(struct timespec *tm, s8 millis, s4 nanos)
1576 {
1577         if ((millis != 0x7fffffffffffffffLLU) && (millis || nanos)) {
1578                 struct timeval tv;
1579                 long nsec;
1580                 gettimeofday(&tv, NULL);
1581                 tv.tv_sec += millis / 1000;
1582                 millis %= 1000;
1583                 nsec = tv.tv_usec * 1000 + (s4) millis * 1000000 + nanos;
1584                 tm->tv_sec = tv.tv_sec + nsec / 1000000000;
1585                 tm->tv_nsec = nsec % 1000000000;
1586         }
1587         else {
1588                 tm->tv_sec = 0;
1589                 tm->tv_nsec = 0;
1590         }
1591 }
1592
1593
1594 /* threads_thread_interrupt ****************************************************
1595
1596    Interrupt the given thread.
1597
1598    The thread gets the "waitcond" signal and 
1599    its interrupted flag is set to true.
1600
1601    IN:
1602       thread............the thread to interrupt
1603
1604 *******************************************************************************/
1605
1606 void threads_thread_interrupt(threadobject *thread)
1607 {
1608         /* Signal the thread a "waitcond" and tell it that it has been
1609            interrupted. */
1610
1611         pthread_mutex_lock(&thread->waitmutex);
1612
1613         /* Interrupt blocking system call using a signal. */
1614
1615         pthread_kill(thread->tid, SIGHUP);
1616
1617         if (thread->sleeping)
1618                 pthread_cond_signal(&thread->waitcond);
1619
1620         thread->interrupted = true;
1621
1622         pthread_mutex_unlock(&thread->waitmutex);
1623 }
1624
1625
1626 /* threads_check_if_interrupted_and_reset **************************************
1627
1628    Check if the current thread has been interrupted and reset the
1629    interruption flag.
1630
1631    RETURN VALUE:
1632       true, if the current thread had been interrupted
1633
1634 *******************************************************************************/
1635
1636 bool threads_check_if_interrupted_and_reset(void)
1637 {
1638         threadobject *thread;
1639         bool intr;
1640
1641         thread = THREADOBJECT;
1642
1643         /* get interrupted flag */
1644
1645         intr = thread->interrupted;
1646
1647         /* reset interrupted flag */
1648
1649         thread->interrupted = false;
1650
1651         return intr;
1652 }
1653
1654
1655 /* threads_thread_has_been_interrupted *****************************************
1656
1657    Check if the given thread has been interrupted
1658
1659    IN:
1660       t............the thread to check
1661
1662    RETURN VALUE:
1663       true, if the given thread had been interrupted
1664
1665 *******************************************************************************/
1666
1667 bool threads_thread_has_been_interrupted(threadobject *thread)
1668 {
1669         return thread->interrupted;
1670 }
1671
1672
1673 /* threads_sleep ***************************************************************
1674
1675    Sleep the current thread for the specified amount of time.
1676
1677 *******************************************************************************/
1678
1679 void threads_sleep(s8 millis, s4 nanos)
1680 {
1681         threadobject    *thread;
1682         struct timespec  wakeupTime;
1683         bool             wasinterrupted;
1684
1685         thread = THREADOBJECT;
1686
1687         threads_calc_absolute_time(&wakeupTime, millis, nanos);
1688
1689         wasinterrupted = threads_wait_with_timeout(thread, &wakeupTime);
1690
1691         if (wasinterrupted)
1692                 exceptions_throw_interruptedexception();
1693 }
1694
1695
1696 /* threads_yield ***************************************************************
1697
1698    Yield to the scheduler.
1699
1700 *******************************************************************************/
1701
1702 void threads_yield(void)
1703 {
1704         sched_yield();
1705 }
1706
1707
1708 /*
1709  * These are local overrides for various environment variables in Emacs.
1710  * Please do not remove this and leave it at the end of the file, where
1711  * Emacs will automagically detect them.
1712  * ---------------------------------------------------------------------
1713  * Local variables:
1714  * mode: c
1715  * indent-tabs-mode: t
1716  * c-basic-offset: 4
1717  * tab-width: 4
1718  * End:
1719  * vim:noexpandtab:sw=4:ts=4:
1720  */