thr_rwlock.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #ifndef THR_RWLOCK_INCLUDED
  2. #define THR_RWLOCK_INCLUDED
  3. /* Copyright (c) 2014, 2017, 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. @file include/thr_rwlock.h
  22. MySQL rwlock implementation.
  23. There are two "layers":
  24. 1) native_rw_*()
  25. Functions that map directly down to OS primitives.
  26. Windows - SRWLock
  27. Other OSes - pthread
  28. 2) mysql_rw*()
  29. Functions that include Performance Schema instrumentation.
  30. See include/mysql/psi/mysql_thread.h
  31. This file also includes rw_pr_*(), which implements a special
  32. version of rwlocks that prefer readers. The P_S version of these
  33. are mysql_prlock_*() - see include/mysql/psi/mysql_thread.h
  34. */
  35. #include <stddef.h>
  36. #include <sys/types.h>
  37. #ifdef _WIN32
  38. #include <windows.h>
  39. #endif
  40. #include "my_dbug.h"
  41. #include "my_inttypes.h"
  42. #include "my_macros.h"
  43. #include "my_thread.h"
  44. #include "mysql/components/services/thr_rwlock_bits.h"
  45. #include "thr_cond.h"
  46. #include "thr_mutex.h"
  47. static inline int native_rw_init(native_rw_lock_t *rwp) {
  48. #ifdef _WIN32
  49. InitializeSRWLock(&rwp->srwlock);
  50. rwp->have_exclusive_srwlock = false;
  51. return 0;
  52. #else
  53. /* pthread_rwlockattr_t is not used in MySQL */
  54. return pthread_rwlock_init(rwp, NULL);
  55. #endif
  56. }
  57. static inline int native_rw_destroy(
  58. native_rw_lock_t *rwp MY_ATTRIBUTE((unused))) {
  59. #ifdef _WIN32
  60. return 0; /* no destroy function */
  61. #else
  62. return pthread_rwlock_destroy(rwp);
  63. #endif
  64. }
  65. static inline int native_rw_rdlock(native_rw_lock_t *rwp) {
  66. #ifdef _WIN32
  67. AcquireSRWLockShared(&rwp->srwlock);
  68. return 0;
  69. #else
  70. return pthread_rwlock_rdlock(rwp);
  71. #endif
  72. }
  73. static inline int native_rw_tryrdlock(native_rw_lock_t *rwp) {
  74. #ifdef _WIN32
  75. if (!TryAcquireSRWLockShared(&rwp->srwlock)) return EBUSY;
  76. return 0;
  77. #else
  78. return pthread_rwlock_tryrdlock(rwp);
  79. #endif
  80. }
  81. static inline int native_rw_wrlock(native_rw_lock_t *rwp) {
  82. #ifdef _WIN32
  83. AcquireSRWLockExclusive(&rwp->srwlock);
  84. rwp->have_exclusive_srwlock = true;
  85. return 0;
  86. #else
  87. return pthread_rwlock_wrlock(rwp);
  88. #endif
  89. }
  90. static inline int native_rw_trywrlock(native_rw_lock_t *rwp) {
  91. #ifdef _WIN32
  92. if (!TryAcquireSRWLockExclusive(&rwp->srwlock)) return EBUSY;
  93. rwp->have_exclusive_srwlock = true;
  94. return 0;
  95. #else
  96. return pthread_rwlock_trywrlock(rwp);
  97. #endif
  98. }
  99. static inline int native_rw_unlock(native_rw_lock_t *rwp) {
  100. #ifdef _WIN32
  101. if (rwp->have_exclusive_srwlock) {
  102. rwp->have_exclusive_srwlock = false;
  103. ReleaseSRWLockExclusive(&rwp->srwlock);
  104. } else
  105. ReleaseSRWLockShared(&rwp->srwlock);
  106. return 0;
  107. #else
  108. return pthread_rwlock_unlock(rwp);
  109. #endif
  110. }
  111. extern int rw_pr_init(rw_pr_lock_t *);
  112. extern int rw_pr_rdlock(rw_pr_lock_t *);
  113. extern int rw_pr_wrlock(rw_pr_lock_t *);
  114. extern int rw_pr_unlock(rw_pr_lock_t *);
  115. extern int rw_pr_destroy(rw_pr_lock_t *);
  116. #ifdef SAFE_MUTEX
  117. static inline void rw_pr_lock_assert_write_owner(const rw_pr_lock_t *rwlock) {
  118. DBUG_ASSERT(rwlock->active_writer &&
  119. my_thread_equal(my_thread_self(), rwlock->writer_thread));
  120. }
  121. static inline void rw_pr_lock_assert_not_write_owner(
  122. const rw_pr_lock_t *rwlock) {
  123. DBUG_ASSERT(!rwlock->active_writer ||
  124. !my_thread_equal(my_thread_self(), rwlock->writer_thread));
  125. }
  126. #endif
  127. #endif /* THR_RWLOCK_INCLUDED */