From: Christian Thalinger Date: Sat, 26 Jul 2008 16:48:35 +0000 (+0200) Subject: * src/threads/posix/thread-posix.c (threads_sleep): Yield the thread X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=commitdiff_plain;h=142b53c6e64878f8db3ef0981eaeb39e547fe6e7;p=cacao.git * src/threads/posix/thread-posix.c (threads_sleep): Yield the thread when sleep time is zero. --HG-- extra : transplant_source : %9A%E9cC%0B%D6%5B%8C%BA1p%B1%AFI%F4%FA%83%93%19%04 --- diff --git a/src/threads/posix/thread-posix.cpp b/src/threads/posix/thread-posix.cpp index 6f6165703..8b789d6d3 100644 --- a/src/threads/posix/thread-posix.cpp +++ b/src/threads/posix/thread-posix.cpp @@ -1566,12 +1566,12 @@ void threads_thread_interrupt(threadobject *t) } -/* threads_sleep *************************************************************** - - Sleep the current thread for the specified amount of time. - -*******************************************************************************/ - +/** + * Sleep the current thread for the specified amount of time. + * + * @param millis Milliseconds to sleep. + * @param nanos Nanoseconds to sleep. + */ void threads_sleep(int64_t millis, int32_t nanos) { threadobject *t; @@ -1597,20 +1597,29 @@ void threads_sleep(int64_t millis, int32_t nanos) return; } - threads_calc_absolute_time(&wakeupTime, millis, nanos); - - threads_wait_with_timeout(t, &wakeupTime); + // (Note taken from classpath/vm/reference/java/lang/VMThread.java (sleep)) + // Note: JDK treats a zero length sleep is like Thread.yield(), + // without checking the interrupted status of the thread. It's + // unclear if this is a bug in the implementation or the spec. + // See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6213203 */ + if (millis == 0 && nanos == 0) { + threads_yield(); + } + else { + threads_calc_absolute_time(&wakeupTime, millis, nanos); - interrupted = thread_is_interrupted(t); + threads_wait_with_timeout(t, &wakeupTime); - if (interrupted) { - thread_set_interrupted(t, false); + interrupted = thread_is_interrupted(t); - /* An other exception could have been thrown - (e.g. ThreadDeathException). */ + if (interrupted) { + thread_set_interrupted(t, false); - if (!exceptions_get_exception()) - exceptions_throw_interruptedexception(); + // An other exception could have been thrown + // (e.g. ThreadDeathException). + if (!exceptions_get_exception()) + exceptions_throw_interruptedexception(); + } } }