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