| 1 | /* Copyright (c) 2018, 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 | #include <gtest/gtest.h> |
| 25 | |
| 26 | #include <include/my_rcu_lock.h> |
| 27 | #include <my_systime.h> // my_sleep |
| 28 | #include <atomic> |
| 29 | #include <thread> |
| 30 | |
| 31 | #include "scope_guard.h" |
| 32 | |
| 33 | namespace my_rcu_lock_unittest { |
| 34 | |
| 35 | class payload_s { |
| 36 | public: |
| 37 | payload_s(const char *a, const char *b, const char *c) |
| 38 | : d1(a), d2(b), d3(c) {} |
| 39 | payload_s(const payload_s &other) = default; |
| 40 | const char *d1; |
| 41 | const char *d2; |
| 42 | const char *d3; |
| 43 | }; |
| 44 | |
| 45 | typedef MyRcuLock<payload_s> MyRcuLockTest; |
| 46 | |
| 47 | class my_rcu_lock_test : public ::testing::Test { |
| 48 | protected: |
| 49 | my_rcu_lock_test() = default; |
| 50 | |
| 51 | void SetUp() override {} |
| 52 | |
| 53 | void TearDown() override {} |
| 54 | |
| 55 | static void SetUpTestCase() { |
| 56 | lock = new MyRcuLockTest(new payload_s("a", "b", "c")); |
| 57 | reads = 0; |
| 58 | writes = 0; |
| 59 | } |
| 60 | |
| 61 | static void TearDownTestCase() { delete lock; } |
| 62 | |
| 63 | static void rcu_reader(size_t reps) { |
| 64 | for (size_t i = 0; i < reps; i++) { |
| 65 | { |
| 66 | MyRcuLockTest::ReadLock rl(lock); |
| 67 | const payload_s *ptr = rl; |
| 68 | EXPECT_FALSE(ptr->d1[0] != 'a' || ptr->d2[0] != 'b' || |
| 69 | ptr->d3[0] != 'c'); |
| 70 | } |
| 71 | reads.fetch_add(1, std::memory_order_relaxed); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | static void rcu_writer(size_t reps, time_t waitms) { |
| 76 | for (size_t i = 0; i < reps; i++) { |
| 77 | payload_s *newp = new payload_s("a", "b", "c"); |
| 78 | |
| 79 | bool ret = lock->write_wait_and_delete(newp); |
| 80 | EXPECT_EQ(ret, false); |
| 81 | /* |
| 82 | RCU works best with relatively infrequent writes compared to reads. |
| 83 | Trying to simulate this by spacing out the writes via adding a 100ms |
| 84 | waits. |
| 85 | It will work without this too, but will not be testing the code in |
| 86 | "normal" conditions. |
| 87 | */ |
| 88 | my_sleep(waitms); |
| 89 | |
| 90 | writes.fetch_add(1, std::memory_order_relaxed); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static MyRcuLockTest *lock; |
| 95 | |
| 96 | static unsigned char p1[128]; |
| 97 | static std::atomic<long> reads; |
| 98 | static unsigned char p2[128]; |
| 99 | static std::atomic<long> writes; |
| 100 | static unsigned char p3[128]; |
| 101 | }; |
| 102 | |
| 103 | MyRcuLockTest *my_rcu_lock_test::lock; |
| 104 | std::atomic<long> my_rcu_lock_test::reads; |
| 105 | std::atomic<long> my_rcu_lock_test::writes; |
| 106 | |
| 107 | TEST_F(my_rcu_lock_test, multi_threaded_run) { |
| 108 | // Capping this at 300 since a std::system_error will be thrown on |
| 109 | // i686 when creating more than ~400 threads. |
| 110 | constexpr size_t NUM_READERS = 300; |
| 111 | std::thread readerts[NUM_READERS]; |
| 112 | |
| 113 | constexpr size_t NUM_WRITERS = 10; |
| 114 | std::thread writerts[NUM_WRITERS]; |
| 115 | |
| 116 | { |
| 117 | auto join_guard = create_scope_guard([&]() { |
| 118 | // Need to join with those threads already started so that |
| 119 | // std::terminate is not called in their destructor |
| 120 | for (auto &rt : readerts) { |
| 121 | if (rt.joinable()) rt.join(); |
| 122 | } |
| 123 | for (auto &wt : writerts) { |
| 124 | if (wt.joinable()) wt.join(); |
| 125 | } |
| 126 | }); |
| 127 | |
| 128 | for (auto &rt : readerts) rt = std::thread(rcu_reader, 100000); |
| 129 | for (auto &wt : writerts) wt = std::thread(rcu_writer, 5, 100); |
| 130 | |
| 131 | // When leaving this scope the scope guard ensures that we will |
| 132 | // attempt join every joinable thread |
| 133 | } |
| 134 | |
| 135 | ASSERT_EQ(reads.load(), 100000 * NUM_READERS); |
| 136 | ASSERT_EQ(writes.load(), NUM_WRITERS * 5); |
| 137 | } |
| 138 | |
| 139 | } // namespace my_rcu_lock_unittest |