From c77e68595cf28639352e548241f648988641fc8d Mon Sep 17 00:00:00 2001 From: Brian Norris Date: Tue, 16 Jul 2013 19:22:52 -0700 Subject: [PATCH] test: insanesync: convert to C++ The syntax is clearer this way, and I can make it more consistent with the other satisfaction cycle examples I wrote. --- test/{insanesync.c => insanesync.cc} | 40 +++++++++++++++------------- 1 file changed, 21 insertions(+), 19 deletions(-) rename test/{insanesync.c => insanesync.cc} (51%) diff --git a/test/insanesync.c b/test/insanesync.cc similarity index 51% rename from test/insanesync.c rename to test/insanesync.cc index 2f446dd..57c610e 100644 --- a/test/insanesync.c +++ b/test/insanesync.cc @@ -1,22 +1,26 @@ #include #include #include -#include +#include #include "librace.h" #include "model-assert.h" +using namespace std; + atomic_int x; atomic_int y; atomic_intptr_t z; atomic_intptr_t z2; -/** +int r1, r2, r3; /* "local" variables */ + +/** This example illustrates a self-satisfying cycle involving synchronization. A failed synchronization creates the store that - causes the synchronization to fail. - - The C++11 memory model nominally allows t1=0, t2=1, t3=5. + causes the synchronization to fail. + + The C++11 memory model nominally allows r1=0, r2=1, r3=5. This example is insane, we don't support that behavior. */ @@ -24,33 +28,30 @@ atomic_intptr_t z2; static void a(void *obj) { - atomic_store_explicit(&z, (intptr_t) &y, memory_order_relaxed); - int t1=atomic_fetch_add_explicit(&y, 1, memory_order_release); - atomic_store_explicit(&z, (intptr_t) &x, memory_order_relaxed); - int t2=atomic_fetch_add_explicit(&y, 1, memory_order_release); - printf("t1=%d, t2=%d\n", t1, t2); + z.store((intptr_t)&y, memory_order_relaxed); + r1 = y.fetch_add(1, memory_order_release); + z.store((intptr_t)&x, memory_order_relaxed); + r2 = y.fetch_add(1, memory_order_release); } static void b(void *obj) { - int t3=atomic_fetch_add_explicit(&y, 1, memory_order_acquire); - void *ptr = (void *)atomic_load_explicit(&z, memory_order_relaxed); - atomic_store_explicit(&z2, (intptr_t)ptr, memory_order_relaxed); - printf("t3=%d\n", t3); + r3 = y.fetch_add(1, memory_order_acquire); + void *ptr = (void *)z.load(memory_order_relaxed); + z2.store((intptr_t)ptr, memory_order_relaxed); } static void c(void *obj) { - void * ptr2=(void *) atomic_load_explicit(&z2, memory_order_relaxed); - atomic_store_explicit((atomic_int *)ptr2, 5, memory_order_relaxed); + atomic_int *ptr2 = (atomic_int *)z2.load(memory_order_relaxed); + (*ptr2).store(5, memory_order_relaxed); } int user_main(int argc, char **argv) { thrd_t t1, t2, t3; - atomic_init(&x, 0); atomic_init(&y, 0); atomic_init(&z, (intptr_t) &x); @@ -59,11 +60,12 @@ int user_main(int argc, char **argv) thrd_create(&t1, (thrd_start_t)&a, NULL); thrd_create(&t2, (thrd_start_t)&b, NULL); thrd_create(&t3, (thrd_start_t)&c, NULL); - + thrd_join(t1); thrd_join(t2); thrd_join(t3); - + + printf("r1=%d, r2=%d, r3=%d\n", r1, r2, r3); return 0; } -- 2.34.1