| 1 | /* Copyright (c) 2021, 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 | /* See http://code.google.com/p/googletest/wiki/Primer */ |
| 25 | |
| 26 | #include <gmock/gmock.h> |
| 27 | #include <gtest/gtest.h> |
| 28 | #include <atomic> |
| 29 | #include <chrono> |
| 30 | #include <fstream> |
| 31 | #include <iostream> |
| 32 | #include <iterator> |
| 33 | #include <thread> |
| 34 | |
| 35 | #include "os0thread.h" |
| 36 | #include "sync0arr_impl.h" |
| 37 | |
| 38 | namespace innodb_sync0rw_unittest { |
| 39 | |
| 40 | struct phase_task { |
| 41 | size_t thread_idx; |
| 42 | std::function<void(void)> action; |
| 43 | bool blocking{true}; |
| 44 | }; |
| 45 | /** It runs a machinery to execute the phases supplied. It creates |
| 46 | threads that move through phases to perform tasks needed to complete the |
| 47 | algorithm specified by these phases.*/ |
| 48 | void execute_multithreaded_phase_plan(const std::vector<phase_task> phases[], |
| 49 | const size_t phase_count) { |
| 50 | std::vector<std::atomic<size_t>> completed_tasks_in_phase(phase_count); |
| 51 | std::vector<std::thread> threads; |
| 52 | std::atomic<size_t> current_phase{0}; |
| 53 | size_t max_thread_idx_used = 0; |
| 54 | |
| 55 | /* Scan phase list to search for max thread index used to create that many |
| 56 | threads. */ |
| 57 | for (size_t p = 0; p < phase_count; ++p) { |
| 58 | completed_tasks_in_phase[p] = 0; |
| 59 | for (const auto &t : phases[p]) { |
| 60 | max_thread_idx_used = std::max(max_thread_idx_used, t.thread_idx); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /* Specifies actions taken by a single thread to be created, depending on the |
| 65 | thread index. It waits for each phase to be executed in chronological |
| 66 | order, and for each phase it executes all tasks that are assigned for the |
| 67 | thread index, as specified by the phases algorithm supplied. */ |
| 68 | auto thread_action = [&](auto my_thread_idx) { |
| 69 | /* Run phases one by one in chronological order. */ |
| 70 | for (size_t p = 0; p < phase_count; ++p) { |
| 71 | /* Wait for a specified phase to start. */ |
| 72 | while (current_phase < p) { |
| 73 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| 74 | } |
| 75 | /* Find all tasks in this phase that are assigned for current thread |
| 76 | index. */ |
| 77 | for (const auto &t : phases[p]) { |
| 78 | if (t.thread_idx == my_thread_idx) { |
| 79 | /* If the action is blocking, execute it first and only then |
| 80 | allow to move the phases forward. */ |
| 81 | if (t.blocking) t.action(); |
| 82 | /* Move phases forward - set this task as executed. */ |
| 83 | if (++completed_tasks_in_phase[p] == phases[p].size()) { |
| 84 | /* If this task was last in the phase to execute, then move to |
| 85 | the next phase. */ |
| 86 | current_phase++; |
| 87 | } |
| 88 | /* If the task was meant to not be blocking, execute it after |
| 89 | moving phases forward. Note, that tasks in next phase may |
| 90 | want to wait a bit for this thread to do its work. As this is |
| 91 | blocking, there is no way to synchronize and tell other when |
| 92 | to move on. */ |
| 93 | if (!t.blocking) t.action(); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | }; |
| 98 | /* Creates threads that execute the algorithm. */ |
| 99 | for (size_t thread_idx = 0; thread_idx <= max_thread_idx_used; ++thread_idx) { |
| 100 | threads.emplace_back(thread_action, thread_idx); |
| 101 | } |
| 102 | /* Cleanup */ |
| 103 | for (auto &t : threads) { |
| 104 | t.join(); |
| 105 | } |
| 106 | EXPECT_EQ(current_phase.load(), phase_count); |
| 107 | } |
| 108 | |
| 109 | TEST(sync0rw, rw_lock_reader_thread) { |
| 110 | /* This test tests if the reader_thread is calculated correctly. |
| 111 | We have three rw_locks: |
| 112 | - lock 0 is S-latched by 3 threads in this order: t2, t1, t3. Then t2 |
| 113 | and t3 unlocks it, so only t1 have it locked. |
| 114 | - lock 1 is S-latched by t1 and t2. |
| 115 | - lock 2 is only X-latched by t1. |
| 116 | Now we try to X-latch locks 0, 1 and 2 with threads t2, t3 and t4. |
| 117 | They will be waiting on rw_locks with different number of readers: 1, 2 and 0, |
| 118 | respectively. |
| 119 | We print cell info and let t1 to continue, which in turn lets all threads to |
| 120 | finish. */ |
| 121 | |
| 122 | os_event_global_init(); |
| 123 | sync_check_init(4); |
| 124 | |
| 125 | rw_lock_t *rw_locks[3]; |
| 126 | for (auto &rw_lock : rw_locks) { |
| 127 | rw_lock = static_cast<rw_lock_t *>(malloc(sizeof(rw_lock_t))); |
| 128 | rw_lock_create(PSI_NOT_INSTRUMENTED, rw_lock, LATCH_ID_BUF_BLOCK_LOCK); |
| 129 | } |
| 130 | |
| 131 | std::atomic<std::thread::id> thread_1_id; |
| 132 | |
| 133 | auto check_reader_counts_action = [&] { |
| 134 | /* Let all threads place their X-lock waits. */ |
| 135 | std::this_thread::sleep_for(std::chrono::seconds(2)); |
| 136 | |
| 137 | EXPECT_EQ(rw_lock_get_reader_count(rw_locks[0]), 1); |
| 138 | EXPECT_EQ(rw_lock_get_reader_count(rw_locks[1]), 2); |
| 139 | EXPECT_EQ(rw_lock_get_reader_count(rw_locks[2]), 0); |
| 140 | EXPECT_EQ(rw_locks[0]->reader_thread.recover_if_single(), |
| 141 | thread_1_id.load()); |
| 142 | |
| 143 | FILE *tmp_file = tmpfile(); |
| 144 | EXPECT_NE(tmp_file, nullptr); |
| 145 | |
| 146 | for (size_t i = 0; i < sync_array_size; ++i) { |
| 147 | sync_array_t *arr = sync_wait_array[i]; |
| 148 | |
| 149 | mutex_enter(&arr->mutex); |
| 150 | |
| 151 | for (size_t j = 0; j < arr->next_free_slot; j++) { |
| 152 | sync_cell_t &cell = arr->cells[j]; |
| 153 | |
| 154 | void *latch = cell.latch.mutex; |
| 155 | |
| 156 | if (latch == nullptr || !cell.waiting) { |
| 157 | continue; |
| 158 | } |
| 159 | |
| 160 | sync_array_cell_print(tmp_file, &cell); |
| 161 | } |
| 162 | |
| 163 | mutex_exit(&arr->mutex); |
| 164 | } |
| 165 | |
| 166 | /* Print cells to temporary file, read all content and check it is correct. |
| 167 | */ |
| 168 | const auto len = ftell(tmp_file); |
| 169 | EXPECT_NE(-1, len); |
| 170 | auto content = std::make_unique<char[]>(len); |
| 171 | EXPECT_EQ(0, fseek(tmp_file, 0, SEEK_SET)); |
| 172 | EXPECT_EQ(len, fread(content.get(), 1, len, tmp_file)); |
| 173 | EXPECT_EQ(0, fclose(tmp_file)); |
| 174 | std::string cell_print(content.get(), len); |
| 175 | |
| 176 | EXPECT_THAT(cell_print, testing::HasSubstr("number of readers 0, waiters")); |
| 177 | /* Note that std::to_string(thread_id_to_uint64(thread_id) could be |
| 178 | something different than to_string(thread_id). */ |
| 179 | EXPECT_THAT(cell_print, testing::HasSubstr( |
| 180 | "number of readers 1 (thread id " + |
| 181 | to_string(thread_1_id.load()) + "), waiters")); |
| 182 | EXPECT_THAT(cell_print, testing::HasSubstr("number of readers 2, waiters")); |
| 183 | }; |
| 184 | |
| 185 | /* We proceed the test in the following phases: */ |
| 186 | std::vector<phase_task> phases[] = { |
| 187 | /* Place all required S-latches and X-latches for locks 1 and 2. |
| 188 | Place first S-latch for lock 0 (in thread 2) - other threads will |
| 189 | place their lock 1 latches in next phases to synchronize order of |
| 190 | S-latching. |
| 191 | */ |
| 192 | { |
| 193 | phase_task{1, [&] { thread_1_id = std::this_thread::get_id(); }}, |
| 194 | phase_task{1, [&] { rw_lock_s_lock(rw_locks[1], UT_LOCATION_HERE); }}, |
| 195 | phase_task{1, [&] { rw_lock_x_lock(rw_locks[2], UT_LOCATION_HERE); }}, |
| 196 | phase_task{2, [&] { rw_lock_s_lock(rw_locks[0], UT_LOCATION_HERE); }}, |
| 197 | phase_task{2, [&] { rw_lock_s_lock(rw_locks[1], UT_LOCATION_HERE); }}, |
| 198 | }, |
| 199 | /* Place second S-latch on lock 0, now from thread 1. */ |
| 200 | { |
| 201 | phase_task{1, [&] { rw_lock_s_lock(rw_locks[0], UT_LOCATION_HERE); }}, |
| 202 | }, |
| 203 | /* Place third S-latch on lock 0, now from thread 3. */ |
| 204 | { |
| 205 | phase_task{3, [&] { rw_lock_s_lock(rw_locks[0], UT_LOCATION_HERE); }}, |
| 206 | }, |
| 207 | /* Unlatch S-latches on lock 0 from threads 2 and 3. */ |
| 208 | { |
| 209 | phase_task{2, [&] { rw_lock_s_unlock(rw_locks[0]); }}, |
| 210 | phase_task{3, [&] { rw_lock_s_unlock(rw_locks[0]); }}, |
| 211 | }, |
| 212 | /* Now place all blocking X-latches from threads 2, 3 and 4. */ |
| 213 | { |
| 214 | phase_task{2, [&] { rw_lock_x_lock(rw_locks[0], UT_LOCATION_HERE); }, |
| 215 | false}, |
| 216 | phase_task{3, [&] { rw_lock_x_lock(rw_locks[1], UT_LOCATION_HERE); }, |
| 217 | false}, |
| 218 | phase_task{4, [&] { rw_lock_x_lock(rw_locks[2], UT_LOCATION_HERE); }, |
| 219 | false}, |
| 220 | }, |
| 221 | /* Now run the rw_locks check. */ |
| 222 | { |
| 223 | phase_task{0, check_reader_counts_action}, |
| 224 | }, |
| 225 | /* Unlock all remaining latches. */ |
| 226 | { |
| 227 | phase_task{1, [&] { rw_lock_s_unlock(rw_locks[0]); }}, |
| 228 | phase_task{1, [&] { rw_lock_s_unlock(rw_locks[1]); }}, |
| 229 | |
| 230 | /* Unlocking thread 1, it will allow next threads to be able to |
| 231 | get their X-latches and then release them all. */ |
| 232 | phase_task{1, [&] { rw_lock_x_unlock(rw_locks[2]); }}, |
| 233 | phase_task{2, [&] { rw_lock_s_unlock(rw_locks[1]); }}, |
| 234 | phase_task{2, [&] { rw_lock_x_unlock(rw_locks[0]); }}, |
| 235 | phase_task{3, [&] { rw_lock_x_unlock(rw_locks[1]); }}, |
| 236 | phase_task{4, [&] { rw_lock_x_unlock(rw_locks[2]); }}, |
| 237 | }}; |
| 238 | |
| 239 | execute_multithreaded_phase_plan(phases, UT_ARR_SIZE(phases)); |
| 240 | |
| 241 | for (auto &rw_lock : rw_locks) { |
| 242 | rw_lock_free(rw_lock); |
| 243 | free(rw_lock); |
| 244 | } |
| 245 | |
| 246 | sync_check_close(); |
| 247 | os_event_global_destroy(); |
| 248 | } |
| 249 | |
| 250 | } // namespace innodb_sync0rw_unittest |