1 /* threads/native/threads.c - native threads support
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
29 $Id: threads.c 1735 2004-12-07 14:33:27Z twisti $
37 #include <sys/types.h>
47 #include "mm/memory.h"
48 #include "native/native.h"
49 #include "native/include/java_lang_Object.h"
50 #include "native/include/java_lang_Throwable.h"
51 #include "native/include/java_lang_Thread.h"
52 #include "native/include/java_lang_ThreadGroup.h"
53 #include "native/include/java_lang_VMThread.h"
54 #include "threads/native/threads.h"
55 #include "toolbox/avl.h"
56 #include "toolbox/logging.h"
57 #include "vm/builtin.h"
58 #include "vm/exceptions.h"
59 #include "vm/global.h"
60 #include "vm/loader.h"
61 #include "vm/tables.h"
62 #include "vm/jit/asmpart.h"
65 #include <semaphore.h>
67 #if !defined(__DARWIN__)
68 #if defined(__LINUX__)
69 #define GC_LINUX_THREADS
70 #elif defined(__MIPS__)
71 #define GC_IRIX_THREADS
73 #include "boehm-gc/include/gc.h"
78 /* We need this for older MacOSX (10.1.x) */
81 pthread_mutex_t mutex;
84 } pthread_mutex_rec_t;
86 static void pthread_mutex_init_rec(pthread_mutex_rec_t *m)
88 pthread_mutex_init(&m->mutex, NULL);
92 static void pthread_mutex_destroy_rec(pthread_mutex_rec_t *m)
94 pthread_mutex_destroy(&m->mutex);
97 static void pthread_mutex_lock_rec(pthread_mutex_rec_t *m)
102 pthread_mutex_lock(&m->mutex);
103 m->owner = pthread_self();
107 if (m->owner != pthread_self())
108 pthread_mutex_lock(&m->mutex);
117 static void pthread_mutex_unlock_rec(pthread_mutex_rec_t *m)
120 pthread_mutex_unlock(&m->mutex);
125 #define pthread_mutex_lock_rec pthread_mutex_lock
126 #define pthread_mutex_unlock_rec pthread_mutex_unlock
127 #define pthread_mutex_rec_t pthread_mutex_t
129 #endif /* MUTEXSIM */
131 static void setPriority(pthread_t tid, int priority)
133 struct sched_param schedp;
136 pthread_getschedparam(tid, &policy, &schedp);
137 schedp.sched_priority = priority;
138 pthread_setschedparam(tid, policy, &schedp);
141 #include "machine-instr.h"
143 static struct avl_table *criticaltree;
144 static threadobject *mainthreadobj;
146 #ifndef HAVE___THREAD
147 pthread_key_t tkey_threadinfo;
149 __thread threadobject *threadobj;
152 static pthread_mutex_rec_t compiler_mutex;
153 static pthread_mutex_rec_t tablelock;
157 pthread_mutex_lock_rec(&compiler_mutex);
160 void compiler_unlock()
162 pthread_mutex_unlock_rec(&compiler_mutex);
167 pthread_mutex_lock_rec(&tablelock);
172 pthread_mutex_unlock_rec(&tablelock);
175 static int criticalcompare(const void *pa, const void *pb, void *param)
177 const threadcritnode *na = pa;
178 const threadcritnode *nb = pb;
180 if (na->mcodebegin < nb->mcodebegin)
182 if (na->mcodebegin > nb->mcodebegin)
187 static const threadcritnode *findcritical(u1 *mcodeptr)
189 struct avl_node *n = criticaltree->avl_root;
190 const threadcritnode *m = NULL;
195 const threadcritnode *d = n->avl_data;
196 if (mcodeptr == d->mcodebegin)
198 if (mcodeptr < d->mcodebegin) {
204 if (n->avl_link[1]) {
213 void thread_registercritical(threadcritnode *n)
215 avl_insert(criticaltree, n);
218 u1 *thread_checkcritical(u1 *mcodeptr)
220 const threadcritnode *n = findcritical(mcodeptr);
221 return (n && mcodeptr < n->mcodeend && mcodeptr > n->mcodebegin) ? n->mcoderestart : NULL;
224 static void thread_addstaticcritical()
226 threadcritnode *n = &asm_criticalsections;
228 while (n->mcodebegin)
229 thread_registercritical(n++);
232 static pthread_mutex_t threadlistlock;
234 static pthread_mutex_t stopworldlock;
235 volatile int stopworldwhere;
237 static sem_t suspend_ack;
238 #if defined(__MIPS__)
239 static pthread_mutex_t suspend_ack_lock = PTHREAD_MUTEX_INITIALIZER;
240 static pthread_cond_t suspend_cond = PTHREAD_COND_INITIALIZER;
244 * where - 1 from within GC
247 void lock_stopworld(int where)
249 pthread_mutex_lock(&stopworldlock);
250 stopworldwhere = where;
253 void unlock_stopworld()
256 pthread_mutex_unlock(&stopworldlock);
259 #if !defined(__DARWIN__)
260 /* Caller must hold threadlistlock */
261 static int cast_sendsignals(int sig, int count)
264 threadobject *tobj = mainthreadobj;
265 nativethread *infoself = THREADINFO;
270 tobj = tobj->info.next;
271 } while (tobj != mainthreadobj);
274 nativethread *info = &tobj->info;
275 if (info != infoself)
276 pthread_kill(info->tid, sig);
277 tobj = tobj->info.next;
278 } while (tobj != mainthreadobj);
285 static void cast_darwinstop()
287 threadobject *tobj = mainthreadobj;
288 nativethread *infoself = THREADINFO;
291 nativethread *info = &tobj->info;
292 if (info != infoself)
294 thread_state_flavor_t flavor = PPC_THREAD_STATE;
295 mach_msg_type_number_t thread_state_count = PPC_THREAD_STATE_COUNT;
296 ppc_thread_state_t thread_state;
297 mach_port_t thread = info->mach_thread;
300 r = thread_suspend(thread);
301 if (r != KERN_SUCCESS)
302 panic("thread_suspend failed");
304 r = thread_get_state(thread, flavor,
305 (natural_t*)&thread_state, &thread_state_count);
306 if (r != KERN_SUCCESS)
307 panic("thread_get_state failed");
309 thread_restartcriticalsection(&thread_state);
311 r = thread_set_state(thread, flavor,
312 (natural_t*)&thread_state, thread_state_count);
313 if (r != KERN_SUCCESS)
314 panic("thread_set_state failed");
316 tobj = tobj->info.next;
317 } while (tobj != mainthreadobj);
320 static void cast_darwinresume()
322 threadobject *tobj = mainthreadobj;
323 nativethread *infoself = THREADINFO;
326 nativethread *info = &tobj->info;
327 if (info != infoself)
329 mach_port_t thread = info->mach_thread;
332 r = thread_resume(thread);
333 if (r != KERN_SUCCESS)
334 panic("thread_resume failed");
336 tobj = tobj->info.next;
337 } while (tobj != mainthreadobj);
342 #if defined(__MIPS__)
343 static void cast_irixresume()
345 pthread_mutex_lock(&suspend_ack_lock);
346 pthread_cond_broadcast(&suspend_cond);
347 pthread_mutex_unlock(&suspend_ack_lock);
351 void cast_stopworld()
355 pthread_mutex_lock(&threadlistlock);
356 #if defined(__DARWIN__)
359 count = cast_sendsignals(GC_signum1(), 0);
360 for (i=0; i<count; i++)
361 sem_wait(&suspend_ack);
363 pthread_mutex_unlock(&threadlistlock);
366 void cast_startworld()
368 pthread_mutex_lock(&threadlistlock);
369 #if defined(__DARWIN__)
371 #elif defined(__MIPS__)
374 cast_sendsignals(GC_signum2(), -1);
376 pthread_mutex_unlock(&threadlistlock);
380 #if !defined(__DARWIN__)
381 static void sigsuspend_handler(ucontext_t *ctx)
386 thread_restartcriticalsection(ctx);
388 /* Do as Boehm does. On IRIX a condition variable is used for wake-up
389 (not POSIX async-safe). */
390 #if defined(__IRIX__)
391 pthread_mutex_lock(&suspend_ack_lock);
392 sem_post(&suspend_ack);
393 pthread_cond_wait(&suspend_cond, &suspend_ack_lock);
394 pthread_mutex_unlock(&suspend_ack_lock);
396 sem_post(&suspend_ack);
400 sigdelset(&sigs, sig);
405 int cacao_suspendhandler(ucontext_t *ctx)
407 if (stopworldwhere != 2)
410 sigsuspend_handler(ctx);
415 static void setthreadobject(threadobject *thread)
417 #if !defined(HAVE___THREAD)
418 pthread_setspecific(tkey_threadinfo, thread);
424 static monitorLockRecord *dummyLR;
426 static void initPools();
429 * Initialize threads.
435 pthread_mutexattr_t mutexattr;
436 pthread_mutexattr_init(&mutexattr);
437 pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE);
438 pthread_mutex_init(&compiler_mutex, &mutexattr);
439 pthread_mutex_init(&tablelock, &mutexattr);
440 pthread_mutexattr_destroy(&mutexattr);
442 pthread_mutex_init_rec(&compiler_mutex);
443 pthread_mutex_init_rec(&tablelock);
446 pthread_mutex_init(&threadlistlock, NULL);
447 pthread_mutex_init(&stopworldlock, NULL);
449 /* Allocate something so the garbage collector's signal handlers are */
451 heap_allocate(1, false, NULL);
453 mainthreadobj = NEW(threadobject);
454 mainthreadobj->info.tid = pthread_self();
455 #if !defined(HAVE___THREAD)
456 pthread_key_create(&tkey_threadinfo, NULL);
458 setthreadobject(mainthreadobj);
461 criticaltree = avl_create(criticalcompare, NULL, NULL);
462 thread_addstaticcritical();
463 sem_init(&suspend_ack, 0, 0);
465 /* Every newly created object's monitorPtr points here so we save a check
467 dummyLR = NEW(monitorLockRecord);
469 dummyLR->ownerThread = NULL;
470 dummyLR->waiting = false;
473 static pthread_attr_t threadattr;
475 static void freeLockRecordPools(lockRecordPool *);
478 initThreads(u1 *stackbottom)
480 classinfo *threadclass;
481 classinfo *threadgroupclass;
482 java_lang_String *threadname;
483 java_lang_Thread *mainthread;
484 java_lang_ThreadGroup *threadgroup;
485 threadobject *tempthread = mainthreadobj;
488 threadclass = class_new(utf_new_char("java/lang/VMThread"));
489 class_load(threadclass);
490 class_link(threadclass);
493 throw_exception_exit();
495 freeLockRecordPools(mainthreadobj->ee.lrpool);
496 /* This is kinda tricky, we grow the java.lang.Thread object so we can keep
497 * the execution environment there. No Thread object must have been created
498 * at an earlier time */
499 threadclass->instancesize = sizeof(threadobject);
501 /* Create a VMThread */
502 mainthreadobj = (threadobject *) builtin_new(threadclass);
505 throw_exception_exit();
507 FREE(tempthread, threadobject);
508 initThread(&mainthreadobj->o);
510 setthreadobject(mainthreadobj);
513 mainthreadobj->info.next = mainthreadobj;
514 mainthreadobj->info.prev = mainthreadobj;
516 threadname = javastring_new(utf_new_char("main"));
518 /* Allocate and init ThreadGroup */
519 threadgroupclass = class_new(utf_new_char("java/lang/ThreadGroup"));
521 (java_lang_ThreadGroup *) native_new_and_init(threadgroupclass);
524 throw_exception_exit();
526 /* Create a Thread */
527 threadclass = class_new(utf_new_char("java/lang/Thread"));
528 mainthread = (java_lang_Thread*) builtin_new(threadclass);
529 mainthreadobj->o.thread = mainthread;
532 throw_exception_exit();
534 /* Call Thread constructor */
535 method = class_resolveclassmethod(threadclass,
536 utf_new_char("<init>"),
537 utf_new_char("(Ljava/lang/VMThread;Ljava/lang/String;IZ)V"),
542 throw_exception_exit();
544 asm_calljavafunction(method, mainthread, mainthreadobj, threadname, (void*) 5);
546 throw_exception_exit();
548 mainthread->group = threadgroup;
549 /* XXX This is a hack because the fourth argument was omitted */
550 mainthread->daemon = false;
552 /* Add mainthread to ThreadGroup */
553 method = class_resolveclassmethod(threadgroupclass,
554 utf_new_char("addThread"),
555 utf_new_char("(Ljava/lang/Thread;)V"),
560 throw_exception_exit();
562 asm_calljavafunction(method, threadgroup, mainthread, NULL, NULL);
564 throw_exception_exit();
566 setPriority(pthread_self(), 5);
568 pthread_attr_init(&threadattr);
569 pthread_attr_setdetachstate(&threadattr, PTHREAD_CREATE_DETACHED);
572 void initThread(java_lang_VMThread *t)
574 threadobject *thread = (threadobject*) t;
575 nativethread *info = &thread->info;
576 info->tid = pthread_self();
577 /* TODO destroy all those things */
578 pthread_mutex_init(&info->joinMutex, NULL);
579 pthread_cond_init(&info->joinCond, NULL);
581 pthread_mutex_init(&thread->waitLock, NULL);
582 pthread_cond_init(&thread->waitCond, NULL);
583 thread->interrupted = false;
584 thread->signaled = false;
585 thread->isSleeping = false;
588 static void initThreadLocks(threadobject *);
591 threadobject *thread;
595 static void *threadstartup(void *t)
597 startupinfo *startup = t;
598 threadobject *thread = startup->thread;
599 sem_t *psem = startup->psem;
600 nativethread *info = &thread->info;
605 #if defined(__DARWIN__)
606 info->mach_thread = mach_thread_self();
608 setthreadobject(thread);
610 pthread_mutex_lock(&threadlistlock);
611 info->prev = mainthreadobj;
612 info->next = tnext = mainthreadobj->info.next;
613 mainthreadobj->info.next = thread;
614 tnext->info.prev = thread;
615 pthread_mutex_unlock(&threadlistlock);
617 initThreadLocks(thread);
622 setPriority(info->tid, thread->o.thread->priority);
625 /* Find the run()V method and call it */
626 method = class_resolveclassmethod(thread->o.header.vftbl->class,
629 thread->o.header.vftbl->class,
632 /* if method != NULL, we had not exception */
634 asm_calljavafunction(method, thread, NULL, NULL, NULL);
640 /* Allow lock record pools to be used by other threads. They cannot be
641 * deleted so we'd better not waste them. */
642 freeLockRecordPools(thread->ee.lrpool);
644 pthread_mutex_lock(&threadlistlock);
645 info->next->info.prev = info->prev;
646 info->prev->info.next = info->next;
647 pthread_mutex_unlock(&threadlistlock);
649 pthread_mutex_lock(&info->joinMutex);
651 pthread_mutex_unlock(&info->joinMutex);
652 pthread_cond_broadcast(&info->joinCond);
657 void startThread(thread *t)
659 nativethread *info = &((threadobject*) t->vmThread)->info;
663 startup.thread = (threadobject*) t->vmThread;
666 sem_init(&sem, 0, 0);
668 if (pthread_create(&info->tid, &threadattr, threadstartup, &startup))
669 panic("pthread_create failed");
671 /* Wait here until the thread has entered itself into the thread list */
676 /* At the end of the program, we wait for all running non-daemon threads to die
679 static threadobject *findNonDaemon(threadobject *thread)
681 while (thread != mainthreadobj) {
682 if (!thread->o.thread->daemon)
684 thread = thread->info.prev;
690 void joinAllThreads()
692 threadobject *thread;
693 pthread_mutex_lock(&threadlistlock);
694 while ((thread = findNonDaemon(mainthreadobj->info.prev)) != NULL) {
695 nativethread *info = &thread->info;
696 pthread_mutex_lock(&info->joinMutex);
697 pthread_mutex_unlock(&threadlistlock);
699 pthread_cond_wait(&info->joinCond, &info->joinMutex);
700 pthread_mutex_unlock(&info->joinMutex);
701 pthread_mutex_lock(&threadlistlock);
703 pthread_mutex_unlock(&threadlistlock);
706 static void initLockRecord(monitorLockRecord *r, threadobject *t)
713 r->incharge = (monitorLockRecord *) &dummyLR;
715 sem_init(&r->queueSem, 0, 0);
716 pthread_mutex_init(&r->resolveLock, NULL);
717 pthread_cond_init(&r->resolveWait, NULL);
720 /* No lock record must ever be destroyed because there may still be references
723 static void destroyLockRecord(monitorLockRecord *r)
725 sem_destroy(&r->queueSem);
726 pthread_mutex_destroy(&r->resolveLock);
727 pthread_cond_destroy(&r->resolveWait);
733 initThreadLocks(mainthreadobj);
736 static void initThreadLocks(threadobject *thread)
738 thread->ee.firstLR = NULL;
739 thread->ee.lrpool = NULL;
740 thread->ee.numlr = 0;
743 static lockRecordPool *allocNewLockRecordPool(threadobject *thread, int size)
745 lockRecordPool *p = mem_alloc(sizeof(lockRecordPoolHeader) + sizeof(monitorLockRecord) * size);
748 p->header.size = size;
749 for (i=0; i<size; i++) {
750 initLockRecord(&p->lr[i], thread);
751 p->lr[i].nextFree = &p->lr[i+1];
753 p->lr[i-1].nextFree = NULL;
757 #define INITIALLOCKRECORDS 8
759 static pthread_mutex_t pool_lock;
760 static lockRecordPool *global_pool;
762 static void initPools()
764 pthread_mutex_init(&pool_lock, NULL);
767 static lockRecordPool *allocLockRecordPool(threadobject *t, int size)
769 pthread_mutex_lock(&pool_lock);
772 lockRecordPool *pool = global_pool;
773 global_pool = pool->header.next;
774 pthread_mutex_unlock(&pool_lock);
776 for (i=0; i < pool->header.size; i++)
777 pool->lr[i].ownerThread = t;
781 pthread_mutex_unlock(&pool_lock);
783 return allocNewLockRecordPool(t, size);
786 static void freeLockRecordPools(lockRecordPool *pool)
788 lockRecordPoolHeader *last;
789 pthread_mutex_lock(&pool_lock);
790 last = &pool->header;
792 last = &last->next->header;
793 last->next = global_pool;
795 pthread_mutex_unlock(&pool_lock);
798 static monitorLockRecord *allocLockRecordSimple(threadobject *t)
800 monitorLockRecord *r = t->ee.firstLR;
803 int poolsize = t->ee.numlr ? t->ee.numlr * 2 : INITIALLOCKRECORDS;
804 lockRecordPool *pool = allocLockRecordPool(t, poolsize);
805 pool->header.next = t->ee.lrpool;
808 t->ee.numlr += pool->header.size;
811 t->ee.firstLR = r->nextFree;
815 static inline void recycleLockRecord(threadobject *t, monitorLockRecord *r)
817 r->nextFree = t->ee.firstLR;
821 void initObjectLock(java_objectheader *o)
823 o->monitorPtr = dummyLR;
826 static void queueOnLockRecord(monitorLockRecord *lr, java_objectheader *o)
828 atomic_add(&lr->queuers, 1);
829 MEMORY_BARRIER_AFTER_ATOMIC();
831 sem_wait(&lr->queueSem);
832 atomic_add(&lr->queuers, -1);
835 static void freeLockRecord(monitorLockRecord *lr)
842 sem_post(&lr->queueSem);
845 static inline void handleWaiter(monitorLockRecord *mlr, monitorLockRecord *lr)
851 monitorLockRecord *monitorEnter(threadobject *t, java_objectheader *o)
854 monitorLockRecord *lr = o->monitorPtr;
856 monitorLockRecord *nlr, *mlr = allocLockRecordSimple(t);
862 handleWaiter(mlr, lr);
866 if (lr->ownerThread != t)
868 MEMORY_BARRIER_BEFORE_ATOMIC();
869 nlr = (void*) compare_and_swap((long*) &o->monitorPtr, (long) lr, (long) mlr);
872 if (mlr == lr || lr->o != o) {
873 handleWaiter(mlr, lr);
877 queueOnLockRecord(lr, o);
878 handleWaiter(mlr, lr);
882 recycleLockRecord(t, mlr);
883 queueOnLockRecord(nlr, o);
885 if (lr->ownerThread == t) {
889 queueOnLockRecord(lr, o);
894 static void wakeWaiters(monitorLockRecord *lr)
899 sem_post(&lr->queueSem);
904 #define GRAB_LR(lr,t) \
905 if (lr->ownerThread != t) { \
909 #define CHECK_MONITORSTATE(lr,t,mo,a) \
910 if (lr->o != mo || lr->ownerThread != t) { \
911 *exceptionptr = new_exception(string_java_lang_IllegalMonitorStateException); \
915 bool monitorExit(threadobject *t, java_objectheader *o)
917 monitorLockRecord *lr = o->monitorPtr;
919 CHECK_MONITORSTATE(lr, t, o, return false);
920 if (lr->lockCount > 1) {
925 monitorLockRecord *wlr = lr->waiter;
926 if (o->monitorPtr != lr ||
927 (void*) compare_and_swap((long*) &o->monitorPtr, (long) lr, (long) wlr) != lr)
929 monitorLockRecord *nlr = o->monitorPtr;
931 STORE_ORDER_BARRIER();
937 recycleLockRecord(t, lr);
941 static void removeFromWaiters(monitorLockRecord *lr, monitorLockRecord *wlr)
944 if (lr->waiter == wlr) {
945 lr->waiter = wlr->waiter;
952 static inline bool timespec_less(const struct timespec *tv1, const struct timespec *tv2)
954 return tv1->tv_sec < tv2->tv_sec || (tv1->tv_sec == tv2->tv_sec && tv1->tv_nsec < tv2->tv_nsec);
957 static bool timeIsEarlier(const struct timespec *tv)
959 struct timeval tvnow;
960 struct timespec tsnow;
961 gettimeofday(&tvnow, NULL);
962 tsnow.tv_sec = tvnow.tv_sec;
963 tsnow.tv_nsec = tvnow.tv_usec * 1000;
964 return timespec_less(&tsnow, tv);
967 bool waitWithTimeout(threadobject *t, monitorLockRecord *lr, struct timespec *wakeupTime)
971 pthread_mutex_lock(&t->waitLock);
972 t->isSleeping = true;
973 if (wakeupTime->tv_sec || wakeupTime->tv_nsec)
974 while (!t->interrupted && !t->signaled && timeIsEarlier(wakeupTime))
975 pthread_cond_timedwait(&t->waitCond, &t->waitLock, wakeupTime);
977 while (!t->interrupted && !t->signaled)
978 pthread_cond_wait(&t->waitCond, &t->waitLock);
979 wasinterrupted = t->interrupted;
980 t->interrupted = false;
982 t->isSleeping = false;
983 pthread_mutex_unlock(&t->waitLock);
984 return wasinterrupted;
987 static void calcAbsoluteTime(struct timespec *tm, s8 millis, s4 nanos)
989 if (millis || nanos) {
992 gettimeofday(&tv, NULL);
993 tv.tv_sec += millis / 1000;
995 nsec = tv.tv_usec * 1000 + (s4) millis * 1000000 + nanos;
996 tm->tv_sec = tv.tv_sec + nsec / 1000000000;
997 tm->tv_nsec = nsec % 1000000000;
1004 void monitorWait(threadobject *t, java_objectheader *o, s8 millis, s4 nanos)
1006 bool wasinterrupted;
1007 struct timespec wakeupTime;
1008 monitorLockRecord *mlr, *lr = o->monitorPtr;
1010 CHECK_MONITORSTATE(lr, t, o, return);
1012 calcAbsoluteTime(&wakeupTime, millis, nanos);
1015 wakeWaiters(lr->waiter);
1017 STORE_ORDER_BARRIER();
1019 wasinterrupted = waitWithTimeout(t, lr, &wakeupTime);
1020 mlr = monitorEnter(t, o);
1021 removeFromWaiters(mlr, lr);
1022 mlr->lockCount = lr->lockCount;
1024 lr->waiting = false;
1026 recycleLockRecord(t, lr);
1029 *exceptionptr = new_exception(string_java_lang_InterruptedException);
1032 static void notifyOneOrAll(threadobject *t, java_objectheader *o, bool one)
1034 monitorLockRecord *lr = o->monitorPtr;
1036 CHECK_MONITORSTATE(lr, t, o, return);
1038 threadobject *wthread;
1039 monitorLockRecord *wlr = lr->waiter;
1042 wthread = wlr->ownerThread;
1043 pthread_mutex_lock(&wthread->waitLock);
1044 if (wthread->isSleeping)
1045 pthread_cond_signal(&wthread->waitCond);
1046 wthread->signaled = true;
1047 pthread_mutex_unlock(&wthread->waitLock);
1052 bool threadHoldsLock(threadobject *t, java_objectheader *o)
1054 monitorLockRecord *lr = o->monitorPtr;
1056 return lr->o == o && lr->ownerThread == t;
1059 void interruptThread(java_lang_VMThread *thread)
1061 threadobject *t = (threadobject*) thread;
1063 t->interrupted = true;
1064 pthread_mutex_lock(&t->waitLock);
1066 pthread_cond_signal(&t->waitCond);
1067 pthread_mutex_unlock(&t->waitLock);
1070 bool interruptedThread()
1072 threadobject *t = (threadobject*) THREADOBJECT;
1073 bool intr = t->interrupted;
1074 t->interrupted = false;
1078 bool isInterruptedThread(java_lang_VMThread *thread)
1080 threadobject *t = (threadobject*) thread;
1081 return t->interrupted;
1084 void sleepThread(s8 millis, s4 nanos)
1086 bool wasinterrupted;
1087 threadobject *t = (threadobject*) THREADOBJECT;
1088 monitorLockRecord *lr;
1089 struct timespec wakeupTime;
1090 calcAbsoluteTime(&wakeupTime, millis, nanos);
1092 lr = allocLockRecordSimple(t);
1093 wasinterrupted = waitWithTimeout(t, lr, &wakeupTime);
1094 recycleLockRecord(t, lr);
1097 *exceptionptr = new_exception(string_java_lang_InterruptedException);
1105 void setPriorityThread(thread *t, s4 priority)
1107 nativethread *info = &((threadobject*) t->vmThread)->info;
1108 setPriority(info->tid, priority);
1111 void wait_cond_for_object(java_objectheader *o, s8 time, s4 nanos)
1113 threadobject *t = (threadobject*) THREADOBJECT;
1114 monitorWait(t, o, time, nanos);
1117 void signal_cond_for_object(java_objectheader *o)
1119 threadobject *t = (threadobject*) THREADOBJECT;
1120 notifyOneOrAll(t, o, true);
1123 void broadcast_cond_for_object(java_objectheader *o)
1125 threadobject *t = (threadobject*) THREADOBJECT;
1126 notifyOneOrAll(t, o, false);
1131 * These are local overrides for various environment variables in Emacs.
1132 * Please do not remove this and leave it at the end of the file, where
1133 * Emacs will automagically detect them.
1134 * ---------------------------------------------------------------------
1137 * indent-tabs-mode: t