diff --git a/thread.c b/thread.c index 6065593712..74a556ba38 100644 --- a/thread.c +++ b/thread.c @@ -4710,7 +4710,7 @@ rb_thread_shield_wait(VALUE self) rb_mutex_t *m; if (!mutex) return Qfalse; - GetMutexPtr(mutex, m); + m = mutex_ptr(mutex); if (m->th == GET_THREAD()) return Qnil; rb_thread_shield_waiting_inc(self); rb_mutex_lock(mutex); @@ -5197,8 +5197,7 @@ debug_deadlock_check(rb_vm_t *vm, VALUE msg) "native:%"PRI_THREAD_ID" int:%u", th->self, (void *)th, thread_id_str(th), th->ec->interrupt_flag); if (th->locking_mutex) { - rb_mutex_t *mutex; - GetMutexPtr(th->locking_mutex, mutex); + rb_mutex_t *mutex = mutex_ptr(th->locking_mutex); rb_str_catf(msg, " mutex:%p cond:%"PRIuSIZE, (void *)mutex->th, rb_mutex_num_waiting(mutex)); } @@ -5230,8 +5229,7 @@ rb_check_deadlock(rb_vm_t *vm) found = 1; } else if (th->locking_mutex) { - rb_mutex_t *mutex; - GetMutexPtr(th->locking_mutex, mutex); + rb_mutex_t *mutex = mutex_ptr(th->locking_mutex); if (mutex->th == th || (!mutex->th && !list_empty(&mutex->waitq))) { found = 1; diff --git a/thread_sync.c b/thread_sync.c index 5adb97f6b2..76632ebe5f 100644 --- a/thread_sync.c +++ b/thread_sync.c @@ -81,9 +81,6 @@ static const char* rb_mutex_unlock_th(rb_mutex_t *mutex, rb_thread_t *th); * */ -#define GetMutexPtr(obj, tobj) \ - TypedData_Get_Struct((obj), rb_mutex_t, &mutex_data_type, (tobj)) - #define mutex_mark NULL static size_t @@ -123,6 +120,15 @@ static const rb_data_type_t mutex_data_type = { 0, 0, RUBY_TYPED_FREE_IMMEDIATELY }; +static rb_mutex_t * +mutex_ptr(VALUE obj) +{ + rb_mutex_t *mutex; + + TypedData_Get_Struct(obj, rb_mutex_t, &mutex_data_type, mutex); + return mutex; +} + VALUE rb_obj_is_mutex(VALUE obj) { @@ -172,16 +178,15 @@ rb_mutex_new(void) VALUE rb_mutex_locked_p(VALUE self) { - rb_mutex_t *mutex; - GetMutexPtr(self, mutex); + rb_mutex_t *mutex = mutex_ptr(self); + return mutex->th ? Qtrue : Qfalse; } static void mutex_locked(rb_thread_t *th, VALUE self) { - rb_mutex_t *mutex; - GetMutexPtr(self, mutex); + rb_mutex_t *mutex = mutex_ptr(self); if (th->keeping_mutexes) { mutex->next_mutex = th->keeping_mutexes; @@ -199,9 +204,8 @@ mutex_locked(rb_thread_t *th, VALUE self) VALUE rb_mutex_trylock(VALUE self) { - rb_mutex_t *mutex; + rb_mutex_t *mutex = mutex_ptr(self); VALUE locked = Qfalse; - GetMutexPtr(self, mutex); if (mutex->th == 0) { rb_thread_t *th = GET_THREAD(); @@ -225,8 +229,7 @@ static VALUE do_mutex_lock(VALUE self, int interruptible_p) { rb_thread_t *th = GET_THREAD(); - rb_mutex_t *mutex; - GetMutexPtr(self, mutex); + rb_mutex_t *mutex = mutex_ptr(self); /* When running trap handler */ if (!FL_TEST_RAW(self, MUTEX_ALLOW_TRAP) && @@ -325,9 +328,7 @@ rb_mutex_owned_p(VALUE self) { VALUE owned = Qfalse; rb_thread_t *th = GET_THREAD(); - rb_mutex_t *mutex; - - GetMutexPtr(self, mutex); + rb_mutex_t *mutex = mutex_ptr(self); if (mutex->th == th) owned = Qtrue; @@ -388,8 +389,7 @@ VALUE rb_mutex_unlock(VALUE self) { const char *err; - rb_mutex_t *mutex; - GetMutexPtr(self, mutex); + rb_mutex_t *mutex = mutex_ptr(self); err = rb_mutex_unlock_th(mutex, GET_THREAD()); if (err) rb_raise(rb_eThreadError, "%s", err); @@ -410,14 +410,13 @@ rb_mutex_abandon_keeping_mutexes(rb_thread_t *th) static void rb_mutex_abandon_locking_mutex(rb_thread_t *th) { - rb_mutex_t *mutex; + if (th->locking_mutex) { + rb_mutex_t *mutex = mutex_ptr(th->locking_mutex); - if (!th->locking_mutex) return; - - GetMutexPtr(th->locking_mutex, mutex); - if (mutex->th == th) - rb_mutex_abandon_all(mutex); - th->locking_mutex = Qfalse; + if (mutex->th == th) + rb_mutex_abandon_all(mutex); + th->locking_mutex = Qfalse; + } } static void