From 3fcf98c8d3ddce418ffebf7bec161a52bb4dd154 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Witold=20Kr=C4=99cicki?= Date: Thu, 16 May 2019 19:27:01 +0200 Subject: [PATCH] isc/stats: use isc_refcount_t --- lib/ns/stats.c | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/lib/ns/stats.c b/lib/ns/stats.c index 8a39a84940..4c558275e9 100644 --- a/lib/ns/stats.c +++ b/lib/ns/stats.c @@ -13,6 +13,7 @@ #include #include +#include #include #include @@ -22,14 +23,10 @@ #define NS_STATS_VALID(x) ISC_MAGIC_VALID(x, NS_STATS_MAGIC) struct ns_stats { - /*% Unlocked */ unsigned int magic; isc_mem_t *mctx; - isc_mutex_t lock; isc_stats_t *counters; - - /*% Locked by lock */ - unsigned int references; + isc_refcount_t references; }; void @@ -37,9 +34,7 @@ ns_stats_attach(ns_stats_t *stats, ns_stats_t **statsp) { REQUIRE(NS_STATS_VALID(stats)); REQUIRE(statsp != NULL && *statsp == NULL); - LOCK(&stats->lock); - stats->references++; - UNLOCK(&stats->lock); + isc_refcount_increment(&stats->references); *statsp = stats; } @@ -53,13 +48,8 @@ ns_stats_detach(ns_stats_t **statsp) { stats = *statsp; *statsp = NULL; - LOCK(&stats->lock); - stats->references--; - UNLOCK(&stats->lock); - - if (stats->references == 0) { + if (isc_refcount_decrement(&stats->references) == 1) { isc_stats_detach(&stats->counters); - isc_mutex_destroy(&stats->lock); isc_mem_putanddetach(&stats->mctx, stats, sizeof(*stats)); } } @@ -76,9 +66,7 @@ ns_stats_create(isc_mem_t *mctx, int ncounters, ns_stats_t **statsp) { return (ISC_R_NOMEMORY); stats->counters = NULL; - stats->references = 1; - - isc_mutex_init(&stats->lock); + atomic_init(&stats->references, 1); result = isc_stats_create(mctx, &stats->counters, ncounters); if (result != ISC_R_SUCCESS) @@ -92,7 +80,6 @@ ns_stats_create(isc_mem_t *mctx, int ncounters, ns_stats_t **statsp) { return (ISC_R_SUCCESS); clean_mutex: - isc_mutex_destroy(&stats->lock); isc_mem_put(mctx, stats, sizeof(*stats)); return (result);