Skip to content

Commit 7ca26e8

Browse files
committed
Add lock_file and lock_line to VM lock in debug mode
1 parent dc94d3c commit 7ca26e8

File tree

7 files changed

+25
-8
lines changed

7 files changed

+25
-8
lines changed

thread.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,7 @@ rb_thread_sleep(int sec)
14721472
static void
14731473
rb_thread_schedule_limits(uint32_t limits_us)
14741474
{
1475-
VM_ASSERT(GET_VM()->ractor.sync.lock_owner != GET_RACTOR());
1475+
ASSERT_vm_unlocking();
14761476
if (!rb_thread_alone()) {
14771477
rb_thread_t *th = GET_THREAD();
14781478
RUBY_DEBUG_LOG("us:%u", (unsigned int)limits_us);
@@ -2596,6 +2596,7 @@ rb_threadptr_execute_interrupts(rb_thread_t *th, int blocking_timing)
25962596
int ret = FALSE;
25972597

25982598
VM_ASSERT(GET_THREAD() == th);
2599+
ASSERT_vm_unlocking();
25992600

26002601
if (th->ec->raised_flag) return ret;
26012602

thread_pthread.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ thread_sched_to_waiting_common(struct rb_thread_sched *sched, rb_thread_t *th)
10251025
static void
10261026
thread_sched_to_waiting(struct rb_thread_sched *sched, rb_thread_t *th)
10271027
{
1028-
VM_ASSERT(GET_VM()->ractor.sync.lock_owner != GET_RACTOR()); // never context switch with VM lock held
1028+
ASSERT_vm_unlocking();
10291029
thread_sched_lock(sched, th);
10301030
{
10311031
thread_sched_to_waiting_common(sched, th);

vm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,7 +1675,7 @@ invoke_block_from_c_bh(rb_execution_context_t *ec, VALUE block_handler,
16751675
int kw_splat, VALUE passed_block_handler, const rb_cref_t *cref,
16761676
int is_lambda, int force_blockarg)
16771677
{
1678-
VM_ASSERT(GET_VM()->ractor.sync.lock_owner != GET_RACTOR());
1678+
ASSERT_vm_unlocking();
16791679
again:
16801680
switch (vm_block_handler_type(block_handler)) {
16811681
case block_handler_type_iseq:
@@ -1718,7 +1718,7 @@ check_block_handler(rb_execution_context_t *ec)
17181718
static VALUE
17191719
vm_yield_with_cref(rb_execution_context_t *ec, int argc, const VALUE *argv, int kw_splat, const rb_cref_t *cref, int is_lambda)
17201720
{
1721-
VM_ASSERT(GET_VM()->ractor.sync.lock_owner != GET_RACTOR());
1721+
ASSERT_vm_unlocking();
17221722
return invoke_block_from_c_bh(ec, check_block_handler(ec),
17231723
argc, argv, kw_splat, VM_BLOCK_HANDLER_NONE,
17241724
cref, is_lambda, FALSE);

vm_core.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,10 @@ typedef struct rb_vm_struct {
675675
rb_nativethread_lock_t lock;
676676
struct rb_ractor_struct *lock_owner;
677677
unsigned int lock_rec;
678+
#if VM_CHECK_MODE > 0
679+
const char *lock_file;
680+
int lock_line;
681+
#endif
678682

679683
// join at exit
680684
rb_nativethread_cond_t terminate_cond;
@@ -687,7 +691,7 @@ typedef struct rb_vm_struct {
687691
rb_nativethread_cond_t barrier_complete_cond;
688692
rb_nativethread_cond_t barrier_release_cond;
689693
#endif
690-
} sync;
694+
} sync; // VM lock
691695

692696
#ifdef RUBY_THREAD_PTHREAD_H
693697
// ractor scheduling

vm_exec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ rb_vm_get_insns_address_table(void)
129129
static VALUE
130130
vm_exec_core(rb_execution_context_t *ec)
131131
{
132-
VM_ASSERT(GET_VM()->ractor.sync.lock_owner != GET_RACTOR());
132+
ASSERT_vm_unlocking();
133133
register rb_control_frame_t *reg_cfp = ec->cfp;
134134
rb_thread_t *th;
135135

vm_sync.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@ void
2727
RUBY_ASSERT_vm_unlocking(void)
2828
{
2929
rb_vm_t *vm = GET_VM();
30-
VM_ASSERT(!vm_locked(vm));
30+
if (vm_locked(vm)) {
31+
const char *file = vm->ractor.sync.lock_file;
32+
int line = vm->ractor.sync.lock_line;
33+
if (file) {
34+
fprintf(stderr, "Last locked at %s:%d\n", file, line);
35+
}
36+
VM_ASSERT(!vm_locked(vm));
37+
}
3138
}
3239
#endif
3340

@@ -75,6 +82,11 @@ vm_lock_enter(rb_ractor_t *cr, rb_vm_t *vm, bool locked, bool no_barrier, unsign
7582
VM_ASSERT(vm->ractor.sync.lock_owner == NULL);
7683
VM_ASSERT(vm->ractor.sync.lock_rec == 0);
7784

85+
#if VM_CHECK_MODE > 0
86+
vm->ractor.sync.lock_file = file;
87+
vm->ractor.sync.lock_line = line;
88+
#endif
89+
7890
// barrier
7991
if (vm_need_barrier(no_barrier, cr, vm)) {
8092
rb_execution_context_t *ec = GET_EC();

vm_sync.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "vm_debug.h"
55
#include "debug_counter.h"
66

7-
#if USE_RUBY_DEBUG_LOG
7+
#if USE_RUBY_DEBUG_LOG || RUBY_DEBUG > 0 || VM_CHECK_MODE > 0
88
#define LOCATION_ARGS const char *file, int line
99
#define LOCATION_PARAMS file, line
1010
#define APPEND_LOCATION_ARGS , const char *file, int line

0 commit comments

Comments
 (0)