Debian Codegraph @

storage/innobase/include/sync0sharded_rw.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 2017, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License, version 2.0, for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 Portions of this file contain modifications contributed and copyrighted by
27 Google, Inc. Those modifications are gratefully acknowledged and are described
28 briefly in the InnoDB documentation. The contributions by Google are
29 incorporated with their permission, and subject to the conditions contained in
30 the file COPYING.Google.
31
32 *****************************************************************************/
33
34 /**************************************************/ /**
35 @file include/sync0sharded_rw.h
36
37 The sharded read-write lock (for threads).
38
39 The s-lock scales better than in single rw-lock,
40 but the x-lock is much slower.
41
42 *******************************************************/
43
44 #ifndef sync0sharded_rw_h
45 #define sync0sharded_rw_h
46
47 #include "sync0rw.h"
48 #include "ut0cpu_cache.h"
49 #include "ut0rnd.h"
50 #include "ut0ut.h"
51
52 #ifndef UNIV_HOTBACKUP
53 #ifndef UNIV_LIBRARY
54
55 /** Rw-lock with very fast, highly concurrent s-lock but slower x-lock.
56 It's basically array of rw-locks. When s-lock is being acquired, single
57 rw-lock from array is selected randomly and s-locked. Therefore, all
58 rw-locks from array has to be x-locked when x-lock is being acquired.
59
60 Purpose of this data structure is to reduce contention on single atomic
61 in single rw-lock when a lot of threads need to acquire s-lock very often,
62 but x-lock is very rare. */
63 class Sharded_rw_lock {
64 public:
65 void create(
66 #ifdef UNIV_PFS_RWLOCK
67 mysql_pfs_key_t pfs_key,
68 #endif
69 latch_id_t latch_id, size_t n_shards) {
70 ut_ad(ut_is_2pow(n_shards));
71 m_n_shards = n_shards;
72
73 m_shards = static_cast<Shard *>(
74 ut::zalloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, sizeof(Shard) * n_shards));
75
76 for_each([
77 #ifdef UNIV_PFS_RWLOCK
78 pfs_key,
79 #endif
80 latch_id](rw_lock_t &lock) {
81 static_cast<void>(latch_id); // clang -Wunused-lambda-capture
82 rw_lock_create(pfs_key, &lock, latch_id);
83 });
84 }
85
86 void free() {
87 ut_a(m_shards != nullptr);
88
89 for_each([](rw_lock_t &lock) { rw_lock_free(&lock); });
90
91 ut::free(m_shards);
92 m_shards = nullptr;
93 m_n_shards = 0;
94 }
95
96 size_t s_lock(ut::Location location) {
97 const size_t shard_no =
98 default_indexer_t<>::get_rnd_index() & (m_n_shards - 1);
99 rw_lock_s_lock_gen(&m_shards[shard_no], 0, location);
100 return shard_no;
101 }
102
103 void s_unlock(size_t shard_no) {
104 ut_a(shard_no < m_n_shards);
105 rw_lock_s_unlock(&m_shards[shard_no]);
106 }
107 /** Checks if there is a thread requesting an x-latch waiting for threads to
108 release their s-latches on given shard.
109 @param[in] shard_no The shard to check.
110 @return true iff there is an x-latcher blocked by s-latchers on shard_no. */
111 bool is_x_blocked_by_s(size_t shard_no) {
112 ut_a(shard_no < m_n_shards);
113 return m_shards[shard_no].is_x_blocked_by_s();
114 }
115 /**
116 Tries to obtain exclusive latch - similar to x_lock(), but non-blocking, and
117 thus can fail.
118 @return true iff succeeded to acquire the exclusive latch
119 */
120 bool try_x_lock(ut::Location location) {
121 for (size_t shard_no = 0; shard_no < m_n_shards; ++shard_no) {
122 if (!rw_lock_x_lock_nowait(&m_shards[shard_no], location)) {
123 while (0 < shard_no--) {
124 rw_lock_x_unlock(&m_shards[shard_no]);
125 }
126 return (false);
127 }
128 }
129 return (true);
130 }
131
132 void x_lock(ut::Location location) {
133 for_each([location](rw_lock_t &lock) {
134 rw_lock_x_lock_gen(&lock, 0, location);
135 });
136 }
137
138 void x_unlock() {
139 for_each([](rw_lock_t &lock) { rw_lock_x_unlock(&lock); });
140 }
141
142 #ifdef UNIV_DEBUG
143 bool s_own(size_t shard_no) const {
144 return rw_lock_own(&m_shards[shard_no], RW_LOCK_S);
145 }
146
147 bool x_own() const { return rw_lock_own(&m_shards[0], RW_LOCK_X); }
148 #endif /* !UNIV_DEBUG */
149
150 private:
151 using Shard = ut::Cacheline_padded<rw_lock_t>;
152
153 template <typename F>
154 void for_each(F f) {
155 std::for_each(m_shards, m_shards + m_n_shards, f);
156 }
157
158 Shard *m_shards = nullptr;
159
160 size_t m_n_shards = 0;
161 };
162
163 #else /* !UNIV_LIBRARY */
164
165 /* For UNIV_LIBRARY, rw_lock is no-op, so sharded rw-lock is also no-op. */
166
167 class Sharded_rw_lock {
168 public:
169 void create(
170 #ifdef UNIV_PFS_RWLOCK
171 mysql_pfs_key_t pfs_key,
172 #endif
173 latch_id_t latch_id, size_t n_shards) {
174 }
175
176 void free() {}
177
178 size_t s_lock() { return 0; }
179
180 void s_unlock(size_t shard_no) { ut_a(shard_no == 0); }
181
182 void x_lock() {}
183
184 void x_unlock() {}
185 };
186
187 #endif /* UNIV_LIBRARY */
188 #endif /* UNIV_HOTBACKUP */
189
190 #endif /* sync0sharded_rw.h */