| 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> | |
| 8 #include <string.h> | 7 #include <string.h> |
| 9 | 8 |
| 9 #include "base/port.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 template <class AtomicType> | 12 template <class AtomicType> |
| 13 static void TestAtomicIncrement() { | 13 static void TestAtomicIncrement() { |
| 14 // For now, we just test single threaded execution | 14 // For now, we just test single threaded execution |
| 15 | 15 |
| 16 // use a guard value to make sure the NoBarrier_AtomicIncrement doesn't go | 16 // use a guard value to make sure the NoBarrier_AtomicIncrement doesn't go |
| 17 // outside the expected address bounds. This is in particular to | 17 // outside the expected address bounds. This is in particular to |
| 18 // test that some future change to the asm code doesn't cause the | 18 // test that some future change to the asm code doesn't cause the |
| 19 // 32-bit NoBarrier_AtomicIncrement doesn't do the wrong thing on 64-bit | 19 // 32-bit NoBarrier_AtomicIncrement doesn't do the wrong thing on 64-bit |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 // Use test value that has non-zero bits in both halves, more for testing | 92 // Use test value that has non-zero bits in both halves, more for testing |
| 93 // 64-bit implementation on 32-bit platforms. | 93 // 64-bit implementation on 32-bit platforms. |
| 94 const AtomicType k_test_val = (static_cast<uint64_t>(1) << | 94 const AtomicType k_test_val = (GG_ULONGLONG(1) << |
| 95 (NUM_BITS(AtomicType) - 2)) + 11; | 95 (NUM_BITS(AtomicType) - 2)) + 11; |
| 96 value = k_test_val; | 96 value = k_test_val; |
| 97 prev = base::subtle::NoBarrier_CompareAndSwap(&value, 0, 5); | 97 prev = base::subtle::NoBarrier_CompareAndSwap(&value, 0, 5); |
| 98 EXPECT_EQ(k_test_val, value); | 98 EXPECT_EQ(k_test_val, value); |
| 99 EXPECT_EQ(k_test_val, prev); | 99 EXPECT_EQ(k_test_val, prev); |
| 100 | 100 |
| 101 value = k_test_val; | 101 value = k_test_val; |
| 102 prev = base::subtle::NoBarrier_CompareAndSwap(&value, k_test_val, 5); | 102 prev = base::subtle::NoBarrier_CompareAndSwap(&value, k_test_val, 5); |
| 103 EXPECT_EQ(5, value); | 103 EXPECT_EQ(5, value); |
| 104 EXPECT_EQ(k_test_val, prev); | 104 EXPECT_EQ(k_test_val, prev); |
| 105 } | 105 } |
| 106 | 106 |
| 107 | 107 |
| 108 template <class AtomicType> | 108 template <class AtomicType> |
| 109 static void TestAtomicExchange() { | 109 static void TestAtomicExchange() { |
| 110 AtomicType value = 0; | 110 AtomicType value = 0; |
| 111 AtomicType new_value = base::subtle::NoBarrier_AtomicExchange(&value, 1); | 111 AtomicType new_value = base::subtle::NoBarrier_AtomicExchange(&value, 1); |
| 112 EXPECT_EQ(1, value); | 112 EXPECT_EQ(1, value); |
| 113 EXPECT_EQ(0, new_value); | 113 EXPECT_EQ(0, new_value); |
| 114 | 114 |
| 115 // Use test value that has non-zero bits in both halves, more for testing | 115 // Use test value that has non-zero bits in both halves, more for testing |
| 116 // 64-bit implementation on 32-bit platforms. | 116 // 64-bit implementation on 32-bit platforms. |
| 117 const AtomicType k_test_val = (static_cast<uint64_t>(1) << | 117 const AtomicType k_test_val = (GG_ULONGLONG(1) << |
| 118 (NUM_BITS(AtomicType) - 2)) + 11; | 118 (NUM_BITS(AtomicType) - 2)) + 11; |
| 119 value = k_test_val; | 119 value = k_test_val; |
| 120 new_value = base::subtle::NoBarrier_AtomicExchange(&value, k_test_val); | 120 new_value = base::subtle::NoBarrier_AtomicExchange(&value, k_test_val); |
| 121 EXPECT_EQ(k_test_val, value); | 121 EXPECT_EQ(k_test_val, value); |
| 122 EXPECT_EQ(k_test_val, new_value); | 122 EXPECT_EQ(k_test_val, new_value); |
| 123 | 123 |
| 124 value = k_test_val; | 124 value = k_test_val; |
| 125 new_value = base::subtle::NoBarrier_AtomicExchange(&value, 5); | 125 new_value = base::subtle::NoBarrier_AtomicExchange(&value, 5); |
| 126 EXPECT_EQ(5, value); | 126 EXPECT_EQ(5, value); |
| 127 EXPECT_EQ(k_test_val, new_value); | 127 EXPECT_EQ(k_test_val, new_value); |
| 128 } | 128 } |
| 129 | 129 |
| 130 | 130 |
| 131 template <class AtomicType> | 131 template <class AtomicType> |
| 132 static void TestAtomicIncrementBounds() { | 132 static void TestAtomicIncrementBounds() { |
| 133 // Test at rollover boundary between int_max and int_min | 133 // Test at rollover boundary between int_max and int_min |
| 134 AtomicType test_val = (static_cast<uint64_t>(1) << | 134 AtomicType test_val = (GG_ULONGLONG(1) << |
| 135 (NUM_BITS(AtomicType) - 1)); | 135 (NUM_BITS(AtomicType) - 1)); |
| 136 AtomicType value = -1 ^ test_val; | 136 AtomicType value = -1 ^ test_val; |
| 137 AtomicType new_value = base::subtle::NoBarrier_AtomicIncrement(&value, 1); | 137 AtomicType new_value = base::subtle::NoBarrier_AtomicIncrement(&value, 1); |
| 138 EXPECT_EQ(test_val, value); | 138 EXPECT_EQ(test_val, value); |
| 139 EXPECT_EQ(value, new_value); | 139 EXPECT_EQ(value, new_value); |
| 140 | 140 |
| 141 base::subtle::NoBarrier_AtomicIncrement(&value, -1); | 141 base::subtle::NoBarrier_AtomicIncrement(&value, -1); |
| 142 EXPECT_EQ(-1 ^ test_val, value); | 142 EXPECT_EQ(-1 ^ test_val, value); |
| 143 | 143 |
| 144 // Test at 32-bit boundary for 64-bit atomic type. | 144 // Test at 32-bit boundary for 64-bit atomic type. |
| 145 test_val = static_cast<uint64_t>(1) << (NUM_BITS(AtomicType) / 2); | 145 test_val = GG_ULONGLONG(1) << (NUM_BITS(AtomicType) / 2); |
| 146 value = test_val - 1; | 146 value = test_val - 1; |
| 147 new_value = base::subtle::NoBarrier_AtomicIncrement(&value, 1); | 147 new_value = base::subtle::NoBarrier_AtomicIncrement(&value, 1); |
| 148 EXPECT_EQ(test_val, value); | 148 EXPECT_EQ(test_val, value); |
| 149 EXPECT_EQ(value, new_value); | 149 EXPECT_EQ(value, new_value); |
| 150 | 150 |
| 151 base::subtle::NoBarrier_AtomicIncrement(&value, -1); | 151 base::subtle::NoBarrier_AtomicIncrement(&value, -1); |
| 152 EXPECT_EQ(test_val - 1, value); | 152 EXPECT_EQ(test_val - 1, value); |
| 153 } | 153 } |
| 154 | 154 |
| 155 // Return an AtomicType with the value 0xa5a5a5.. | 155 // Return an AtomicType with the value 0xa5a5a5.. |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 | 232 |
| 233 TEST(AtomicOpsTest, Store) { | 233 TEST(AtomicOpsTest, Store) { |
| 234 TestStore<base::subtle::Atomic32>(); | 234 TestStore<base::subtle::Atomic32>(); |
| 235 TestStore<base::subtle::AtomicWord>(); | 235 TestStore<base::subtle::AtomicWord>(); |
| 236 } | 236 } |
| 237 | 237 |
| 238 TEST(AtomicOpsTest, Load) { | 238 TEST(AtomicOpsTest, Load) { |
| 239 TestLoad<base::subtle::Atomic32>(); | 239 TestLoad<base::subtle::Atomic32>(); |
| 240 TestLoad<base::subtle::AtomicWord>(); | 240 TestLoad<base::subtle::AtomicWord>(); |
| 241 } | 241 } |
| OLD | NEW |