mirror of
https://gitlab.isc.org/isc-projects/bind9
synced 2025-08-31 14:35:26 +00:00
Convert all variables accessed between multiple threads to atomic
This commit is contained in:
@@ -23,6 +23,7 @@
|
|||||||
#define UNIT_TESTING
|
#define UNIT_TESTING
|
||||||
#include <cmocka.h>
|
#include <cmocka.h>
|
||||||
|
|
||||||
|
#include <isc/atomic.h>
|
||||||
#include <isc/condition.h>
|
#include <isc/condition.h>
|
||||||
#include <isc/commandline.h>
|
#include <isc/commandline.h>
|
||||||
#include <isc/mem.h>
|
#include <isc/mem.h>
|
||||||
@@ -48,7 +49,7 @@ static isc_time_t endtime;
|
|||||||
static isc_time_t lasttime;
|
static isc_time_t lasttime;
|
||||||
static int seconds;
|
static int seconds;
|
||||||
static int nanoseconds;
|
static int nanoseconds;
|
||||||
static int eventcnt;
|
static atomic_int_fast32_t eventcnt;
|
||||||
static int nevents;
|
static int nevents;
|
||||||
|
|
||||||
static int
|
static int
|
||||||
@@ -102,7 +103,7 @@ setup_test(isc_timertype_t timertype, isc_time_t *expires,
|
|||||||
isc_result_t result;
|
isc_result_t result;
|
||||||
isc_task_t *task = NULL;
|
isc_task_t *task = NULL;
|
||||||
isc_time_settoepoch(&endtime);
|
isc_time_settoepoch(&endtime);
|
||||||
eventcnt = 0;
|
atomic_init(&eventcnt, 0);
|
||||||
|
|
||||||
isc_mutex_init(&mx);
|
isc_mutex_init(&mx);
|
||||||
|
|
||||||
@@ -127,7 +128,7 @@ setup_test(isc_timertype_t timertype, isc_time_t *expires,
|
|||||||
/*
|
/*
|
||||||
* Wait for shutdown processing to complete.
|
* Wait for shutdown processing to complete.
|
||||||
*/
|
*/
|
||||||
while (eventcnt != nevents) {
|
while (atomic_load(&eventcnt) != nevents) {
|
||||||
result = isc_condition_wait(&cv, &mx);
|
result = isc_condition_wait(&cv, &mx);
|
||||||
assert_int_equal(result, ISC_R_SUCCESS);
|
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_interval_t interval;
|
||||||
isc_eventtype_t expected_event_type;
|
isc_eventtype_t expected_event_type;
|
||||||
|
|
||||||
++eventcnt;
|
int tick = atomic_fetch_add(&eventcnt, 1);
|
||||||
|
|
||||||
if (verbose) {
|
if (verbose) {
|
||||||
print_message("# tick %d\n", eventcnt);
|
print_message("# tick %d\n", tick);
|
||||||
}
|
}
|
||||||
|
|
||||||
expected_event_type = ISC_TIMEREVENT_LIFE;
|
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);
|
assert_true(isc_time_compare(&ulim, &now) >= 0);
|
||||||
lasttime = now;
|
lasttime = now;
|
||||||
|
|
||||||
if (eventcnt == nevents) {
|
if (atomic_load(&eventcnt) == nevents) {
|
||||||
result = isc_time_now(&endtime);
|
result = isc_time_now(&endtime);
|
||||||
assert_int_equal(result, ISC_R_SUCCESS);
|
assert_int_equal(result, ISC_R_SUCCESS);
|
||||||
isc_timer_detach(&timer);
|
isc_timer_detach(&timer);
|
||||||
@@ -246,10 +247,10 @@ test_idle(isc_task_t *task, isc_event_t *event) {
|
|||||||
isc_time_t llim;
|
isc_time_t llim;
|
||||||
isc_interval_t interval;
|
isc_interval_t interval;
|
||||||
|
|
||||||
++eventcnt;
|
int tick = atomic_fetch_add(&eventcnt, 1);
|
||||||
|
|
||||||
if (verbose) {
|
if (verbose) {
|
||||||
print_message("# tick %d\n", eventcnt);
|
print_message("# tick %d\n", tick);
|
||||||
}
|
}
|
||||||
|
|
||||||
result = isc_time_now(&now);
|
result = isc_time_now(&now);
|
||||||
@@ -310,10 +311,10 @@ test_reset(isc_task_t *task, isc_event_t *event) {
|
|||||||
isc_time_t expires;
|
isc_time_t expires;
|
||||||
isc_interval_t interval;
|
isc_interval_t interval;
|
||||||
|
|
||||||
++eventcnt;
|
int tick = atomic_fetch_add(&eventcnt, 1);
|
||||||
|
|
||||||
if (verbose) {
|
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);
|
assert_true(isc_time_compare(&ulim, &now) >= 0);
|
||||||
lasttime = now;
|
lasttime = now;
|
||||||
|
|
||||||
if (eventcnt < 3) {
|
int _eventcnt = atomic_load(&eventcnt);
|
||||||
|
|
||||||
|
if (_eventcnt < 3) {
|
||||||
assert_int_equal(event->ev_type, ISC_TIMEREVENT_TICK);
|
assert_int_equal(event->ev_type, ISC_TIMEREVENT_TICK);
|
||||||
|
|
||||||
if (eventcnt == 2) {
|
if (_eventcnt == 2) {
|
||||||
isc_interval_set(&interval, seconds, nanoseconds);
|
isc_interval_set(&interval, seconds, nanoseconds);
|
||||||
result = isc_time_nowplusinterval(&expires, &interval);
|
result = isc_time_nowplusinterval(&expires, &interval);
|
||||||
assert_int_equal(result, ISC_R_SUCCESS);
|
assert_int_equal(result, ISC_R_SUCCESS);
|
||||||
@@ -416,16 +419,16 @@ tick_event(isc_task_t *task, isc_event_t *event) {
|
|||||||
|
|
||||||
UNUSED(task);
|
UNUSED(task);
|
||||||
|
|
||||||
++eventcnt;
|
int tick = atomic_fetch_add(&eventcnt, 1);
|
||||||
if (verbose) {
|
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
|
* On the first tick, purge all remaining tick events
|
||||||
* and then shut down the task.
|
* and then shut down the task.
|
||||||
*/
|
*/
|
||||||
if (eventcnt == 1) {
|
if (tick == 0) {
|
||||||
isc_time_settoepoch(&expires);
|
isc_time_settoepoch(&expires);
|
||||||
isc_interval_set(&interval, seconds, 0);
|
isc_interval_set(&interval, seconds, 0);
|
||||||
result = isc_timer_reset(tickertimer, isc_timertype_ticker,
|
result = isc_timer_reset(tickertimer, isc_timertype_ticker,
|
||||||
@@ -496,7 +499,7 @@ purge(void **state) {
|
|||||||
|
|
||||||
startflag = 0;
|
startflag = 0;
|
||||||
shutdownflag = 0;
|
shutdownflag = 0;
|
||||||
eventcnt = 0;
|
atomic_init(&eventcnt, 0);
|
||||||
seconds = 1;
|
seconds = 1;
|
||||||
nanoseconds = 0;
|
nanoseconds = 0;
|
||||||
|
|
||||||
@@ -551,7 +554,7 @@ purge(void **state) {
|
|||||||
|
|
||||||
UNLOCK(&mx);
|
UNLOCK(&mx);
|
||||||
|
|
||||||
assert_int_equal(eventcnt, 1);
|
assert_int_equal(atomic_load(&eventcnt), 1);
|
||||||
|
|
||||||
isc_timer_detach(&tickertimer);
|
isc_timer_detach(&tickertimer);
|
||||||
isc_timer_detach(&oncetimer);
|
isc_timer_detach(&oncetimer);
|
||||||
|
Reference in New Issue
Block a user