Add debug #define to call sched_yield before each pthread_mutex_lock

This is useful for debugging mutex issues as it increases contention for locks.
It is off by default.
This commit is contained in:
Luke Gruber 2025-10-07 10:28:37 -04:00 committed by John Hawthorn
parent 7089a4e2d8
commit 446257c84b
Notes: git 2025-10-07 20:02:04 +00:00

View File

@ -90,9 +90,16 @@ static const void *const condattr_monotonic = NULL;
#endif
#endif
#ifdef HAVE_SCHED_YIELD
#define native_thread_yield() (void)sched_yield()
#else
#define native_thread_yield() ((void)0)
#endif
// native thread wrappers
#define NATIVE_MUTEX_LOCK_DEBUG 0
#define NATIVE_MUTEX_LOCK_DEBUG_YIELD 0
static void
mutex_debug(const char *msg, void *lock)
@ -111,6 +118,9 @@ void
rb_native_mutex_lock(pthread_mutex_t *lock)
{
int r;
#if NATIVE_MUTEX_LOCK_DEBUG_YIELD
native_thread_yield();
#endif
mutex_debug("lock", lock);
if ((r = pthread_mutex_lock(lock)) != 0) {
rb_bug_errno("pthread_mutex_lock", r);
@ -310,12 +320,6 @@ static rb_serial_t current_fork_gen = 1; /* We can't use GET_VM()->fork_gen */
static void threadptr_trap_interrupt(rb_thread_t *);
#ifdef HAVE_SCHED_YIELD
#define native_thread_yield() (void)sched_yield()
#else
#define native_thread_yield() ((void)0)
#endif
static void native_thread_dedicated_inc(rb_vm_t *vm, rb_ractor_t *cr, struct rb_native_thread *nt);
static void native_thread_dedicated_dec(rb_vm_t *vm, rb_ractor_t *cr, struct rb_native_thread *nt);
static void native_thread_assign(struct rb_native_thread *nt, rb_thread_t *th);