/** * Copyright (C) 2018-present MongoDB, Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the Server Side Public License, version 1, * as published by MongoDB, Inc. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * Server Side Public License for more details. * * You should have received a copy of the Server Side Public License * along with this program. If not, see * . * * As a special exception, the copyright holders give permission to link the * code of portions of this program with the OpenSSL library under certain * conditions as described in each individual source file and distribute * linked combinations including the program with the OpenSSL library. You * must comply with the Server Side Public License in all respects for * all of the code used other than as permitted herein. If you modify file(s) * with this exception, you may extend this exception to your version of the * file(s), but you are not obligated to do so. If you do not wish to do so, * delete this exception statement from your version. If you delete this * exception statement from all source files in the program, then also delete * it in the license file. */ #include "mongo/platform/basic.h" #include #include "mongo/platform/atomic_word.h" #include "mongo/unittest/unittest.h" namespace mongo { namespace { /* * Modified CAS for AtomicWord to return boolean based on whether or not the swap occurred. * This helper function mimics the old implementation, which returned the original value of * expected. */ template WordType testAtomicWordCompareAndSwap(AtomicWord& word, WordType expected, WordType desired) { auto prevWord = word.loadRelaxed(); auto didSwap = word.compareAndSwap(&expected, desired); ASSERT_EQUALS(expected, prevWord); if (didSwap) { ASSERT_EQUALS(word.load(), desired); } else { ASSERT_EQUALS(word.load(), prevWord); } return expected; } template void testAtomicWordBasicOperations() { typedef typename _AtomicWordType::WordType WordType; _AtomicWordType w; ASSERT_EQUALS(WordType(0), w.load()); w.store(1); ASSERT_EQUALS(WordType(1), w.load()); ASSERT_EQUALS(WordType(1), w.swap(2)); ASSERT_EQUALS(WordType(2), w.load()); ASSERT_EQUALS(WordType(2), testAtomicWordCompareAndSwap(w, 0, 1)); ASSERT_EQUALS(WordType(2), w.load()); ASSERT_EQUALS(WordType(2), testAtomicWordCompareAndSwap(w, 2, 1)); ASSERT_EQUALS(WordType(1), w.load()); ASSERT_EQUALS(WordType(1), w.fetchAndAdd(14)); ASSERT_EQUALS(WordType(17), w.addAndFetch(2)); ASSERT_EQUALS(WordType(16), w.subtractAndFetch(1)); ASSERT_EQUALS(WordType(16), w.fetchAndSubtract(1)); ASSERT_EQUALS(WordType(15), testAtomicWordCompareAndSwap(w, 15, 0)); ASSERT_EQUALS(WordType(0), w.load()); } template void testAtomicWordBitOperations() { typedef typename AtomicWordType::WordType WordType; AtomicWordType w; WordType highBit = 1ull << ((sizeof(WordType) * 8) - 1); w.store(highBit | 0xFFull); ASSERT_EQUALS(WordType(highBit | 0xFFull), w.fetchAndBitAnd(highBit)); ASSERT_EQUALS(WordType(highBit), w.fetchAndBitOr(highBit | 0xFFull)); ASSERT_EQUALS(WordType(highBit | 0xFFull), w.fetchAndBitXor(0xFFFF)); ASSERT_EQUALS(WordType(highBit | 0xFF00ull), w.load()); } ASSERT_DOES_NOT_COMPILE(CharFetchAndBitAnd, typename T = int, AtomicWord().fetchAndBitAnd(0)); ASSERT_DOES_NOT_COMPILE(CharFetchAndBitOr, typename T = int, AtomicWord().fetchAndBitOr(0)); ASSERT_DOES_NOT_COMPILE(CharFetchAndBitXor, typename T = int, AtomicWord().fetchAndBitXor(0)); ASSERT_DOES_NOT_COMPILE(IntFetchAndBitAnd, typename T = char, AtomicWord().fetchAndBitAnd(0)); ASSERT_DOES_NOT_COMPILE(IntFetchAndBitOr, typename T = char, AtomicWord().fetchAndBitOr(0)); ASSERT_DOES_NOT_COMPILE(IntFetchAndBitXor, typename T = char, AtomicWord().fetchAndBitXor(0)); enum TestEnum { E0, E1, E2, E3 }; TEST(AtomicWordTests, BasicOperationsEnum) { MONGO_STATIC_ASSERT(sizeof(AtomicWord) == sizeof(TestEnum)); AtomicWord w; ASSERT_EQUALS(E0, w.load()); ASSERT_EQUALS(E0, testAtomicWordCompareAndSwap(w, E0, E1)); ASSERT_EQUALS(E1, w.load()); ASSERT_EQUALS(E1, testAtomicWordCompareAndSwap(w, E0, E2)); ASSERT_EQUALS(E1, w.load()); } TEST(AtomicWordTests, BasicOperationsUnsigned32Bit) { typedef unsigned WordType; testAtomicWordBasicOperations>(); testAtomicWordBitOperations>(); AtomicWord w(0xdeadbeef); ASSERT_EQUALS(WordType(0xdeadbeef), testAtomicWordCompareAndSwap(w, 0, 1)); ASSERT_EQUALS(WordType(0xdeadbeef), testAtomicWordCompareAndSwap(w, 0xdeadbeef, 0xcafe1234)); ASSERT_EQUALS(WordType(0xcafe1234), w.fetchAndAdd(0xf000)); ASSERT_EQUALS(WordType(0xcaff0234), w.swap(0)); ASSERT_EQUALS(WordType(0), w.load()); } TEST(AtomicWordTests, BasicOperationsUnsigned64Bit) { typedef unsigned long long WordType; testAtomicWordBasicOperations>(); testAtomicWordBitOperations>(); AtomicWord w(0xdeadbeefcafe1234ULL); ASSERT_EQUALS(WordType(0xdeadbeefcafe1234ULL), testAtomicWordCompareAndSwap(w, 0, 1)); ASSERT_EQUALS( WordType(0xdeadbeefcafe1234ULL), testAtomicWordCompareAndSwap(w, 0xdeadbeefcafe1234ULL, 0xfedcba9876543210ULL)); ASSERT_EQUALS(WordType(0xfedcba9876543210ULL), w.fetchAndAdd(0xf0000000ULL)); ASSERT_EQUALS(WordType(0xfedcba9966543210ULL), w.swap(0)); ASSERT_EQUALS(WordType(0), w.load()); } TEST(AtomicWordTests, BasicOperationsSigned32Bit) { typedef int WordType; testAtomicWordBasicOperations>(); AtomicWord w(0xdeadbeef); ASSERT_EQUALS(WordType(0xdeadbeef), testAtomicWordCompareAndSwap(w, 0, 1)); ASSERT_EQUALS(WordType(0xdeadbeef), testAtomicWordCompareAndSwap(w, 0xdeadbeef, 0xcafe1234)); ASSERT_EQUALS(WordType(0xcafe1234), w.fetchAndAdd(0xf000)); ASSERT_EQUALS(WordType(0xcaff0234), w.swap(0)); ASSERT_EQUALS(WordType(0), w.load()); } TEST(AtomicWordTests, BasicOperationsSigned64Bit) { typedef long long WordType; testAtomicWordBasicOperations>(); AtomicWord w(0xdeadbeefcafe1234ULL); ASSERT_EQUALS(WordType(0xdeadbeefcafe1234LL), testAtomicWordCompareAndSwap(w, 0, 1)); ASSERT_EQUALS( WordType(0xdeadbeefcafe1234LL), testAtomicWordCompareAndSwap(w, 0xdeadbeefcafe1234LL, 0xfedcba9876543210LL)); ASSERT_EQUALS(WordType(0xfedcba9876543210LL), w.fetchAndAdd(0xf0000000LL)); ASSERT_EQUALS(WordType(0xfedcba9966543210LL), w.swap(0)); ASSERT_EQUALS(WordType(0), w.load()); } TEST(AtomicWordTests, BasicOperationsFloat) { typedef AtomicWord::WordType WordType; AtomicWord w; ASSERT_EQUALS(WordType(0), w.load()); w.store(1); ASSERT_EQUALS(WordType(1), w.load()); ASSERT_EQUALS(WordType(1), w.swap(2)); ASSERT_EQUALS(WordType(2), w.load()); ASSERT_EQUALS(WordType(2), testAtomicWordCompareAndSwap(w, 0, 1)); ASSERT_EQUALS(WordType(2), w.load()); ASSERT_EQUALS(WordType(2), testAtomicWordCompareAndSwap(w, 2, 1)); ASSERT_EQUALS(WordType(1), w.load()); w.store(15); ASSERT_EQUALS(WordType(15), testAtomicWordCompareAndSwap(w, 15, 0)); ASSERT_EQUALS(WordType(0), w.load()); } struct Chars { static constexpr size_t kLength = 6; Chars(const char* chars = "") { invariant(std::strlen(chars) < kLength); std::strncpy(_storage.data(), chars, sizeof(_storage)); } std::array _storage = {}; friend bool operator==(const Chars& lhs, const Chars& rhs) { return lhs._storage == rhs._storage; } friend bool operator!=(const Chars& lhs, const Chars& rhs) { return !(lhs == rhs); } }; std::ostream& operator<<(std::ostream& os, const Chars& chars) { return (os << chars._storage.data()); } template void verifyAtomicityHelper() { ASSERT(std::atomic{}.is_lock_free()); // NOLINT ASSERT(std::atomic::type>{}.is_lock_free()); // NOLINT ASSERT(std::atomic::type>{}.is_lock_free()); // NOLINT } template void verifyAtomicity() { using expander = int[]; (void)expander{(verifyAtomicityHelper(), 0)...}; } TEST(AtomicWordTests, StdAtomicOfIntegralIsLockFree) { // 2 means that they're always atomic. Instead of 1, that means sometimes, and 0, which means // never. ASSERT_EQUALS(2, ATOMIC_CHAR_LOCK_FREE); ASSERT_EQUALS(2, ATOMIC_CHAR16_T_LOCK_FREE); ASSERT_EQUALS(2, ATOMIC_CHAR32_T_LOCK_FREE); ASSERT_EQUALS(2, ATOMIC_WCHAR_T_LOCK_FREE); ASSERT_EQUALS(2, ATOMIC_SHORT_LOCK_FREE); ASSERT_EQUALS(2, ATOMIC_INT_LOCK_FREE); ASSERT_EQUALS(2, ATOMIC_LONG_LOCK_FREE); ASSERT_EQUALS(2, ATOMIC_LLONG_LOCK_FREE); ASSERT_EQUALS(2, ATOMIC_POINTER_LOCK_FREE); verifyAtomicity(); ASSERT(std::atomic{}.is_lock_free()); // NOLINT } } // namespace } // namespace mongo