2 * Copyright (c) 2013 Ed Schouten <ed@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <stdatomic.h>
37 * Tool for testing the logical behaviour of operations on atomic
38 * integer types. These tests make no attempt to actually test whether
39 * the functions are atomic or provide the right barrier semantics.
41 * For every type, we create an array of 16 elements and repeat the test
42 * on every element in the array. This allows us to test whether the
43 * atomic operations have no effect on surrounding values. This is
44 * especially useful for the smaller integer types, as it may be the
45 * case that these operations are implemented by processing entire words
49 static inline intmax_t
54 arc4random_buf(&v, sizeof(v));
58 #define DO_FETCH_TEST(T, a, name, result) do { \
59 T v1 = atomic_load(a); \
61 assert(atomic_##name(a, v2) == v1); \
62 assert(atomic_load(a) == (T)(result)); \
65 #define DO_COMPARE_EXCHANGE_TEST(T, a, name) do { \
66 T v1 = atomic_load(a); \
69 if (atomic_compare_exchange_##name(a, &v2, v3)) \
72 assert(atomic_compare_exchange_##name(a, &v2, v3)); \
73 assert(atomic_load(a) == v3); \
76 #define DO_ALL_TESTS(T, a) do { \
80 assert(atomic_load(a) == v1); \
84 atomic_store(a, v1); \
85 assert(atomic_load(a) == v1); \
88 DO_FETCH_TEST(T, a, exchange, v2); \
89 DO_FETCH_TEST(T, a, fetch_add, v1 + v2); \
90 DO_FETCH_TEST(T, a, fetch_and, v1 & v2); \
91 DO_FETCH_TEST(T, a, fetch_or, v1 | v2); \
92 DO_FETCH_TEST(T, a, fetch_sub, v1 - v2); \
93 DO_FETCH_TEST(T, a, fetch_xor, v1 ^ v2); \
95 DO_COMPARE_EXCHANGE_TEST(T, a, weak); \
96 DO_COMPARE_EXCHANGE_TEST(T, a, strong); \
99 #define TEST_TYPE(T) do { \
101 struct { _Atomic(T) v[16]; } list, cmp; \
102 arc4random_buf(&cmp, sizeof(cmp)); \
103 for (j = 0; j < 16; j++) { \
105 DO_ALL_TESTS(T, &list.v[j]); \
106 list.v[j] = cmp.v[j]; \
107 assert(memcmp(&list, &cmp, sizeof(list)) == 0); \
116 for (i = 0; i < 1000; i++) {