2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-30 05:57:52 +00:00

Convert all variables accessed between multiple threads to atomic

This commit is contained in:
Ondřej Surý 2019-07-04 14:21:15 +02:00
parent 3a0a69a9cd
commit e06a34674a

View File

@ -23,6 +23,7 @@
#define UNIT_TESTING
#include <cmocka.h>
#include <isc/atomic.h>
#include <isc/condition.h>
#include <isc/commandline.h>
#include <isc/mem.h>
@ -48,7 +49,7 @@ static isc_time_t endtime;
static isc_time_t lasttime;
static int seconds;
static int nanoseconds;
static int eventcnt;
static atomic_int_fast32_t eventcnt;
static int nevents;
static int
@ -102,7 +103,7 @@ setup_test(isc_timertype_t timertype, isc_time_t *expires,
isc_result_t result;
isc_task_t *task = NULL;
isc_time_settoepoch(&endtime);
eventcnt = 0;
atomic_init(&eventcnt, 0);
isc_mutex_init(&mx);
@ -127,7 +128,7 @@ setup_test(isc_timertype_t timertype, isc_time_t *expires,
/*
* Wait for shutdown processing to complete.
*/
while (eventcnt != nevents) {
while (atomic_load(&eventcnt) != nevents) {
result = isc_condition_wait(&cv, &mx);
assert_int_equal(result, ISC_R_SUCCESS);
}
@ -149,10 +150,10 @@ ticktock(isc_task_t *task, isc_event_t *event) {
isc_interval_t interval;
isc_eventtype_t expected_event_type;
++eventcnt;
int tick = atomic_fetch_add(&eventcnt, 1);
if (verbose) {
print_message("# tick %d\n", eventcnt);
print_message("# tick %d\n", tick);
}
expected_event_type = ISC_TIMEREVENT_LIFE;
@ -183,7 +184,7 @@ ticktock(isc_task_t *task, isc_event_t *event) {
assert_true(isc_time_compare(&ulim, &now) >= 0);
lasttime = now;
if (eventcnt == nevents) {
if (atomic_load(&eventcnt) == nevents) {
result = isc_time_now(&endtime);
assert_int_equal(result, ISC_R_SUCCESS);
isc_timer_detach(&timer);
@ -246,10 +247,10 @@ test_idle(isc_task_t *task, isc_event_t *event) {
isc_time_t llim;
isc_interval_t interval;
++eventcnt;
int tick = atomic_fetch_add(&eventcnt, 1);
if (verbose) {
print_message("# tick %d\n", eventcnt);
print_message("# tick %d\n", tick);
}
result = isc_time_now(&now);
@ -310,10 +311,10 @@ test_reset(isc_task_t *task, isc_event_t *event) {
isc_time_t expires;
isc_interval_t interval;
++eventcnt;
int tick = atomic_fetch_add(&eventcnt, 1);
if (verbose) {
print_message("# tick %d\n", eventcnt);
print_message("# tick %d\n", tick);
}
/*
@ -338,10 +339,12 @@ test_reset(isc_task_t *task, isc_event_t *event) {
assert_true(isc_time_compare(&ulim, &now) >= 0);
lasttime = now;
if (eventcnt < 3) {
int _eventcnt = atomic_load(&eventcnt);
if (_eventcnt < 3) {
assert_int_equal(event->ev_type, ISC_TIMEREVENT_TICK);
if (eventcnt == 2) {
if (_eventcnt == 2) {
isc_interval_set(&interval, seconds, nanoseconds);
result = isc_time_nowplusinterval(&expires, &interval);
assert_int_equal(result, ISC_R_SUCCESS);
@ -416,16 +419,16 @@ tick_event(isc_task_t *task, isc_event_t *event) {
UNUSED(task);
++eventcnt;
int tick = atomic_fetch_add(&eventcnt, 1);
if (verbose) {
print_message("# tick_event %d\n", eventcnt);
print_message("# tick_event %d\n", tick);
}
/*
* On the first tick, purge all remaining tick events
* and then shut down the task.
*/
if (eventcnt == 1) {
if (tick == 0) {
isc_time_settoepoch(&expires);
isc_interval_set(&interval, seconds, 0);
result = isc_timer_reset(tickertimer, isc_timertype_ticker,
@ -496,7 +499,7 @@ purge(void **state) {
startflag = 0;
shutdownflag = 0;
eventcnt = 0;
atomic_init(&eventcnt, 0);
seconds = 1;
nanoseconds = 0;
@ -551,7 +554,7 @@ purge(void **state) {
UNLOCK(&mx);
assert_int_equal(eventcnt, 1);
assert_int_equal(atomic_load(&eventcnt), 1);
isc_timer_detach(&tickertimer);
isc_timer_detach(&oncetimer);