MySQL  8.0.19
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, 2017, Oracle and/or its affiliates. All rights reserved.
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 <stddef.h>
45 #include <sys/types.h>
46 #ifdef _WIN32
47 #include <windows.h>
48 #endif
49 
50 #include "my_dbug.h"
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 
58 static 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, NULL);
66 #endif
67 }
68 
69 static inline int native_rw_destroy(
70  native_rw_lock_t *rwp MY_ATTRIBUTE((unused))) {
71 #ifdef _WIN32
72  return 0; /* no destroy function */
73 #else
74  return pthread_rwlock_destroy(rwp);
75 #endif
76 }
77 
78 static 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 
87 static 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 
96 static 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 
106 static 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 
116 static 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 
129 extern int rw_pr_init(rw_pr_lock_t *);
130 extern int rw_pr_rdlock(rw_pr_lock_t *);
131 extern int rw_pr_wrlock(rw_pr_lock_t *);
132 extern int rw_pr_unlock(rw_pr_lock_t *);
133 extern int rw_pr_destroy(rw_pr_lock_t *);
134 
135 #ifdef SAFE_MUTEX
136 static inline void rw_pr_lock_assert_write_owner(const rw_pr_lock_t *rwlock) {
137  DBUG_ASSERT(rwlock->active_writer &&
139 }
140 
141 static inline void rw_pr_lock_assert_not_write_owner(
142  const rw_pr_lock_t *rwlock) {
143  DBUG_ASSERT(!rwlock->active_writer ||
145 }
146 #endif
147 
148 #endif /* THR_RWLOCK_INCLUDED */
rw_pr_init
int rw_pr_init(rw_pr_lock_t *)
Definition: thr_rwlock.cc:35
native_rw_trywrlock
static int native_rw_trywrlock(native_rw_lock_t *rwp)
Definition: thr_rwlock.h:106
NULL
#define NULL
Definition: types.h:55
thr_mutex.h
rw_pr_rdlock
int rw_pr_rdlock(rw_pr_lock_t *)
Definition: thr_rwlock.cc:53
my_dbug.h
my_thread.h
native_rw_init
static int native_rw_init(native_rw_lock_t *rwp)
Definition: thr_rwlock.h:58
thr_rwlock_bits.h
native_rw_tryrdlock
static int native_rw_tryrdlock(native_rw_lock_t *rwp)
Definition: thr_rwlock.h:87
my_inttypes.h
native_rw_unlock
static int native_rw_unlock(native_rw_lock_t *rwp)
Definition: thr_rwlock.h:116
thr_cond.h
my_thread_equal
static int my_thread_equal(my_thread_t t1, my_thread_t t2)
Definition: my_thread.h:92
native_rw_rdlock
static int native_rw_rdlock(native_rw_lock_t *rwp)
Definition: thr_rwlock.h:78
native_rw_destroy
static int native_rw_destroy(native_rw_lock_t *rwp)
Definition: thr_rwlock.h:69
rw_pr_unlock
int rw_pr_unlock(rw_pr_lock_t *)
Definition: thr_rwlock.cc:99
rw_pr_lock_t
Portable implementation of special type of read-write locks.
Definition: thr_rwlock_bits.h:94
DBUG_ASSERT
#define DBUG_ASSERT(A)
Definition: my_dbug.h:197
native_rw_lock_t
pthread_rwlock_t native_rw_lock_t
Definition: thr_rwlock_bits.h:60
my_thread_self
static my_thread_t my_thread_self()
Definition: my_thread.h:84
rw_pr_lock_t::active_writer
bool active_writer
Indicates whether there is an active writer.
Definition: thr_rwlock_bits.h:110
native_rw_wrlock
static int native_rw_wrlock(native_rw_lock_t *rwp)
Definition: thr_rwlock.h:96
my_macros.h
rw_pr_destroy
int rw_pr_destroy(rw_pr_lock_t *)
Definition: thr_rwlock.cc:47
rw_pr_wrlock
int rw_pr_wrlock(rw_pr_lock_t *)
Definition: thr_rwlock.cc:66
rw_pr_lock_t::writer_thread
my_thread_t writer_thread
Thread holding wr-lock (for debug purposes only).
Definition: thr_rwlock_bits.h:112