summary refs log tree commit diff
path: root/csu
diff options
context:
space:
mode:
authorTorvald Riegel <triegel@redhat.com>2014-10-20 20:25:40 +0200
committerTorvald Riegel <triegel@redhat.com>2014-11-20 11:59:52 +0100
commit1eccfecd40a6b7e228154e4c616902e22ca10c41 (patch)
tree04dd29a0204426baae51f7bfd04af2cc74039016 /csu
parentff8714269c9312d9164456279a56b6f6c47e2771 (diff)
downloadglibc-1eccfecd40a6b7e228154e4c616902e22ca10c41.tar.gz
glibc-1eccfecd40a6b7e228154e4c616902e22ca10c41.tar.xz
glibc-1eccfecd40a6b7e228154e4c616902e22ca10c41.zip
Add tests for C11-like atomic operations.
Diffstat (limited to 'csu')
-rw-r--r--csu/tst-atomic.c130
1 files changed, 129 insertions, 1 deletions
diff --git a/csu/tst-atomic.c b/csu/tst-atomic.c
index d16c66dc31..c6e786d92e 100644
--- a/csu/tst-atomic.c
+++ b/csu/tst-atomic.c
@@ -28,7 +28,7 @@
 static int
 do_test (void)
 {
-  atomic_t mem;
+  atomic_t mem, expected;
   int ret = 0;
 
 #ifdef atomic_compare_and_exchange_val_acq
@@ -489,6 +489,134 @@ do_test (void)
       ret = 1;
     }
 
+  /* Tests for C11-like atomics.  */
+  mem = 11;
+  if (atomic_load_relaxed (&mem) != 11 || atomic_load_acquire (&mem) != 11)
+    {
+      puts ("atomic_load_{relaxed,acquire} test failed");
+      ret = 1;
+    }
+
+  atomic_store_relaxed (&mem, 12);
+  if (mem != 12)
+    {
+      puts ("atomic_store_relaxed test failed");
+      ret = 1;
+    }
+  atomic_store_release (&mem, 13);
+  if (mem != 13)
+    {
+      puts ("atomic_store_release test failed");
+      ret = 1;
+    }
+
+  mem = 14;
+  expected = 14;
+  if (!atomic_compare_exchange_weak_relaxed (&mem, &expected, 25)
+      || mem != 25 || expected != 14)
+    {
+      puts ("atomic_compare_exchange_weak_relaxed test 1 failed");
+      ret = 1;
+    }
+  if (atomic_compare_exchange_weak_relaxed (&mem, &expected, 14)
+      || mem != 25 || expected != 25)
+    {
+      puts ("atomic_compare_exchange_weak_relaxed test 2 failed");
+      ret = 1;
+    }
+  mem = 14;
+  expected = 14;
+  if (!atomic_compare_exchange_weak_acquire (&mem, &expected, 25)
+      || mem != 25 || expected != 14)
+    {
+      puts ("atomic_compare_exchange_weak_acquire test 1 failed");
+      ret = 1;
+    }
+  if (atomic_compare_exchange_weak_acquire (&mem, &expected, 14)
+      || mem != 25 || expected != 25)
+    {
+      puts ("atomic_compare_exchange_weak_acquire test 2 failed");
+      ret = 1;
+    }
+  mem = 14;
+  expected = 14;
+  if (!atomic_compare_exchange_weak_release (&mem, &expected, 25)
+      || mem != 25 || expected != 14)
+    {
+      puts ("atomic_compare_exchange_weak_release test 1 failed");
+      ret = 1;
+    }
+  if (atomic_compare_exchange_weak_release (&mem, &expected, 14)
+      || mem != 25 || expected != 25)
+    {
+      puts ("atomic_compare_exchange_weak_release test 2 failed");
+      ret = 1;
+    }
+
+  mem = 23;
+  if (atomic_exchange_acquire (&mem, 42) != 23 || mem != 42)
+    {
+      puts ("atomic_exchange_acquire test failed");
+      ret = 1;
+    }
+  mem = 23;
+  if (atomic_exchange_release (&mem, 42) != 23 || mem != 42)
+    {
+      puts ("atomic_exchange_release test failed");
+      ret = 1;
+    }
+
+  mem = 23;
+  if (atomic_fetch_add_relaxed (&mem, 1) != 23 || mem != 24)
+    {
+      puts ("atomic_fetch_add_relaxed test failed");
+      ret = 1;
+    }
+  mem = 23;
+  if (atomic_fetch_add_acquire (&mem, 1) != 23 || mem != 24)
+    {
+      puts ("atomic_fetch_add_acquire test failed");
+      ret = 1;
+    }
+  mem = 23;
+  if (atomic_fetch_add_release (&mem, 1) != 23 || mem != 24)
+    {
+      puts ("atomic_fetch_add_release test failed");
+      ret = 1;
+    }
+  mem = 23;
+  if (atomic_fetch_add_acq_rel (&mem, 1) != 23 || mem != 24)
+    {
+      puts ("atomic_fetch_add_acq_rel test failed");
+      ret = 1;
+    }
+
+  mem = 3;
+  if (atomic_fetch_and_acquire (&mem, 2) != 3 || mem != 2)
+    {
+      puts ("atomic_fetch_and_acquire test failed");
+      ret = 1;
+    }
+
+  mem = 4;
+  if (atomic_fetch_or_relaxed (&mem, 2) != 4 || mem != 6)
+    {
+      puts ("atomic_fetch_or_relaxed test failed");
+      ret = 1;
+    }
+  mem = 4;
+  if (atomic_fetch_or_acquire (&mem, 2) != 4 || mem != 6)
+    {
+      puts ("atomic_fetch_or_acquire test failed");
+      ret = 1;
+    }
+
+  /* This is a single-threaded test, so we can't test the effects of the
+     fences.  */
+  atomic_thread_fence_acquire ();
+  atomic_thread_fence_release ();
+  atomic_thread_fence_seq_cst ();
+
   return ret;
 }