From 070557afc4ca83876b951fe090806b59e3867ae5 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Thu, 6 Feb 2020 09:14:40 +0900 Subject: Distinguish signal and timeout [Bug #16608] --- thread_sync.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'thread_sync.c') diff --git a/thread_sync.c b/thread_sync.c index 894235e9e1..44290136b3 100644 --- a/thread_sync.c +++ b/thread_sync.c @@ -533,14 +533,15 @@ rb_mutex_wait_for(VALUE time) { rb_hrtime_t *rel = (rb_hrtime_t *)time; /* permit spurious check */ - sleep_hrtime(GET_THREAD(), *rel, 0); - return Qnil; + if (sleep_hrtime(GET_THREAD(), *rel, 0)) return Qtrue; + return Qfalse; } VALUE rb_mutex_sleep(VALUE self, VALUE timeout) { struct timeval t; + VALUE woken = Qtrue; if (!NIL_P(timeout)) { t = rb_time_interval(timeout); @@ -560,18 +561,19 @@ rb_mutex_sleep(VALUE self, VALUE timeout) } else { rb_hrtime_t rel = rb_timeval2hrtime(&t); - rb_ensure(rb_mutex_wait_for, (VALUE)&rel, mutex_lock_uninterruptible, self); + woken = rb_ensure(rb_mutex_wait_for, (VALUE)&rel, mutex_lock_uninterruptible, self); } } RUBY_VM_CHECK_INTS_BLOCKING(GET_EC()); + if (!woken) return Qnil; time_t end = time(0) - beg; return TIMET2NUM(end); } /* * call-seq: - * mutex.sleep(timeout = nil) -> number + * mutex.sleep(timeout = nil) -> number or nil * * Releases the lock and sleeps +timeout+ seconds if it is given and * non-nil or forever. Raises +ThreadError+ if +mutex+ wasn't locked by @@ -582,6 +584,8 @@ rb_mutex_sleep(VALUE self, VALUE timeout) * * Note that this method can wakeup without explicit Thread#wakeup call. * For example, receiving signal and so on. + * + * Returns the slept time in seconds if woken up, or +nil+ if timed out. */ static VALUE mutex_sleep(int argc, VALUE *argv, VALUE self) @@ -1483,6 +1487,8 @@ do_sleep(VALUE args) * * If +timeout+ is given, this method returns after +timeout+ seconds passed, * even if no other thread doesn't signal. + * + * Returns the slept result on +mutex+. */ static VALUE @@ -1502,9 +1508,7 @@ rb_condvar_wait(int argc, VALUE *argv, VALUE self) }; list_add_tail(&cv->waitq, &sync_waiter.node); - rb_ensure(do_sleep, (VALUE)&args, delete_from_waitq, (VALUE)&sync_waiter); - - return self; + return rb_ensure(do_sleep, (VALUE)&args, delete_from_waitq, (VALUE)&sync_waiter); } /* -- cgit v1.2.3