Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/atomicops.h" | 5 #include "base/atomicops.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 82 #define NUM_BITS(T) (sizeof(T) * 8) | 82 #define NUM_BITS(T) (sizeof(T) * 8) |
| 83 | 83 |
| 84 | 84 |
| 85 template <class AtomicType> | 85 template <class AtomicType> |
| 86 static void TestCompareAndSwap() { | 86 static void TestCompareAndSwap() { |
| 87 AtomicType value = 0; | 87 AtomicType value = 0; |
| 88 AtomicType prev = base::subtle::NoBarrier_CompareAndSwap(&value, 0, 1); | 88 AtomicType prev = base::subtle::NoBarrier_CompareAndSwap(&value, 0, 1); |
| 89 EXPECT_EQ(1, value); | 89 EXPECT_EQ(1, value); |
| 90 EXPECT_EQ(0, prev); | 90 EXPECT_EQ(0, prev); |
| 91 | 91 |
| 92 // Verify that CAS will *not* change "value" if it doesn't match the | |
| 93 // expected number. CAS will always return the actual value of the | |
|
Alexander Potapenko
2015/11/02 23:06:43
Please remove the double space.
bcwhite
2015/11/03 15:11:36
Done. (As I said in our other review... It's sim
| |
| 94 // variable from before any change. | |
| 95 AtomicType fail = base::subtle::NoBarrier_CompareAndSwap(&value, 0, 2); | |
| 96 EXPECT_EQ(1, value); | |
| 97 EXPECT_EQ(1, fail); | |
| 98 | |
| 92 // Use test value that has non-zero bits in both halves, more for testing | 99 // Use test value that has non-zero bits in both halves, more for testing |
| 93 // 64-bit implementation on 32-bit platforms. | 100 // 64-bit implementation on 32-bit platforms. |
| 94 const AtomicType k_test_val = (static_cast<uint64_t>(1) << | 101 const AtomicType k_test_val = (static_cast<uint64_t>(1) << |
| 95 (NUM_BITS(AtomicType) - 2)) + 11; | 102 (NUM_BITS(AtomicType) - 2)) + 11; |
| 96 value = k_test_val; | 103 value = k_test_val; |
| 97 prev = base::subtle::NoBarrier_CompareAndSwap(&value, 0, 5); | 104 prev = base::subtle::NoBarrier_CompareAndSwap(&value, 0, 5); |
| 98 EXPECT_EQ(k_test_val, value); | 105 EXPECT_EQ(k_test_val, value); |
| 99 EXPECT_EQ(k_test_val, prev); | 106 EXPECT_EQ(k_test_val, prev); |
| 100 | 107 |
| 101 value = k_test_val; | 108 value = k_test_val; |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 232 | 239 |
| 233 TEST(AtomicOpsTest, Store) { | 240 TEST(AtomicOpsTest, Store) { |
| 234 TestStore<base::subtle::Atomic32>(); | 241 TestStore<base::subtle::Atomic32>(); |
| 235 TestStore<base::subtle::AtomicWord>(); | 242 TestStore<base::subtle::AtomicWord>(); |
| 236 } | 243 } |
| 237 | 244 |
| 238 TEST(AtomicOpsTest, Load) { | 245 TEST(AtomicOpsTest, Load) { |
| 239 TestLoad<base::subtle::Atomic32>(); | 246 TestLoad<base::subtle::Atomic32>(); |
| 240 TestLoad<base::subtle::AtomicWord>(); | 247 TestLoad<base::subtle::AtomicWord>(); |
| 241 } | 248 } |
| OLD | NEW |