From 48e0ee172a80a3ecd12fe086726c6b9e94e772cc Mon Sep 17 00:00:00 2001 From: mjg Date: Mon, 20 Feb 2017 19:08:36 +0000 Subject: [PATCH] mtx: fix spin mutexes interaction with failed fcmpset While doing so move recursion support down to the fallback routine. --- sys/kern/kern_mutex.c | 8 ++++++++ sys/sys/mutex.h | 9 +++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/sys/kern/kern_mutex.c b/sys/kern/kern_mutex.c index 7ad6507bb325..f2aed4907b3e 100644 --- a/sys/kern/kern_mutex.c +++ b/sys/kern/kern_mutex.c @@ -696,6 +696,14 @@ _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v, uintptr_t tid, lock_delay_arg_init(&lda, &mtx_spin_delay); m = mtxlock2mtx(c); + if (__predict_false(v == MTX_UNOWNED)) + v = MTX_READ_VALUE(m); + + if (__predict_false(v == tid)) { + m->mtx_recurse++; + return; + } + if (LOCK_LOG_TEST(&m->lock_object, opts)) CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m); KTR_STATE1(KTR_SCHED, "thread", sched_tdname((struct thread *)tid), diff --git a/sys/sys/mutex.h b/sys/sys/mutex.h index 9d8ca4e467b0..cc56c907f4b4 100644 --- a/sys/sys/mutex.h +++ b/sys/sys/mutex.h @@ -223,12 +223,9 @@ void thread_lock_flags_(struct thread *, int, const char *, int); uintptr_t _v = MTX_UNOWNED; \ \ spinlock_enter(); \ - if (!_mtx_obtain_lock_fetch((mp), &_v, _tid)) { \ - if (_v == _tid) \ - (mp)->mtx_recurse++; \ - else \ - _mtx_lock_spin((mp), _v, _tid, (opts), (file), (line));\ - } else \ + if (!_mtx_obtain_lock_fetch((mp), &_v, _tid)) \ + _mtx_lock_spin((mp), _v, _tid, (opts), (file), (line)); \ + else \ LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, \ mp, 0, 0, file, line); \ } while (0)