1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
#ifndef PARTITIONED_RWLOCK_INCLUDED
#define PARTITIONED_RWLOCK_INCLUDED
/* Copyright (c) 2015, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
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
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include <new>
#include "my_compiler.h"
#include "my_psi_config.h"
#include "mysql/psi/mysql_rwlock.h"
/**
Implementation of read-write lock partitioned by thread id.
This rwlock provides better scalability in read-heavy environments by
employing the following simple trick:
*) Read lock is acquired only on one of its partitions. The specific
partition is chosen according to thread id.
*) Write lock is acquired on all partitions.
This way concurrent request for read lock made by different threads
have a good chance not to disturb each other by doing cache invalidation
and atomic operations. As result scalability in this scenario improves.
OTOH acquisition of write lock becomes more expensive. So this rwlock
is not supposed to be used in cases when number of write requests is
significant.
*/
class Partitioned_rwlock {
public:
Partitioned_rwlock() = default;
/**
@param parts Number of partitions.
@param psi_key P_S instrumentation key to use for rwlock instances
for partitions.
*/
bool init(uint parts
#ifdef HAVE_PSI_INTERFACE
,
PSI_rwlock_key psi_key [[maybe_unused]]
#endif
) {
m_parts = parts;
if (!(m_locks_array = new (std::nothrow) mysql_rwlock_t[m_parts]))
return true;
for (uint i = 0; i < m_parts; ++i)
mysql_rwlock_init(psi_key, &m_locks_array[i]);
return false;
}
void destroy() {
for (uint i = 0; i < m_parts; ++i) mysql_rwlock_destroy(&m_locks_array[i]);
delete[] m_locks_array;
}
void wrlock() {
for (uint i = 0; i < m_parts; ++i) mysql_rwlock_wrlock(&m_locks_array[i]);
}
void wrunlock() {
for (uint i = 0; i < m_parts; ++i) mysql_rwlock_unlock(&m_locks_array[i]);
}
void rdlock(uint thread_id) {
mysql_rwlock_rdlock(&m_locks_array[thread_id % m_parts]);
}
/*
One should use the same thread number for releasing read lock
as was used for acquiring it,
*/
void rdunlock(uint thread_id) {
mysql_rwlock_unlock(&m_locks_array[thread_id % m_parts]);
}
private:
mysql_rwlock_t *m_locks_array;
uint m_parts;
Partitioned_rwlock(const Partitioned_rwlock &); // Non-copyable
Partitioned_rwlock &operator=(const Partitioned_rwlock &); // Non-copyable
};
/**
Read lock guard class for Partitioned_rwlock. Supports early unlocking.
*/
class Partitioned_rwlock_read_guard {
public:
/**
Acquires read lock on partitioned rwlock on behalf of thread.
Automatically release lock in destructor.
*/
Partitioned_rwlock_read_guard(Partitioned_rwlock *rwlock, uint thread_id)
: m_rwlock(rwlock), m_thread_id(thread_id) {
m_rwlock->rdlock(m_thread_id);
}
~Partitioned_rwlock_read_guard() {
if (m_rwlock) m_rwlock->rdunlock(m_thread_id);
}
/** Release read lock. Optional method for early unlocking. */
void unlock() {
m_rwlock->rdunlock(m_thread_id);
m_rwlock = nullptr;
}
private:
/**
Pointer to partitioned rwlock which was acquired. NULL if lock was
released early so destructor should not do anything.
*/
Partitioned_rwlock *m_rwlock;
/**
Id of thread on which behalf lock was acquired and which is to be used for
unlocking.
*/
uint m_thread_id;
// Non-copyable
Partitioned_rwlock_read_guard(const Partitioned_rwlock_read_guard &);
Partitioned_rwlock_read_guard &operator=(
const Partitioned_rwlock_read_guard &);
};
/**
Write lock guard class for Partitioned_rwlock. Supports early unlocking.
*/
class Partitioned_rwlock_write_guard {
public:
/**
Acquires write lock on partitioned rwlock.
Automatically release it in destructor.
*/
explicit Partitioned_rwlock_write_guard(Partitioned_rwlock *rwlock)
: m_rwlock(rwlock) {
m_rwlock->wrlock();
}
~Partitioned_rwlock_write_guard() {
if (m_rwlock) m_rwlock->wrunlock();
}
/** Release write lock. Optional method for early unlocking. */
void unlock() {
m_rwlock->wrunlock();
m_rwlock = nullptr;
}
private:
/**
Pointer to partitioned rwlock which was acquired. NULL if lock was
released early so destructor should not do anything.
*/
Partitioned_rwlock *m_rwlock;
// Non-copyable
Partitioned_rwlock_write_guard(const Partitioned_rwlock_write_guard &);
Partitioned_rwlock_write_guard &operator=(
const Partitioned_rwlock_write_guard &);
};
#endif /* PARTITIONED_RWLOCK_INCLUDED */
|