| OLD | NEW |
| 1 /* Copyright (c) 2006, Google Inc. | 1 /* Copyright (c) 2006, Google Inc. |
| 2 * All rights reserved. | 2 * All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 } s; | 53 } s; |
| 54 | 54 |
| 55 AtomicType prev_word_value, next_word_value; | 55 AtomicType prev_word_value, next_word_value; |
| 56 memset(&prev_word_value, 0xFF, sizeof(AtomicType)); | 56 memset(&prev_word_value, 0xFF, sizeof(AtomicType)); |
| 57 memset(&next_word_value, 0xEE, sizeof(AtomicType)); | 57 memset(&next_word_value, 0xEE, sizeof(AtomicType)); |
| 58 | 58 |
| 59 s.prev_word = prev_word_value; | 59 s.prev_word = prev_word_value; |
| 60 s.count = 0; | 60 s.count = 0; |
| 61 s.next_word = next_word_value; | 61 s.next_word = next_word_value; |
| 62 | 62 |
| 63 ASSERT_EQ(1, base::subtle::NoBarrier_AtomicIncrement(&s.count, 1)); | 63 CHECK_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, 1), 1); |
| 64 ASSERT_EQ(1, s.count); | 64 CHECK_EQ(s.count, 1); |
| 65 ASSERT_EQ(prev_word_value, s.prev_word); | 65 CHECK_EQ(s.prev_word, prev_word_value); |
| 66 ASSERT_EQ(next_word_value, s.next_word); | 66 CHECK_EQ(s.next_word, next_word_value); |
| 67 | 67 |
| 68 ASSERT_EQ(3, base::subtle::NoBarrier_AtomicIncrement(&s.count, 2)); | 68 CHECK_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, 2), 3); |
| 69 ASSERT_EQ(3, s.count); | 69 CHECK_EQ(s.count, 3); |
| 70 ASSERT_EQ(prev_word_value, s.prev_word); | 70 CHECK_EQ(s.prev_word, prev_word_value); |
| 71 ASSERT_EQ(next_word_value, s.next_word); | 71 CHECK_EQ(s.next_word, next_word_value); |
| 72 | 72 |
| 73 ASSERT_EQ(6, base::subtle::NoBarrier_AtomicIncrement(&s.count, 3)); | 73 CHECK_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, 3), 6); |
| 74 ASSERT_EQ(6, s.count); | 74 CHECK_EQ(s.count, 6); |
| 75 ASSERT_EQ(prev_word_value, s.prev_word); | 75 CHECK_EQ(s.prev_word, prev_word_value); |
| 76 ASSERT_EQ(next_word_value, s.next_word); | 76 CHECK_EQ(s.next_word, next_word_value); |
| 77 | 77 |
| 78 ASSERT_EQ(3, base::subtle::NoBarrier_AtomicIncrement(&s.count, -3)); | 78 CHECK_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, -3), 3); |
| 79 ASSERT_EQ(3, s.count); | 79 CHECK_EQ(s.count, 3); |
| 80 ASSERT_EQ(prev_word_value, s.prev_word); | 80 CHECK_EQ(s.prev_word, prev_word_value); |
| 81 ASSERT_EQ(next_word_value, s.next_word); | 81 CHECK_EQ(s.next_word, next_word_value); |
| 82 | 82 |
| 83 ASSERT_EQ(1, base::subtle::NoBarrier_AtomicIncrement(&s.count, -2)); | 83 CHECK_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, -2), 1); |
| 84 ASSERT_EQ(1, s.count); | 84 CHECK_EQ(s.count, 1); |
| 85 ASSERT_EQ(prev_word_value, s.prev_word); | 85 CHECK_EQ(s.prev_word, prev_word_value); |
| 86 ASSERT_EQ(next_word_value, s.next_word); | 86 CHECK_EQ(s.next_word, next_word_value); |
| 87 | 87 |
| 88 ASSERT_EQ(0, base::subtle::NoBarrier_AtomicIncrement(&s.count, -1)); | 88 CHECK_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, -1), 0); |
| 89 ASSERT_EQ(0, s.count); | 89 CHECK_EQ(s.count, 0); |
| 90 ASSERT_EQ(prev_word_value, s.prev_word); | 90 CHECK_EQ(s.prev_word, prev_word_value); |
| 91 ASSERT_EQ(next_word_value, s.next_word); | 91 CHECK_EQ(s.next_word, next_word_value); |
| 92 | 92 |
| 93 ASSERT_EQ(-1, base::subtle::NoBarrier_AtomicIncrement(&s.count, -1)); | 93 CHECK_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, -1), -1); |
| 94 ASSERT_EQ(-1, s.count); | 94 CHECK_EQ(s.count, -1); |
| 95 ASSERT_EQ(prev_word_value, s.prev_word); | 95 CHECK_EQ(s.prev_word, prev_word_value); |
| 96 ASSERT_EQ(next_word_value, s.next_word); | 96 CHECK_EQ(s.next_word, next_word_value); |
| 97 | 97 |
| 98 ASSERT_EQ(-5, base::subtle::NoBarrier_AtomicIncrement(&s.count, -4)); | 98 CHECK_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, -4), -5); |
| 99 ASSERT_EQ(-5, s.count); | 99 CHECK_EQ(s.count, -5); |
| 100 ASSERT_EQ(prev_word_value, s.prev_word); | 100 CHECK_EQ(s.prev_word, prev_word_value); |
| 101 ASSERT_EQ(next_word_value, s.next_word); | 101 CHECK_EQ(s.next_word, next_word_value); |
| 102 | 102 |
| 103 ASSERT_EQ(0, base::subtle::NoBarrier_AtomicIncrement(&s.count, 5)); | 103 CHECK_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, 5), 0); |
| 104 ASSERT_EQ(0, s.count); | 104 CHECK_EQ(s.count, 0); |
| 105 ASSERT_EQ(prev_word_value, s.prev_word); | 105 CHECK_EQ(s.prev_word, prev_word_value); |
| 106 ASSERT_EQ(next_word_value, s.next_word); | 106 CHECK_EQ(s.next_word, next_word_value); |
| 107 } | 107 } |
| 108 | 108 |
| 109 | 109 |
| 110 #define NUM_BITS(T) (sizeof(T) * 8) | 110 #define NUM_BITS(T) (sizeof(T) * 8) |
| 111 | 111 |
| 112 | 112 |
| 113 template <class AtomicType> | 113 template <class AtomicType> |
| 114 static void TestCompareAndSwap() { | 114 static void TestCompareAndSwap() { |
| 115 AtomicType value = 0; | 115 AtomicType value = 0; |
| 116 AtomicType prev = base::subtle::NoBarrier_CompareAndSwap(&value, 0, 1); | 116 AtomicType prev = base::subtle::NoBarrier_CompareAndSwap(&value, 0, 1); |
| 117 ASSERT_EQ(1, value); | 117 CHECK_EQ(1, value); |
| 118 ASSERT_EQ(0, prev); | 118 CHECK_EQ(0, prev); |
| 119 | 119 |
| 120 // Use test value that has non-zero bits in both halves, more for testing | 120 // Use test value that has non-zero bits in both halves, more for testing |
| 121 // 64-bit implementation on 32-bit platforms. | 121 // 64-bit implementation on 32-bit platforms. |
| 122 const AtomicType k_test_val = (GG_ULONGLONG(1) << | 122 const AtomicType k_test_val = (GG_ULONGLONG(1) << |
| 123 (NUM_BITS(AtomicType) - 2)) + 11; | 123 (NUM_BITS(AtomicType) - 2)) + 11; |
| 124 value = k_test_val; | 124 value = k_test_val; |
| 125 prev = base::subtle::NoBarrier_CompareAndSwap(&value, 0, 5); | 125 prev = base::subtle::NoBarrier_CompareAndSwap(&value, 0, 5); |
| 126 ASSERT_EQ(k_test_val, value); | 126 CHECK_EQ(k_test_val, value); |
| 127 ASSERT_EQ(k_test_val, prev); | 127 CHECK_EQ(k_test_val, prev); |
| 128 | 128 |
| 129 value = k_test_val; | 129 value = k_test_val; |
| 130 prev = base::subtle::NoBarrier_CompareAndSwap(&value, k_test_val, 5); | 130 prev = base::subtle::NoBarrier_CompareAndSwap(&value, k_test_val, 5); |
| 131 ASSERT_EQ(5, value); | 131 CHECK_EQ(5, value); |
| 132 ASSERT_EQ(k_test_val, prev); | 132 CHECK_EQ(k_test_val, prev); |
| 133 } | 133 } |
| 134 | 134 |
| 135 | 135 |
| 136 template <class AtomicType> | 136 template <class AtomicType> |
| 137 static void TestAtomicExchange() { | 137 static void TestAtomicExchange() { |
| 138 AtomicType value = 0; | 138 AtomicType value = 0; |
| 139 AtomicType new_value = base::subtle::NoBarrier_AtomicExchange(&value, 1); | 139 AtomicType new_value = base::subtle::NoBarrier_AtomicExchange(&value, 1); |
| 140 ASSERT_EQ(1, value); | 140 CHECK_EQ(1, value); |
| 141 ASSERT_EQ(0, new_value); | 141 CHECK_EQ(0, new_value); |
| 142 | 142 |
| 143 // Use test value that has non-zero bits in both halves, more for testing | 143 // Use test value that has non-zero bits in both halves, more for testing |
| 144 // 64-bit implementation on 32-bit platforms. | 144 // 64-bit implementation on 32-bit platforms. |
| 145 const AtomicType k_test_val = (GG_ULONGLONG(1) << | 145 const AtomicType k_test_val = (GG_ULONGLONG(1) << |
| 146 (NUM_BITS(AtomicType) - 2)) + 11; | 146 (NUM_BITS(AtomicType) - 2)) + 11; |
| 147 value = k_test_val; | 147 value = k_test_val; |
| 148 new_value = base::subtle::NoBarrier_AtomicExchange(&value, k_test_val); | 148 new_value = base::subtle::NoBarrier_AtomicExchange(&value, k_test_val); |
| 149 ASSERT_EQ(k_test_val, value); | 149 CHECK_EQ(k_test_val, value); |
| 150 ASSERT_EQ(k_test_val, new_value); | 150 CHECK_EQ(k_test_val, new_value); |
| 151 | 151 |
| 152 value = k_test_val; | 152 value = k_test_val; |
| 153 new_value = base::subtle::NoBarrier_AtomicExchange(&value, 5); | 153 new_value = base::subtle::NoBarrier_AtomicExchange(&value, 5); |
| 154 ASSERT_EQ(5, value); | 154 CHECK_EQ(5, value); |
| 155 ASSERT_EQ(k_test_val, new_value); | 155 CHECK_EQ(k_test_val, new_value); |
| 156 } | 156 } |
| 157 | 157 |
| 158 | 158 |
| 159 template <class AtomicType> | 159 template <class AtomicType> |
| 160 static void TestAtomicIncrementBounds() { | 160 static void TestAtomicIncrementBounds() { |
| 161 // Test increment at the half-width boundary of the atomic type. | 161 // Test increment at the half-width boundary of the atomic type. |
| 162 // It is primarily for testing at the 32-bit boundary for 64-bit atomic type. | 162 // It is primarily for testing at the 32-bit boundary for 64-bit atomic type. |
| 163 AtomicType test_val = GG_ULONGLONG(1) << (NUM_BITS(AtomicType) / 2); | 163 AtomicType test_val = GG_ULONGLONG(1) << (NUM_BITS(AtomicType) / 2); |
| 164 AtomicType value = test_val - 1; | 164 AtomicType value = test_val - 1; |
| 165 AtomicType new_value = base::subtle::NoBarrier_AtomicIncrement(&value, 1); | 165 AtomicType new_value = base::subtle::NoBarrier_AtomicIncrement(&value, 1); |
| 166 ASSERT_EQ(test_val, value); | 166 CHECK_EQ(test_val, value); |
| 167 ASSERT_EQ(value, new_value); | 167 CHECK_EQ(value, new_value); |
| 168 | 168 |
| 169 base::subtle::NoBarrier_AtomicIncrement(&value, -1); | 169 base::subtle::NoBarrier_AtomicIncrement(&value, -1); |
| 170 ASSERT_EQ(test_val - 1, value); | 170 CHECK_EQ(test_val - 1, value); |
| 171 } | 171 } |
| 172 | 172 |
| 173 // This is a simple sanity check that values are correct. Not testing | 173 // This is a simple sanity check that values are correct. Not testing |
| 174 // atomicity | 174 // atomicity |
| 175 template <class AtomicType> | 175 template <class AtomicType> |
| 176 static void TestStore() { | 176 static void TestStore() { |
| 177 const AtomicType kVal1 = static_cast<AtomicType>(0xa5a5a5a5a5a5a5a5LL); | 177 const AtomicType kVal1 = static_cast<AtomicType>(0xa5a5a5a5a5a5a5a5LL); |
| 178 const AtomicType kVal2 = static_cast<AtomicType>(-1); | 178 const AtomicType kVal2 = static_cast<AtomicType>(-1); |
| 179 | 179 |
| 180 AtomicType value; | 180 AtomicType value; |
| 181 | 181 |
| 182 base::subtle::NoBarrier_Store(&value, kVal1); | 182 base::subtle::NoBarrier_Store(&value, kVal1); |
| 183 ASSERT_EQ(kVal1, value); | 183 CHECK_EQ(kVal1, value); |
| 184 base::subtle::NoBarrier_Store(&value, kVal2); | 184 base::subtle::NoBarrier_Store(&value, kVal2); |
| 185 ASSERT_EQ(kVal2, value); | 185 CHECK_EQ(kVal2, value); |
| 186 | 186 |
| 187 base::subtle::Acquire_Store(&value, kVal1); | 187 base::subtle::Acquire_Store(&value, kVal1); |
| 188 ASSERT_EQ(kVal1, value); | 188 CHECK_EQ(kVal1, value); |
| 189 base::subtle::Acquire_Store(&value, kVal2); | 189 base::subtle::Acquire_Store(&value, kVal2); |
| 190 ASSERT_EQ(kVal2, value); | 190 CHECK_EQ(kVal2, value); |
| 191 | 191 |
| 192 base::subtle::Release_Store(&value, kVal1); | 192 base::subtle::Release_Store(&value, kVal1); |
| 193 ASSERT_EQ(kVal1, value); | 193 CHECK_EQ(kVal1, value); |
| 194 base::subtle::Release_Store(&value, kVal2); | 194 base::subtle::Release_Store(&value, kVal2); |
| 195 ASSERT_EQ(kVal2, value); | 195 CHECK_EQ(kVal2, value); |
| 196 } | 196 } |
| 197 | 197 |
| 198 // This is a simple sanity check that values are correct. Not testing | 198 // This is a simple sanity check that values are correct. Not testing |
| 199 // atomicity | 199 // atomicity |
| 200 template <class AtomicType> | 200 template <class AtomicType> |
| 201 static void TestLoad() { | 201 static void TestLoad() { |
| 202 const AtomicType kVal1 = static_cast<AtomicType>(0xa5a5a5a5a5a5a5a5LL); | 202 const AtomicType kVal1 = static_cast<AtomicType>(0xa5a5a5a5a5a5a5a5LL); |
| 203 const AtomicType kVal2 = static_cast<AtomicType>(-1); | 203 const AtomicType kVal2 = static_cast<AtomicType>(-1); |
| 204 | 204 |
| 205 AtomicType value; | 205 AtomicType value; |
| 206 | 206 |
| 207 value = kVal1; | 207 value = kVal1; |
| 208 ASSERT_EQ(kVal1, base::subtle::NoBarrier_Load(&value)); | 208 CHECK_EQ(kVal1, base::subtle::NoBarrier_Load(&value)); |
| 209 value = kVal2; | 209 value = kVal2; |
| 210 ASSERT_EQ(kVal2, base::subtle::NoBarrier_Load(&value)); | 210 CHECK_EQ(kVal2, base::subtle::NoBarrier_Load(&value)); |
| 211 | 211 |
| 212 value = kVal1; | 212 value = kVal1; |
| 213 ASSERT_EQ(kVal1, base::subtle::Acquire_Load(&value)); | 213 CHECK_EQ(kVal1, base::subtle::Acquire_Load(&value)); |
| 214 value = kVal2; | 214 value = kVal2; |
| 215 ASSERT_EQ(kVal2, base::subtle::Acquire_Load(&value)); | 215 CHECK_EQ(kVal2, base::subtle::Acquire_Load(&value)); |
| 216 | 216 |
| 217 value = kVal1; | 217 value = kVal1; |
| 218 ASSERT_EQ(kVal1, base::subtle::Release_Load(&value)); | 218 CHECK_EQ(kVal1, base::subtle::Release_Load(&value)); |
| 219 value = kVal2; | 219 value = kVal2; |
| 220 ASSERT_EQ(kVal2, base::subtle::Release_Load(&value)); | 220 CHECK_EQ(kVal2, base::subtle::Release_Load(&value)); |
| 221 } | 221 } |
| 222 | 222 |
| 223 template <class AtomicType> | 223 template <class AtomicType> |
| 224 static void TestAtomicOps() { | 224 static void TestAtomicOps() { |
| 225 TestCompareAndSwap<AtomicType>(); | 225 TestCompareAndSwap<AtomicType>(); |
| 226 TestAtomicExchange<AtomicType>(); | 226 TestAtomicExchange<AtomicType>(); |
| 227 TestAtomicIncrementBounds<AtomicType>(); | 227 TestAtomicIncrementBounds<AtomicType>(); |
| 228 TestStore<AtomicType>(); | 228 TestStore<AtomicType>(); |
| 229 TestLoad<AtomicType>(); | 229 TestLoad<AtomicType>(); |
| 230 } | 230 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 248 // If we ever *do* want to enable this, try adding -msse (or -mmmx?) | 248 // If we ever *do* want to enable this, try adding -msse (or -mmmx?) |
| 249 // to the CXXFLAGS in Makefile.am. | 249 // to the CXXFLAGS in Makefile.am. |
| 250 #if 0 and defined(BASE_HAS_ATOMIC64) | 250 #if 0 and defined(BASE_HAS_ATOMIC64) |
| 251 TestAtomicIncrement<base::subtle::Atomic64>(); | 251 TestAtomicIncrement<base::subtle::Atomic64>(); |
| 252 TestAtomicOps<base::subtle::Atomic64>(); | 252 TestAtomicOps<base::subtle::Atomic64>(); |
| 253 #endif | 253 #endif |
| 254 | 254 |
| 255 printf("PASS\n"); | 255 printf("PASS\n"); |
| 256 return 0; | 256 return 0; |
| 257 } | 257 } |
| OLD | NEW |