Clone of PhatAC @ https://github.com/floaterxk/PhatAC

thr_rwlock.h 5.9KB

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