From 64fbffbbaaeeb6185eba84bccd2f981bee23710d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Thu, 16 Aug 2018 15:03:42 +0200 Subject: [PATCH] Use simple pthread_rwlock in place of our custom adaptive rwlock --- config.h.in | 3 ++ configure | 15 +++++--- configure.ac | 6 +-- lib/isc/include/isc/rwlock.h | 11 ++++++ lib/isc/rwlock.c | 74 ++++++++++++++++++++++++++++++++++++ 5 files changed, 99 insertions(+), 10 deletions(-) diff --git a/config.h.in b/config.h.in index 4f40e31458..cf742bc31c 100644 --- a/config.h.in +++ b/config.h.in @@ -309,6 +309,9 @@ /* Have PTHREAD_PRIO_INHERIT. */ #undef HAVE_PTHREAD_PRIO_INHERIT +/* Define to 1 if you have the `pthread_rwlock_rdlock' function. */ +#undef HAVE_PTHREAD_RWLOCK_RDLOCK + /* Define to 1 if you have the `pthread_setaffinity_np' function. */ #undef HAVE_PTHREAD_SETAFFINITY_NP diff --git a/configure b/configure index 09167c62f4..24cca62c9c 100755 --- a/configure +++ b/configure @@ -15615,12 +15615,17 @@ $as_echo "no" >&6; } esac -# -# If PIC is disabled, shared libraries must also be -# -if test "$pic_mode" = "no"; then : - enable_shared="no" +for ac_func in pthread_rwlock_rdlock +do : + ac_fn_c_check_func "$LINENO" "pthread_rwlock_rdlock" "ac_cv_func_pthread_rwlock_rdlock" +if test "x$ac_cv_func_pthread_rwlock_rdlock" = xyes; then : + cat >>confdefs.h <<_ACEOF +#define HAVE_PTHREAD_RWLOCK_RDLOCK 1 +_ACEOF + fi +done + CRYPTO=OpenSSL diff --git a/configure.ac b/configure.ac index 1e20f1724e..272706314e 100644 --- a/configure.ac +++ b/configure.ac @@ -733,11 +733,7 @@ case $use_libtool in esac AC_SUBST(INSTALL_LIBRARY) -# -# If PIC is disabled, shared libraries must also be -# -AS_IF([test "$pic_mode" = "no"], - [enable_shared="no"]) +AC_CHECK_FUNCS([pthread_rwlock_rdlock]) CRYPTO=OpenSSL diff --git a/lib/isc/include/isc/rwlock.h b/lib/isc/include/isc/rwlock.h index ed1ff66312..50d06d72d0 100644 --- a/lib/isc/include/isc/rwlock.h +++ b/lib/isc/include/isc/rwlock.h @@ -14,6 +14,7 @@ #define ISC_RWLOCK_H 1 #include +#include /*! \file isc/rwlock.h */ @@ -31,6 +32,14 @@ typedef enum { isc_rwlocktype_write } isc_rwlocktype_t; +#if HAVE_PTHREAD_RWLOCK_RDLOCK + +struct isc_rwlock { + pthread_rwlock_t rwlock; +}; + +#else /* HAVE_PTHREAD_RWLOCK_RDLOCK */ + struct isc_rwlock { /* Unlocked. */ unsigned int magic; @@ -68,6 +77,8 @@ struct isc_rwlock { }; +#endif /* HAVE_PTHREAD_RWLOCK_RDLOCK */ + isc_result_t isc_rwlock_init(isc_rwlock_t *rwl, unsigned int read_quota, unsigned int write_quota); diff --git a/lib/isc/rwlock.c b/lib/isc/rwlock.c index 1071fecc07..2619619d1b 100644 --- a/lib/isc/rwlock.c +++ b/lib/isc/rwlock.c @@ -27,6 +27,78 @@ #include #include +#if HAVE_PTHREAD_RWLOCK_RDLOCK + +#include +#include + +isc_result_t +isc_rwlock_init(isc_rwlock_t *rwl, unsigned int read_quota, + unsigned int write_quota) +{ + UNUSED(read_quota); + UNUSED(write_quota); + REQUIRE(pthread_rwlock_init(&rwl->rwlock, NULL) == 0); + return (ISC_R_SUCCESS); +} + +isc_result_t +isc_rwlock_lock(isc_rwlock_t *rwl, isc_rwlocktype_t type) { + switch (type) { + case isc_rwlocktype_read: REQUIRE(pthread_rwlock_rdlock(&rwl->rwlock) == 0); break; + case isc_rwlocktype_write: REQUIRE(pthread_rwlock_wrlock(&rwl->rwlock) == 0); break; + default: INSIST(0); + } + return (ISC_R_SUCCESS); +} + +isc_result_t +isc_rwlock_trylock(isc_rwlock_t *rwl, isc_rwlocktype_t type) { + int ret = 0; + switch (type) { + case isc_rwlocktype_read: + ret = pthread_rwlock_tryrdlock(&rwl->rwlock); + break; + case isc_rwlocktype_write: + ret = pthread_rwlock_trywrlock(&rwl->rwlock); + break; + default: INSIST(0); + } + + switch (ret) { + case 0: return (ISC_R_SUCCESS); + case EBUSY: return (ISC_R_LOCKBUSY); + case EAGAIN: return (ISC_R_LOCKBUSY); + default: INSIST(0); ISC_UNREACHABLE(); + } +} + + +isc_result_t +isc_rwlock_unlock(isc_rwlock_t *rwl, isc_rwlocktype_t type) { + UNUSED(type); + REQUIRE(pthread_rwlock_unlock(&rwl->rwlock) == 0); + return (ISC_R_SUCCESS); +} + +isc_result_t +isc_rwlock_tryupgrade(isc_rwlock_t *rwl) { + return (ISC_R_LOCKBUSY); +} + +void +isc_rwlock_downgrade(isc_rwlock_t *rwl) { + isc_rwlock_unlock(rwl, isc_rwlocktype_write); + isc_rwlock_lock(rwl, isc_rwlocktype_read); +} + +void +isc_rwlock_destroy(isc_rwlock_t *rwl) { + pthread_rwlock_destroy(&rwl->rwlock); +} + +#else + #define RWLOCK_MAGIC ISC_MAGIC('R', 'W', 'L', 'k') #define VALID_RWLOCK(rwl) ISC_MAGIC_VALID(rwl, RWLOCK_MAGIC) @@ -549,3 +621,5 @@ isc_rwlock_unlock(isc_rwlock_t *rwl, isc_rwlocktype_t type) { return (ISC_R_SUCCESS); } + +#endif /* HAVE_PTHREAD_RWLOCK_RDLOCK */