1 /* Test atomic_compare_exchange routines for existence and proper
2    execution on 2-byte values with each valid memory model.  */
3 /* { dg-do run } */
4 /* { dg-options "-std=c11 -pedantic-errors" } */
5 
6 #include <stdatomic.h>
7 
8 extern void abort (void);
9 
10 _Atomic short v = ATOMIC_VAR_INIT (0);
11 short expected = 0;
12 short max = ~0;
13 short desired = ~0;
14 short zero = 0;
15 
16 int
main()17 main ()
18 {
19 
20   if (!atomic_compare_exchange_strong_explicit (&v, &expected, max, memory_order_relaxed, memory_order_relaxed))
21     abort ();
22   if (expected != 0)
23     abort ();
24 
25   if (atomic_compare_exchange_strong_explicit (&v, &expected, 0, memory_order_acquire, memory_order_relaxed))
26     abort ();
27   if (expected != max)
28     abort ();
29 
30   if (!atomic_compare_exchange_strong_explicit (&v, &expected, 0, memory_order_release, memory_order_acquire))
31     abort ();
32   if (expected != max)
33     abort ();
34   if (v != 0)
35     abort ();
36 
37   if (atomic_compare_exchange_weak_explicit (&v, &expected, desired, memory_order_acq_rel, memory_order_acquire))
38     abort ();
39   if (expected != 0)
40     abort ();
41 
42   if (!atomic_compare_exchange_strong_explicit (&v, &expected, desired, memory_order_seq_cst, memory_order_seq_cst))
43     abort ();
44   if (expected != 0)
45     abort ();
46   if (v != max)
47     abort ();
48 
49   v = 0;
50 
51   if (!atomic_compare_exchange_strong (&v, &expected, max))
52     abort ();
53   if (expected != 0)
54     abort ();
55 
56   if (atomic_compare_exchange_strong (&v, &expected, zero))
57     abort ();
58   if (expected != max)
59     abort ();
60 
61   if (!atomic_compare_exchange_strong (&v, &expected, zero))
62     abort ();
63   if (expected != max)
64     abort ();
65   if (v != 0)
66     abort ();
67 
68   if (atomic_compare_exchange_weak (&v, &expected, desired))
69     abort ();
70   if (expected != 0)
71     abort ();
72 
73   if (!atomic_compare_exchange_strong (&v, &expected, desired))
74     abort ();
75   if (expected != 0)
76     abort ();
77   if (v != max)
78     abort ();
79 
80   return 0;
81 }
82