From 7b9084d45d7af50d55fc40c99494eeb22c0928d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Thu, 4 Jul 2019 11:04:52 +0200 Subject: [PATCH] Use atomic_bool for variables shared among threads to make zt_test.c thread-safe --- lib/dns/tests/zt_test.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/dns/tests/zt_test.c b/lib/dns/tests/zt_test.c index ede2977341..77d36a94f2 100644 --- a/lib/dns/tests/zt_test.c +++ b/lib/dns/tests/zt_test.c @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -79,21 +80,21 @@ count_zone(dns_zone_t *zone, void *uap) { static isc_result_t 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 */ - bool *done = (bool *) zt; + atomic_bool *done = (atomic_bool *) zt; UNUSED(zone); UNUSED(task); - *done = true; + atomic_store(done, true); isc_app_shutdown(); return (ISC_R_SUCCESS); } static isc_result_t all_done(void *arg) { - bool *done = (bool *) arg; + atomic_bool *done = (atomic_bool *) arg; - *done = true; + atomic_store(done, true); isc_app_shutdown(); return (ISC_R_SUCCESS); } @@ -164,7 +165,7 @@ asyncload_zone(void **state) { dns_db_t *db = NULL; FILE* zonefile, *origfile; char buf[4096]; - bool done = false; + atomic_bool done = ATOMIC_VAR_INIT(false); int i = 0; struct args args; @@ -182,7 +183,7 @@ asyncload_zone(void **state) { assert_non_null(view->zonetable); assert_false(dns__zone_loadpending(zone)); - assert_false(done); + assert_false(atomic_load(&done)); zonefile = fopen("./zone.data", "wb"); assert_non_null(zonefile); origfile = fopen("./testdata/zt/zone1.db", "r+b"); @@ -203,7 +204,7 @@ asyncload_zone(void **state) { isc_app_run(); while (dns__zone_loadpending(zone) && i++ < 5000) dns_test_nap(1000); - assert_true(done); + assert_true(atomic_load(&done)); /* The zone should now be loaded; test it */ result = dns_zone_getdb(zone, &db); assert_int_equal(result, ISC_R_SUCCESS); @@ -225,7 +226,7 @@ asyncload_zone(void **state) { while (dns__zone_loadpending(zone) && i++ < 5000) dns_test_nap(1000); - assert_true(done); + assert_true(atomic_load(&done)); /* The zone should now be loaded; test it */ result = dns_zone_getdb(zone, &db); assert_int_equal(result, ISC_R_SUCCESS); @@ -241,7 +242,7 @@ asyncload_zone(void **state) { while (dns__zone_loadpending(zone) && i++ < 5000) dns_test_nap(1000); - assert_true(done); + assert_true(atomic_load(&done)); /* The zone should now be loaded; test it */ result = dns_zone_getdb(zone, &db); assert_int_equal(result, ISC_R_SUCCESS); @@ -265,7 +266,7 @@ asyncload_zt(void **state) { dns_view_t *view; dns_zt_t *zt = NULL; dns_db_t *db = NULL; - bool done = false; + atomic_bool done = ATOMIC_VAR_INIT(false); int i = 0; struct args args; @@ -302,16 +303,17 @@ asyncload_zt(void **state) { assert_false(dns__zone_loadpending(zone1)); assert_false(dns__zone_loadpending(zone2)); - assert_false(done); + assert_false(atomic_load(&done)); args.arg1 = zt; args.arg2 = &done; isc_app_onrun(dt_mctx, maintask, start_zt_asyncload, &args); isc_app_run(); - while (!done && i++ < 5000) + while (!atomic_load(&done) && i++ < 5000) { dns_test_nap(1000); - assert_true(done); + } + assert_true(atomic_load(&done)); /* Both zones should now be loaded; test them */ result = dns_zone_getdb(zone1, &db);