From ef53ded0574d1200ca8b5325ae88555f0fdfca30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Mon, 22 Jan 2018 09:18:58 +0200 Subject: [PATCH] Fix test_atomic As the function documentation states, the expected value must be read again after a call to atomic_cas_ptr. This is due to the fact that if the values are not the same, the __atomic builtin version will store the current value into the expected value. The new value given to the atomic_cas_ptr function was the address of the new value, not the new value itself. The behavior of the atomic_cas_ptr is what caused the test to pass on systeems that implement the __atomic builtins. On older systems that do not implement it, the expected value was never modified which caused the test to hang. --- server/core/test/test_atomic.cc | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/server/core/test/test_atomic.cc b/server/core/test/test_atomic.cc index 35786d52b..1e5542136 100644 --- a/server/core/test/test_atomic.cc +++ b/server/core/test/test_atomic.cc @@ -54,18 +54,20 @@ static void* cas_dest = (void*)1; void test_cas(void* data) { - int id = (size_t)data; + size_t id = (size_t)data - 1; static int loops = 0; while (atomic_load_int32(&running)) { - intptr_t my_value = (id + 1) % NTHR; - intptr_t my_expected = id; + void* my_value; + void* my_expected; - while (!atomic_cas_ptr(&cas_dest, (void**)&my_expected, (void*)&my_value)) + do { - ; + my_value = (void*)((id + 1) % NTHR); + my_expected = (void*)id; } + while (!atomic_cas_ptr(&cas_dest, &my_expected, my_value)); loops++; } @@ -103,8 +105,11 @@ int main(int argc, char** argv) { int rval = 0; + printf("test_load_store\n"); run_test(test_load_store); + printf("test_add\n"); run_test(test_add); + printf("test_cas\n"); run_test(test_cas); return rval;