blob: a9665d18ed25e4958cfbfd0e4c430834d35145c6 [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
19#if defined(__ANDROID__)
20#include <bits/stdatomic.h>
21#else
22#undef _USING_LIBCXX //TODO(b/137876753): Remove this
Dan Albert3a5aeba2014-09-26 15:37:52 -070023#include <stdatomic.h>
Tom Cherry76e2b152019-07-18 13:15:47 -070024#endif
25
Hans Boehm00aaea32014-08-19 16:14:01 -070026#include <pthread.h>
27#include <stdint.h>
Elliott Hughese6c57fc2014-05-23 20:06:03 -070028
29TEST(stdatomic, LOCK_FREE) {
30 ASSERT_TRUE(ATOMIC_BOOL_LOCK_FREE);
31 ASSERT_TRUE(ATOMIC_CHAR16_T_LOCK_FREE);
32 ASSERT_TRUE(ATOMIC_CHAR32_T_LOCK_FREE);
33 ASSERT_TRUE(ATOMIC_CHAR_LOCK_FREE);
34 ASSERT_TRUE(ATOMIC_INT_LOCK_FREE);
35 ASSERT_TRUE(ATOMIC_LLONG_LOCK_FREE);
36 ASSERT_TRUE(ATOMIC_LONG_LOCK_FREE);
37 ASSERT_TRUE(ATOMIC_POINTER_LOCK_FREE);
38 ASSERT_TRUE(ATOMIC_SHORT_LOCK_FREE);
39 ASSERT_TRUE(ATOMIC_WCHAR_T_LOCK_FREE);
40}
41
42TEST(stdatomic, init) {
43 atomic_int v = ATOMIC_VAR_INIT(123);
44 ASSERT_EQ(123, atomic_load(&v));
45
46 atomic_init(&v, 456);
47 ASSERT_EQ(456, atomic_load(&v));
48
49 atomic_flag f = ATOMIC_FLAG_INIT;
50 ASSERT_FALSE(atomic_flag_test_and_set(&f));
51}
52
53TEST(stdatomic, atomic_thread_fence) {
54 atomic_thread_fence(memory_order_relaxed);
55 atomic_thread_fence(memory_order_consume);
56 atomic_thread_fence(memory_order_acquire);
57 atomic_thread_fence(memory_order_release);
58 atomic_thread_fence(memory_order_acq_rel);
59 atomic_thread_fence(memory_order_seq_cst);
60}
61
62TEST(stdatomic, atomic_signal_fence) {
63 atomic_signal_fence(memory_order_relaxed);
64 atomic_signal_fence(memory_order_consume);
65 atomic_signal_fence(memory_order_acquire);
66 atomic_signal_fence(memory_order_release);
67 atomic_signal_fence(memory_order_acq_rel);
68 atomic_signal_fence(memory_order_seq_cst);
69}
70
71TEST(stdatomic, atomic_is_lock_free) {
72 atomic_char small;
Elliott Hughese6c57fc2014-05-23 20:06:03 -070073 ASSERT_TRUE(atomic_is_lock_free(&small));
Hans Boehm32429602014-08-28 15:21:32 -070074 atomic_intmax_t big;
Raghu Gandhamf1837372014-07-24 15:56:51 -070075 // atomic_intmax_t(size = 64) is not lock free on mips32.
76#if defined(__mips__) && !defined(__LP64__)
77 ASSERT_FALSE(atomic_is_lock_free(&big));
78#else
Elliott Hughese6c57fc2014-05-23 20:06:03 -070079 ASSERT_TRUE(atomic_is_lock_free(&big));
Raghu Gandhamf1837372014-07-24 15:56:51 -070080#endif
Elliott Hughese6c57fc2014-05-23 20:06:03 -070081}
82
83TEST(stdatomic, atomic_flag) {
84 atomic_flag f = ATOMIC_FLAG_INIT;
85 ASSERT_FALSE(atomic_flag_test_and_set(&f));
86 ASSERT_TRUE(atomic_flag_test_and_set(&f));
87
88 atomic_flag_clear(&f);
89
90 ASSERT_FALSE(atomic_flag_test_and_set_explicit(&f, memory_order_relaxed));
91 ASSERT_TRUE(atomic_flag_test_and_set_explicit(&f, memory_order_relaxed));
92
93 atomic_flag_clear_explicit(&f, memory_order_relaxed);
94 ASSERT_FALSE(atomic_flag_test_and_set_explicit(&f, memory_order_relaxed));
95}
96
97TEST(stdatomic, atomic_store) {
98 atomic_int i;
99 atomic_store(&i, 123);
100 ASSERT_EQ(123, atomic_load(&i));
101 atomic_store_explicit(&i, 123, memory_order_relaxed);
102 ASSERT_EQ(123, atomic_load_explicit(&i, memory_order_relaxed));
103}
104
105TEST(stdatomic, atomic_exchange) {
106 atomic_int i;
107 atomic_store(&i, 123);
108 ASSERT_EQ(123, atomic_exchange(&i, 456));
109 ASSERT_EQ(456, atomic_exchange_explicit(&i, 123, memory_order_relaxed));
110}
111
112TEST(stdatomic, atomic_compare_exchange) {
113 atomic_int i;
Dan Albert6b3beb22014-05-28 16:27:32 -0700114 int expected;
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700115
116 atomic_store(&i, 123);
Dan Albert6b3beb22014-05-28 16:27:32 -0700117 expected = 123;
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700118 ASSERT_TRUE(atomic_compare_exchange_strong(&i, &expected, 456));
119 ASSERT_FALSE(atomic_compare_exchange_strong(&i, &expected, 456));
Dan Albert6b3beb22014-05-28 16:27:32 -0700120 ASSERT_EQ(456, expected);
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700121
122 atomic_store(&i, 123);
Dan Albert6b3beb22014-05-28 16:27:32 -0700123 expected = 123;
Hans Boehm590a4102017-04-04 17:34:59 -0700124 ASSERT_TRUE(atomic_compare_exchange_strong_explicit(&i, &expected, 456, memory_order_relaxed,
125 memory_order_relaxed));
126 ASSERT_FALSE(atomic_compare_exchange_strong_explicit(&i, &expected, 456, memory_order_relaxed,
127 memory_order_relaxed));
Dan Albert6b3beb22014-05-28 16:27:32 -0700128 ASSERT_EQ(456, expected);
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700129
130 atomic_store(&i, 123);
Dan Albert6b3beb22014-05-28 16:27:32 -0700131 expected = 123;
Hans Boehm590a4102017-04-04 17:34:59 -0700132 int iter_count = 0;
133 do {
134 ++iter_count;
135 ASSERT_LT(iter_count, 100); // Arbitrary limit on spurious compare_exchange failures.
136 ASSERT_EQ(expected, 123);
137 } while(!atomic_compare_exchange_weak(&i, &expected, 456));
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700138 ASSERT_FALSE(atomic_compare_exchange_weak(&i, &expected, 456));
Dan Albert6b3beb22014-05-28 16:27:32 -0700139 ASSERT_EQ(456, expected);
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700140
141 atomic_store(&i, 123);
Dan Albert6b3beb22014-05-28 16:27:32 -0700142 expected = 123;
Hans Boehm590a4102017-04-04 17:34:59 -0700143 iter_count = 0;
144 do {
145 ++iter_count;
146 ASSERT_LT(iter_count, 100);
147 ASSERT_EQ(expected, 123);
148 } while(!atomic_compare_exchange_weak_explicit(&i, &expected, 456, memory_order_relaxed,
149 memory_order_relaxed));
150 ASSERT_FALSE(atomic_compare_exchange_weak_explicit(&i, &expected, 456, memory_order_relaxed,
151 memory_order_relaxed));
Dan Albert6b3beb22014-05-28 16:27:32 -0700152 ASSERT_EQ(456, expected);
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700153}
154
155TEST(stdatomic, atomic_fetch_add) {
156 atomic_int i = ATOMIC_VAR_INIT(123);
157 ASSERT_EQ(123, atomic_fetch_add(&i, 1));
158 ASSERT_EQ(124, atomic_fetch_add_explicit(&i, 1, memory_order_relaxed));
159 ASSERT_EQ(125, atomic_load(&i));
160}
161
162TEST(stdatomic, atomic_fetch_sub) {
163 atomic_int i = ATOMIC_VAR_INIT(123);
164 ASSERT_EQ(123, atomic_fetch_sub(&i, 1));
165 ASSERT_EQ(122, atomic_fetch_sub_explicit(&i, 1, memory_order_relaxed));
166 ASSERT_EQ(121, atomic_load(&i));
167}
168
169TEST(stdatomic, atomic_fetch_or) {
170 atomic_int i = ATOMIC_VAR_INIT(0x100);
171 ASSERT_EQ(0x100, atomic_fetch_or(&i, 0x020));
172 ASSERT_EQ(0x120, atomic_fetch_or_explicit(&i, 0x003, memory_order_relaxed));
173 ASSERT_EQ(0x123, atomic_load(&i));
174}
175
176TEST(stdatomic, atomic_fetch_xor) {
177 atomic_int i = ATOMIC_VAR_INIT(0x100);
178 ASSERT_EQ(0x100, atomic_fetch_xor(&i, 0x120));
179 ASSERT_EQ(0x020, atomic_fetch_xor_explicit(&i, 0x103, memory_order_relaxed));
180 ASSERT_EQ(0x123, atomic_load(&i));
181}
182
183TEST(stdatomic, atomic_fetch_and) {
184 atomic_int i = ATOMIC_VAR_INIT(0x123);
185 ASSERT_EQ(0x123, atomic_fetch_and(&i, 0x00f));
186 ASSERT_EQ(0x003, atomic_fetch_and_explicit(&i, 0x2, memory_order_relaxed));
187 ASSERT_EQ(0x002, atomic_load(&i));
188}
189
Hans Boehm00aaea32014-08-19 16:14:01 -0700190// And a rudimentary test of acquire-release memory ordering:
191
192constexpr static uint_least32_t BIG = 10000000ul; // Assumed even below.
193
194struct three_atomics {
195 atomic_uint_least32_t x;
196 char a[123]; // Everything in different cache lines,
197 // increase chance of compiler getting alignment wrong.
198 atomic_uint_least32_t y;
199 char b[4013];
200 atomic_uint_least32_t z;
201};
202
203// Very simple acquire/release memory ordering sanity check.
204static void* writer(void* arg) {
205 three_atomics* a = reinterpret_cast<three_atomics*>(arg);
206 for (uint_least32_t i = 0; i <= BIG; i+=2) {
207 atomic_store_explicit(&a->x, i, memory_order_relaxed);
208 atomic_store_explicit(&a->z, i, memory_order_relaxed);
209 atomic_store_explicit(&a->y, i, memory_order_release);
210 atomic_store_explicit(&a->x, i+1, memory_order_relaxed);
211 atomic_store_explicit(&a->z, i+1, memory_order_relaxed);
212 atomic_store_explicit(&a->y, i+1, memory_order_release);
213 }
Yi Kong32bc0fc2018-08-02 17:31:13 -0700214 return nullptr;
Hans Boehm00aaea32014-08-19 16:14:01 -0700215}
216
217static void* reader(void* arg) {
218 three_atomics* a = reinterpret_cast<three_atomics*>(arg);
219 uint_least32_t xval = 0, yval = 0, zval = 0;
220 size_t repeat = 0;
221 size_t repeat_limit = 1000;
222 while (yval != BIG + 1) {
223 yval = atomic_load_explicit(&a->y, memory_order_acquire);
224 zval = atomic_load_explicit(&a->z, memory_order_relaxed);
225 xval = atomic_load_explicit(&a->x, memory_order_relaxed);
226 // If we see a given value of y, the immediately preceding
227 // stores to z and x, or later ones, should also be visible.
228 if (zval < yval) {
229 // Cant just ASSERT, since we are in a non-void function.
230 ADD_FAILURE() << "acquire-release ordering violation: "
231 << zval << " < " << yval << ", " << xval << "\n";
Yi Kong32bc0fc2018-08-02 17:31:13 -0700232 return nullptr; // Only report once.
Hans Boehm00aaea32014-08-19 16:14:01 -0700233 }
234 if (xval < yval) {
235 // Cant just ASSERT, since we are in a non-void function.
236 ADD_FAILURE() << "acquire-release ordering violation: "
237 << xval << " < " << yval << ", " << zval << "\n";
Yi Kong32bc0fc2018-08-02 17:31:13 -0700238 return nullptr; // Only report once.
Hans Boehm00aaea32014-08-19 16:14:01 -0700239 }
240 if (repeat < repeat_limit) ++repeat;
241 }
242 // The following assertion is not technically guaranteed to hold.
243 // But if it fails to hold, this test was useless, and we have a
244 // serious scheduling issue that we should probably know about.
245 EXPECT_EQ(repeat, repeat_limit);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700246 return nullptr;
Hans Boehm00aaea32014-08-19 16:14:01 -0700247}
248
249TEST(stdatomic, ordering) {
250 // Run a memory ordering sanity test.
251 void* result;
252 three_atomics a;
253 atomic_init(&a.x, 0ul);
254 atomic_init(&a.y, 0ul);
255 atomic_init(&a.z, 0ul);
256 pthread_t t1,t2;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700257 ASSERT_EQ(0, pthread_create(&t1, nullptr, reader, &a));
258 ASSERT_EQ(0, pthread_create(&t2, nullptr, writer, &a));
Hans Boehm00aaea32014-08-19 16:14:01 -0700259 ASSERT_EQ(0, pthread_join(t1, &result));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700260 EXPECT_EQ(nullptr, result);
Hans Boehm00aaea32014-08-19 16:14:01 -0700261 ASSERT_EQ(0, pthread_join(t2, &result));
Yi Kong32bc0fc2018-08-02 17:31:13 -0700262 EXPECT_EQ(nullptr, result);
Hans Boehm00aaea32014-08-19 16:14:01 -0700263 EXPECT_EQ(atomic_load_explicit(&a.x, memory_order_consume), BIG + 1);
264 EXPECT_EQ(atomic_load_explicit(&a.y, memory_order_seq_cst), BIG + 1);
265 EXPECT_EQ(atomic_load(&a.z), BIG + 1);
266}