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);
37 static void *b(void *obj)
39 r3 = y.fetch_add(1, memory_order_acquire);
40 intptr_t ptr = z.load(memory_order_relaxed);
41 z2.store(ptr, memory_order_relaxed);
45 static void *c(void *obj)
47 atomic_int *ptr2 = (atomic_int *)z2.load(memory_order_relaxed);
48 (*ptr2).store(5, memory_order_relaxed);
52 int user_main(int argc, char **argv)
58 atomic_init(&z, (intptr_t) &x);
59 atomic_init(&z2, (intptr_t) &x);
61 pthread_create(&t1,NULL, &a, NULL);
62 pthread_create(&t2,NULL, &b, NULL);
63 pthread_create(&t3,NULL, &c, NULL);
65 pthread_join(t1,NULL);
66 pthread_join(t2,NULL);
67 pthread_join(t3,NULL);
69 printf("r1=%d, r2=%d, r3=%d\n", r1, r2, r3);