MySQL 8.4.2
Source Code Documentation
mysql_rwlock.h
Go to the documentation of this file.
1/* Copyright (c) 2008, 2024, 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#ifndef MYSQL_RWLOCK_H
25#define MYSQL_RWLOCK_H
26
27/**
28 @file include/mysql/psi/mysql_rwlock.h
29 Instrumentation helpers for rwlock.
30*/
31
32/* HAVE_PSI_*_INTERFACE */
33#include "my_psi_config.h" // IWYU pragma: keep
34
37#include "thr_rwlock.h"
38
39#if defined(MYSQL_SERVER) || defined(PFS_DIRECT_CALL)
40/* PSI_RWLOCK_CALL() as direct call. */
41#include "pfs_rwlock_provider.h" // IWYU pragma: keep
42#endif
43
44#ifndef PSI_RWLOCK_CALL
45#define PSI_RWLOCK_CALL(M) psi_rwlock_service->M
46#endif
47
48/**
49 @defgroup psi_api_rwlock Rwlock Instrumentation (API)
50 @ingroup psi_api
51 @{
52*/
53
54/**
55 @def mysql_prlock_assert_write_owner(M)
56 Drop-in replacement
57 for @c rw_pr_lock_assert_write_owner.
58*/
59#ifdef SAFE_MUTEX
60#define mysql_prlock_assert_write_owner(M) \
61 rw_pr_lock_assert_write_owner(&(M)->m_prlock)
62#else
63#define mysql_prlock_assert_write_owner(M) \
64 {}
65#endif
66
67/**
68 @def mysql_prlock_assert_not_write_owner(M)
69 Drop-in replacement
70 for @c rw_pr_lock_assert_not_write_owner.
71*/
72#ifdef SAFE_MUTEX
73#define mysql_prlock_assert_not_write_owner(M) \
74 rw_pr_lock_assert_not_write_owner(&(M)->m_prlock)
75#else
76#define mysql_prlock_assert_not_write_owner(M) \
77 {}
78#endif
79
80#ifndef DISABLE_MYSQL_THREAD_H
81
82/**
83 @def mysql_rwlock_register(P1, P2, P3)
84 Rwlock registration.
85*/
86#define mysql_rwlock_register(P1, P2, P3) \
87 inline_mysql_rwlock_register(P1, P2, P3)
88
89/**
90 @def mysql_rwlock_init(K, T)
91 Instrumented rwlock_init.
92 @c mysql_rwlock_init is a replacement for @c pthread_rwlock_init.
93 Note that pthread_rwlockattr_t is not supported in MySQL.
94 @param K The PSI_rwlock_key for this instrumented rwlock
95 @param T The rwlock to initialize
96*/
97
98#define mysql_rwlock_init(K, T) \
99 mysql_rwlock_init_with_src(K, T, __FILE__, __LINE__)
100
101#define mysql_rwlock_init_with_src(K, T, F, L) \
102 inline_mysql_rwlock_init(K, T, F, L)
103
104/**
105 @def mysql_prlock_init(K, T)
106 Instrumented rw_pr_init.
107 @c mysql_prlock_init is a replacement for @c rw_pr_init.
108 @param K The PSI_rwlock_key for this instrumented prlock
109 @param T The prlock to initialize
110*/
111
112#define mysql_prlock_init(K, T) \
113 mysql_prlock_init_with_src(K, T, __FILE__, __LINE__)
114
115#define mysql_prlock_init_with_src(K, T, F, L) \
116 inline_mysql_prlock_init(K, T, F, L)
117
118/**
119 @def mysql_rwlock_destroy(T)
120 Instrumented rwlock_destroy.
121 @c mysql_rwlock_destroy is a drop-in replacement
122 for @c pthread_rwlock_destroy.
123*/
124
125#define mysql_rwlock_destroy(T) \
126 mysql_rwlock_destroy_with_src(T, __FILE__, __LINE__)
127
128#define mysql_rwlock_destroy_with_src(T, F, L) \
129 inline_mysql_rwlock_destroy(T, F, L)
130
131/**
132 @def mysql_prlock_destroy(T)
133 Instrumented rw_pr_destroy.
134 @c mysql_prlock_destroy is a drop-in replacement
135 for @c rw_pr_destroy.
136*/
137#define mysql_prlock_destroy(T) \
138 mysql_prlock_destroy_with_src(T, __FILE__, __LINE__)
139
140#define mysql_prlock_destroy_with_src(T, F, L) \
141 inline_mysql_prlock_destroy(T, F, L)
142
143/**
144 @def mysql_rwlock_rdlock(T)
145 Instrumented rwlock_rdlock.
146 @c mysql_rwlock_rdlock is a drop-in replacement
147 for @c pthread_rwlock_rdlock.
148*/
149
150#define mysql_rwlock_rdlock(T) \
151 mysql_rwlock_rdlock_with_src(T, __FILE__, __LINE__)
152
153#define mysql_rwlock_rdlock_with_src(T, F, L) \
154 inline_mysql_rwlock_rdlock(T, F, L)
155
156/**
157 @def mysql_prlock_rdlock(T)
158 Instrumented rw_pr_rdlock.
159 @c mysql_prlock_rdlock is a drop-in replacement
160 for @c rw_pr_rdlock.
161*/
162
163#define mysql_prlock_rdlock(T) \
164 mysql_prlock_rdlock_with_src(T, __FILE__, __LINE__)
165
166#define mysql_prlock_rdlock_with_src(T, F, L) \
167 inline_mysql_prlock_rdlock(T, F, L)
168
169/**
170 @def mysql_rwlock_wrlock(T)
171 Instrumented rwlock_wrlock.
172 @c mysql_rwlock_wrlock is a drop-in replacement
173 for @c pthread_rwlock_wrlock.
174*/
175
176#define mysql_rwlock_wrlock(T) \
177 mysql_rwlock_wrlock_with_src(T, __FILE__, __LINE__)
178
179#define mysql_rwlock_wrlock_with_src(T, F, L) \
180 inline_mysql_rwlock_wrlock(T, F, L)
181
182/**
183 @def mysql_prlock_wrlock(T)
184 Instrumented rw_pr_wrlock.
185 @c mysql_prlock_wrlock is a drop-in replacement
186 for @c rw_pr_wrlock.
187*/
188
189#define mysql_prlock_wrlock(T) \
190 mysql_prlock_wrlock_with_src(T, __FILE__, __LINE__)
191
192#define mysql_prlock_wrlock_with_src(T, F, L) \
193 inline_mysql_prlock_wrlock(T, F, L)
194
195/**
196 @def mysql_rwlock_tryrdlock(T)
197 Instrumented rwlock_tryrdlock.
198 @c mysql_rwlock_tryrdlock is a drop-in replacement
199 for @c pthread_rwlock_tryrdlock.
200*/
201
202#define mysql_rwlock_tryrdlock(T) \
203 mysql_rwlock_tryrdlock_with_src(T, __FILE__, __LINE__)
204
205#define mysql_rwlock_tryrdlock_with_src(T, F, L) \
206 inline_mysql_rwlock_tryrdlock(T, F, L)
207
208/**
209 @def mysql_rwlock_trywrlock(T)
210 Instrumented rwlock_trywrlock.
211 @c mysql_rwlock_trywrlock is a drop-in replacement
212 for @c pthread_rwlock_trywrlock.
213*/
214
215#define mysql_rwlock_trywrlock(T) \
216 mysql_rwlock_trywrlock_with_src(T, __FILE__, __LINE__)
217
218#define mysql_rwlock_trywrlock_with_src(T, F, L) \
219 inline_mysql_rwlock_trywrlock(T, F, L)
220
221/**
222 @def mysql_rwlock_unlock(T)
223 Instrumented rwlock_unlock.
224 @c mysql_rwlock_unlock is a drop-in replacement
225 for @c pthread_rwlock_unlock.
226*/
227#define mysql_rwlock_unlock(T) \
228 mysql_rwlock_unlock_with_src(T, __FILE__, __LINE__)
229
230#define mysql_rwlock_unlock_with_src(T, F, L) \
231 inline_mysql_rwlock_unlock(T, F, L)
232
233/**
234 @def mysql_prlock_unlock(T)
235 Instrumented rw_pr_unlock.
236 @c mysql_prlock_unlock is a drop-in replacement
237 for @c rw_pr_unlock.
238*/
239
240#define mysql_prlock_unlock(T) \
241 mysql_prlock_unlock_with_src(T, __FILE__, __LINE__)
242
243#define mysql_prlock_unlock_with_src(T, F, L) \
244 inline_mysql_prlock_unlock(T, F, L)
245
246static inline void inline_mysql_rwlock_register(const char *category
247 [[maybe_unused]],
248 PSI_rwlock_info *info
249 [[maybe_unused]],
250 int count [[maybe_unused]]) {
251#ifdef HAVE_PSI_RWLOCK_INTERFACE
252 PSI_RWLOCK_CALL(register_rwlock)(category, info, count);
253#endif
254}
255
256static inline int inline_mysql_rwlock_init(PSI_rwlock_key key [[maybe_unused]],
257 mysql_rwlock_t *that,
258 const char *src_file
259 [[maybe_unused]],
260 int src_line [[maybe_unused]]) {
261#ifdef HAVE_PSI_RWLOCK_INTERFACE
262 that->m_psi = PSI_RWLOCK_CALL(init_rwlock)(key, &that->m_rwlock);
263#else
264 that->m_psi = nullptr;
265#endif
266 return native_rw_init(&that->m_rwlock);
267}
268
269#ifndef DISABLE_MYSQL_PRLOCK_H
270static inline int inline_mysql_prlock_init(PSI_rwlock_key key [[maybe_unused]],
271 mysql_prlock_t *that,
272 const char *src_file
273 [[maybe_unused]],
274 int src_line [[maybe_unused]]) {
275#ifdef HAVE_PSI_RWLOCK_INTERFACE
276 that->m_psi = PSI_RWLOCK_CALL(init_rwlock)(key, &that->m_prlock);
277#else
278 that->m_psi = nullptr;
279#endif
280 return rw_pr_init(&that->m_prlock);
281}
282#endif
283
285 const char *src_file
286 [[maybe_unused]],
287 int src_line [[maybe_unused]]) {
288#ifdef HAVE_PSI_RWLOCK_INTERFACE
289 if (that->m_psi != nullptr) {
291 that->m_psi = nullptr;
292 }
293#endif
294 return native_rw_destroy(&that->m_rwlock);
295}
296
297#ifndef DISABLE_MYSQL_PRLOCK_H
299 const char *src_file
300 [[maybe_unused]],
301 int src_line [[maybe_unused]]) {
302#ifdef HAVE_PSI_RWLOCK_INTERFACE
303 if (that->m_psi != nullptr) {
305 that->m_psi = nullptr;
306 }
307#endif
308 return rw_pr_destroy(&that->m_prlock);
309}
310#endif
311
313 const char *src_file
314 [[maybe_unused]],
315 int src_line [[maybe_unused]]) {
316 int result;
317
318#ifdef HAVE_PSI_RWLOCK_INTERFACE
319 if (that->m_psi != nullptr) {
320 if (that->m_psi->m_enabled) {
321 /* Instrumentation start */
322 PSI_rwlock_locker *locker;
324 locker = PSI_RWLOCK_CALL(start_rwlock_rdwait)(
325 &state, that->m_psi, PSI_RWLOCK_READLOCK, src_file, src_line);
326
327 /* Instrumented code */
329
330 /* Instrumentation end */
331 if (locker != nullptr) {
332 PSI_RWLOCK_CALL(end_rwlock_rdwait)(locker, result);
333 }
334
335 return result;
336 }
337 }
338#endif
339
340 /* Non instrumented code */
342
343 return result;
344}
345
346#ifndef DISABLE_MYSQL_PRLOCK_H
348 const char *src_file
349 [[maybe_unused]],
350 int src_line [[maybe_unused]]) {
351 int result;
352
353#ifdef HAVE_PSI_RWLOCK_INTERFACE
354 if (that->m_psi != nullptr) {
355 if (that->m_psi->m_enabled) {
356 /* Instrumentation start */
357 PSI_rwlock_locker *locker;
359 locker = PSI_RWLOCK_CALL(start_rwlock_rdwait)(
360 &state, that->m_psi, PSI_RWLOCK_READLOCK, src_file, src_line);
361
362 /* Instrumented code */
363 result = rw_pr_rdlock(&that->m_prlock);
364
365 /* Instrumentation end */
366 if (locker != nullptr) {
367 PSI_RWLOCK_CALL(end_rwlock_rdwait)(locker, result);
368 }
369
370 return result;
371 }
372 }
373#endif
374
375 /* Non instrumented code */
376 result = rw_pr_rdlock(&that->m_prlock);
377
378 return result;
379}
380#endif
381
383 const char *src_file
384 [[maybe_unused]],
385 int src_line [[maybe_unused]]) {
386 int result;
387
388#ifdef HAVE_PSI_RWLOCK_INTERFACE
389 if (that->m_psi != nullptr) {
390 if (that->m_psi->m_enabled) {
391 /* Instrumentation start */
392 PSI_rwlock_locker *locker;
394 locker = PSI_RWLOCK_CALL(start_rwlock_wrwait)(
395 &state, that->m_psi, PSI_RWLOCK_WRITELOCK, src_file, src_line);
396
397 /* Instrumented code */
399
400 /* Instrumentation end */
401 if (locker != nullptr) {
402 PSI_RWLOCK_CALL(end_rwlock_wrwait)(locker, result);
403 }
404
405 return result;
406 }
407 }
408#endif
409
410 /* Non instrumented code */
412
413 return result;
414}
415
416#ifndef DISABLE_MYSQL_PRLOCK_H
418 const char *src_file
419 [[maybe_unused]],
420 int src_line [[maybe_unused]]) {
421 int result;
422
423#ifdef HAVE_PSI_RWLOCK_INTERFACE
424 if (that->m_psi != nullptr) {
425 if (that->m_psi->m_enabled) {
426 /* Instrumentation start */
427 PSI_rwlock_locker *locker;
429 locker = PSI_RWLOCK_CALL(start_rwlock_wrwait)(
430 &state, that->m_psi, PSI_RWLOCK_WRITELOCK, src_file, src_line);
431
432 /* Instrumented code */
433 result = rw_pr_wrlock(&that->m_prlock);
434
435 /* Instrumentation end */
436 if (locker != nullptr) {
437 PSI_RWLOCK_CALL(end_rwlock_wrwait)(locker, result);
438 }
439
440 return result;
441 }
442 }
443#endif
444
445 /* Non instrumented code */
446 result = rw_pr_wrlock(&that->m_prlock);
447
448 return result;
449}
450#endif
451
453 const char *src_file
454 [[maybe_unused]],
455 int src_line [[maybe_unused]]) {
456 int result;
457
458#ifdef HAVE_PSI_RWLOCK_INTERFACE
459 if (that->m_psi != nullptr) {
460 if (that->m_psi->m_enabled) {
461 /* Instrumentation start */
462 PSI_rwlock_locker *locker;
464 locker = PSI_RWLOCK_CALL(start_rwlock_rdwait)(
465 &state, that->m_psi, PSI_RWLOCK_TRYREADLOCK, src_file, src_line);
466
467 /* Instrumented code */
469
470 /* Instrumentation end */
471 if (locker != nullptr) {
472 PSI_RWLOCK_CALL(end_rwlock_rdwait)(locker, result);
473 }
474
475 return result;
476 }
477 }
478#endif
479
480 /* Non instrumented code */
482
483 return result;
484}
485
487 const char *src_file
488 [[maybe_unused]],
489 int src_line [[maybe_unused]]) {
490 int result;
491
492#ifdef HAVE_PSI_RWLOCK_INTERFACE
493 if (that->m_psi != nullptr) {
494 if (that->m_psi->m_enabled) {
495 /* Instrumentation start */
496 PSI_rwlock_locker *locker;
498 locker = PSI_RWLOCK_CALL(start_rwlock_wrwait)(
499 &state, that->m_psi, PSI_RWLOCK_TRYWRITELOCK, src_file, src_line);
500
501 /* Instrumented code */
503
504 /* Instrumentation end */
505 if (locker != nullptr) {
506 PSI_RWLOCK_CALL(end_rwlock_wrwait)(locker, result);
507 }
508
509 return result;
510 }
511 }
512#endif
513
514 /* Non instrumented code */
516
517 return result;
518}
519
521 const char *src_file
522 [[maybe_unused]],
523 int src_line [[maybe_unused]]) {
524 int result;
525#ifdef HAVE_PSI_RWLOCK_INTERFACE
526 if (that->m_psi != nullptr) {
527 if (that->m_psi->m_enabled) {
528 PSI_RWLOCK_CALL(unlock_rwlock)(that->m_psi, PSI_RWLOCK_UNLOCK);
529 }
530 }
531#endif
533 return result;
534}
535
536#ifndef DISABLE_MYSQL_PRLOCK_H
538 const char *src_file
539 [[maybe_unused]],
540 int src_line [[maybe_unused]]) {
541 int result;
542#ifdef HAVE_PSI_RWLOCK_INTERFACE
543 if (that->m_psi != nullptr) {
544 if (that->m_psi->m_enabled) {
545 PSI_RWLOCK_CALL(unlock_rwlock)(that->m_psi, PSI_RWLOCK_UNLOCK);
546 }
547 }
548#endif
549 result = rw_pr_unlock(&that->m_prlock);
550 return result;
551}
552#endif
553
554#endif /* DISABLE_MYSQL_THREAD_H */
555
556/** @} (end of group psi_api_rwlock) */
557
558#endif
#define PSI_RWLOCK_CALL(M)
Definition: psi_rwlock.h:36
void destroy_rwlock(PFS_rwlock *pfs)
Destroy instrumentation for a rwlock instance.
Definition: pfs_instr.cc:430
unsigned int PSI_rwlock_key
Instrumented rwlock key.
Definition: psi_rwlock_bits.h:44
struct PSI_rwlock_locker PSI_rwlock_locker
Definition: psi_rwlock_bits.h:79
@ PSI_RWLOCK_TRYWRITELOCK
Write lock attempt.
Definition: psi_rwlock_bits.h:132
@ PSI_RWLOCK_WRITELOCK
Write lock.
Definition: psi_rwlock_bits.h:128
@ PSI_RWLOCK_UNLOCK
Unlock (Read or Write).
Definition: psi_rwlock_bits.h:134
@ PSI_RWLOCK_TRYREADLOCK
Read lock attempt.
Definition: psi_rwlock_bits.h:130
@ PSI_RWLOCK_READLOCK
Read lock.
Definition: psi_rwlock_bits.h:126
static int inline_mysql_rwlock_init(PSI_rwlock_key key, mysql_rwlock_t *that, const char *src_file, int src_line)
Definition: mysql_rwlock.h:256
static int inline_mysql_prlock_wrlock(mysql_prlock_t *that, const char *src_file, int src_line)
Definition: mysql_rwlock.h:417
static int inline_mysql_prlock_unlock(mysql_prlock_t *that, const char *src_file, int src_line)
Definition: mysql_rwlock.h:537
static int inline_mysql_prlock_init(PSI_rwlock_key key, mysql_prlock_t *that, const char *src_file, int src_line)
Definition: mysql_rwlock.h:270
static int inline_mysql_rwlock_unlock(mysql_rwlock_t *that, const char *src_file, int src_line)
Definition: mysql_rwlock.h:520
static int inline_mysql_prlock_destroy(mysql_prlock_t *that, const char *src_file, int src_line)
Definition: mysql_rwlock.h:298
static int inline_mysql_rwlock_trywrlock(mysql_rwlock_t *that, const char *src_file, int src_line)
Definition: mysql_rwlock.h:486
static int inline_mysql_rwlock_destroy(mysql_rwlock_t *that, const char *src_file, int src_line)
Definition: mysql_rwlock.h:284
static int inline_mysql_rwlock_rdlock(mysql_rwlock_t *that, const char *src_file, int src_line)
Definition: mysql_rwlock.h:312
static int inline_mysql_rwlock_wrlock(mysql_rwlock_t *that, const char *src_file, int src_line)
Definition: mysql_rwlock.h:382
static int inline_mysql_rwlock_tryrdlock(mysql_rwlock_t *that, const char *src_file, int src_line)
Definition: mysql_rwlock.h:452
static void inline_mysql_rwlock_register(const char *category, PSI_rwlock_info *info, int count)
Definition: mysql_rwlock.h:246
static int inline_mysql_prlock_rdlock(mysql_prlock_t *that, const char *src_file, int src_line)
Definition: mysql_rwlock.h:347
Defines various enable/disable and HAVE_ macros related to the performance schema instrumentation sys...
static int count
Definition: myisam_ftdump.cc:45
Instrumentation helpers for rwlock.
static const char * category
Definition: sha2_password.cc:170
Performance schema instrumentation (declarations).
struct result result
Definition: result.h:34
Performance schema instrumentation interface.
required string key
Definition: replication_asynchronous_connection_failover.proto:60
bool m_enabled
Instrumentation is enabled.
Definition: psi_bits.h:191
Rwlock information.
Definition: psi_rwlock_bits.h:162
State data storage for start_rwlock_rdwait_v1_t, start_rwlock_wrwait_v1_t.
Definition: psi_rwlock_bits.h:194
An instrumented prlock structure.
Definition: mysql_rwlock_bits.h:72
struct PSI_rwlock * m_psi
The instrumentation hook.
Definition: mysql_rwlock_bits.h:80
rw_pr_lock_t m_prlock
The real prlock.
Definition: mysql_rwlock_bits.h:74
An instrumented rwlock structure.
Definition: mysql_rwlock_bits.h:51
struct PSI_rwlock * m_psi
The instrumentation hook.
Definition: mysql_rwlock_bits.h:59
native_rw_lock_t m_rwlock
The real rwlock.
Definition: mysql_rwlock_bits.h:53
Definition: result.h:30
MySQL rwlock implementation.
static int native_rw_init(native_rw_lock_t *rwp)
Definition: thr_rwlock.h:59
int rw_pr_wrlock(rw_pr_lock_t *)
Definition: thr_rwlock.cc:67
int rw_pr_destroy(rw_pr_lock_t *)
Definition: thr_rwlock.cc:48
static int native_rw_trywrlock(native_rw_lock_t *rwp)
Definition: thr_rwlock.h:106
int rw_pr_rdlock(rw_pr_lock_t *)
Definition: thr_rwlock.cc:54
static int native_rw_tryrdlock(native_rw_lock_t *rwp)
Definition: thr_rwlock.h:87
static int native_rw_destroy(native_rw_lock_t *rwp)
Definition: thr_rwlock.h:70
int rw_pr_unlock(rw_pr_lock_t *)
Definition: thr_rwlock.cc:100
static int native_rw_rdlock(native_rw_lock_t *rwp)
Definition: thr_rwlock.h:78
static int native_rw_unlock(native_rw_lock_t *rwp)
Definition: thr_rwlock.h:116
static int native_rw_wrlock(native_rw_lock_t *rwp)
Definition: thr_rwlock.h:96
int rw_pr_init(rw_pr_lock_t *)
Definition: thr_rwlock.cc:36