]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
dns/stats.c: use isc_refcount_t
authorWitold Kręcicki <wpk@isc.org>
Fri, 17 May 2019 11:35:51 +0000 (13:35 +0200)
committerWitold Kręcicki <wpk@isc.org>
Tue, 9 Jul 2019 14:09:36 +0000 (16:09 +0200)
lib/dns/stats.c

index c5f91213205298e5ece419e57b050ef9ddce8d3f..f57e6552b702521ffaef86409427bde03b9a3236 100644 (file)
@@ -17,6 +17,7 @@
 
 #include <isc/magic.h>
 #include <isc/mem.h>
+#include <isc/refcount.h>
 #include <isc/stats.h>
 #include <isc/util.h>
 
@@ -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);