MySQL 8.4.3
Source Code Documentation
thr_rwlock.h
Go to the documentation of this file.
1#ifndef THR_RWLOCK_INCLUDED
2#define THR_RWLOCK_INCLUDED
3
4/* Copyright (c) 2014, 2024, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is designed to work with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have either included with
16 the program or referenced in the documentation.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License, version 2.0, for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
26
27/**
28 @file include/thr_rwlock.h
29 MySQL rwlock implementation.
30
31 There are two "layers":
32 1) native_rw_*()
33 Functions that map directly down to OS primitives.
34 Windows - SRWLock
35 Other OSes - pthread
36 2) mysql_rw*()
37 Functions that include Performance Schema instrumentation.
38 See include/mysql/psi/mysql_thread.h
39
40 This file also includes rw_pr_*(), which implements a special
41 version of rwlocks that prefer readers. The P_S version of these
42 are mysql_prlock_*() - see include/mysql/psi/mysql_thread.h
43*/
44
45#include <assert.h>
46#include <stddef.h>
47#include <sys/types.h>
48#ifdef _WIN32
49#include <windows.h>
50#endif
51
52#include "my_inttypes.h"
53#include "my_macros.h"
54#include "my_thread.h"
56#include "thr_cond.h"
57#include "thr_mutex.h"
58
59static inline int native_rw_init(native_rw_lock_t *rwp) {
60#ifdef _WIN32
61 InitializeSRWLock(&rwp->srwlock);
62 rwp->have_exclusive_srwlock = false;
63 return 0;
64#else
65 /* pthread_rwlockattr_t is not used in MySQL */
66 return pthread_rwlock_init(rwp, nullptr);
67#endif
68}
69
70static inline int native_rw_destroy(native_rw_lock_t *rwp [[maybe_unused]]) {
71#ifdef _WIN32
72 return 0; /* no destroy function */
73#else
74 return pthread_rwlock_destroy(rwp);
75#endif
76}
77
78static inline int native_rw_rdlock(native_rw_lock_t *rwp) {
79#ifdef _WIN32
80 AcquireSRWLockShared(&rwp->srwlock);
81 return 0;
82#else
83 return pthread_rwlock_rdlock(rwp);
84#endif
85}
86
87static inline int native_rw_tryrdlock(native_rw_lock_t *rwp) {
88#ifdef _WIN32
89 if (!TryAcquireSRWLockShared(&rwp->srwlock)) return EBUSY;
90 return 0;
91#else
92 return pthread_rwlock_tryrdlock(rwp);
93#endif
94}
95
96static inline int native_rw_wrlock(native_rw_lock_t *rwp) {
97#ifdef _WIN32
98 AcquireSRWLockExclusive(&rwp->srwlock);
99 rwp->have_exclusive_srwlock = true;
100 return 0;
101#else
102 return pthread_rwlock_wrlock(rwp);
103#endif
104}
105
106static inline int native_rw_trywrlock(native_rw_lock_t *rwp) {
107#ifdef _WIN32
108 if (!TryAcquireSRWLockExclusive(&rwp->srwlock)) return EBUSY;
109 rwp->have_exclusive_srwlock = true;
110 return 0;
111#else
112 return pthread_rwlock_trywrlock(rwp);
113#endif
114}
115
116static inline int native_rw_unlock(native_rw_lock_t *rwp) {
117#ifdef _WIN32
118 if (rwp->have_exclusive_srwlock) {
119 rwp->have_exclusive_srwlock = false;
120 ReleaseSRWLockExclusive(&rwp->srwlock);
121 } else
122 ReleaseSRWLockShared(&rwp->srwlock);
123 return 0;
124#else
125 return pthread_rwlock_unlock(rwp);
126#endif
127}
128
129extern int rw_pr_init(rw_pr_lock_t *);
130extern int rw_pr_rdlock(rw_pr_lock_t *);
131extern int rw_pr_wrlock(rw_pr_lock_t *);
132extern int rw_pr_unlock(rw_pr_lock_t *);
133extern int rw_pr_destroy(rw_pr_lock_t *);
134
135#ifdef SAFE_MUTEX
136static inline void rw_pr_lock_assert_write_owner(const rw_pr_lock_t *rwlock) {
137 assert(rwlock->active_writer &&
139}
140
141static inline void rw_pr_lock_assert_not_write_owner(
142 const rw_pr_lock_t *rwlock) {
143 assert(!rwlock->active_writer ||
145}
146#endif
147
148#endif /* THR_RWLOCK_INCLUDED */
Some integer typedefs for easier portability.
Some common macros.
Defines to make different thread packages compatible.
static my_thread_t my_thread_self()
Definition: my_thread.h:75
static int my_thread_equal(my_thread_t t1, my_thread_t t2)
Definition: my_thread.h:83
Portable implementation of special type of read-write locks.
Definition: thr_rwlock_bits.h:95
my_thread_t writer_thread
Thread holding wr-lock (for debug purposes only).
Definition: thr_rwlock_bits.h:113
bool active_writer
Indicates whether there is an active writer.
Definition: thr_rwlock_bits.h:111
MySQL condition variable implementation.
MySQL mutex 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
MySQL rwlock ABI.
pthread_rwlock_t native_rw_lock_t
Definition: thr_rwlock_bits.h:61