1 /* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
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 also distributed 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 included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #ifndef COMPONENTS_SERVICES_THR_RWLOCK_BITS_H
24 #define COMPONENTS_SERVICES_THR_RWLOCK_BITS_H
25 
26 /**
27   @file
28   MySQL rwlock ABI.
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 <mysql/components/services/my_thread_bits.h>
51 #include <mysql/components/services/thr_cond_bits.h>
52 #include <mysql/components/services/thr_mutex_bits.h>
53 
54 #ifdef _WIN32
55 struct native_rw_lock_t {
56   SRWLOCK srwlock;             /* native reader writer lock */
57   BOOL have_exclusive_srwlock; /* used for unlock */
58 };
59 #else
60 typedef pthread_rwlock_t native_rw_lock_t;
61 #endif
62 
63 /**
64   Portable implementation of special type of read-write locks.
65 
66   These locks have two properties which are unusual for rwlocks:
67   1) They "prefer readers" in the sense that they do not allow
68      situations in which rwlock is rd-locked and there is a
69      pending rd-lock which is blocked (e.g. due to pending
70      request for wr-lock).
71      This is a stronger guarantee than one which is provided for
72      PTHREAD_RWLOCK_PREFER_READER_NP rwlocks in Linux.
73      MDL subsystem deadlock detector relies on this property for
74      its correctness.
75   2) They are optimized for uncontended wr-lock/unlock case.
76      This is scenario in which they are most oftenly used
77      within MDL subsystem. Optimizing for it gives significant
78      performance improvements in some of tests involving many
79      connections.
80 
81   Another important requirement imposed on this type of rwlock
82   by the MDL subsystem is that it should be OK to destroy rwlock
83   object which is in unlocked state even though some threads might
84   have not yet fully left unlock operation for it (of course there
85   is an external guarantee that no thread will try to lock rwlock
86   which is destroyed).
87   Putting it another way the unlock operation should not access
88   rwlock data after changing its state to unlocked.
89 
90   TODO/FIXME: We should consider alleviating this requirement as
91   it blocks us from doing certain performance optimizations.
92 */
93 
94 struct rw_pr_lock_t {
95   /**
96     Lock which protects the structure.
97     Also held for the duration of wr-lock.
98   */
99   native_mutex_t lock;
100   /**
101     Condition variable which is used to wake-up
102     writers waiting for readers to go away.
103   */
104   native_cond_t no_active_readers;
105   /** Number of active readers. */
106   unsigned int active_readers;
107   /** Number of writers waiting for readers to go away. */
108   unsigned int writers_waiting_readers;
109   /** Indicates whether there is an active writer. */
110   bool active_writer;
111   /** Thread holding wr-lock (for debug purposes only). */
112   my_thread_t writer_thread;
113 };
114 
115 #endif /* COMPONENTS_SERVICES_THR_RWLOCK_BITS_H */
116