diff options
author | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2020-02-06 09:14:40 +0900 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2021-07-25 13:09:03 -0700 |
commit | 070557afc4ca83876b951fe090806b59e3867ae5 (patch) | |
tree | 9756aa96026efd552184011ad45c05f2027e19aa /thread_sync.c | |
parent | 8897098b5ca3ce987307d1799f7765e6a279ff0d (diff) | |
download | ruby-070557afc4ca83876b951fe090806b59e3867ae5.tar.gz |
Distinguish signal and timeout [Bug #16608]
Diffstat (limited to 'thread_sync.c')
-rw-r--r-- | thread_sync.c | 18 |
1 files changed, 11 insertions, 7 deletions
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); } /* |