blob: 9911d642eb7e96b9b19c12853b5404589684d66c [file] [log] [blame]
Elliott Hughese6c57fc2014-05-23 20:06:03 -07001/*
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 Boehm00aaea32014-08-19 16:14:01 -070017#include <gtest/gtest.h>
Tom Cherry76e2b152019-07-18 13:15:47 -070018
Tom Cherry32b5f4e2019-08-05 08:20:14 -070019// 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 Cherry76e2b152019-07-18 13:15:47 -070021#include <bits/stdatomic.h>
Tom Cherry76e2b152019-07-18 13:15:47 -070022
Hans Boehm00aaea32014-08-19 16:14:01 -070023#include <pthread.h>
24#include <stdint.h>
Elliott Hughese6c57fc2014-05-23 20:06:03 -070025
26TEST(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
39TEST(stdatomic, init) {
40 atomic_int v = ATOMIC_VAR_INIT(123);
41 ASSERT_EQ(123, atomic_load(&v));
42
43 atomic_init(&v, 456);
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
50TEST(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
59TEST(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
68TEST(stdatomic, atomic_is_lock_free) {
69 atomic_char small;
Elliott Hughese6c57fc2014-05-23 20:06:03 -070070 ASSERT_TRUE(atomic_is_lock_free(&small));
Hans Boehm32429602014-08-28 15:21:32 -070071 atomic_intmax_t big;
Raghu Gandhamf1837372014-07-24 15:56:51 -070072 // atomic_intmax_t(size = 64) is not lock free on mips32.
73#if defined(__mips__) && !defined(__LP64__)
74 ASSERT_FALSE(atomic_is_lock_free(&big));
75#else
Elliott Hughese6c57fc2014-05-23 20:06:03 -070076 ASSERT_TRUE(atomic_is_lock_free(&big));
Raghu Gandhamf1837372014-07-24 15:56:51 -070077#endif
Elliott Hughese6c57fc2014-05-23 20:06:03 -070078}
79
80TEST(stdatomic, atomic_flag) {
81 atomic_flag f = ATOMIC_FLAG_INIT;
82 ASSERT_FALSE(atomic_flag_test_and_set(&f));
83 ASSERT_TRUE(atomic_flag_test_and_set(&f));
84
85 atomic_flag_clear(&f);
86
87 ASSERT_FALSE(atomic_flag_test_and_set_explicit(&f, memory_order_relaxed));
88 ASSERT_TRUE(atomic_flag_test_and_set_explicit(&f, memory_order_relaxed));
89
90 atomic_flag_clear_explicit(&f, memory_order_relaxed);
91 ASSERT_FALSE(atomic_flag_test_and_set_explicit(&f, memory_order_relaxed));
92}
93
94TEST(stdatomic, atomic_store) {
95 atomic_int i;
96 atomic_store(&i, 123);
97 ASSERT_EQ(123, atomic_load(&i));
98 atomic_store_explicit(&i, 123, memory_order_relaxed);
99 ASSERT_EQ(123, atomic_load_explicit(&i, memory_order_relaxed));
100}
101
102TEST(stdatomic, atomic_exchange) {
103 atomic_int i;
104 atomic_store(&i, 123);
105 ASSERT_EQ(123, atomic_exchange(&i, 456));
106 ASSERT_EQ(456, atomic_exchange_explicit(&i, 123, memory_order_relaxed));
107}
108
109TEST(stdatomic, atomic_compare_exchange) {
110 atomic_int i;
Dan Albert6b3beb22014-05-28 16:27:32 -0700111 int expected;
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700112
113 atomic_store(&i, 123);
Dan Albert6b3beb22014-05-28 16:27:32 -0700114 expected = 123;
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700115 ASSERT_TRUE(atomic_compare_exchange_strong(&i, &expected, 456));
116 ASSERT_FALSE(atomic_compare_exchange_strong(&i, &expected, 456));
Dan Albert6b3beb22014-05-28 16:27:32 -0700117 ASSERT_EQ(456, expected);
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700118
119 atomic_store(&i, 123);
Dan Albert6b3beb22014-05-28 16:27:32 -0700120 expected = 123;
Hans Boehm590a4102017-04-04 17:34:59 -0700121 ASSERT_TRUE(atomic_compare_exchange_strong_explicit(&i, &expected, 456, memory_order_relaxed,
122 memory_order_relaxed));
123 ASSERT_FALSE(atomic_compare_exchange_strong_explicit(&i, &expected, 456, memory_order_relaxed,
124 memory_order_relaxed));
Dan Albert6b3beb22014-05-28 16:27:32 -0700125 ASSERT_EQ(456, expected);
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700126
127 atomic_store(&i, 123);
Dan Albert6b3beb22014-05-28 16:27:32 -0700128 expected = 123;
Hans Boehm590a4102017-04-04 17:34:59 -0700129 int iter_count = 0;
130 do {
131 ++iter_count;
132 ASSERT_LT(iter_count, 100); // Arbitrary limit on spurious compare_exchange failures.
133 ASSERT_EQ(expected, 123);
134 } while(!atomic_compare_exchange_weak(&i, &expected, 456));
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700135 ASSERT_FALSE(atomic_compare_exchange_weak(&i, &expected, 456));
Dan Albert6b3beb22014-05-28 16:27:32 -0700136 ASSERT_EQ(456, expected);
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700137
138 atomic_store(&i, 123);
Dan Albert6b3beb22014-05-28 16:27:32 -0700139 expected = 123;
Hans Boehm590a4102017-04-04 17:34:59 -0700140 iter_count = 0;
141 do {
142 ++iter_count;
143 ASSERT_LT(iter_count, 100);
144 ASSERT_EQ(expected, 123);
145 } while(!atomic_compare_exchange_weak_explicit(&i, &expected, 456, memory_order_relaxed,
146 memory_order_relaxed));
147 ASSERT_FALSE(atomic_compare_exchange_weak_explicit(&i, &expected, 456, memory_order_relaxed,
148 memory_order_relaxed));
Dan Albert6b3beb22014-05-28 16:27:32 -0700149 ASSERT_EQ(456, expected);
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700150}
151
152TEST(stdatomic, atomic_fetch_add) {
153 atomic_int i = ATOMIC_VAR_INIT(123);
154 ASSERT_EQ(123, atomic_fetch_add(&i, 1));
155 ASSERT_EQ(124, atomic_fetch_add_explicit(&i, 1, memory_order_relaxed));
156 ASSERT_EQ(125, atomic_load(&i));
157}
158
159TEST(stdatomic, atomic_fetch_sub) {
160 atomic_int i = ATOMIC_VAR_INIT(123);
161 ASSERT_EQ(123, atomic_fetch_sub(&i, 1));
162 ASSERT_EQ(122, atomic_fetch_sub_explicit(&i, 1, memory_order_relaxed));
163 ASSERT_EQ(121, atomic_load(&i));
164}
165
166TEST(stdatomic, atomic_fetch_or) {
167 atomic_int i = ATOMIC_VAR_INIT(0x100);
168 ASSERT_EQ(0x100, atomic_fetch_or(&i, 0x020));
169 ASSERT_EQ(0x120, atomic_fetch_or_explicit(&i, 0x003, memory_order_relaxed));
170 ASSERT_EQ(0x123, atomic_load(&i));
171}
172
173TEST(stdatomic, atomic_fetch_xor) {
174 atomic_int i = ATOMIC_VAR_INIT(0x100);
175 ASSERT_EQ(0x100, atomic_fetch_xor(&i, 0x120));
176 ASSERT_EQ(0x020, atomic_fetch_xor_explicit(&i, 0x103, memory_order_relaxed));
177 ASSERT_EQ(0x123, atomic_load(&i));
178}
179
180TEST(stdatomic, atomic_fetch_and) {
181 atomic_int i = ATOMIC_VAR_INIT(0x123);
182 ASSERT_EQ(0x123, atomic_fetch_and(&i, 0x00f));
183 ASSERT_EQ(0x003, atomic_fetch_and_explicit(&i, 0x2, memory_order_relaxed));
184 ASSERT_EQ(0x002, atomic_load(&i));
185}
186
Hans Boehm00aaea32014-08-19 16:14:01 -0700187// And a rudimentary test of acquire-release memory ordering:
188
189constexpr static uint_least32_t BIG = 10000000ul; // Assumed even below.
190
191struct three_atomics {
192 atomic_uint_least32_t x;
193 char a[123]; // Everything in different cache lines,
194 // increase chance of compiler getting alignment wrong.
195 atomic_uint_least32_t y;
196 char b[4013];
197 atomic_uint_least32_t z;
198};
199
200// Very simple acquire/release memory ordering sanity check.
201static void* writer(void* arg) {
202 three_atomics* a = reinterpret_cast<three_atomics*>(arg);
203 for (uint_least32_t i = 0; i <= BIG; i+=2) {
204 atomic_store_explicit(&a->x, i, memory_order_relaxed);
205 atomic_store_explicit(&a->z, i, memory_order_relaxed);
206 atomic_store_explicit(&a->y, i, memory_order_release);
207 atomic_store_explicit(&a->x, i+1, memory_order_relaxed);
208 atomic_store_explicit(&a->z, i+1, memory_order_relaxed);
209 atomic_store_explicit(&a->y, i+1, memory_order_release);
210 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700211 return nullptr;
Hans Boehm00aaea32014-08-19 16:14:01 -0700212}
213
214static void* reader(void* arg) {
215 three_atomics* a = reinterpret_cast<three_atomics*>(arg);
216 uint_least32_t xval = 0, yval = 0, zval = 0;
217 size_t repeat = 0;
218 size_t repeat_limit = 1000;
219 while (yval != BIG + 1) {
220 yval = atomic_load_explicit(&a->y, memory_order_acquire);
221 zval = atomic_load_explicit(&a->z, memory_order_relaxed);
222 xval = atomic_load_explicit(&a->x, memory_order_relaxed);
223 // If we see a given value of y, the immediately preceding
224 // stores to z and x, or later ones, should also be visible.
225 if (zval < yval) {
226 // Cant just ASSERT, since we are in a non-void function.
227 ADD_FAILURE() << "acquire-release ordering violation: "
228 << zval << " < " << yval << ", " << xval << "\n";
Yi Kong32bc0fc2018-08-02 17:31:13 -0700229 return nullptr; // Only report once.
Hans Boehm00aaea32014-08-19 16:14:01 -0700230 }
231 if (xval < yval) {
232 // Cant just ASSERT, since we are in a non-void function.
233 ADD_FAILURE() << "acquire-release ordering violation: "
234 << xval << " < " << yval << ", " << zval << "\n";
Yi Kong32bc0fc2018-08-02 17:31:13 -0700235 return nullptr; // Only report once.
Hans Boehm00aaea32014-08-19 16:14:01 -0700236 }
237 if (repeat < repeat_limit) ++repeat;
238 }
239 // The following assertion is not technically guaranteed to hold.
240 // But if it fails to hold, this test was useless, and we have a
241 // serious scheduling issue that we should probably know about.
242 EXPECT_EQ(repeat, repeat_limit);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700243 return nullptr;
Hans Boehm00aaea32014-08-19 16:14:01 -0700244}
245
246TEST(stdatomic, ordering) {
247 // Run a memory ordering sanity test.
248 void* result;
249 three_atomics a;
250 atomic_init(&a.x, 0ul);
251 atomic_init(&a.y, 0ul);
252 atomic_init(&a.z, 0ul);
253 pthread_t t1,t2;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700254 ASSERT_EQ(0, pthread_create(&t1, nullptr, reader, &a));
255 ASSERT_EQ(0, pthread_create(&t2, nullptr, writer, &a));
Hans Boehm00aaea32014-08-19 16:14:01 -0700256 ASSERT_EQ(0, pthread_join(t1, &result));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700257 EXPECT_EQ(nullptr, result);
Hans Boehm00aaea32014-08-19 16:14:01 -0700258 ASSERT_EQ(0, pthread_join(t2, &result));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700259 EXPECT_EQ(nullptr, result);
Hans Boehm00aaea32014-08-19 16:14:01 -0700260 EXPECT_EQ(atomic_load_explicit(&a.x, memory_order_consume), BIG + 1);
261 EXPECT_EQ(atomic_load_explicit(&a.y, memory_order_seq_cst), BIG + 1);
262 EXPECT_EQ(atomic_load(&a.z), BIG + 1);
263}