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