1 /* Test __atomic routines for existence and proper execution on 4 byte
2 values with each valid memory model. */
3 /* { dg-do run } */
4 /* { dg-require-effective-target sync_int_long } */
5
6 /* Test the execution of the __atomic_compare_exchange_n builtin for an int. */
7
8 extern void abort(void);
9
10 int v = 0;
11 int expected = 0;
12 int max = ~0;
13 int desired = ~0;
14 int zero = 0;
15
16 #define STRONG 0
17 #define WEAK 1
18
main()19 main ()
20 {
21
22 if (!__atomic_compare_exchange_n (&v, &expected, max, STRONG , __ATOMIC_RELAXED, __ATOMIC_RELAXED))
23 abort ();
24 if (expected != 0)
25 abort ();
26
27 if (__atomic_compare_exchange_n (&v, &expected, 0, STRONG , __ATOMIC_ACQUIRE, __ATOMIC_RELAXED))
28 abort ();
29 if (expected != max)
30 abort ();
31
32 if (!__atomic_compare_exchange_n (&v, &expected, 0, STRONG , __ATOMIC_RELEASE, __ATOMIC_ACQUIRE))
33 abort ();
34 if (expected != max)
35 abort ();
36 if (v != 0)
37 abort ();
38
39 if (__atomic_compare_exchange_n (&v, &expected, desired, WEAK, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE))
40 abort ();
41 if (expected != 0)
42 abort ();
43
44 if (!__atomic_compare_exchange_n (&v, &expected, desired, STRONG , __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
45 abort ();
46 if (expected != 0)
47 abort ();
48 if (v != max)
49 abort ();
50
51 /* Now test the generic version. */
52
53 v = 0;
54
55 if (!__atomic_compare_exchange (&v, &expected, &max, STRONG, __ATOMIC_RELAXED, __ATOMIC_RELAXED))
56 abort ();
57 if (expected != 0)
58 abort ();
59
60 if (__atomic_compare_exchange (&v, &expected, &zero, STRONG , __ATOMIC_ACQUIRE, __ATOMIC_RELAXED))
61 abort ();
62 if (expected != max)
63 abort ();
64
65 if (!__atomic_compare_exchange (&v, &expected, &zero, STRONG , __ATOMIC_RELEASE, __ATOMIC_ACQUIRE))
66 abort ();
67 if (expected != max)
68 abort ();
69 if (v != 0)
70 abort ();
71
72 if (__atomic_compare_exchange (&v, &expected, &desired, WEAK, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE))
73 abort ();
74 if (expected != 0)
75 abort ();
76
77 if (!__atomic_compare_exchange (&v, &expected, &desired, STRONG , __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
78 abort ();
79 if (expected != 0)
80 abort ();
81 if (v != max)
82 abort ();
83
84 return 0;
85 }
86