Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: e18fc632afc6626686eb8cdb16b08250886a9c92 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2011 The Chromium Authors
[email protected]b492eae2008-08-29 09:45:092// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]b492eae2008-08-29 09:45:095#include "base/atomicops.h"
[email protected]2edc2862011-04-04 18:04:376
[email protected]da4639672014-04-08 04:00:367#include <stdint.h>
[email protected]2edc2862011-04-04 18:04:378#include <string.h>
Eugene Zemtsovd29b01732024-12-19 21:20:129
Venkatesh Srinivas325a5c22023-05-29 13:43:4810#include <type_traits>
Eugene Zemtsovd29b01732024-12-19 21:20:1211#include <vector>
[email protected]2edc2862011-04-04 18:04:3712
Tom Sepez7859fac52025-07-22 19:34:1613#include "base/compiler_specific.h"
[email protected]b492eae2008-08-29 09:45:0914#include "testing/gtest/include/gtest/gtest.h"
15
16template <class AtomicType>
17static void TestAtomicIncrement() {
18 // For now, we just test single threaded execution
19
20 // use a guard value to make sure the NoBarrier_AtomicIncrement doesn't go
21 // outside the expected address bounds. This is in particular to
22 // test that some future change to the asm code doesn't cause the
23 // 32-bit NoBarrier_AtomicIncrement doesn't do the wrong thing on 64-bit
24 // machines.
25 struct {
26 AtomicType prev_word;
27 AtomicType count;
28 AtomicType next_word;
29 } s;
30
31 AtomicType prev_word_value, next_word_value;
Tom Sepez7859fac52025-07-22 19:34:1632 UNSAFE_TODO(memset(&prev_word_value, 0xFF, sizeof(AtomicType)));
33 UNSAFE_TODO(memset(&next_word_value, 0xEE, sizeof(AtomicType)));
[email protected]b492eae2008-08-29 09:45:0934
35 s.prev_word = prev_word_value;
36 s.count = 0;
37 s.next_word = next_word_value;
38
39 EXPECT_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, 1), 1);
40 EXPECT_EQ(s.count, 1);
41 EXPECT_EQ(s.prev_word, prev_word_value);
42 EXPECT_EQ(s.next_word, next_word_value);
43
44 EXPECT_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, 2), 3);
45 EXPECT_EQ(s.count, 3);
46 EXPECT_EQ(s.prev_word, prev_word_value);
47 EXPECT_EQ(s.next_word, next_word_value);
48
49 EXPECT_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, 3), 6);
50 EXPECT_EQ(s.count, 6);
51 EXPECT_EQ(s.prev_word, prev_word_value);
52 EXPECT_EQ(s.next_word, next_word_value);
53
54 EXPECT_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, -3), 3);
55 EXPECT_EQ(s.count, 3);
56 EXPECT_EQ(s.prev_word, prev_word_value);
57 EXPECT_EQ(s.next_word, next_word_value);
58
59 EXPECT_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, -2), 1);
60 EXPECT_EQ(s.count, 1);
61 EXPECT_EQ(s.prev_word, prev_word_value);
62 EXPECT_EQ(s.next_word, next_word_value);
63
64 EXPECT_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, -1), 0);
65 EXPECT_EQ(s.count, 0);
66 EXPECT_EQ(s.prev_word, prev_word_value);
67 EXPECT_EQ(s.next_word, next_word_value);
68
69 EXPECT_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, -1), -1);
70 EXPECT_EQ(s.count, -1);
71 EXPECT_EQ(s.prev_word, prev_word_value);
72 EXPECT_EQ(s.next_word, next_word_value);
73
74 EXPECT_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, -4), -5);
75 EXPECT_EQ(s.count, -5);
76 EXPECT_EQ(s.prev_word, prev_word_value);
77 EXPECT_EQ(s.next_word, next_word_value);
78
79 EXPECT_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, 5), 0);
80 EXPECT_EQ(s.count, 0);
81 EXPECT_EQ(s.prev_word, prev_word_value);
82 EXPECT_EQ(s.next_word, next_word_value);
83}
84
[email protected]b492eae2008-08-29 09:45:0985#define NUM_BITS(T) (sizeof(T) * 8)
86
[email protected]b492eae2008-08-29 09:45:0987template <class AtomicType>
88static void TestCompareAndSwap() {
89 AtomicType value = 0;
90 AtomicType prev = base::subtle::NoBarrier_CompareAndSwap(&value, 0, 1);
91 EXPECT_EQ(1, value);
92 EXPECT_EQ(0, prev);
93
bcwhite85c5a0a2015-11-03 18:19:0694 // Verify that CAS will *not* change "value" if it doesn't match the
95 // expected number. CAS will always return the actual value of the
96 // variable from before any change.
97 AtomicType fail = base::subtle::NoBarrier_CompareAndSwap(&value, 0, 2);
98 EXPECT_EQ(1, value);
99 EXPECT_EQ(1, fail);
100
[email protected]b492eae2008-08-29 09:45:09101 // Use test value that has non-zero bits in both halves, more for testing
102 // 64-bit implementation on 32-bit platforms.
Peter Kasting134ef9af2024-12-28 02:30:09103 const AtomicType k_test_val =
104 (static_cast<uint64_t>(1) << (NUM_BITS(AtomicType) - 2)) + 11;
[email protected]b492eae2008-08-29 09:45:09105 value = k_test_val;
106 prev = base::subtle::NoBarrier_CompareAndSwap(&value, 0, 5);
107 EXPECT_EQ(k_test_val, value);
108 EXPECT_EQ(k_test_val, prev);
109
110 value = k_test_val;
111 prev = base::subtle::NoBarrier_CompareAndSwap(&value, k_test_val, 5);
112 EXPECT_EQ(5, value);
113 EXPECT_EQ(k_test_val, prev);
114}
115
[email protected]b492eae2008-08-29 09:45:09116template <class AtomicType>
117static void TestAtomicExchange() {
118 AtomicType value = 0;
119 AtomicType new_value = base::subtle::NoBarrier_AtomicExchange(&value, 1);
120 EXPECT_EQ(1, value);
121 EXPECT_EQ(0, new_value);
122
123 // Use test value that has non-zero bits in both halves, more for testing
124 // 64-bit implementation on 32-bit platforms.
Peter Kasting134ef9af2024-12-28 02:30:09125 const AtomicType k_test_val =
126 (static_cast<uint64_t>(1) << (NUM_BITS(AtomicType) - 2)) + 11;
[email protected]b492eae2008-08-29 09:45:09127 value = k_test_val;
128 new_value = base::subtle::NoBarrier_AtomicExchange(&value, k_test_val);
129 EXPECT_EQ(k_test_val, value);
130 EXPECT_EQ(k_test_val, new_value);
131
132 value = k_test_val;
133 new_value = base::subtle::NoBarrier_AtomicExchange(&value, 5);
134 EXPECT_EQ(5, value);
135 EXPECT_EQ(k_test_val, new_value);
136}
137
[email protected]b492eae2008-08-29 09:45:09138template <class AtomicType>
139static void TestAtomicIncrementBounds() {
140 // Test at rollover boundary between int_max and int_min
Peter Kasting134ef9af2024-12-28 02:30:09141 AtomicType test_val =
142 (static_cast<uint64_t>(1) << (NUM_BITS(AtomicType) - 1));
[email protected]b492eae2008-08-29 09:45:09143 AtomicType value = -1 ^ test_val;
144 AtomicType new_value = base::subtle::NoBarrier_AtomicIncrement(&value, 1);
145 EXPECT_EQ(test_val, value);
146 EXPECT_EQ(value, new_value);
147
148 base::subtle::NoBarrier_AtomicIncrement(&value, -1);
149 EXPECT_EQ(-1 ^ test_val, value);
150
151 // Test at 32-bit boundary for 64-bit atomic type.
[email protected]da4639672014-04-08 04:00:36152 test_val = static_cast<uint64_t>(1) << (NUM_BITS(AtomicType) / 2);
[email protected]b492eae2008-08-29 09:45:09153 value = test_val - 1;
154 new_value = base::subtle::NoBarrier_AtomicIncrement(&value, 1);
155 EXPECT_EQ(test_val, value);
156 EXPECT_EQ(value, new_value);
157
158 base::subtle::NoBarrier_AtomicIncrement(&value, -1);
159 EXPECT_EQ(test_val - 1, value);
160}
161
[email protected]ff845a4e2008-08-29 10:16:14162// Return an AtomicType with the value 0xa5a5a5..
163template <class AtomicType>
164static AtomicType TestFillValue() {
165 AtomicType val = 0;
Tom Sepez7859fac52025-07-22 19:34:16166 UNSAFE_TODO(memset(&val, 0xa5, sizeof(AtomicType)));
[email protected]ff845a4e2008-08-29 10:16:14167 return val;
168}
169
[email protected]b492eae2008-08-29 09:45:09170// This is a simple sanity check that values are correct. Not testing
171// atomicity
172template <class AtomicType>
173static void TestStore() {
[email protected]ff845a4e2008-08-29 10:16:14174 const AtomicType kVal1 = TestFillValue<AtomicType>();
[email protected]b492eae2008-08-29 09:45:09175 const AtomicType kVal2 = static_cast<AtomicType>(-1);
176
177 AtomicType value;
178
Andrew Rayskiy629912382023-10-18 22:58:42179 if constexpr (std::is_same_v<AtomicType, base::subtle::Atomic32>) {
Venkatesh Srinivas325a5c22023-05-29 13:43:48180 base::subtle::NoBarrier_Store(&value, kVal1);
181 EXPECT_EQ(kVal1, value);
182 base::subtle::NoBarrier_Store(&value, kVal2);
183 EXPECT_EQ(kVal2, value);
184 }
[email protected]b492eae2008-08-29 09:45:09185
[email protected]b492eae2008-08-29 09:45:09186 base::subtle::Release_Store(&value, kVal1);
187 EXPECT_EQ(kVal1, value);
188 base::subtle::Release_Store(&value, kVal2);
189 EXPECT_EQ(kVal2, value);
190}
191
192// This is a simple sanity check that values are correct. Not testing
193// atomicity
194template <class AtomicType>
195static void TestLoad() {
[email protected]ff845a4e2008-08-29 10:16:14196 const AtomicType kVal1 = TestFillValue<AtomicType>();
[email protected]b492eae2008-08-29 09:45:09197 const AtomicType kVal2 = static_cast<AtomicType>(-1);
198
199 AtomicType value;
200
201 value = kVal1;
202 EXPECT_EQ(kVal1, base::subtle::NoBarrier_Load(&value));
203 value = kVal2;
204 EXPECT_EQ(kVal2, base::subtle::NoBarrier_Load(&value));
205
206 value = kVal1;
207 EXPECT_EQ(kVal1, base::subtle::Acquire_Load(&value));
208 value = kVal2;
209 EXPECT_EQ(kVal2, base::subtle::Acquire_Load(&value));
[email protected]b492eae2008-08-29 09:45:09210}
211
212TEST(AtomicOpsTest, Inc) {
213 TestAtomicIncrement<base::subtle::Atomic32>();
214 TestAtomicIncrement<base::subtle::AtomicWord>();
215}
216
217TEST(AtomicOpsTest, CompareAndSwap) {
218 TestCompareAndSwap<base::subtle::Atomic32>();
[email protected]b492eae2008-08-29 09:45:09219}
220
221TEST(AtomicOpsTest, Exchange) {
222 TestAtomicExchange<base::subtle::Atomic32>();
223 TestAtomicExchange<base::subtle::AtomicWord>();
224}
225
226TEST(AtomicOpsTest, IncrementBounds) {
227 TestAtomicIncrementBounds<base::subtle::Atomic32>();
228 TestAtomicIncrementBounds<base::subtle::AtomicWord>();
229}
230
231TEST(AtomicOpsTest, Store) {
232 TestStore<base::subtle::Atomic32>();
233 TestStore<base::subtle::AtomicWord>();
234}
235
236TEST(AtomicOpsTest, Load) {
237 TestLoad<base::subtle::Atomic32>();
238 TestLoad<base::subtle::AtomicWord>();
239}
Eugene Zemtsovd29b01732024-12-19 21:20:12240
241TEST(AtomicOpsTest, RelaxedAtomicWriteMemcpy) {
242 std::vector<uint8_t> src(17);
243 for (size_t i = 0; i < src.size(); i++) {
244 src[i] = i + 1;
245 }
246
247 for (size_t i = 0; i < src.size(); i++) {
248 std::vector<uint8_t> dst(src.size());
249 size_t bytes_to_copy = src.size() - i;
250 base::subtle::RelaxedAtomicWriteMemcpy(
251 base::span(dst).first(bytes_to_copy),
252 base::span(src).subspan(i, bytes_to_copy));
253 for (size_t j = 0; j < bytes_to_copy; j++) {
254 EXPECT_EQ(src[i + j], dst[j]);
255 }
256 for (size_t j = bytes_to_copy; j < dst.size(); j++) {
257 EXPECT_EQ(0, dst[j]);
258 }
259 }
260}