From 55a3217fe789c5b6c19b2136dbf38ad3f549eeef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Mon, 1 Jul 2019 15:19:29 +0200 Subject: [PATCH] Convert global variables to atomic to prevent possible data race --- lib/dns/tests/dispatch_test.c | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/lib/dns/tests/dispatch_test.c b/lib/dns/tests/dispatch_test.c index 9785fbfbae8..3467fa69239 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. -- GitLab