From 44135371df3762294964f14388288a2008b370e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ondr=CC=8Cej=20Sury=CC=81?= Date: Fri, 6 Jan 2023 08:49:11 +0100 Subject: [PATCH] Deduplicate DNS_RBTDB_STRONG_RWLOCK_CHECK macros There were couple of redundant macros on both sides of DNS_RBTDB_STRONG_RWLOCK_CHECK #ifdef block. Use a single set of macros, but disable the extra REQUIRES if the #define is not set. --- lib/dns/rbtdb.c | 174 +++++++++++------------------------------------- 1 file changed, 40 insertions(+), 134 deletions(-) diff --git a/lib/dns/rbtdb.c b/lib/dns/rbtdb.c index d1fa0a76a1..4850bc148d 100644 --- a/lib/dns/rbtdb.c +++ b/lib/dns/rbtdb.c @@ -109,30 +109,33 @@ typedef uint32_t rbtdb_rdatatype_t; #define RBTDB_LOCK(l, t) RWLOCK((l), (t)) #define RBTDB_UNLOCK(l, t) RWUNLOCK((l), (t)) -typedef isc_rwlock_t nodelock_t; - #ifdef DNS_RBTDB_STRONG_RWLOCK_CHECK +#define STRONG_RWLOCK_CHECK(cond) REQUIRE(cond) +#else +#define STRONG_RWLOCK_CHECK(cond) +#endif + +typedef isc_rwlock_t nodelock_t; #define NODE_INITLOCK(l) isc_rwlock_init((l), 0, 0) #define NODE_DESTROYLOCK(l) isc_rwlock_destroy(l) -#define NODE_LOCK(l, t, tp) \ - { \ - REQUIRE(*tp == isc_rwlocktype_none); \ - RWLOCK((l), (t)); \ - *tp = t; \ +#define NODE_LOCK(l, t, tp) \ + { \ + STRONG_RWLOCK_CHECK(*tp == isc_rwlocktype_none); \ + RWLOCK((l), (t)); \ + *tp = t; \ + } +#define NODE_UNLOCK(l, tp) \ + { \ + STRONG_RWLOCK_CHECK(*tp != isc_rwlocktype_none); \ + RWUNLOCK(l, *tp); \ + *tp = isc_rwlocktype_none; \ } #define NODE_RDLOCK(l, tp) NODE_LOCK(l, isc_rwlocktype_read, tp); #define NODE_WRLOCK(l, tp) NODE_LOCK(l, isc_rwlocktype_write, tp); - -#define NODE_UNLOCK(l, tp) \ - { \ - REQUIRE(*tp != isc_rwlocktype_none); \ - RWUNLOCK(l, *tp); \ - *tp = isc_rwlocktype_none; \ - } #define NODE_TRYLOCK(l, t, tp) \ ({ \ - REQUIRE(*tp == isc_rwlocktype_none); \ + STRONG_RWLOCK_CHECK(*tp == isc_rwlocktype_none); \ isc_result_t _result = isc_rwlock_trylock(l, t); \ if (_result == ISC_R_SUCCESS) { \ *tp = t; \ @@ -143,42 +146,40 @@ typedef isc_rwlock_t nodelock_t; #define NODE_TRYWRLOCK(l, tp) NODE_TRYLOCK(l, isc_rwlocktype_write, tp) #define NODE_TRYUPGRADE(l, tp) \ ({ \ - REQUIRE(*tp == isc_rwlocktype_read); \ + STRONG_RWLOCK_CHECK(*tp == isc_rwlocktype_read); \ isc_result_t _result = isc_rwlock_tryupgrade(l); \ if (_result == ISC_R_SUCCESS) { \ *tp = isc_rwlocktype_write; \ }; \ _result; \ }) -#define NODE_FORCEUPGRADE(l, tp) \ - { \ - if (NODE_TRYUPGRADE(l, tp) != ISC_R_SUCCESS) { \ - NODE_UNLOCK(l, tp); \ - NODE_WRLOCK(l, tp); \ - } \ +#define NODE_FORCEUPGRADE(l, tp) \ + if (NODE_TRYUPGRADE(l, tp) != ISC_R_SUCCESS) { \ + NODE_UNLOCK(l, tp); \ + NODE_WRLOCK(l, tp); \ } typedef isc_rwlock_t treelock_t; #define TREE_INITLOCK(l) isc_rwlock_init(l, 0, 0) #define TREE_DESTROYLOCK(l) isc_rwlock_destroy(l) -#define TREE_LOCK(l, t, tp) \ - { \ - REQUIRE(*tp == isc_rwlocktype_none); \ - RWLOCK(l, t); \ - *tp = t; \ +#define TREE_LOCK(l, t, tp) \ + { \ + STRONG_RWLOCK_CHECK(*tp == isc_rwlocktype_none); \ + RWLOCK(l, t); \ + *tp = t; \ + } +#define TREE_UNLOCK(l, tp) \ + { \ + STRONG_RWLOCK_CHECK(*tp != isc_rwlocktype_none); \ + RWUNLOCK(l, *tp); \ + *tp = isc_rwlocktype_none; \ } #define TREE_RDLOCK(l, tp) TREE_LOCK(l, isc_rwlocktype_read, tp); #define TREE_WRLOCK(l, tp) TREE_LOCK(l, isc_rwlocktype_write, tp); -#define TREE_UNLOCK(l, tp) \ - { \ - REQUIRE(*tp != isc_rwlocktype_none); \ - RWUNLOCK(l, *tp); \ - *tp = isc_rwlocktype_none; \ - } #define TREE_TRYLOCK(l, t, tp) \ ({ \ - REQUIRE(*tp == isc_rwlocktype_none); \ + STRONG_RWLOCK_CHECK(*tp == isc_rwlocktype_none); \ isc_result_t _result = isc_rwlock_trylock(l, t); \ if (_result == ISC_R_SUCCESS) { \ *tp = t; \ @@ -189,114 +190,19 @@ typedef isc_rwlock_t treelock_t; #define TREE_TRYWRLOCK(l, tp) TREE_TRYLOCK(l, isc_rwlocktype_write, tp) #define TREE_TRYUPGRADE(l, tp) \ ({ \ - REQUIRE(*tp == isc_rwlocktype_read); \ + STRONG_RWLOCK_CHECK(*tp == isc_rwlocktype_read); \ isc_result_t _result = isc_rwlock_tryupgrade(l); \ if (_result == ISC_R_SUCCESS) { \ *tp = isc_rwlocktype_write; \ }; \ _result; \ }) -#define TREE_FORCEUPGRADE(l, tp) \ - { \ - if (TREE_TRYUPGRADE(l, tp) != ISC_R_SUCCESS) { \ - TREE_UNLOCK(l, tp); \ - TREE_WRLOCK(l, tp); \ - } \ +#define TREE_FORCEUPGRADE(l, tp) \ + if (TREE_TRYUPGRADE(l, tp) != ISC_R_SUCCESS) { \ + TREE_UNLOCK(l, tp); \ + TREE_WRLOCK(l, tp); \ } -#else /* DNS_RBTDB_STRONG_RWLOCK_CHECK */ - -#define NODE_INITLOCK(l) isc_rwlock_init((l), 0, 0) -#define NODE_DESTROYLOCK(l) isc_rwlock_destroy(l) -#define NODE_LOCK(l, t, tp) \ - { \ - RWLOCK((l), (t)); \ - *tp = t; \ - } -#define NODE_RDLOCK(l, tp) NODE_LOCK(l, isc_rwlocktype_read, tp); -#define NODE_WRLOCK(l, tp) NODE_LOCK(l, isc_rwlocktype_write, tp); - -#define NODE_UNLOCK(l, tp) \ - { \ - RWUNLOCK(l, *tp); \ - *tp = isc_rwlocktype_none; \ - } -#define NODE_TRYLOCK(l, t, tp) \ - ({ \ - isc_result_t _result = isc_rwlock_trylock(l, t); \ - if (_result == ISC_R_SUCCESS) { \ - *tp = t; \ - }; \ - _result; \ - }) -#define NODE_TRYRDLOCK(l, tp) NODE_TRYLOCK(l, isc_rwlocktype_read, tp) -#define NODE_TRYWRLOCK(l, tp) NODE_TRYLOCK(l, isc_rwlocktype_write, tp) -#define NODE_TRYUPGRADE(l, tp) \ - ({ \ - isc_result_t _result = isc_rwlock_tryupgrade(l); \ - if (_result == ISC_R_SUCCESS) { \ - *tp = isc_rwlocktype_write; \ - }; \ - _result; \ - }) -#define NODE_FORCEUPGRADE(l, tp) \ - { \ - if (NODE_TRYUPGRADE(l, tp) != ISC_R_SUCCESS) { \ - NODE_UNLOCK(l, tp); \ - NODE_WRLOCK(l, tp); \ - } \ - } - -typedef isc_rwlock_t treelock_t; - -#define TREE_INITLOCK(l) isc_rwlock_init(l, 0, 0) -#define TREE_DESTROYLOCK(l) isc_rwlock_destroy(l) -#define TREE_LOCK(l, t, tp) \ - { \ - RWLOCK(l, t); \ - *tp = t; \ - } -#define TREE_RDLOCK(l, tp) \ - { \ - TREE_LOCK(l, isc_rwlocktype_read, tp); \ - } -#define TREE_WRLOCK(l, tp) \ - { \ - TREE_LOCK(l, isc_rwlocktype_write, tp); \ - } -#define TREE_UNLOCK(l, tp) \ - { \ - RWUNLOCK(l, *tp); \ - *tp = isc_rwlocktype_none; \ - } -#define TREE_TRYLOCK(l, t, tp) \ - ({ \ - isc_result_t _result = isc_rwlock_trylock(l, t); \ - if (_result == ISC_R_SUCCESS) { \ - *tp = t; \ - }; \ - _result; \ - }) -#define TREE_TRYRDLOCK(l, tp) TREE_TRYLOCK(l, isc_rwlocktype_read, tp) -#define TREE_TRYWRLOCK(l, tp) TREE_TRYLOCK(l, isc_rwlocktype_write, tp) -#define TREE_TRYUPGRADE(l, tp) \ - ({ \ - isc_result_t _result = isc_rwlock_tryupgrade(l); \ - if (_result == ISC_R_SUCCESS) { \ - *tp = isc_rwlocktype_write; \ - }; \ - _result; \ - }) -#define TREE_FORCEUPGRADE(l, tp) \ - { \ - if (TREE_TRYUPGRADE(l, tp) != ISC_R_SUCCESS) { \ - TREE_UNLOCK(l, tp); \ - TREE_WRLOCK(l, tp); \ - } \ - } - -#endif - /*% * Whether to rate-limit updating the LRU to avoid possible thread contention. * Updating LRU requires write locking, so we don't do it every time the