MySQL 9.1.0
Source Code Documentation
thr_rwlock_bits.h
Go to the documentation of this file.
1/* Copyright (c) 2014, 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 COMPONENTS_SERVICES_BITS_THR_RWLOCK_BITS_H
25#define COMPONENTS_SERVICES_BITS_THR_RWLOCK_BITS_H
26
27/**
28 @file mysql/components/services/bits/thr_rwlock_bits.h
29 MySQL rwlock ABI.
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 <stddef.h>
46#include <sys/types.h>
47#ifdef _WIN32
48#include <windows.h>
49#endif
50
54
55#ifdef _WIN32
56struct native_rw_lock_t {
57 SRWLOCK srwlock; /* native reader writer lock */
58 BOOL have_exclusive_srwlock; /* used for unlock */
59};
60#else
61typedef pthread_rwlock_t native_rw_lock_t;
62#endif
63
64/**
65 Portable implementation of special type of read-write locks.
66
67 These locks have two properties which are unusual for rwlocks:
68 1) They "prefer readers" in the sense that they do not allow
69 situations in which rwlock is rd-locked and there is a
70 pending rd-lock which is blocked (e.g. due to pending
71 request for wr-lock).
72 This is a stronger guarantee than one which is provided for
73 PTHREAD_RWLOCK_PREFER_READER_NP rwlocks in Linux.
74 MDL subsystem deadlock detector relies on this property for
75 its correctness.
76 2) They are optimized for uncontended wr-lock/unlock case.
77 This is scenario in which they are most often used
78 within MDL subsystem. Optimizing for it gives significant
79 performance improvements in some of tests involving many
80 connections.
81
82 Another important requirement imposed on this type of rwlock
83 by the MDL subsystem is that it should be OK to destroy rwlock
84 object which is in unlocked state even though some threads might
85 have not yet fully left unlock operation for it (of course there
86 is an external guarantee that no thread will try to lock rwlock
87 which is destroyed).
88 Putting it another way the unlock operation should not access
89 rwlock data after changing its state to unlocked.
90
91 TODO/FIXME: We should consider alleviating this requirement as
92 it blocks us from doing certain performance optimizations.
93*/
94
96 /**
97 Lock which protects the structure.
98 Also held for the duration of wr-lock.
99 */
101 /**
102 Condition variable which is used to wake-up
103 writers waiting for readers to go away.
104 */
106 /** Number of active readers. */
107 unsigned int active_readers;
108 /** Number of writers waiting for readers to go away. */
110 /** Indicates whether there is an active writer. */
112 /** Thread holding wr-lock (for debug purposes only). */
114};
115
116#endif /* COMPONENTS_SERVICES_BITS_THR_RWLOCK_BITS_H */
Types to make different thread packages compatible.
pthread_t my_thread_t
Definition: my_thread_bits.h:48
Portable implementation of special type of read-write locks.
Definition: thr_rwlock_bits.h:95
unsigned int writers_waiting_readers
Number of writers waiting for readers to go away.
Definition: thr_rwlock_bits.h:109
native_cond_t no_active_readers
Condition variable which is used to wake-up writers waiting for readers to go away.
Definition: thr_rwlock_bits.h:105
my_thread_t writer_thread
Thread holding wr-lock (for debug purposes only).
Definition: thr_rwlock_bits.h:113
unsigned int active_readers
Number of active readers.
Definition: thr_rwlock_bits.h:107
native_mutex_t lock
Lock which protects the structure.
Definition: thr_rwlock_bits.h:100
bool active_writer
Indicates whether there is an active writer.
Definition: thr_rwlock_bits.h:111
MySQL condition variable implementation.
pthread_cond_t native_cond_t
Definition: thr_cond_bits.h:46
ABI for thd_mutex.
pthread_mutex_t native_mutex_t
Definition: thr_mutex_bits.h:55
pthread_rwlock_t native_rw_lock_t
Definition: thr_rwlock_bits.h:61