| 1 | /* Copyright (c) 2020, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 | */ |
| 24 | |
| 25 | #include <array> |
| 26 | #include <chrono> |
| 27 | #include <vector> |
| 28 | |
| 29 | #define private public |
| 30 | #include "sql/memory/aligned_atomic.h" |
| 31 | #undef private |
| 32 | |
| 33 | #include <gmock/gmock.h> |
| 34 | #include <gtest/gtest.h> |
| 35 | |
| 36 | namespace memory { |
| 37 | namespace unittests { |
| 38 | |
| 39 | class Aligned_atomic_test : public ::testing::Test { |
| 40 | protected: |
| 41 | Aligned_atomic_test() = default; |
| 42 | virtual void SetUp() {} |
| 43 | virtual void TearDown() {} |
| 44 | }; |
| 45 | |
| 46 | TEST_F(Aligned_atomic_test, Class_template_test) { |
| 47 | std::string str{"012345678"}; |
| 48 | memory::Aligned_atomic<int> atm{1}; |
| 49 | EXPECT_EQ(atm->load(), 1); |
| 50 | atm->store(2); |
| 51 | EXPECT_EQ((*atm).load(), 2); |
| 52 | EXPECT_EQ(atm.size(), sizeof(std::atomic<int>)); |
| 53 | EXPECT_EQ(atm.allocated_size(), memory::cache_line_size()); |
| 54 | |
| 55 | memory::Aligned_atomic<int> atm2{std::move(atm)}; |
| 56 | EXPECT_EQ(atm2->load(), 2); |
| 57 | EXPECT_EQ(atm == nullptr, true); |
| 58 | memory::Aligned_atomic<int> atm3; |
| 59 | atm3 = std::move(atm2); |
| 60 | EXPECT_EQ(atm3->load(), 2); |
| 61 | } |
| 62 | |
| 63 | TEST_F(Aligned_atomic_test, minimum_cacheline_for) { |
| 64 | EXPECT_EQ(memory::minimum_cacheline_for<char>(), memory::cache_line_size()); |
| 65 | EXPECT_EQ(memory::minimum_cacheline_for<int>(), memory::cache_line_size()); |
| 66 | EXPECT_EQ(memory::minimum_cacheline_for<std::atomic<bool>>(), |
| 67 | memory::cache_line_size()); |
| 68 | EXPECT_EQ(memory::minimum_cacheline_for<std::atomic<int>>(), |
| 69 | memory::cache_line_size()); |
| 70 | } |
| 71 | |
| 72 | TEST_F(Aligned_atomic_test, aligned_allocation) { |
| 73 | memory::Aligned_atomic<int> atm1{1}; |
| 74 | EXPECT_EQ((unsigned long long)atm1.m_underlying % memory::cache_line_size(), |
| 75 | 0); |
| 76 | |
| 77 | memory::Aligned_atomic<bool> atm2{true}; |
| 78 | EXPECT_EQ((unsigned long long)atm2.m_underlying % memory::cache_line_size(), |
| 79 | 0); |
| 80 | |
| 81 | memory::Aligned_atomic<short> atm3{0}; |
| 82 | EXPECT_EQ((unsigned long long)atm3.m_underlying % memory::cache_line_size(), |
| 83 | 0); |
| 84 | } |
| 85 | |
| 86 | TEST_F(Aligned_atomic_test, aligned_allocation_array) { |
| 87 | static const int array_size = 10; |
| 88 | memory::Aligned_atomic<int> atm[array_size]; |
| 89 | |
| 90 | for (int i = 0; i < array_size; i++) |
| 91 | EXPECT_EQ( |
| 92 | (unsigned long long)atm[i].m_underlying % memory::cache_line_size(), 0); |
| 93 | } |
| 94 | |
| 95 | } // namespace unittests |
| 96 | } // namespace memory |