thread_sync.c (rb_mutex_sleep): disable interrupt checking in ensure
This is needed to reliably fix ConditionVariable#wait on Thread#kill [Bug #14999] because there is still a chance an interrupt could fire and prevent lock acquisition after an ensure statement. Arguably, rb_mutex_lock itself should be uninterruptible, but that already prevents bootstraptest/test_thread.rb from completing and probably breaks existing use cases. For reference, POSIX expressly forbids EINTR from pthread_mutex_lock. [ruby-core:88556] [Bug #14999] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64476 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
6056dae7f4
commit
d9cb901779
@ -221,15 +221,8 @@ rb_mutex_trylock(VALUE self)
|
|||||||
*/
|
*/
|
||||||
static const rb_thread_t *patrol_thread = NULL;
|
static const rb_thread_t *patrol_thread = NULL;
|
||||||
|
|
||||||
/*
|
static VALUE
|
||||||
* call-seq:
|
do_mutex_lock(VALUE self, int interruptible_p)
|
||||||
* mutex.lock -> self
|
|
||||||
*
|
|
||||||
* Attempts to grab the lock and waits if it isn't available.
|
|
||||||
* Raises +ThreadError+ if +mutex+ was locked by the current thread.
|
|
||||||
*/
|
|
||||||
VALUE
|
|
||||||
rb_mutex_lock(VALUE self)
|
|
||||||
{
|
{
|
||||||
rb_thread_t *th = GET_THREAD();
|
rb_thread_t *th = GET_THREAD();
|
||||||
rb_mutex_t *mutex;
|
rb_mutex_t *mutex;
|
||||||
@ -290,16 +283,37 @@ rb_mutex_lock(VALUE self)
|
|||||||
th->vm->sleeper--;
|
th->vm->sleeper--;
|
||||||
if (mutex->th == th) mutex_locked(th, self);
|
if (mutex->th == th) mutex_locked(th, self);
|
||||||
|
|
||||||
RUBY_VM_CHECK_INTS_BLOCKING(th->ec); /* may release mutex */
|
if (interruptible_p) {
|
||||||
if (!mutex->th) {
|
RUBY_VM_CHECK_INTS_BLOCKING(th->ec); /* may release mutex */
|
||||||
mutex->th = th;
|
if (!mutex->th) {
|
||||||
mutex_locked(th, self);
|
mutex->th = th;
|
||||||
}
|
mutex_locked(th, self);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
mutex_lock_uninterruptible(VALUE self)
|
||||||
|
{
|
||||||
|
return do_mutex_lock(self, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* call-seq:
|
||||||
|
* mutex.lock -> self
|
||||||
|
*
|
||||||
|
* Attempts to grab the lock and waits if it isn't available.
|
||||||
|
* Raises +ThreadError+ if +mutex+ was locked by the current thread.
|
||||||
|
*/
|
||||||
|
VALUE
|
||||||
|
rb_mutex_lock(VALUE self)
|
||||||
|
{
|
||||||
|
return do_mutex_lock(self, 1);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* mutex.owned? -> true or false
|
* mutex.owned? -> true or false
|
||||||
@ -462,14 +476,15 @@ rb_mutex_sleep(VALUE self, VALUE timeout)
|
|||||||
rb_mutex_unlock(self);
|
rb_mutex_unlock(self);
|
||||||
beg = time(0);
|
beg = time(0);
|
||||||
if (NIL_P(timeout)) {
|
if (NIL_P(timeout)) {
|
||||||
rb_ensure(rb_mutex_sleep_forever, Qnil, rb_mutex_lock, self);
|
rb_ensure(rb_mutex_sleep_forever, Qnil, mutex_lock_uninterruptible, self);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
VALUE tsp = (VALUE)timespec_for(&ts, &t);
|
VALUE tsp = (VALUE)timespec_for(&ts, &t);
|
||||||
|
|
||||||
rb_ensure(rb_mutex_wait_for, tsp, rb_mutex_lock, self);
|
rb_ensure(rb_mutex_wait_for, tsp, mutex_lock_uninterruptible, self);
|
||||||
}
|
}
|
||||||
|
RUBY_VM_CHECK_INTS_BLOCKING(GET_EC());
|
||||||
end = time(0) - beg;
|
end = time(0) - beg;
|
||||||
return INT2FIX(end);
|
return INT2FIX(end);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user