Skip to content

Commit 446257c

Browse files
luke-gruberjhawthorn
authored andcommitted
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.
1 parent 7089a4e commit 446257c

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

thread_pthread.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,16 @@ static const void *const condattr_monotonic = NULL;
9090
#endif
9191
#endif
9292

93+
#ifdef HAVE_SCHED_YIELD
94+
#define native_thread_yield() (void)sched_yield()
95+
#else
96+
#define native_thread_yield() ((void)0)
97+
#endif
98+
9399
// native thread wrappers
94100

95101
#define NATIVE_MUTEX_LOCK_DEBUG 0
102+
#define NATIVE_MUTEX_LOCK_DEBUG_YIELD 0
96103

97104
static void
98105
mutex_debug(const char *msg, void *lock)
@@ -111,6 +118,9 @@ void
111118
rb_native_mutex_lock(pthread_mutex_t *lock)
112119
{
113120
int r;
121+
#if NATIVE_MUTEX_LOCK_DEBUG_YIELD
122+
native_thread_yield();
123+
#endif
114124
mutex_debug("lock", lock);
115125
if ((r = pthread_mutex_lock(lock)) != 0) {
116126
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 */
310320

311321
static void threadptr_trap_interrupt(rb_thread_t *);
312322

313-
#ifdef HAVE_SCHED_YIELD
314-
#define native_thread_yield() (void)sched_yield()
315-
#else
316-
#define native_thread_yield() ((void)0)
317-
#endif
318-
319323
static void native_thread_dedicated_inc(rb_vm_t *vm, rb_ractor_t *cr, struct rb_native_thread *nt);
320324
static void native_thread_dedicated_dec(rb_vm_t *vm, rb_ractor_t *cr, struct rb_native_thread *nt);
321325
static void native_thread_assign(struct rb_native_thread *nt, rb_thread_t *th);

0 commit comments

Comments
 (0)