#include <stdbool.h>
#include <stdlib.h>
+#include <isc/atomic.h>
#include <isc/buffer.h>
#include <isc/event.h>
#include <isc/file.h>
#include <isc/magic.h>
#include <isc/mem.h>
+#include <isc/refcount.h>
#include <isc/print.h>
#include <isc/stdio.h>
#include <isc/string.h>
unsigned int magic;
isc_mem_t *mctx;
isc_mutex_t lock;
- unsigned int references;
- bool canceled;
- bool first;
- bool do_date;
+ isc_refcount_t references;
+ atomic_bool canceled;
+ bool first;
+ bool do_date;
isc_stdtime_t now;
FILE *f;
dns_db_t *db;
REQUIRE(DNS_DCTX_VALID(source));
REQUIRE(target != NULL && *target == NULL);
- LOCK(&source->lock);
- INSIST(source->references > 0);
- source->references++;
- INSIST(source->references != 0); /* Overflow? */
- UNLOCK(&source->lock);
+ isc_refcount_increment(&source->references);
*target = source;
}
void
dns_dumpctx_detach(dns_dumpctx_t **dctxp) {
dns_dumpctx_t *dctx;
- bool need_destroy = false;
REQUIRE(dctxp != NULL);
dctx = *dctxp;
*dctxp = NULL;
- LOCK(&dctx->lock);
- INSIST(dctx->references != 0);
- dctx->references--;
- if (dctx->references == 0)
- need_destroy = true;
- UNLOCK(&dctx->lock);
- if (need_destroy)
+ if (isc_refcount_decrement(&dctx->references) == 1) {
dumpctx_destroy(dctx);
+ }
}
dns_dbversion_t *
dns_dumpctx_cancel(dns_dumpctx_t *dctx) {
REQUIRE(DNS_DCTX_VALID(dctx));
- LOCK(&dctx->lock);
- dctx->canceled = true;
- UNLOCK(&dctx->lock);
+ atomic_store_release(&dctx->canceled, true);
}
static isc_result_t
REQUIRE(event != NULL);
dctx = event->ev_arg;
REQUIRE(DNS_DCTX_VALID(dctx));
- if (dctx->canceled)
+ if (atomic_load_acquire(&dctx->canceled)) {
result = ISC_R_CANCELED;
- else
+ } else {
result = dumptostreaminc(dctx);
+ }
if (result == DNS_R_CONTINUE) {
event->ev_arg = dctx;
isc_task_send(task, &event);
dctx->task = NULL;
dctx->nodes = 0;
dctx->first = true;
- dctx->canceled = false;
+ atomic_init(&dctx->canceled, false);
dctx->file = NULL;
dctx->tmpfile = NULL;
dctx->format = format;
else if (!dns_db_iscache(db))
dns_db_currentversion(dctx->db, &dctx->version);
isc_mem_attach(mctx, &dctx->mctx);
- dctx->references = 1;
+
+ isc_refcount_init(&dctx->references, 1);
dctx->magic = DNS_DCTX_MAGIC;
*dctxp = dctx;
return (ISC_R_SUCCESS);