| 1 | /* Copyright (c) 2015, 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 | /* Simple unit tests for thread id partitioned rwlocks. */ |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | #include <sys/types.h> |
| 28 | |
| 29 | #include "sql/auth/partitioned_rwlock.h" |
| 30 | #include "unittest/gunit/thread_utils.h" |
| 31 | |
| 32 | namespace partitioned_rwlock_unittest { |
| 33 | |
| 34 | using thread::Thread; |
| 35 | |
| 36 | TEST(PartitionedRwlock, InitDestroy) { |
| 37 | Partitioned_rwlock rwlock; |
| 38 | |
| 39 | bool r = rwlock.init(32 |
| 40 | #ifdef HAVE_PSI_INTERFACE |
| 41 | , |
| 42 | PSI_NOT_INSTRUMENTED |
| 43 | #endif |
| 44 | ); |
| 45 | EXPECT_FALSE(r); |
| 46 | rwlock.destroy(); |
| 47 | |
| 48 | r = rwlock.init(8 |
| 49 | #ifdef HAVE_PSI_INTERFACE |
| 50 | , |
| 51 | PSI_NOT_INSTRUMENTED |
| 52 | #endif |
| 53 | ); |
| 54 | EXPECT_FALSE(r); |
| 55 | rwlock.destroy(); |
| 56 | } |
| 57 | |
| 58 | class Reader_thread : public Thread { |
| 59 | public: |
| 60 | void init(uint thread_id, Partitioned_rwlock *rwlock, |
| 61 | volatile uint *shared_counter) { |
| 62 | m_thread_id = thread_id; |
| 63 | m_rwlock = rwlock; |
| 64 | m_shared_counter = shared_counter; |
| 65 | } |
| 66 | void run() override { |
| 67 | for (uint i = 0; i < 1000; ++i) { |
| 68 | Partitioned_rwlock_read_guard lock(m_rwlock, m_thread_id); |
| 69 | /* |
| 70 | With correct rwlock implementation readers should not |
| 71 | observe counter values not divisible by 100. |
| 72 | */ |
| 73 | EXPECT_EQ(0U, (*m_shared_counter % 100)); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | uint m_thread_id; |
| 79 | Partitioned_rwlock *m_rwlock; |
| 80 | volatile uint *m_shared_counter; |
| 81 | }; |
| 82 | |
| 83 | class Writer_thread : public Thread { |
| 84 | public: |
| 85 | Writer_thread(Partitioned_rwlock *rwlock, volatile uint *shared_counter) |
| 86 | : m_rwlock(rwlock), m_shared_counter(shared_counter) {} |
| 87 | void run() override { |
| 88 | for (uint i = 0; i < 1000; ++i) { |
| 89 | Partitioned_rwlock_write_guard lock(m_rwlock); |
| 90 | /* |
| 91 | Add 100 to counter value using 100 single increments. We rely |
| 92 | on counter being "volatile" to prevent compiler optimizations. |
| 93 | */ |
| 94 | for (uint j = 0; j < 100; ++j) { |
| 95 | ++*m_shared_counter; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | private: |
| 101 | Partitioned_rwlock *m_rwlock; |
| 102 | volatile uint *m_shared_counter; |
| 103 | }; |
| 104 | |
| 105 | /** |
| 106 | Concurrent test which easily breaks if rwlock implementation is wrong |
| 107 | (e.g. if wrlock() operation doesn't lock all partitions). |
| 108 | */ |
| 109 | |
| 110 | TEST(PartitionedRwlock, Concurrent) { |
| 111 | const uint PARTS_NUM = 32; |
| 112 | |
| 113 | Partitioned_rwlock rwlock; |
| 114 | volatile uint shared_counter = 0; |
| 115 | rwlock.init(PARTS_NUM |
| 116 | #ifdef HAVE_PSI_INTERFACE |
| 117 | , |
| 118 | PSI_NOT_INSTRUMENTED |
| 119 | #endif |
| 120 | ); |
| 121 | |
| 122 | Reader_thread readers[PARTS_NUM]; |
| 123 | Writer_thread writer(&rwlock, &shared_counter); |
| 124 | for (uint i = 0; i < PARTS_NUM; ++i) |
| 125 | readers[i].init(i, &rwlock, &shared_counter); |
| 126 | |
| 127 | writer.start(); |
| 128 | for (uint i = 0; i < PARTS_NUM; ++i) readers[i].start(); |
| 129 | |
| 130 | for (uint i = 0; i < PARTS_NUM; ++i) readers[i].join(); |
| 131 | writer.join(); |
| 132 | |
| 133 | rwlock.destroy(); |
| 134 | } |
| 135 | |
| 136 | } // namespace partitioned_rwlock_unittest |