Extract vm_locked_by_ractor_p

This introduces a new method to encapsulate checking whether the current
Ractor owns the vm->ractor.sync lock. This allows us to disable TSan on
it since that operation should be safe, and still get validation of
other uses.
This commit is contained in:
John Hawthorn 2025-07-23 14:24:59 -07:00
parent 7465e169da
commit 3ad2019259
3 changed files with 11 additions and 9 deletions

View file

@ -30,13 +30,6 @@ race:check_reserved_signal_
race_top:rb_check_deadlock race_top:rb_check_deadlock
# lock_owner
race_top:thread_sched_setup_running_threads
race_top:vm_lock_enter
race_top:rb_ec_vm_lock_rec
race_top:vm_lock_enter
race_top:vm_locked
# vm->ractor.sched.grq_cnt++ # vm->ractor.sched.grq_cnt++
race_top:ractor_sched_enq race_top:ractor_sched_enq
race_top:ractor_sched_deq race_top:ractor_sched_deq

View file

@ -2065,12 +2065,21 @@ void rb_ec_vm_lock_rec_release(const rb_execution_context_t *ec,
unsigned int recorded_lock_rec, unsigned int recorded_lock_rec,
unsigned int current_lock_rec); unsigned int current_lock_rec);
/* This technically is a data race, as it's checked without the lock, however we
* check against a value only our own thread will write. */
NO_SANITIZE("thread", static inline bool
vm_locked_by_ractor_p(rb_vm_t *vm, rb_ractor_t *cr))
{
VM_ASSERT(cr == GET_RACTOR());
return vm->ractor.sync.lock_owner == cr;
}
static inline unsigned int static inline unsigned int
rb_ec_vm_lock_rec(const rb_execution_context_t *ec) rb_ec_vm_lock_rec(const rb_execution_context_t *ec)
{ {
rb_vm_t *vm = rb_ec_vm_ptr(ec); rb_vm_t *vm = rb_ec_vm_ptr(ec);
if (vm->ractor.sync.lock_owner != rb_ec_ractor_ptr(ec)) { if (!vm_locked_by_ractor_p(vm, rb_ec_ractor_ptr(ec))) {
return 0; return 0;
} }
else { else {

View file

@ -12,7 +12,7 @@ void rb_ractor_sched_barrier_end(rb_vm_t *vm, rb_ractor_t *cr);
static bool static bool
vm_locked(rb_vm_t *vm) vm_locked(rb_vm_t *vm)
{ {
return vm->ractor.sync.lock_owner == GET_RACTOR(); return vm_locked_by_ractor_p(vm, GET_RACTOR());
} }
#if RUBY_DEBUG > 0 #if RUBY_DEBUG > 0