From 100d0d2ec64ab1a85f8c0d2da9b47ae411a10b21 Mon Sep 17 00:00:00 2001 From: Brian Wellington Date: Tue, 13 Feb 2001 03:57:06 +0000 Subject: [PATCH] Use a refcount instead of an explicit mutex and integer. --- lib/dns/include/dns/tsig.h | 9 +++----- lib/dns/tsig.c | 47 ++++++++++++++------------------------ 2 files changed, 20 insertions(+), 36 deletions(-) diff --git a/lib/dns/include/dns/tsig.h b/lib/dns/include/dns/tsig.h index 545d727ada..72065b655e 100644 --- a/lib/dns/include/dns/tsig.h +++ b/lib/dns/include/dns/tsig.h @@ -15,13 +15,13 @@ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -/* $Id: tsig.h,v 1.37 2001/01/09 21:53:36 bwelling Exp $ */ +/* $Id: tsig.h,v 1.38 2001/02/13 03:57:06 bwelling Exp $ */ #ifndef DNS_TSIG_H #define DNS_TSIG_H 1 #include -#include +#include #include #include @@ -63,10 +63,7 @@ struct dns_tsigkey { isc_stdtime_t inception; /* start of validity period */ isc_stdtime_t expire; /* end of validity period */ dns_tsig_keyring_t *ring; /* the enclosing keyring */ - isc_mutex_t lock; - /* Locked */ - isc_uint32_t refs; /* reference counter */ - /* Unlocked */ + isc_refcount_t refs; /* reference counter */ }; #define dns_tsigkey_identity(tsigkey) \ diff --git a/lib/dns/tsig.c b/lib/dns/tsig.c index 0f5203e9ed..815f782f67 100644 --- a/lib/dns/tsig.c +++ b/lib/dns/tsig.c @@ -16,7 +16,7 @@ */ /* - * $Id: tsig.c,v 1.105 2001/01/22 20:27:04 bwelling Exp $ + * $Id: tsig.c,v 1.106 2001/02/13 03:57:04 bwelling Exp $ */ #include @@ -25,6 +25,7 @@ #include #include #include +#include #include /* Required for HP/UX (and others?) */ #include @@ -126,6 +127,7 @@ dns_tsigkey_createfromkey(dns_name_t *name, dns_name_t *algorithm, { dns_tsigkey_t *tkey; isc_result_t ret; + unsigned int refs = 0; REQUIRE(key == NULL || *key == NULL); REQUIRE(name != NULL); @@ -182,7 +184,6 @@ dns_tsigkey_createfromkey(dns_name_t *name, dns_name_t *algorithm, tkey->key = dstkey; tkey->ring = ring; - tkey->refs = 0; if (ring != NULL) { RWLOCK(&ring->lock, isc_rwlocktype_write); @@ -191,23 +192,17 @@ dns_tsigkey_createfromkey(dns_name_t *name, dns_name_t *algorithm, RWUNLOCK(&ring->lock, isc_rwlocktype_write); goto cleanup_algorithm; } - tkey->refs++; + refs++; RWUNLOCK(&ring->lock, isc_rwlocktype_write); } if (key != NULL) - tkey->refs++; + refs++; + isc_refcount_init(&tkey->refs, refs); tkey->generated = generated; tkey->inception = inception; tkey->expire = expire; tkey->mctx = mctx; - ret = isc_mutex_init(&tkey->lock); - if (ret != ISC_R_SUCCESS) { - UNEXPECTED_ERROR(__FILE__, __LINE__, - "isc_mutex_init() failed: %s", - isc_result_totext(ret)); - return (ISC_R_UNEXPECTED); - } tkey->magic = TSIG_MAGIC; @@ -281,9 +276,7 @@ dns_tsigkey_attach(dns_tsigkey_t *source, dns_tsigkey_t **targetp) { REQUIRE(VALID_TSIG_KEY(source)); REQUIRE(targetp != NULL && *targetp == NULL); - LOCK(&source->lock); - source->refs++; - UNLOCK(&source->lock); + isc_refcount_increment(&source->refs, NULL); *targetp = source; } @@ -303,27 +296,25 @@ tsigkey_free(dns_tsigkey_t *key) { dns_name_free(key->creator, key->mctx); isc_mem_put(key->mctx, key->creator, sizeof(dns_name_t)); } - DESTROYLOCK(&key->lock); + isc_refcount_destroy(&key->refs); isc_mem_put(key->mctx, key, sizeof(dns_tsigkey_t)); } void dns_tsigkey_detach(dns_tsigkey_t **keyp) { dns_tsigkey_t *key; - isc_boolean_t should_free = ISC_FALSE; + unsigned int refs; REQUIRE(keyp != NULL); REQUIRE(VALID_TSIG_KEY(*keyp)); - key = *keyp; - *keyp = NULL; - LOCK(&key->lock); - key->refs--; - if (key->refs == 0) - should_free = ISC_TRUE; - UNLOCK(&key->lock); - if (should_free) + key = *keyp; + isc_refcount_decrement(&key->refs, &refs); + + if (refs == 0) tsigkey_free(key); + + *keyp = NULL; } void @@ -1125,18 +1116,14 @@ dns_tsigkey_find(dns_tsigkey_t **tsigkey, dns_name_t *name, * The key has expired. */ RWUNLOCK(&ring->lock, isc_rwlocktype_read); - LOCK(&key->lock); - key->refs--; - UNLOCK(&key->lock); + isc_refcount_decrement(&key->refs, NULL); RWLOCK(&ring->lock, isc_rwlocktype_write); (void) dns_rbt_deletename(ring->keys, name, ISC_FALSE); RWUNLOCK(&ring->lock, isc_rwlocktype_write); return (ISC_R_NOTFOUND); } - LOCK(&key->lock); - key->refs++; - UNLOCK(&key->lock); + isc_refcount_increment(&key->refs, NULL); RWUNLOCK(&ring->lock, isc_rwlocktype_read); *tsigkey = key; return (ISC_R_SUCCESS);