blob: b7fb19b0d242ffe50b5885116a6c6a043c56216c [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
Elliott Hughese6c57fc2014-05-23 20:06:03 -070017#include <stdatomic.h>
Hans Boehmc8cf3512014-08-19 16:14:01 -070018#include <gtest/gtest.h>
19#include <pthread.h>
20#include <stdint.h>
Elliott Hughese6c57fc2014-05-23 20:06:03 -070021
22TEST(stdatomic, LOCK_FREE) {
23 ASSERT_TRUE(ATOMIC_BOOL_LOCK_FREE);
24 ASSERT_TRUE(ATOMIC_CHAR16_T_LOCK_FREE);
25 ASSERT_TRUE(ATOMIC_CHAR32_T_LOCK_FREE);
26 ASSERT_TRUE(ATOMIC_CHAR_LOCK_FREE);
27 ASSERT_TRUE(ATOMIC_INT_LOCK_FREE);
28 ASSERT_TRUE(ATOMIC_LLONG_LOCK_FREE);
29 ASSERT_TRUE(ATOMIC_LONG_LOCK_FREE);
30 ASSERT_TRUE(ATOMIC_POINTER_LOCK_FREE);
31 ASSERT_TRUE(ATOMIC_SHORT_LOCK_FREE);
32 ASSERT_TRUE(ATOMIC_WCHAR_T_LOCK_FREE);
33}
34
35TEST(stdatomic, init) {
36 atomic_int v = ATOMIC_VAR_INIT(123);
37 ASSERT_EQ(123, atomic_load(&v));
38
39 atomic_init(&v, 456);
40 ASSERT_EQ(456, atomic_load(&v));
41
42 atomic_flag f = ATOMIC_FLAG_INIT;
43 ASSERT_FALSE(atomic_flag_test_and_set(&f));
44}
45
46TEST(stdatomic, atomic_thread_fence) {
47 atomic_thread_fence(memory_order_relaxed);
48 atomic_thread_fence(memory_order_consume);
49 atomic_thread_fence(memory_order_acquire);
50 atomic_thread_fence(memory_order_release);
51 atomic_thread_fence(memory_order_acq_rel);
52 atomic_thread_fence(memory_order_seq_cst);
53}
54
55TEST(stdatomic, atomic_signal_fence) {
56 atomic_signal_fence(memory_order_relaxed);
57 atomic_signal_fence(memory_order_consume);
58 atomic_signal_fence(memory_order_acquire);
59 atomic_signal_fence(memory_order_release);
60 atomic_signal_fence(memory_order_acq_rel);
61 atomic_signal_fence(memory_order_seq_cst);
62}
63
64TEST(stdatomic, atomic_is_lock_free) {
65 atomic_char small;
Elliott Hughese6c57fc2014-05-23 20:06:03 -070066 ASSERT_TRUE(atomic_is_lock_free(&small));
Hans Boehm9ac60bf2014-08-28 15:21:32 -070067#if defined(__clang__) || __GNUC_PREREQ(4, 7)
68 // Otherwise stdatomic.h doesn't handle this.
69 atomic_intmax_t big;
Raghu Gandham34b258d2014-07-24 15:56:51 -070070 // atomic_intmax_t(size = 64) is not lock free on mips32.
71#if defined(__mips__) && !defined(__LP64__)
72 ASSERT_FALSE(atomic_is_lock_free(&big));
73#else
Elliott Hughese6c57fc2014-05-23 20:06:03 -070074 ASSERT_TRUE(atomic_is_lock_free(&big));
Raghu Gandham34b258d2014-07-24 15:56:51 -070075#endif
Hans Boehm9ac60bf2014-08-28 15:21:32 -070076#endif
Elliott Hughese6c57fc2014-05-23 20:06:03 -070077}
78
79TEST(stdatomic, atomic_flag) {
80 atomic_flag f = ATOMIC_FLAG_INIT;
81 ASSERT_FALSE(atomic_flag_test_and_set(&f));
82 ASSERT_TRUE(atomic_flag_test_and_set(&f));
83
84 atomic_flag_clear(&f);
85
86 ASSERT_FALSE(atomic_flag_test_and_set_explicit(&f, memory_order_relaxed));
87 ASSERT_TRUE(atomic_flag_test_and_set_explicit(&f, memory_order_relaxed));
88
89 atomic_flag_clear_explicit(&f, memory_order_relaxed);
90 ASSERT_FALSE(atomic_flag_test_and_set_explicit(&f, memory_order_relaxed));
91}
92
93TEST(stdatomic, atomic_store) {
94 atomic_int i;
95 atomic_store(&i, 123);
96 ASSERT_EQ(123, atomic_load(&i));
97 atomic_store_explicit(&i, 123, memory_order_relaxed);
98 ASSERT_EQ(123, atomic_load_explicit(&i, memory_order_relaxed));
99}
100
101TEST(stdatomic, atomic_exchange) {
102 atomic_int i;
103 atomic_store(&i, 123);
104 ASSERT_EQ(123, atomic_exchange(&i, 456));
105 ASSERT_EQ(456, atomic_exchange_explicit(&i, 123, memory_order_relaxed));
106}
107
108TEST(stdatomic, atomic_compare_exchange) {
109 atomic_int i;
Dan Albert6b3beb22014-05-28 16:27:32 -0700110 int expected;
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700111
112 atomic_store(&i, 123);
Dan Albert6b3beb22014-05-28 16:27:32 -0700113 expected = 123;
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700114 ASSERT_TRUE(atomic_compare_exchange_strong(&i, &expected, 456));
115 ASSERT_FALSE(atomic_compare_exchange_strong(&i, &expected, 456));
Dan Albert6b3beb22014-05-28 16:27:32 -0700116 ASSERT_EQ(456, expected);
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700117
118 atomic_store(&i, 123);
Dan Albert6b3beb22014-05-28 16:27:32 -0700119 expected = 123;
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700120 ASSERT_TRUE(atomic_compare_exchange_strong_explicit(&i, &expected, 456, memory_order_relaxed, memory_order_relaxed));
121 ASSERT_FALSE(atomic_compare_exchange_strong_explicit(&i, &expected, 456, memory_order_relaxed, memory_order_relaxed));
Dan Albert6b3beb22014-05-28 16:27:32 -0700122 ASSERT_EQ(456, expected);
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700123
124 atomic_store(&i, 123);
Dan Albert6b3beb22014-05-28 16:27:32 -0700125 expected = 123;
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700126 ASSERT_TRUE(atomic_compare_exchange_weak(&i, &expected, 456));
127 ASSERT_FALSE(atomic_compare_exchange_weak(&i, &expected, 456));
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;
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700132 ASSERT_TRUE(atomic_compare_exchange_weak_explicit(&i, &expected, 456, memory_order_relaxed, memory_order_relaxed));
133 ASSERT_FALSE(atomic_compare_exchange_weak_explicit(&i, &expected, 456, memory_order_relaxed, memory_order_relaxed));
Dan Albert6b3beb22014-05-28 16:27:32 -0700134 ASSERT_EQ(456, expected);
Elliott Hughese6c57fc2014-05-23 20:06:03 -0700135}
136
137TEST(stdatomic, atomic_fetch_add) {
138 atomic_int i = ATOMIC_VAR_INIT(123);
139 ASSERT_EQ(123, atomic_fetch_add(&i, 1));
140 ASSERT_EQ(124, atomic_fetch_add_explicit(&i, 1, memory_order_relaxed));
141 ASSERT_EQ(125, atomic_load(&i));
142}
143
144TEST(stdatomic, atomic_fetch_sub) {
145 atomic_int i = ATOMIC_VAR_INIT(123);
146 ASSERT_EQ(123, atomic_fetch_sub(&i, 1));
147 ASSERT_EQ(122, atomic_fetch_sub_explicit(&i, 1, memory_order_relaxed));
148 ASSERT_EQ(121, atomic_load(&i));
149}
150
151TEST(stdatomic, atomic_fetch_or) {
152 atomic_int i = ATOMIC_VAR_INIT(0x100);
153 ASSERT_EQ(0x100, atomic_fetch_or(&i, 0x020));
154 ASSERT_EQ(0x120, atomic_fetch_or_explicit(&i, 0x003, memory_order_relaxed));
155 ASSERT_EQ(0x123, atomic_load(&i));
156}
157
158TEST(stdatomic, atomic_fetch_xor) {
159 atomic_int i = ATOMIC_VAR_INIT(0x100);
160 ASSERT_EQ(0x100, atomic_fetch_xor(&i, 0x120));
161 ASSERT_EQ(0x020, atomic_fetch_xor_explicit(&i, 0x103, memory_order_relaxed));
162 ASSERT_EQ(0x123, atomic_load(&i));
163}
164
165TEST(stdatomic, atomic_fetch_and) {
166 atomic_int i = ATOMIC_VAR_INIT(0x123);
167 ASSERT_EQ(0x123, atomic_fetch_and(&i, 0x00f));
168 ASSERT_EQ(0x003, atomic_fetch_and_explicit(&i, 0x2, memory_order_relaxed));
169 ASSERT_EQ(0x002, atomic_load(&i));
170}
171
Hans Boehmc8cf3512014-08-19 16:14:01 -0700172// And a rudimentary test of acquire-release memory ordering:
173
174constexpr static uint_least32_t BIG = 10000000ul; // Assumed even below.
175
176struct three_atomics {
177 atomic_uint_least32_t x;
178 char a[123]; // Everything in different cache lines,
179 // increase chance of compiler getting alignment wrong.
180 atomic_uint_least32_t y;
181 char b[4013];
182 atomic_uint_least32_t z;
183};
184
185// Very simple acquire/release memory ordering sanity check.
186static void* writer(void* arg) {
187 three_atomics* a = reinterpret_cast<three_atomics*>(arg);
188 for (uint_least32_t i = 0; i <= BIG; i+=2) {
189 atomic_store_explicit(&a->x, i, memory_order_relaxed);
190 atomic_store_explicit(&a->z, i, memory_order_relaxed);
191 atomic_store_explicit(&a->y, i, memory_order_release);
192 atomic_store_explicit(&a->x, i+1, memory_order_relaxed);
193 atomic_store_explicit(&a->z, i+1, memory_order_relaxed);
194 atomic_store_explicit(&a->y, i+1, memory_order_release);
195 }
196 return 0;
197}
198
199static void* reader(void* arg) {
200 three_atomics* a = reinterpret_cast<three_atomics*>(arg);
201 uint_least32_t xval = 0, yval = 0, zval = 0;
202 size_t repeat = 0;
203 size_t repeat_limit = 1000;
204 while (yval != BIG + 1) {
205 yval = atomic_load_explicit(&a->y, memory_order_acquire);
206 zval = atomic_load_explicit(&a->z, memory_order_relaxed);
207 xval = atomic_load_explicit(&a->x, memory_order_relaxed);
208 // If we see a given value of y, the immediately preceding
209 // stores to z and x, or later ones, should also be visible.
210 if (zval < yval) {
211 // Cant just ASSERT, since we are in a non-void function.
212 ADD_FAILURE() << "acquire-release ordering violation: "
213 << zval << " < " << yval << ", " << xval << "\n";
214 return 0; // Only report once.
215 }
216 if (xval < yval) {
217 // Cant just ASSERT, since we are in a non-void function.
218 ADD_FAILURE() << "acquire-release ordering violation: "
219 << xval << " < " << yval << ", " << zval << "\n";
220 return 0; // Only report once.
221 }
222 if (repeat < repeat_limit) ++repeat;
223 }
224 // The following assertion is not technically guaranteed to hold.
225 // But if it fails to hold, this test was useless, and we have a
226 // serious scheduling issue that we should probably know about.
227 EXPECT_EQ(repeat, repeat_limit);
228 return 0;
229}
230
231TEST(stdatomic, ordering) {
232 // Run a memory ordering sanity test.
233 void* result;
234 three_atomics a;
235 atomic_init(&a.x, 0ul);
236 atomic_init(&a.y, 0ul);
237 atomic_init(&a.z, 0ul);
238 pthread_t t1,t2;
239 ASSERT_EQ(0, pthread_create(&t1, 0, reader, &a));
240 ASSERT_EQ(0, pthread_create(&t2, 0, writer, &a));
241 ASSERT_EQ(0, pthread_join(t1, &result));
242 EXPECT_EQ(0, result);
243 ASSERT_EQ(0, pthread_join(t2, &result));
244 EXPECT_EQ(0, result);
245 EXPECT_EQ(atomic_load_explicit(&a.x, memory_order_consume), BIG + 1);
246 EXPECT_EQ(atomic_load_explicit(&a.y, memory_order_seq_cst), BIG + 1);
247 EXPECT_EQ(atomic_load(&a.z), BIG + 1);
248}