MySQL 8.0.31
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, 2022, 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 also distributed 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 included with MySQL.
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/**
27 @file include/thr_rwlock.h
28 MySQL rwlock implementation.
29
30 There are two "layers":
31 1) native_rw_*()
32 Functions that map directly down to OS primitives.
33 Windows - SRWLock
34 Other OSes - pthread
35 2) mysql_rw*()
36 Functions that include Performance Schema instrumentation.
37 See include/mysql/psi/mysql_thread.h
38
39 This file also includes rw_pr_*(), which implements a special
40 version of rwlocks that prefer readers. The P_S version of these
41 are mysql_prlock_*() - see include/mysql/psi/mysql_thread.h
42*/
43
44#include <assert.h>
45#include <stddef.h>
46#include <sys/types.h>
47#ifdef _WIN32
48#include <windows.h>
49#endif
50
51#include "my_inttypes.h"
52#include "my_macros.h"
53#include "my_thread.h"
55#include "thr_cond.h"
56#include "thr_mutex.h"
57
58static inline int native_rw_init(native_rw_lock_t *rwp) {
59#ifdef _WIN32
60 InitializeSRWLock(&rwp->srwlock);
61 rwp->have_exclusive_srwlock = false;
62 return 0;
63#else
64 /* pthread_rwlockattr_t is not used in MySQL */
65 return pthread_rwlock_init(rwp, nullptr);
66#endif
67}
68
69static inline int native_rw_destroy(native_rw_lock_t *rwp [[maybe_unused]]) {
70#ifdef _WIN32
71 return 0; /* no destroy function */
72#else
73 return pthread_rwlock_destroy(rwp);
74#endif
75}
76
77static inline int native_rw_rdlock(native_rw_lock_t *rwp) {
78#ifdef _WIN32
79 AcquireSRWLockShared(&rwp->srwlock);
80 return 0;
81#else
82 return pthread_rwlock_rdlock(rwp);
83#endif
84}
85
86static inline int native_rw_tryrdlock(native_rw_lock_t *rwp) {
87#ifdef _WIN32
88 if (!TryAcquireSRWLockShared(&rwp->srwlock)) return EBUSY;
89 return 0;
90#else
91 return pthread_rwlock_tryrdlock(rwp);
92#endif
93}
94
95static inline int native_rw_wrlock(native_rw_lock_t *rwp) {
96#ifdef _WIN32
97 AcquireSRWLockExclusive(&rwp->srwlock);
98 rwp->have_exclusive_srwlock = true;
99 return 0;
100#else
101 return pthread_rwlock_wrlock(rwp);
102#endif
103}
104
105static inline int native_rw_trywrlock(native_rw_lock_t *rwp) {
106#ifdef _WIN32
107 if (!TryAcquireSRWLockExclusive(&rwp->srwlock)) return EBUSY;
108 rwp->have_exclusive_srwlock = true;
109 return 0;
110#else
111 return pthread_rwlock_trywrlock(rwp);
112#endif
113}
114
115static inline int native_rw_unlock(native_rw_lock_t *rwp) {
116#ifdef _WIN32
117 if (rwp->have_exclusive_srwlock) {
118 rwp->have_exclusive_srwlock = false;
119 ReleaseSRWLockExclusive(&rwp->srwlock);
120 } else
121 ReleaseSRWLockShared(&rwp->srwlock);
122 return 0;
123#else
124 return pthread_rwlock_unlock(rwp);
125#endif
126}
127
128extern int rw_pr_init(rw_pr_lock_t *);
129extern int rw_pr_rdlock(rw_pr_lock_t *);
130extern int rw_pr_wrlock(rw_pr_lock_t *);
131extern int rw_pr_unlock(rw_pr_lock_t *);
132extern int rw_pr_destroy(rw_pr_lock_t *);
133
134#ifdef SAFE_MUTEX
135static inline void rw_pr_lock_assert_write_owner(const rw_pr_lock_t *rwlock) {
136 assert(rwlock->active_writer &&
138}
139
140static inline void rw_pr_lock_assert_not_write_owner(
141 const rw_pr_lock_t *rwlock) {
142 assert(!rwlock->active_writer ||
144}
145#endif
146
147#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:74
static int my_thread_equal(my_thread_t t1, my_thread_t t2)
Definition: my_thread.h:82
Portable implementation of special type of read-write locks.
Definition: thr_rwlock_bits.h:94
my_thread_t writer_thread
Thread holding wr-lock (for debug purposes only).
Definition: thr_rwlock_bits.h:112
bool active_writer
Indicates whether there is an active writer.
Definition: thr_rwlock_bits.h:110
MySQL condition variable implementation.
MySQL mutex implementation.
static int native_rw_init(native_rw_lock_t *rwp)
Definition: thr_rwlock.h:58
int rw_pr_wrlock(rw_pr_lock_t *)
Definition: thr_rwlock.cc:66
int rw_pr_destroy(rw_pr_lock_t *)
Definition: thr_rwlock.cc:47
static int native_rw_trywrlock(native_rw_lock_t *rwp)
Definition: thr_rwlock.h:105
int rw_pr_rdlock(rw_pr_lock_t *)
Definition: thr_rwlock.cc:53
static int native_rw_tryrdlock(native_rw_lock_t *rwp)
Definition: thr_rwlock.h:86
static int native_rw_destroy(native_rw_lock_t *rwp)
Definition: thr_rwlock.h:69
int rw_pr_unlock(rw_pr_lock_t *)
Definition: thr_rwlock.cc:99
static int native_rw_rdlock(native_rw_lock_t *rwp)
Definition: thr_rwlock.h:77
static int native_rw_unlock(native_rw_lock_t *rwp)
Definition: thr_rwlock.h:115
static int native_rw_wrlock(native_rw_lock_t *rwp)
Definition: thr_rwlock.h:95
int rw_pr_init(rw_pr_lock_t *)
Definition: thr_rwlock.cc:35
MySQL rwlock ABI.
pthread_rwlock_t native_rw_lock_t
Definition: thr_rwlock_bits.h:60