From 9712b7c0c088433260b9008d5ea68b624da504a8 Mon Sep 17 00:00:00 2001 From: vangyzen Date: Sat, 20 May 2017 17:30:48 +0000 Subject: [PATCH] libthr: change CHECK_AND_INIT_RWLOCK to an inline function This was prompted by a compiler warning about 'ret' shadowing a local variable in the callers of the macro. Reviewed by: kib MFC after: 3 days Sponsored by: Dell EMC Differential Revision: https://reviews.freebsd.org/D10832 --- lib/libthr/thread/thr_rwlock.c | 71 ++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 24 deletions(-) diff --git a/lib/libthr/thread/thr_rwlock.c b/lib/libthr/thread/thr_rwlock.c index 748eb52a2022..105b35c4219f 100644 --- a/lib/libthr/thread/thr_rwlock.c +++ b/lib/libthr/thread/thr_rwlock.c @@ -49,27 +49,42 @@ __weak_reference(_pthread_rwlock_unlock, pthread_rwlock_unlock); __weak_reference(_pthread_rwlock_wrlock, pthread_rwlock_wrlock); __weak_reference(_pthread_rwlock_timedwrlock, pthread_rwlock_timedwrlock); -#define CHECK_AND_INIT_RWLOCK \ - if (*rwlock == THR_PSHARED_PTR) { \ - prwlock = __thr_pshared_offpage(rwlock, 0); \ - if (prwlock == NULL) \ - return (EINVAL); \ - } else if (__predict_false((prwlock = (*rwlock)) <= \ - THR_RWLOCK_DESTROYED)) { \ - if (prwlock == THR_RWLOCK_INITIALIZER) { \ - int ret; \ - ret = init_static(_get_curthread(), rwlock); \ - if (ret) \ - return (ret); \ - } else if (prwlock == THR_RWLOCK_DESTROYED) { \ - return (EINVAL); \ - } \ - prwlock = *rwlock; \ - } +static int init_static(struct pthread *thread, pthread_rwlock_t *rwlock); +static int init_rwlock(pthread_rwlock_t *rwlock, pthread_rwlock_t *rwlock_out); -/* - * Prototypes - */ +static int __always_inline +check_and_init_rwlock(pthread_rwlock_t *rwlock, pthread_rwlock_t *rwlock_out) +{ + if (__predict_false(*rwlock == THR_PSHARED_PTR || + *rwlock <= THR_RWLOCK_DESTROYED)) + return (init_rwlock(rwlock, rwlock_out)); + *rwlock_out = *rwlock; + return (0); +} + +static int __noinline +init_rwlock(pthread_rwlock_t *rwlock, pthread_rwlock_t *rwlock_out) +{ + pthread_rwlock_t prwlock; + int ret; + + if (*rwlock == THR_PSHARED_PTR) { + prwlock = __thr_pshared_offpage(rwlock, 0); + if (prwlock == NULL) + return (EINVAL); + } else if ((prwlock = *rwlock) <= THR_RWLOCK_DESTROYED) { + if (prwlock == THR_RWLOCK_INITIALIZER) { + ret = init_static(_get_curthread(), rwlock); + if (ret != 0) + return (ret); + } else if (prwlock == THR_RWLOCK_DESTROYED) { + return (EINVAL); + } + prwlock = *rwlock; + } + *rwlock_out = prwlock; + return (0); +} static int rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr) @@ -148,7 +163,9 @@ rwlock_rdlock_common(pthread_rwlock_t *rwlock, const struct timespec *abstime) int flags; int ret; - CHECK_AND_INIT_RWLOCK + ret = check_and_init_rwlock(rwlock, &prwlock); + if (ret != 0) + return (ret); if (curthread->rdlock_count) { /* @@ -220,7 +237,9 @@ _pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock) int flags; int ret; - CHECK_AND_INIT_RWLOCK + ret = check_and_init_rwlock(rwlock, &prwlock); + if (ret != 0) + return (ret); if (curthread->rdlock_count) { /* @@ -253,7 +272,9 @@ _pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock) pthread_rwlock_t prwlock; int ret; - CHECK_AND_INIT_RWLOCK + ret = check_and_init_rwlock(rwlock, &prwlock); + if (ret != 0) + return (ret); ret = _thr_rwlock_trywrlock(&prwlock->lock); if (ret == 0) @@ -268,7 +289,9 @@ rwlock_wrlock_common (pthread_rwlock_t *rwlock, const struct timespec *abstime) pthread_rwlock_t prwlock; int ret; - CHECK_AND_INIT_RWLOCK + ret = check_and_init_rwlock(rwlock, &prwlock); + if (ret != 0) + return (ret); /* * POSIX said the validity of the abstimeout parameter need