]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
lib/dns/dbtable.c: use isc_refcount_t
authorOndřej Surý <ondrej@sury.org>
Mon, 20 May 2019 14:22:15 +0000 (16:22 +0200)
committerWitold Kręcicki <wpk@isc.org>
Tue, 9 Jul 2019 14:09:36 +0000 (16:09 +0200)
lib/dns/dbtable.c

index e3ca6a78667c0417a00c8ee69e7d861176a4f9ba..177830db92c1781f7f49ec0a2c694846ee9ff560 100644 (file)
@@ -25,10 +25,9 @@ struct dns_dbtable {
        unsigned int            magic;
        isc_mem_t *             mctx;
        dns_rdataclass_t        rdclass;
-       isc_mutex_t             lock;
        isc_rwlock_t            tree_lock;
-       /* Locked by lock. */
-       unsigned int            references;
+       /* Protected by atomics */
+       isc_refcount_t          references;
        /* Locked by tree_lock. */
        dns_rbt_t *             rbt;
        dns_db_t *              default_db;
@@ -65,8 +64,6 @@ dns_dbtable_create(isc_mem_t *mctx, dns_rdataclass_t rdclass,
        if (result != ISC_R_SUCCESS)
                goto clean1;
 
-       isc_mutex_init(&dbtable->lock);
-
        result = isc_rwlock_init(&dbtable->tree_lock, 0, 0);
        if (result != ISC_R_SUCCESS)
                goto clean3;
@@ -76,15 +73,13 @@ dns_dbtable_create(isc_mem_t *mctx, dns_rdataclass_t rdclass,
        isc_mem_attach(mctx, &dbtable->mctx);
        dbtable->rdclass = rdclass;
        dbtable->magic = DBTABLE_MAGIC;
-       dbtable->references = 1;
+       isc_refcount_init(&dbtable->references, 1);
 
        *dbtablep = dbtable;
 
        return (ISC_R_SUCCESS);
 
  clean3:
-       isc_mutex_destroy(&dbtable->lock);
-
        dns_rbt_destroy(&dbtable->rbt);
 
  clean1:
@@ -120,13 +115,7 @@ dns_dbtable_attach(dns_dbtable_t *source, dns_dbtable_t **targetp) {
        REQUIRE(VALID_DBTABLE(source));
        REQUIRE(targetp != NULL && *targetp == NULL);
 
-       LOCK(&source->lock);
-
-       INSIST(source->references > 0);
-       source->references++;
-       INSIST(source->references != 0);
-
-       UNLOCK(&source->lock);
+       isc_refcount_increment(&source->references);
 
        *targetp = source;
 }
@@ -134,25 +123,15 @@ dns_dbtable_attach(dns_dbtable_t *source, dns_dbtable_t **targetp) {
 void
 dns_dbtable_detach(dns_dbtable_t **dbtablep) {
        dns_dbtable_t *dbtable;
-       bool free_dbtable = false;
 
        REQUIRE(dbtablep != NULL);
        dbtable = *dbtablep;
        REQUIRE(VALID_DBTABLE(dbtable));
+       *dbtablep = NULL;
 
-       LOCK(&dbtable->lock);
-
-       INSIST(dbtable->references > 0);
-       dbtable->references--;
-       if (dbtable->references == 0)
-               free_dbtable = true;
-
-       UNLOCK(&dbtable->lock);
-
-       if (free_dbtable)
+       if (isc_refcount_decrement(&dbtable->references) == 1) {
                dbtable_free(dbtable);
-
-       *dbtablep = NULL;
+       }
 }
 
 isc_result_t