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.
This commit is contained in:
Markus Mäkelä
2018-01-22 09:18:58 +02:00
parent ccebf38bc0
commit ef53ded057

View File

@ -54,18 +54,20 @@ static void* cas_dest = (void*)1;
void test_cas(void* data) void test_cas(void* data)
{ {
int id = (size_t)data; size_t id = (size_t)data - 1;
static int loops = 0; static int loops = 0;
while (atomic_load_int32(&running)) while (atomic_load_int32(&running))
{ {
intptr_t my_value = (id + 1) % NTHR; void* my_value;
intptr_t my_expected = id; 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++; loops++;
} }
@ -103,8 +105,11 @@ int main(int argc, char** argv)
{ {
int rval = 0; int rval = 0;
printf("test_load_store\n");
run_test(test_load_store); run_test(test_load_store);
printf("test_add\n");
run_test(test_add); run_test(test_add);
printf("test_cas\n");
run_test(test_cas); run_test(test_cas);
return rval; return rval;