thr_rwlock.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #ifndef THR_RWLOCK_INCLUDED
  2. #define THR_RWLOCK_INCLUDED
  3. /* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License, version 2.0,
  6. as published by the Free Software Foundation.
  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. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License, version 2.0, for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  20. /**
  21. MySQL rwlock implementation.
  22. There are two "layers":
  23. 1) native_rw_*()
  24. Functions that map directly down to OS primitives.
  25. Windows - SRWLock
  26. Other OSes - pthread
  27. 2) mysql_rw*()
  28. Functions that include Performance Schema instrumentation.
  29. See include/mysql/psi/mysql_thread.h
  30. This file also includes rw_pr_*(), which implements a special
  31. version of rwlocks that prefer readers. The P_S version of these
  32. are mysql_prlock_*() - see include/mysql/psi/mysql_thread.h
  33. */
  34. #include "my_global.h"
  35. #include "my_thread.h"
  36. #include "thr_cond.h"
  37. C_MODE_START
  38. #ifdef _WIN32
  39. typedef struct st_my_rw_lock_t
  40. {
  41. SRWLOCK srwlock; /* native reader writer lock */
  42. BOOL have_exclusive_srwlock; /* used for unlock */
  43. } native_rw_lock_t;
  44. #else
  45. typedef pthread_rwlock_t native_rw_lock_t;
  46. #endif
  47. static inline int native_rw_init(native_rw_lock_t *rwp)
  48. {
  49. #ifdef _WIN32
  50. InitializeSRWLock(&rwp->srwlock);
  51. rwp->have_exclusive_srwlock = FALSE;
  52. return 0;
  53. #else
  54. /* pthread_rwlockattr_t is not used in MySQL */
  55. return pthread_rwlock_init(rwp, NULL);
  56. #endif
  57. }
  58. static inline int native_rw_destroy(native_rw_lock_t *rwp)
  59. {
  60. #ifdef _WIN32
  61. return 0; /* no destroy function */
  62. #else
  63. return pthread_rwlock_destroy(rwp);
  64. #endif
  65. }
  66. static inline int native_rw_rdlock(native_rw_lock_t *rwp)
  67. {
  68. #ifdef _WIN32
  69. AcquireSRWLockShared(&rwp->srwlock);
  70. return 0;
  71. #else
  72. return pthread_rwlock_rdlock(rwp);
  73. #endif
  74. }
  75. static inline int native_rw_tryrdlock(native_rw_lock_t *rwp)
  76. {
  77. #ifdef _WIN32
  78. if (!TryAcquireSRWLockShared(&rwp->srwlock))
  79. return EBUSY;
  80. return 0;
  81. #else
  82. return pthread_rwlock_tryrdlock(rwp);
  83. #endif
  84. }
  85. static inline int native_rw_wrlock(native_rw_lock_t *rwp)
  86. {
  87. #ifdef _WIN32
  88. AcquireSRWLockExclusive(&rwp->srwlock);
  89. rwp->have_exclusive_srwlock= TRUE;
  90. return 0;
  91. #else
  92. return pthread_rwlock_wrlock(rwp);
  93. #endif
  94. }
  95. static inline int native_rw_trywrlock(native_rw_lock_t *rwp)
  96. {
  97. #ifdef _WIN32
  98. if (!TryAcquireSRWLockExclusive(&rwp->srwlock))
  99. return EBUSY;
  100. rwp->have_exclusive_srwlock= TRUE;
  101. return 0;
  102. #else
  103. return pthread_rwlock_trywrlock(rwp);
  104. #endif
  105. }
  106. static inline int native_rw_unlock(native_rw_lock_t *rwp)
  107. {
  108. #ifdef _WIN32
  109. if (rwp->have_exclusive_srwlock)
  110. {
  111. rwp->have_exclusive_srwlock= FALSE;
  112. ReleaseSRWLockExclusive(&rwp->srwlock);
  113. }
  114. else
  115. ReleaseSRWLockShared(&rwp->srwlock);
  116. return 0;
  117. #else
  118. return pthread_rwlock_unlock(rwp);
  119. #endif
  120. }
  121. /**
  122. Portable implementation of special type of read-write locks.
  123. These locks have two properties which are unusual for rwlocks:
  124. 1) They "prefer readers" in the sense that they do not allow
  125. situations in which rwlock is rd-locked and there is a
  126. pending rd-lock which is blocked (e.g. due to pending
  127. request for wr-lock).
  128. This is a stronger guarantee than one which is provided for
  129. PTHREAD_RWLOCK_PREFER_READER_NP rwlocks in Linux.
  130. MDL subsystem deadlock detector relies on this property for
  131. its correctness.
  132. 2) They are optimized for uncontended wr-lock/unlock case.
  133. This is scenario in which they are most oftenly used
  134. within MDL subsystem. Optimizing for it gives significant
  135. performance improvements in some of tests involving many
  136. connections.
  137. Another important requirement imposed on this type of rwlock
  138. by the MDL subsystem is that it should be OK to destroy rwlock
  139. object which is in unlocked state even though some threads might
  140. have not yet fully left unlock operation for it (of course there
  141. is an external guarantee that no thread will try to lock rwlock
  142. which is destroyed).
  143. Putting it another way the unlock operation should not access
  144. rwlock data after changing its state to unlocked.
  145. TODO/FIXME: We should consider alleviating this requirement as
  146. it blocks us from doing certain performance optimizations.
  147. */
  148. typedef struct st_rw_pr_lock_t {
  149. /**
  150. Lock which protects the structure.
  151. Also held for the duration of wr-lock.
  152. */
  153. native_mutex_t lock;
  154. /**
  155. Condition variable which is used to wake-up
  156. writers waiting for readers to go away.
  157. */
  158. native_cond_t no_active_readers;
  159. /** Number of active readers. */
  160. uint active_readers;
  161. /** Number of writers waiting for readers to go away. */
  162. uint writers_waiting_readers;
  163. /** Indicates whether there is an active writer. */
  164. my_bool active_writer;
  165. #ifdef SAFE_MUTEX
  166. /** Thread holding wr-lock (for debug purposes only). */
  167. my_thread_t writer_thread;
  168. #endif
  169. } rw_pr_lock_t;
  170. extern int rw_pr_init(rw_pr_lock_t *);
  171. extern int rw_pr_rdlock(rw_pr_lock_t *);
  172. extern int rw_pr_wrlock(rw_pr_lock_t *);
  173. extern int rw_pr_unlock(rw_pr_lock_t *);
  174. extern int rw_pr_destroy(rw_pr_lock_t *);
  175. static inline void
  176. rw_pr_lock_assert_write_owner(const rw_pr_lock_t *rwlock MY_ATTRIBUTE((unused)))
  177. {
  178. #ifdef SAFE_MUTEX
  179. DBUG_ASSERT(rwlock->active_writer &&
  180. my_thread_equal(my_thread_self(), rwlock->writer_thread));
  181. #endif
  182. }
  183. static inline void
  184. rw_pr_lock_assert_not_write_owner(const rw_pr_lock_t *rwlock MY_ATTRIBUTE((unused)))
  185. {
  186. #ifdef SAFE_MUTEX
  187. DBUG_ASSERT(!rwlock->active_writer ||
  188. !my_thread_equal(my_thread_self(), rwlock->writer_thread));
  189. #endif
  190. }
  191. C_MODE_END
  192. #endif /* THR_RWLOCK_INCLUDED */