Elliott Hughes | e6c57fc | 2014-05-23 20:06:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Hans Boehm | 00aaea3 | 2014-08-19 16:14:01 -0700 | [diff] [blame] | 17 | #include <gtest/gtest.h> |
Tom Cherry | 76e2b15 | 2019-07-18 13:15:47 -0700 | [diff] [blame] | 18 | |
Tom Cherry | 32b5f4e | 2019-08-05 08:20:14 -0700 | [diff] [blame] | 19 | // The real <stdatomic.h> checks for the availability of C++'s atomics and uses them if present. Since |
| 20 | // we want to test the libc versions, we instead include <bits/stdatomic.h> where they're actually defined. |
Tom Cherry | 76e2b15 | 2019-07-18 13:15:47 -0700 | [diff] [blame] | 21 | #include <bits/stdatomic.h> |
Tom Cherry | 76e2b15 | 2019-07-18 13:15:47 -0700 | [diff] [blame] | 22 | |
Hans Boehm | 00aaea3 | 2014-08-19 16:14:01 -0700 | [diff] [blame] | 23 | #include <pthread.h> |
| 24 | #include <stdint.h> |
Elliott Hughes | e6c57fc | 2014-05-23 20:06:03 -0700 | [diff] [blame] | 25 | |
| 26 | TEST(stdatomic, LOCK_FREE) { |
| 27 | ASSERT_TRUE(ATOMIC_BOOL_LOCK_FREE); |
| 28 | ASSERT_TRUE(ATOMIC_CHAR16_T_LOCK_FREE); |
| 29 | ASSERT_TRUE(ATOMIC_CHAR32_T_LOCK_FREE); |
| 30 | ASSERT_TRUE(ATOMIC_CHAR_LOCK_FREE); |
| 31 | ASSERT_TRUE(ATOMIC_INT_LOCK_FREE); |
| 32 | ASSERT_TRUE(ATOMIC_LLONG_LOCK_FREE); |
| 33 | ASSERT_TRUE(ATOMIC_LONG_LOCK_FREE); |
| 34 | ASSERT_TRUE(ATOMIC_POINTER_LOCK_FREE); |
| 35 | ASSERT_TRUE(ATOMIC_SHORT_LOCK_FREE); |
| 36 | ASSERT_TRUE(ATOMIC_WCHAR_T_LOCK_FREE); |
| 37 | } |
| 38 | |
| 39 | TEST(stdatomic, init) { |
Christopher Ferris | 81ce887 | 2024-09-19 16:20:21 -0700 | [diff] [blame] | 40 | atomic_int v = 123; |
Elliott Hughes | e6c57fc | 2014-05-23 20:06:03 -0700 | [diff] [blame] | 41 | ASSERT_EQ(123, atomic_load(&v)); |
| 42 | |
Nick Desaulniers | 2e65afe | 2024-11-19 09:27:06 -0800 | [diff] [blame^] | 43 | atomic_store_explicit(&v, 456, memory_order_relaxed); |
Elliott Hughes | e6c57fc | 2014-05-23 20:06:03 -0700 | [diff] [blame] | 44 | ASSERT_EQ(456, atomic_load(&v)); |
| 45 | |
| 46 | atomic_flag f = ATOMIC_FLAG_INIT; |
| 47 | ASSERT_FALSE(atomic_flag_test_and_set(&f)); |
| 48 | } |
| 49 | |
| 50 | TEST(stdatomic, atomic_thread_fence) { |
| 51 | atomic_thread_fence(memory_order_relaxed); |
| 52 | atomic_thread_fence(memory_order_consume); |
| 53 | atomic_thread_fence(memory_order_acquire); |
| 54 | atomic_thread_fence(memory_order_release); |
| 55 | atomic_thread_fence(memory_order_acq_rel); |
| 56 | atomic_thread_fence(memory_order_seq_cst); |
| 57 | } |
| 58 | |
| 59 | TEST(stdatomic, atomic_signal_fence) { |
| 60 | atomic_signal_fence(memory_order_relaxed); |
| 61 | atomic_signal_fence(memory_order_consume); |
| 62 | atomic_signal_fence(memory_order_acquire); |
| 63 | atomic_signal_fence(memory_order_release); |
| 64 | atomic_signal_fence(memory_order_acq_rel); |
| 65 | atomic_signal_fence(memory_order_seq_cst); |
| 66 | } |
| 67 | |
| 68 | TEST(stdatomic, atomic_is_lock_free) { |
| 69 | atomic_char small; |
Elliott Hughes | e6c57fc | 2014-05-23 20:06:03 -0700 | [diff] [blame] | 70 | ASSERT_TRUE(atomic_is_lock_free(&small)); |
Hans Boehm | 3242960 | 2014-08-28 15:21:32 -0700 | [diff] [blame] | 71 | atomic_intmax_t big; |
Elliott Hughes | e6c57fc | 2014-05-23 20:06:03 -0700 | [diff] [blame] | 72 | ASSERT_TRUE(atomic_is_lock_free(&big)); |
| 73 | } |
| 74 | |
| 75 | TEST(stdatomic, atomic_flag) { |
| 76 | atomic_flag f = ATOMIC_FLAG_INIT; |
| 77 | ASSERT_FALSE(atomic_flag_test_and_set(&f)); |
| 78 | ASSERT_TRUE(atomic_flag_test_and_set(&f)); |
| 79 | |
| 80 | atomic_flag_clear(&f); |
| 81 | |
| 82 | ASSERT_FALSE(atomic_flag_test_and_set_explicit(&f, memory_order_relaxed)); |
| 83 | ASSERT_TRUE(atomic_flag_test_and_set_explicit(&f, memory_order_relaxed)); |
| 84 | |
| 85 | atomic_flag_clear_explicit(&f, memory_order_relaxed); |
| 86 | ASSERT_FALSE(atomic_flag_test_and_set_explicit(&f, memory_order_relaxed)); |
| 87 | } |
| 88 | |
| 89 | TEST(stdatomic, atomic_store) { |
| 90 | atomic_int i; |
| 91 | atomic_store(&i, 123); |
| 92 | ASSERT_EQ(123, atomic_load(&i)); |
| 93 | atomic_store_explicit(&i, 123, memory_order_relaxed); |
| 94 | ASSERT_EQ(123, atomic_load_explicit(&i, memory_order_relaxed)); |
| 95 | } |
| 96 | |
| 97 | TEST(stdatomic, atomic_exchange) { |
| 98 | atomic_int i; |
| 99 | atomic_store(&i, 123); |
| 100 | ASSERT_EQ(123, atomic_exchange(&i, 456)); |
| 101 | ASSERT_EQ(456, atomic_exchange_explicit(&i, 123, memory_order_relaxed)); |
| 102 | } |
| 103 | |
| 104 | TEST(stdatomic, atomic_compare_exchange) { |
| 105 | atomic_int i; |
Dan Albert | 6b3beb2 | 2014-05-28 16:27:32 -0700 | [diff] [blame] | 106 | int expected; |
Elliott Hughes | e6c57fc | 2014-05-23 20:06:03 -0700 | [diff] [blame] | 107 | |
| 108 | atomic_store(&i, 123); |
Dan Albert | 6b3beb2 | 2014-05-28 16:27:32 -0700 | [diff] [blame] | 109 | expected = 123; |
Elliott Hughes | e6c57fc | 2014-05-23 20:06:03 -0700 | [diff] [blame] | 110 | ASSERT_TRUE(atomic_compare_exchange_strong(&i, &expected, 456)); |
| 111 | ASSERT_FALSE(atomic_compare_exchange_strong(&i, &expected, 456)); |
Dan Albert | 6b3beb2 | 2014-05-28 16:27:32 -0700 | [diff] [blame] | 112 | ASSERT_EQ(456, expected); |
Elliott Hughes | e6c57fc | 2014-05-23 20:06:03 -0700 | [diff] [blame] | 113 | |
| 114 | atomic_store(&i, 123); |
Dan Albert | 6b3beb2 | 2014-05-28 16:27:32 -0700 | [diff] [blame] | 115 | expected = 123; |
Hans Boehm | 590a410 | 2017-04-04 17:34:59 -0700 | [diff] [blame] | 116 | ASSERT_TRUE(atomic_compare_exchange_strong_explicit(&i, &expected, 456, memory_order_relaxed, |
| 117 | memory_order_relaxed)); |
| 118 | ASSERT_FALSE(atomic_compare_exchange_strong_explicit(&i, &expected, 456, memory_order_relaxed, |
| 119 | memory_order_relaxed)); |
Dan Albert | 6b3beb2 | 2014-05-28 16:27:32 -0700 | [diff] [blame] | 120 | ASSERT_EQ(456, expected); |
Elliott Hughes | e6c57fc | 2014-05-23 20:06:03 -0700 | [diff] [blame] | 121 | |
| 122 | atomic_store(&i, 123); |
Dan Albert | 6b3beb2 | 2014-05-28 16:27:32 -0700 | [diff] [blame] | 123 | expected = 123; |
Hans Boehm | 590a410 | 2017-04-04 17:34:59 -0700 | [diff] [blame] | 124 | int iter_count = 0; |
| 125 | do { |
| 126 | ++iter_count; |
| 127 | ASSERT_LT(iter_count, 100); // Arbitrary limit on spurious compare_exchange failures. |
| 128 | ASSERT_EQ(expected, 123); |
| 129 | } while(!atomic_compare_exchange_weak(&i, &expected, 456)); |
Elliott Hughes | e6c57fc | 2014-05-23 20:06:03 -0700 | [diff] [blame] | 130 | ASSERT_FALSE(atomic_compare_exchange_weak(&i, &expected, 456)); |
Dan Albert | 6b3beb2 | 2014-05-28 16:27:32 -0700 | [diff] [blame] | 131 | ASSERT_EQ(456, expected); |
Elliott Hughes | e6c57fc | 2014-05-23 20:06:03 -0700 | [diff] [blame] | 132 | |
| 133 | atomic_store(&i, 123); |
Dan Albert | 6b3beb2 | 2014-05-28 16:27:32 -0700 | [diff] [blame] | 134 | expected = 123; |
Hans Boehm | 590a410 | 2017-04-04 17:34:59 -0700 | [diff] [blame] | 135 | iter_count = 0; |
| 136 | do { |
| 137 | ++iter_count; |
| 138 | ASSERT_LT(iter_count, 100); |
| 139 | ASSERT_EQ(expected, 123); |
| 140 | } while(!atomic_compare_exchange_weak_explicit(&i, &expected, 456, memory_order_relaxed, |
| 141 | memory_order_relaxed)); |
| 142 | ASSERT_FALSE(atomic_compare_exchange_weak_explicit(&i, &expected, 456, memory_order_relaxed, |
| 143 | memory_order_relaxed)); |
Dan Albert | 6b3beb2 | 2014-05-28 16:27:32 -0700 | [diff] [blame] | 144 | ASSERT_EQ(456, expected); |
Elliott Hughes | e6c57fc | 2014-05-23 20:06:03 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | TEST(stdatomic, atomic_fetch_add) { |
Christopher Ferris | 81ce887 | 2024-09-19 16:20:21 -0700 | [diff] [blame] | 148 | atomic_int i = 123; |
Elliott Hughes | e6c57fc | 2014-05-23 20:06:03 -0700 | [diff] [blame] | 149 | ASSERT_EQ(123, atomic_fetch_add(&i, 1)); |
| 150 | ASSERT_EQ(124, atomic_fetch_add_explicit(&i, 1, memory_order_relaxed)); |
| 151 | ASSERT_EQ(125, atomic_load(&i)); |
| 152 | } |
| 153 | |
| 154 | TEST(stdatomic, atomic_fetch_sub) { |
Christopher Ferris | 81ce887 | 2024-09-19 16:20:21 -0700 | [diff] [blame] | 155 | atomic_int i = 123; |
Elliott Hughes | e6c57fc | 2014-05-23 20:06:03 -0700 | [diff] [blame] | 156 | ASSERT_EQ(123, atomic_fetch_sub(&i, 1)); |
| 157 | ASSERT_EQ(122, atomic_fetch_sub_explicit(&i, 1, memory_order_relaxed)); |
| 158 | ASSERT_EQ(121, atomic_load(&i)); |
| 159 | } |
| 160 | |
| 161 | TEST(stdatomic, atomic_fetch_or) { |
Christopher Ferris | 81ce887 | 2024-09-19 16:20:21 -0700 | [diff] [blame] | 162 | atomic_int i = 0x100; |
Elliott Hughes | e6c57fc | 2014-05-23 20:06:03 -0700 | [diff] [blame] | 163 | ASSERT_EQ(0x100, atomic_fetch_or(&i, 0x020)); |
| 164 | ASSERT_EQ(0x120, atomic_fetch_or_explicit(&i, 0x003, memory_order_relaxed)); |
| 165 | ASSERT_EQ(0x123, atomic_load(&i)); |
| 166 | } |
| 167 | |
| 168 | TEST(stdatomic, atomic_fetch_xor) { |
Christopher Ferris | 81ce887 | 2024-09-19 16:20:21 -0700 | [diff] [blame] | 169 | atomic_int i = 0x100; |
Elliott Hughes | e6c57fc | 2014-05-23 20:06:03 -0700 | [diff] [blame] | 170 | ASSERT_EQ(0x100, atomic_fetch_xor(&i, 0x120)); |
| 171 | ASSERT_EQ(0x020, atomic_fetch_xor_explicit(&i, 0x103, memory_order_relaxed)); |
| 172 | ASSERT_EQ(0x123, atomic_load(&i)); |
| 173 | } |
| 174 | |
| 175 | TEST(stdatomic, atomic_fetch_and) { |
Christopher Ferris | 81ce887 | 2024-09-19 16:20:21 -0700 | [diff] [blame] | 176 | atomic_int i = 0x123; |
Elliott Hughes | e6c57fc | 2014-05-23 20:06:03 -0700 | [diff] [blame] | 177 | ASSERT_EQ(0x123, atomic_fetch_and(&i, 0x00f)); |
| 178 | ASSERT_EQ(0x003, atomic_fetch_and_explicit(&i, 0x2, memory_order_relaxed)); |
| 179 | ASSERT_EQ(0x002, atomic_load(&i)); |
| 180 | } |
| 181 | |
Hans Boehm | 00aaea3 | 2014-08-19 16:14:01 -0700 | [diff] [blame] | 182 | // And a rudimentary test of acquire-release memory ordering: |
| 183 | |
Hans Boehm | 71eb46f | 2023-11-13 15:55:05 -0800 | [diff] [blame] | 184 | static constexpr uint_least32_t BIG = 30'000'000ul; |
| 185 | static_assert((BIG % 2) == 0); // Assumed below. |
Hans Boehm | 00aaea3 | 2014-08-19 16:14:01 -0700 | [diff] [blame] | 186 | |
| 187 | struct three_atomics { |
| 188 | atomic_uint_least32_t x; |
| 189 | char a[123]; // Everything in different cache lines, |
| 190 | // increase chance of compiler getting alignment wrong. |
| 191 | atomic_uint_least32_t y; |
| 192 | char b[4013]; |
| 193 | atomic_uint_least32_t z; |
| 194 | }; |
| 195 | |
Hans Boehm | 71eb46f | 2023-11-13 15:55:05 -0800 | [diff] [blame] | 196 | atomic_bool read_enough(false); |
| 197 | |
Elliott Hughes | 68ae6ad | 2020-07-21 16:11:30 -0700 | [diff] [blame] | 198 | // Very simple acquire/release memory ordering smoke test. |
Hans Boehm | 00aaea3 | 2014-08-19 16:14:01 -0700 | [diff] [blame] | 199 | static void* writer(void* arg) { |
| 200 | three_atomics* a = reinterpret_cast<three_atomics*>(arg); |
| 201 | for (uint_least32_t i = 0; i <= BIG; i+=2) { |
| 202 | atomic_store_explicit(&a->x, i, memory_order_relaxed); |
| 203 | atomic_store_explicit(&a->z, i, memory_order_relaxed); |
| 204 | atomic_store_explicit(&a->y, i, memory_order_release); |
Hans Boehm | 71eb46f | 2023-11-13 15:55:05 -0800 | [diff] [blame] | 205 | |
| 206 | // Force stores to be visible in spite of being overwritten below. |
| 207 | asm volatile("" ::: "memory"); |
| 208 | |
Hans Boehm | 00aaea3 | 2014-08-19 16:14:01 -0700 | [diff] [blame] | 209 | atomic_store_explicit(&a->x, i+1, memory_order_relaxed); |
| 210 | atomic_store_explicit(&a->z, i+1, memory_order_relaxed); |
| 211 | atomic_store_explicit(&a->y, i+1, memory_order_release); |
Hans Boehm | 71eb46f | 2023-11-13 15:55:05 -0800 | [diff] [blame] | 212 | if (i >= BIG - 1000 && !atomic_load(&read_enough)) { |
| 213 | // Give reader a chance to catch up, at the expense of making the test |
| 214 | // less effective. |
| 215 | usleep(1000); |
| 216 | } |
Hans Boehm | 00aaea3 | 2014-08-19 16:14:01 -0700 | [diff] [blame] | 217 | } |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 218 | return nullptr; |
Hans Boehm | 00aaea3 | 2014-08-19 16:14:01 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | static void* reader(void* arg) { |
| 222 | three_atomics* a = reinterpret_cast<three_atomics*>(arg); |
| 223 | uint_least32_t xval = 0, yval = 0, zval = 0; |
| 224 | size_t repeat = 0; |
| 225 | size_t repeat_limit = 1000; |
| 226 | while (yval != BIG + 1) { |
| 227 | yval = atomic_load_explicit(&a->y, memory_order_acquire); |
| 228 | zval = atomic_load_explicit(&a->z, memory_order_relaxed); |
| 229 | xval = atomic_load_explicit(&a->x, memory_order_relaxed); |
| 230 | // If we see a given value of y, the immediately preceding |
| 231 | // stores to z and x, or later ones, should also be visible. |
| 232 | if (zval < yval) { |
| 233 | // Cant just ASSERT, since we are in a non-void function. |
| 234 | ADD_FAILURE() << "acquire-release ordering violation: " |
| 235 | << zval << " < " << yval << ", " << xval << "\n"; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 236 | return nullptr; // Only report once. |
Hans Boehm | 00aaea3 | 2014-08-19 16:14:01 -0700 | [diff] [blame] | 237 | } |
| 238 | if (xval < yval) { |
| 239 | // Cant just ASSERT, since we are in a non-void function. |
| 240 | ADD_FAILURE() << "acquire-release ordering violation: " |
| 241 | << xval << " < " << yval << ", " << zval << "\n"; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 242 | return nullptr; // Only report once. |
Hans Boehm | 00aaea3 | 2014-08-19 16:14:01 -0700 | [diff] [blame] | 243 | } |
Hans Boehm | 71eb46f | 2023-11-13 15:55:05 -0800 | [diff] [blame] | 244 | if (repeat < repeat_limit) { |
| 245 | ++repeat; |
| 246 | } else if (!atomic_load_explicit(&read_enough, memory_order_relaxed)) { |
| 247 | atomic_store_explicit(&read_enough, true, memory_order_relaxed); |
| 248 | } |
Hans Boehm | 00aaea3 | 2014-08-19 16:14:01 -0700 | [diff] [blame] | 249 | } |
| 250 | // The following assertion is not technically guaranteed to hold. |
| 251 | // But if it fails to hold, this test was useless, and we have a |
| 252 | // serious scheduling issue that we should probably know about. |
| 253 | EXPECT_EQ(repeat, repeat_limit); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 254 | return nullptr; |
Hans Boehm | 00aaea3 | 2014-08-19 16:14:01 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | TEST(stdatomic, ordering) { |
Elliott Hughes | 68ae6ad | 2020-07-21 16:11:30 -0700 | [diff] [blame] | 258 | // Run a memory ordering smoke test. |
Hans Boehm | 00aaea3 | 2014-08-19 16:14:01 -0700 | [diff] [blame] | 259 | void* result; |
| 260 | three_atomics a; |
Nick Desaulniers | 2e65afe | 2024-11-19 09:27:06 -0800 | [diff] [blame^] | 261 | atomic_store_explicit(&a.x, 0ul, memory_order_relaxed); |
| 262 | atomic_store_explicit(&a.y, 0ul, memory_order_relaxed); |
| 263 | atomic_store_explicit(&a.z, 0ul, memory_order_relaxed); |
Hans Boehm | 00aaea3 | 2014-08-19 16:14:01 -0700 | [diff] [blame] | 264 | pthread_t t1,t2; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 265 | ASSERT_EQ(0, pthread_create(&t1, nullptr, reader, &a)); |
| 266 | ASSERT_EQ(0, pthread_create(&t2, nullptr, writer, &a)); |
Hans Boehm | 00aaea3 | 2014-08-19 16:14:01 -0700 | [diff] [blame] | 267 | ASSERT_EQ(0, pthread_join(t1, &result)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 268 | EXPECT_EQ(nullptr, result); |
Hans Boehm | 00aaea3 | 2014-08-19 16:14:01 -0700 | [diff] [blame] | 269 | ASSERT_EQ(0, pthread_join(t2, &result)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 270 | EXPECT_EQ(nullptr, result); |
Hans Boehm | 00aaea3 | 2014-08-19 16:14:01 -0700 | [diff] [blame] | 271 | EXPECT_EQ(atomic_load_explicit(&a.x, memory_order_consume), BIG + 1); |
| 272 | EXPECT_EQ(atomic_load_explicit(&a.y, memory_order_seq_cst), BIG + 1); |
| 273 | EXPECT_EQ(atomic_load(&a.z), BIG + 1); |
| 274 | } |