test: fences: add simple, semi-useful fence tests
authorBrian Norris <banorris@uci.edu>
Fri, 22 Feb 2013 07:18:18 +0000 (23:18 -0800)
committerBrian Norris <banorris@uci.edu>
Fri, 22 Feb 2013 17:43:42 +0000 (09:43 -0800)
Note that due to the quirks discovered in double-read-fv, we don't see
all the expected behaviors in the fences2 test.

test/fences.c [new file with mode: 0644]
test/fences2.c [new file with mode: 0644]

diff --git a/test/fences.c b/test/fences.c
new file mode 100644 (file)
index 0000000..4d0328f
--- /dev/null
@@ -0,0 +1,42 @@
+#include <stdio.h>
+#include <threads.h>
+#include <stdatomic.h>
+
+#include "librace.h"
+
+atomic_int x;
+atomic_int y;
+
+static void a(void *obj)
+{
+       atomic_store_explicit(&x, 1, memory_order_relaxed);
+       atomic_store_explicit(&x, 2, memory_order_relaxed);
+       atomic_thread_fence(memory_order_seq_cst);
+       printf("Thread A reads: %d\n", atomic_load_explicit(&y, memory_order_relaxed));
+}
+
+static void b(void *obj)
+{
+       atomic_store_explicit(&y, 1, memory_order_relaxed);
+       atomic_store_explicit(&y, 2, memory_order_relaxed);
+       atomic_thread_fence(memory_order_seq_cst);
+       printf("Thread B reads: %d\n", atomic_load_explicit(&x, memory_order_relaxed));
+}
+
+int user_main(int argc, char **argv)
+{
+       thrd_t t1, t2;
+
+       atomic_init(&x, 0);
+       atomic_init(&y, 0);
+
+       printf("Main thread: creating 2 threads\n");
+       thrd_create(&t1, (thrd_start_t)&a, NULL);
+       thrd_create(&t2, (thrd_start_t)&b, NULL);
+
+       thrd_join(t1);
+       thrd_join(t2);
+       printf("Main thread is finishing\n");
+
+       return 0;
+}
diff --git a/test/fences2.c b/test/fences2.c
new file mode 100644 (file)
index 0000000..2c80d61
--- /dev/null
@@ -0,0 +1,46 @@
+#include <stdio.h>
+#include <threads.h>
+#include <stdatomic.h>
+
+#include "librace.h"
+#include "model-assert.h"
+
+atomic_int x;
+atomic_int y;
+
+static void a(void *obj)
+{
+       atomic_store_explicit(&x, 1, memory_order_relaxed);
+       atomic_thread_fence(memory_order_release);
+       atomic_store_explicit(&x, 2, memory_order_relaxed);
+}
+
+static void b(void *obj)
+{
+       int r1, r2;
+       r1 = atomic_load_explicit(&x, memory_order_relaxed);
+       atomic_thread_fence(memory_order_acquire);
+       r2 = atomic_load_explicit(&x, memory_order_relaxed);
+
+       printf("FENCES: r1 = %d, r2 = %d\n", r1, r2);
+       if (r1 == 2)
+               MODEL_ASSERT(r2 != 1);
+}
+
+int user_main(int argc, char **argv)
+{
+       thrd_t t1, t2;
+
+       atomic_init(&x, 0);
+       atomic_init(&y, 0);
+
+       printf("Main thread: creating 2 threads\n");
+       thrd_create(&t1, (thrd_start_t)&a, NULL);
+       thrd_create(&t2, (thrd_start_t)&b, NULL);
+
+       thrd_join(t1);
+       thrd_join(t2);
+       printf("Main thread is finishing\n");
+
+       return 0;
+}