diff --git a/lib/dns/include/dns/nta.h b/lib/dns/include/dns/nta.h index cce47b0a44..3bd5d9f9fc 100644 --- a/lib/dns/include/dns/nta.h +++ b/lib/dns/include/dns/nta.h @@ -50,8 +50,9 @@ struct dns_ntatable { isc_taskmgr_t *taskmgr; isc_timermgr_t *timermgr; isc_task_t *task; + /* Protected by atomics */ + isc_refcount_t references; /* Locked by rwlock. */ - uint32_t references; dns_rbt_t *table; }; diff --git a/lib/dns/nta.c b/lib/dns/nta.c index 444684c2e9..c495c3cff4 100644 --- a/lib/dns/nta.c +++ b/lib/dns/nta.c @@ -133,7 +133,7 @@ dns_ntatable_create(dns_view_t *view, ntatable->taskmgr = taskmgr; ntatable->view = view; - ntatable->references = 1; + isc_refcount_init(&ntatable->references, 1); ntatable->magic = NTATABLE_MAGIC; *ntatablep = ntatable; @@ -157,20 +157,13 @@ dns_ntatable_attach(dns_ntatable_t *source, dns_ntatable_t **targetp) { REQUIRE(VALID_NTATABLE(source)); REQUIRE(targetp != NULL && *targetp == NULL); - RWLOCK(&source->rwlock, isc_rwlocktype_write); - - INSIST(source->references > 0); - source->references++; - INSIST(source->references != 0); - - RWUNLOCK(&source->rwlock, isc_rwlocktype_write); + isc_refcount_increment(&source->references); *targetp = source; } void dns_ntatable_detach(dns_ntatable_t **ntatablep) { - bool destroy = false; dns_ntatable_t *ntatable; REQUIRE(ntatablep != NULL && VALID_NTATABLE(*ntatablep)); @@ -178,16 +171,10 @@ dns_ntatable_detach(dns_ntatable_t **ntatablep) { ntatable = *ntatablep; *ntatablep = NULL; - RWLOCK(&ntatable->rwlock, isc_rwlocktype_write); - INSIST(ntatable->references > 0); - ntatable->references--; - if (ntatable->references == 0) - destroy = true; - RWUNLOCK(&ntatable->rwlock, isc_rwlocktype_write); - - if (destroy) { + if (isc_refcount_decrement(&ntatable->references) == 1) { dns_rbt_destroy(&ntatable->table); isc_rwlock_destroy(&ntatable->rwlock); + isc_refcount_destroy(&ntatable->references); if (ntatable->task != NULL) isc_task_detach(&ntatable->task); ntatable->timermgr = NULL;