Commit 7b9084d4 authored by Ondřej Surý's avatar Ondřej Surý
Browse files

Use atomic_bool for variables shared among threads to make zt_test.c thread-safe

parent 6858ef9a
Pipeline #25717 failed with stages
in 50 minutes and 44 seconds
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include <cmocka.h> #include <cmocka.h>
#include <isc/app.h> #include <isc/app.h>
#include <isc/atomic.h>
#include <isc/buffer.h> #include <isc/buffer.h>
#include <isc/print.h> #include <isc/print.h>
#include <isc/task.h> #include <isc/task.h>
...@@ -79,21 +80,21 @@ count_zone(dns_zone_t *zone, void *uap) { ...@@ -79,21 +80,21 @@ count_zone(dns_zone_t *zone, void *uap) {
static isc_result_t static isc_result_t
load_done(dns_zt_t *zt, dns_zone_t *zone, isc_task_t *task) { load_done(dns_zt_t *zt, dns_zone_t *zone, isc_task_t *task) {
/* We treat zt as a pointer to a boolean for testing purposes */ /* We treat zt as a pointer to a boolean for testing purposes */
bool *done = (bool *) zt; atomic_bool *done = (atomic_bool *) zt;
UNUSED(zone); UNUSED(zone);
UNUSED(task); UNUSED(task);
*done = true; atomic_store(done, true);
isc_app_shutdown(); isc_app_shutdown();
return (ISC_R_SUCCESS); return (ISC_R_SUCCESS);
} }
static isc_result_t static isc_result_t
all_done(void *arg) { all_done(void *arg) {
bool *done = (bool *) arg; atomic_bool *done = (atomic_bool *) arg;
*done = true; atomic_store(done, true);
isc_app_shutdown(); isc_app_shutdown();
return (ISC_R_SUCCESS); return (ISC_R_SUCCESS);
} }
...@@ -164,7 +165,7 @@ asyncload_zone(void **state) { ...@@ -164,7 +165,7 @@ asyncload_zone(void **state) {
dns_db_t *db = NULL; dns_db_t *db = NULL;
FILE* zonefile, *origfile; FILE* zonefile, *origfile;
char buf[4096]; char buf[4096];
bool done = false; atomic_bool done = ATOMIC_VAR_INIT(false);
int i = 0; int i = 0;
struct args args; struct args args;
...@@ -182,7 +183,7 @@ asyncload_zone(void **state) { ...@@ -182,7 +183,7 @@ asyncload_zone(void **state) {
assert_non_null(view->zonetable); assert_non_null(view->zonetable);
assert_false(dns__zone_loadpending(zone)); assert_false(dns__zone_loadpending(zone));
assert_false(done); assert_false(atomic_load(&done));
zonefile = fopen("./zone.data", "wb"); zonefile = fopen("./zone.data", "wb");
assert_non_null(zonefile); assert_non_null(zonefile);
origfile = fopen("./testdata/zt/zone1.db", "r+b"); origfile = fopen("./testdata/zt/zone1.db", "r+b");
...@@ -203,7 +204,7 @@ asyncload_zone(void **state) { ...@@ -203,7 +204,7 @@ asyncload_zone(void **state) {
isc_app_run(); isc_app_run();
while (dns__zone_loadpending(zone) && i++ < 5000) while (dns__zone_loadpending(zone) && i++ < 5000)
dns_test_nap(1000); dns_test_nap(1000);
assert_true(done); assert_true(atomic_load(&done));
/* The zone should now be loaded; test it */ /* The zone should now be loaded; test it */
result = dns_zone_getdb(zone, &db); result = dns_zone_getdb(zone, &db);
assert_int_equal(result, ISC_R_SUCCESS); assert_int_equal(result, ISC_R_SUCCESS);
...@@ -225,7 +226,7 @@ asyncload_zone(void **state) { ...@@ -225,7 +226,7 @@ asyncload_zone(void **state) {
while (dns__zone_loadpending(zone) && i++ < 5000) while (dns__zone_loadpending(zone) && i++ < 5000)
dns_test_nap(1000); dns_test_nap(1000);
assert_true(done); assert_true(atomic_load(&done));
/* The zone should now be loaded; test it */ /* The zone should now be loaded; test it */
result = dns_zone_getdb(zone, &db); result = dns_zone_getdb(zone, &db);
assert_int_equal(result, ISC_R_SUCCESS); assert_int_equal(result, ISC_R_SUCCESS);
...@@ -241,7 +242,7 @@ asyncload_zone(void **state) { ...@@ -241,7 +242,7 @@ asyncload_zone(void **state) {
while (dns__zone_loadpending(zone) && i++ < 5000) while (dns__zone_loadpending(zone) && i++ < 5000)
dns_test_nap(1000); dns_test_nap(1000);
assert_true(done); assert_true(atomic_load(&done));
/* The zone should now be loaded; test it */ /* The zone should now be loaded; test it */
result = dns_zone_getdb(zone, &db); result = dns_zone_getdb(zone, &db);
assert_int_equal(result, ISC_R_SUCCESS); assert_int_equal(result, ISC_R_SUCCESS);
...@@ -265,7 +266,7 @@ asyncload_zt(void **state) { ...@@ -265,7 +266,7 @@ asyncload_zt(void **state) {
dns_view_t *view; dns_view_t *view;
dns_zt_t *zt = NULL; dns_zt_t *zt = NULL;
dns_db_t *db = NULL; dns_db_t *db = NULL;
bool done = false; atomic_bool done = ATOMIC_VAR_INIT(false);
int i = 0; int i = 0;
struct args args; struct args args;
...@@ -302,16 +303,17 @@ asyncload_zt(void **state) { ...@@ -302,16 +303,17 @@ asyncload_zt(void **state) {
assert_false(dns__zone_loadpending(zone1)); assert_false(dns__zone_loadpending(zone1));
assert_false(dns__zone_loadpending(zone2)); assert_false(dns__zone_loadpending(zone2));
assert_false(done); assert_false(atomic_load(&done));
args.arg1 = zt; args.arg1 = zt;
args.arg2 = &done; args.arg2 = &done;
isc_app_onrun(dt_mctx, maintask, start_zt_asyncload, &args); isc_app_onrun(dt_mctx, maintask, start_zt_asyncload, &args);
isc_app_run(); isc_app_run();
while (!done && i++ < 5000) while (!atomic_load(&done) && i++ < 5000) {
dns_test_nap(1000); dns_test_nap(1000);
assert_true(done); }
assert_true(atomic_load(&done));
/* Both zones should now be loaded; test them */ /* Both zones should now be loaded; test them */
result = dns_zone_getdb(zone1, &db); result = dns_zone_getdb(zone1, &db);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment