Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 1 | // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
| 2 | /* Copyright (c) 2006, Google Inc. |
| 3 | * All rights reserved. |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame^] | 4 | * |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are |
| 7 | * met: |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame^] | 8 | * |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 9 | * * Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * * Redistributions in binary form must reproduce the above |
| 12 | * copyright notice, this list of conditions and the following disclaimer |
| 13 | * in the documentation and/or other materials provided with the |
| 14 | * distribution. |
| 15 | * * Neither the name of Google Inc. nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame^] | 18 | * |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | * |
| 31 | * --- |
| 32 | * Author: Sanjay Ghemawat |
| 33 | */ |
| 34 | |
| 35 | #include <stdio.h> |
| 36 | #include "base/logging.h" |
| 37 | #include "base/atomicops.h" |
| 38 | |
| 39 | #define GG_ULONGLONG(x) static_cast<uint64>(x) |
| 40 | |
| 41 | |
| 42 | #define NUM_BITS(T) (sizeof(T) * 8) |
| 43 | |
| 44 | |
| 45 | template <class AtomicType> |
| 46 | static void TestCompareAndSwap(AtomicType (*compare_and_swap_func) |
| 47 | (volatile AtomicType*, AtomicType, AtomicType)) { |
| 48 | AtomicType value = 0; |
| 49 | AtomicType prev = (*compare_and_swap_func)(&value, 0, 1); |
| 50 | ASSERT_EQ(1, value); |
| 51 | ASSERT_EQ(0, prev); |
| 52 | |
| 53 | // Use test value that has non-zero bits in both halves, more for testing |
| 54 | // 64-bit implementation on 32-bit platforms. |
| 55 | const AtomicType k_test_val = (GG_ULONGLONG(1) << |
| 56 | (NUM_BITS(AtomicType) - 2)) + 11; |
| 57 | value = k_test_val; |
| 58 | prev = (*compare_and_swap_func)(&value, 0, 5); |
| 59 | ASSERT_EQ(k_test_val, value); |
| 60 | ASSERT_EQ(k_test_val, prev); |
| 61 | |
| 62 | value = k_test_val; |
| 63 | prev = (*compare_and_swap_func)(&value, k_test_val, 5); |
| 64 | ASSERT_EQ(5, value); |
| 65 | ASSERT_EQ(k_test_val, prev); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | template <class AtomicType> |
| 70 | static void TestAtomicExchange(AtomicType (*atomic_exchange_func) |
| 71 | (volatile AtomicType*, AtomicType)) { |
| 72 | AtomicType value = 0; |
| 73 | AtomicType new_value = (*atomic_exchange_func)(&value, 1); |
| 74 | ASSERT_EQ(1, value); |
| 75 | ASSERT_EQ(0, new_value); |
| 76 | |
| 77 | // Use test value that has non-zero bits in both halves, more for testing |
| 78 | // 64-bit implementation on 32-bit platforms. |
| 79 | const AtomicType k_test_val = (GG_ULONGLONG(1) << |
| 80 | (NUM_BITS(AtomicType) - 2)) + 11; |
| 81 | value = k_test_val; |
| 82 | new_value = (*atomic_exchange_func)(&value, k_test_val); |
| 83 | ASSERT_EQ(k_test_val, value); |
| 84 | ASSERT_EQ(k_test_val, new_value); |
| 85 | |
| 86 | value = k_test_val; |
| 87 | new_value = (*atomic_exchange_func)(&value, 5); |
| 88 | ASSERT_EQ(5, value); |
| 89 | ASSERT_EQ(k_test_val, new_value); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | // This is a simple sanity check that values are correct. Not testing |
| 94 | // atomicity |
| 95 | template <class AtomicType> |
| 96 | static void TestStore() { |
| 97 | const AtomicType kVal1 = static_cast<AtomicType>(0xa5a5a5a5a5a5a5a5LL); |
| 98 | const AtomicType kVal2 = static_cast<AtomicType>(-1); |
| 99 | |
| 100 | AtomicType value; |
| 101 | |
| 102 | base::subtle::NoBarrier_Store(&value, kVal1); |
| 103 | ASSERT_EQ(kVal1, value); |
| 104 | base::subtle::NoBarrier_Store(&value, kVal2); |
| 105 | ASSERT_EQ(kVal2, value); |
| 106 | |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 107 | base::subtle::Release_Store(&value, kVal1); |
| 108 | ASSERT_EQ(kVal1, value); |
| 109 | base::subtle::Release_Store(&value, kVal2); |
| 110 | ASSERT_EQ(kVal2, value); |
| 111 | } |
| 112 | |
| 113 | // This is a simple sanity check that values are correct. Not testing |
| 114 | // atomicity |
| 115 | template <class AtomicType> |
| 116 | static void TestLoad() { |
| 117 | const AtomicType kVal1 = static_cast<AtomicType>(0xa5a5a5a5a5a5a5a5LL); |
| 118 | const AtomicType kVal2 = static_cast<AtomicType>(-1); |
| 119 | |
| 120 | AtomicType value; |
| 121 | |
| 122 | value = kVal1; |
| 123 | ASSERT_EQ(kVal1, base::subtle::NoBarrier_Load(&value)); |
| 124 | value = kVal2; |
| 125 | ASSERT_EQ(kVal2, base::subtle::NoBarrier_Load(&value)); |
| 126 | |
| 127 | value = kVal1; |
| 128 | ASSERT_EQ(kVal1, base::subtle::Acquire_Load(&value)); |
| 129 | value = kVal2; |
| 130 | ASSERT_EQ(kVal2, base::subtle::Acquire_Load(&value)); |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | template <class AtomicType> |
| 134 | static void TestAtomicOps() { |
| 135 | TestCompareAndSwap<AtomicType>(base::subtle::NoBarrier_CompareAndSwap); |
| 136 | TestCompareAndSwap<AtomicType>(base::subtle::Acquire_CompareAndSwap); |
| 137 | TestCompareAndSwap<AtomicType>(base::subtle::Release_CompareAndSwap); |
| 138 | |
| 139 | TestAtomicExchange<AtomicType>(base::subtle::NoBarrier_AtomicExchange); |
| 140 | TestAtomicExchange<AtomicType>(base::subtle::Acquire_AtomicExchange); |
| 141 | TestAtomicExchange<AtomicType>(base::subtle::Release_AtomicExchange); |
| 142 | |
| 143 | TestStore<AtomicType>(); |
| 144 | TestLoad<AtomicType>(); |
| 145 | } |
| 146 | |
| 147 | int main(int argc, char** argv) { |
| 148 | TestAtomicOps<AtomicWord>(); |
| 149 | TestAtomicOps<Atomic32>(); |
| 150 | printf("PASS\n"); |
| 151 | return 0; |
| 152 | } |