diff --git a/lib/dns/tests/dispatch_test.c b/lib/dns/tests/dispatch_test.c index 9785fbfbae8caf529179d7c3645b1cd441b4cce5..3467fa6923949d32c4f1024631408a9d7d15db4f 100644 --- a/lib/dns/tests/dispatch_test.c +++ b/lib/dns/tests/dispatch_test.c @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -212,27 +213,20 @@ nameserver(isc_task_t *task, isc_event_t *event) { static dns_dispatch_t *dispatch = NULL; static dns_dispentry_t *dispentry = NULL; -static bool first = true; -static isc_mutex_t lock; +static atomic_bool first = ATOMIC_VAR_INIT(true); static isc_sockaddr_t local; -static unsigned int responses = 0; +static isc_refcount_t responses; static void response(isc_task_t *task, isc_event_t *event) { dns_dispatchevent_t *devent = (dns_dispatchevent_t *)event; - isc_result_t result; - bool wasfirst; + bool exp_true = true; UNUSED(task); - LOCK(&lock); - wasfirst = first; - first = false; - responses++; - UNLOCK(&lock); - - if (wasfirst) { - result = dns_dispatch_getnext(dispentry, &devent); + isc_refcount_increment(&responses); + if (atomic_compare_exchange_strong(&first, &exp_true, false)) { + isc_result_t result = dns_dispatch_getnext(dispentry, &devent); assert_int_equal(result, ISC_R_SUCCESS); } else { dns_dispatch_removeresponse(&dispentry, &devent); @@ -267,7 +261,7 @@ dispatch_getnext(void **state) { UNUSED(state); - isc_mutex_init(&lock); + isc_refcount_init(&responses, 0); result = isc_task_create(taskmgr, 0, &task); assert_int_equal(result, ISC_R_SUCCESS); @@ -298,7 +292,6 @@ dispatch_getnext(void **state) { result = isc_socket_getsockname(sock, &local); assert_int_equal(result, ISC_R_SUCCESS); - first = true; region.base = rbuf; region.length = sizeof(rbuf); result = isc_socket_recv(sock, ®ion, 1, task, nameserver, sock); @@ -320,7 +313,7 @@ dispatch_getnext(void **state) { result = isc_app_run(); assert_int_equal(result, ISC_R_SUCCESS); - assert_int_equal(responses, 2); + assert_int_equal(isc_refcount_current(&responses), 2); /* * Shutdown nameserver.