#include <isc/mem.h>
#include <isc/mutex.h>
#include <isc/once.h>
+#include <isc/refcount.h>
#include <isc/util.h>
#include <dns/db.h>
static isc_mem_t *dns_g_mctx = NULL;
static dns_dbimplementation_t *dbimp = NULL;
static bool initialize_done = false;
-static isc_mutex_t reflock;
-static unsigned int references = 0;
+static isc_refcount_t references = 0;
static void
initialize(void) {
if (result != ISC_R_SUCCESS)
goto cleanup_db;
- isc_mutex_init(&reflock);
-
initialize_done = true;
return;
if (!initialize_done)
return (ISC_R_FAILURE);
- LOCK(&reflock);
- references++;
- UNLOCK(&reflock);
+ isc_refcount_increment(&references);
return (ISC_R_SUCCESS);
}
void
dns_lib_shutdown(void) {
- bool cleanup_ok = false;
-
- LOCK(&reflock);
- if (--references == 0)
- cleanup_ok = true;
- UNLOCK(&reflock);
-
- if (!cleanup_ok)
- return;
-
- dst_lib_destroy();
-
- if (dbimp != NULL)
- dns_ecdb_unregister(&dbimp);
- if (dns_g_mctx != NULL)
- isc_mem_detach(&dns_g_mctx);
+ if (isc_refcount_decrement(&references) == 1) {
+ dst_lib_destroy();
+
+ isc_refcount_destroy(&references);
+
+ if (dbimp != NULL) {
+ dns_ecdb_unregister(&dbimp);
+ }
+ if (dns_g_mctx != NULL) {
+ isc_mem_detach(&dns_g_mctx);
+ }
+ }
}