Convert global variables to atomic to prevent possible data race

This commit is contained in:
Ondřej Surý 2019-07-01 15:19:29 +02:00
parent 2aec2e6425
commit 55a3217fe7

View file

@ -27,6 +27,7 @@
#include <isc/app.h>
#include <isc/buffer.h>
#include <isc/refcount.h>
#include <isc/socket.h>
#include <isc/task.h>
#include <isc/timer.h>
@ -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, &region, 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.