diff --git a/lib/dns/stats.c b/lib/dns/stats.c index c5f9121320..f57e6552b7 100644 --- a/lib/dns/stats.c +++ b/lib/dns/stats.c @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -64,15 +65,11 @@ enum { }; struct dns_stats { - /*% Unlocked */ unsigned int magic; dns_statstype_t type; isc_mem_t *mctx; - isc_mutex_t lock; isc_stats_t *counters; - - /*% Locked by lock */ - unsigned int references; + isc_refcount_t references; }; typedef struct rdatadumparg { @@ -99,9 +96,7 @@ dns_stats_attach(dns_stats_t *stats, dns_stats_t **statsp) { REQUIRE(DNS_STATS_VALID(stats)); REQUIRE(statsp != NULL && *statsp == NULL); - LOCK(&stats->lock); - stats->references++; - UNLOCK(&stats->lock); + isc_refcount_increment(&stats->references); *statsp = stats; } @@ -115,13 +110,8 @@ dns_stats_detach(dns_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)); } } @@ -141,9 +131,7 @@ create_stats(isc_mem_t *mctx, dns_statstype_t type, int ncounters, return (ISC_R_NOMEMORY); stats->counters = NULL; - stats->references = 1; - - isc_mutex_init(&stats->lock); + isc_refcount_init(&stats->references, 1); result = isc_stats_create(mctx, &stats->counters, ncounters); if (result != ISC_R_SUCCESS) @@ -158,7 +146,6 @@ create_stats(isc_mem_t *mctx, dns_statstype_t type, int ncounters, return (ISC_R_SUCCESS); clean_mutex: - isc_mutex_destroy(&stats->lock); isc_mem_put(mctx, stats, sizeof(*stats)); return (result);