]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
isc/stats: use isc_refcount_t
authorWitold Kręcicki <wpk@isc.org>
Thu, 16 May 2019 17:27:01 +0000 (19:27 +0200)
committerWitold Kręcicki <wpk@isc.org>
Tue, 9 Jul 2019 14:09:36 +0000 (16:09 +0200)
lib/ns/stats.c

index 8a39a849409e4868eb0c4a1909bda75fd57f0d8e..4c558275e9be1e18343beeea603cedc156b5542a 100644 (file)
@@ -13,6 +13,7 @@
 
 #include <isc/magic.h>
 #include <isc/mem.h>
+#include <isc/refcount.h>
 #include <isc/stats.h>
 #include <isc/util.h>
 
 #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);