7 #include "model-assert.h"
12 atomic_intptr_t z, z2;
14 int r1, r2, r3; /* "local" variables */
17 This example illustrates a self-satisfying cycle involving
18 synchronization. A failed synchronization creates the store that
19 causes the synchronization to fail.
21 The C++11 memory model nominally allows r1=0, r2=1, r3=5.
23 This example is insane, we don't support that behavior.
27 static void a(void *obj)
29 z.store((intptr_t)&y, memory_order_relaxed);
30 r1 = y.fetch_add(1, memory_order_release);
31 z.store((intptr_t)&x, memory_order_relaxed);
32 r2 = y.fetch_add(1, memory_order_release);
36 static void b(void *obj)
38 r3 = y.fetch_add(1, memory_order_acquire);
39 intptr_t ptr = z.load(memory_order_relaxed);
40 z2.store(ptr, memory_order_relaxed);
43 static void c(void *obj)
45 atomic_int *ptr2 = (atomic_int *)z2.load(memory_order_relaxed);
46 (*ptr2).store(5, memory_order_relaxed);
49 int user_main(int argc, char **argv)
55 atomic_init(&z, (intptr_t) &x);
56 atomic_init(&z2, (intptr_t) &x);
58 thrd_create(&t1, (thrd_start_t)&a, NULL);
59 thrd_create(&t2, (thrd_start_t)&b, NULL);
60 thrd_create(&t3, (thrd_start_t)&c, NULL);
66 printf("r1=%d, r2=%d, r3=%d\n", r1, r2, r3);