}
-/* 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;
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();
+ }
}
}