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

index 3fddd9c830bb981d0a43385cc6f1f715107994ca..731b79a5605bb52531b17502dcc02c4eb6df07c2 100644 (file)
@@ -18,6 +18,7 @@
 
 #include <isc/mem.h>
 #include <isc/print.h>
+#include <isc/refcount.h>
 #include <isc/string.h>
 #include <isc/stats.h>
 #include <isc/task.h>
@@ -130,9 +131,9 @@ struct dns_cache {
        isc_mem_t               *mctx;          /* Main cache memory */
        isc_mem_t               *hmctx;         /* Heap memory */
        char                    *name;
+       isc_refcount_t          references;
 
        /* Locked by 'lock'. */
-       int                     references;
        int                     live_tasks;
        dns_rdataclass_t        rdclass;
        dns_db_t                *db;
@@ -214,7 +215,7 @@ dns_cache_create(isc_mem_t *cmctx, isc_mem_t *hmctx, isc_taskmgr_t *taskmgr,
        isc_mutex_init(&cache->lock);
        isc_mutex_init(&cache->filelock);
 
-       cache->references = 1;
+       isc_refcount_init(&cache->references, 1);
        cache->live_tasks = 0;
        cache->rdclass = rdclass;
        cache->serve_stale_ttl = 0;
@@ -337,7 +338,7 @@ cache_free(dns_cache_t *cache) {
        int i;
 
        REQUIRE(VALID_CACHE(cache));
-       REQUIRE(cache->references == 0);
+       REQUIRE(isc_refcount_current(&cache->references) == 0);
 
        isc_mem_setwater(cache->mctx, NULL, NULL, 0, 0);
 
@@ -402,9 +403,7 @@ dns_cache_attach(dns_cache_t *cache, dns_cache_t **targetp) {
        REQUIRE(VALID_CACHE(cache));
        REQUIRE(targetp != NULL && *targetp == NULL);
 
-       LOCK(&cache->lock);
-       cache->references++;
-       UNLOCK(&cache->lock);
+       isc_refcount_increment(&cache->references);
 
        *targetp = cache;
 }
@@ -417,18 +416,12 @@ dns_cache_detach(dns_cache_t **cachep) {
        REQUIRE(cachep != NULL);
        cache = *cachep;
        REQUIRE(VALID_CACHE(cache));
-
-       LOCK(&cache->lock);
-       REQUIRE(cache->references > 0);
-       cache->references--;
-       if (cache->references == 0) {
-               cache->cleaner.overmem = false;
-               free_cache = true;
-       }
-
        *cachep = NULL;
 
-       if (free_cache) {
+       if (isc_refcount_decrement(&cache->references) == 1) {
+               LOCK(&cache->lock);
+               free_cache = true;
+               cache->cleaner.overmem = false;
                /*
                 * When the cache is shut down, dump it to a file if one is
                 * specified.
@@ -447,12 +440,13 @@ dns_cache_detach(dns_cache_t **cachep) {
                        isc_task_shutdown(cache->cleaner.task);
                        free_cache = false;
                }
+               UNLOCK(&cache->lock);
        }
 
-       UNLOCK(&cache->lock);
 
-       if (free_cache)
+       if (free_cache) {
                cache_free(cache);
+       }
 }
 
 void
@@ -1030,8 +1024,9 @@ cleaner_shutdown_action(isc_task_t *task, isc_event_t *event) {
        cache->live_tasks--;
        INSIST(cache->live_tasks == 0);
 
-       if (cache->references == 0)
+       if (isc_refcount_current(&cache->references) == 0) {
                should_free = true;
+       }
 
        /* Make sure we don't reschedule anymore. */
        (void)isc_task_purge(task, NULL, DNS_EVENT_CACHECLEAN, NULL);