* src/threads/posix/thread-posix.c (threads_sleep): Yield the thread
authorChristian Thalinger <twisti@complang.tuwien.ac.at>
Sat, 26 Jul 2008 16:48:35 +0000 (18:48 +0200)
committerChristian Thalinger <twisti@complang.tuwien.ac.at>
Sat, 26 Jul 2008 16:48:35 +0000 (18:48 +0200)
when sleep time is zero.

--HG--
extra : transplant_source : %9A%E9cC%0B%D6%5B%8C%BA1p%B1%AFI%F4%FA%83%93%19%04

src/threads/posix/thread-posix.cpp

index 6f61657031d8d2fe21495b19bee358483adeb0c9..8b789d6d34818af78ca6e0a59a55abc907e5fdf1 100644 (file)
@@ -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();
+               }
        }
 }