thr_rwlock.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. Without limiting anything contained in the foregoing, this file,
  12. which is part of C Driver for MySQL (Connector/C), is also subject to the
  13. Universal FOSS Exception, version 1.0, a copy of which can be found at
  14. http://oss.oracle.com/licenses/universal-foss-exception.
  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. 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. @file mysys/thr_rwlock.cc
  24. Synchronization - readers / writer thread locks
  25. */
  26. #include "thr_rwlock.h"
  27. int rw_pr_init(rw_pr_lock_t *rwlock) {
  28. native_mutex_init(&rwlock->lock, NULL);
  29. native_cond_init(&rwlock->no_active_readers);
  30. rwlock->active_readers = 0;
  31. rwlock->writers_waiting_readers = 0;
  32. rwlock->active_writer = false;
  33. #ifdef SAFE_MUTEX
  34. rwlock->writer_thread = 0;
  35. #endif
  36. return 0;
  37. }
  38. int rw_pr_destroy(rw_pr_lock_t *rwlock) {
  39. native_cond_destroy(&rwlock->no_active_readers);
  40. native_mutex_destroy(&rwlock->lock);
  41. return 0;
  42. }
  43. int rw_pr_rdlock(rw_pr_lock_t *rwlock) {
  44. native_mutex_lock(&rwlock->lock);
  45. /*
  46. The fact that we were able to acquire 'lock' mutex means
  47. that there are no active writers and we can acquire rd-lock.
  48. Increment active readers counter to prevent requests for
  49. wr-lock from succeeding and unlock mutex.
  50. */
  51. rwlock->active_readers++;
  52. native_mutex_unlock(&rwlock->lock);
  53. return 0;
  54. }
  55. int rw_pr_wrlock(rw_pr_lock_t *rwlock) {
  56. native_mutex_lock(&rwlock->lock);
  57. if (rwlock->active_readers != 0) {
  58. /* There are active readers. We have to wait until they are gone. */
  59. rwlock->writers_waiting_readers++;
  60. while (rwlock->active_readers != 0)
  61. native_cond_wait(&rwlock->no_active_readers, &rwlock->lock);
  62. rwlock->writers_waiting_readers--;
  63. }
  64. /*
  65. We own 'lock' mutex so there is no active writers.
  66. Also there are no active readers.
  67. This means that we can grant wr-lock.
  68. Not releasing 'lock' mutex until unlock will block
  69. both requests for rd and wr-locks.
  70. Set 'active_writer' flag to simplify unlock.
  71. Thanks to the fact wr-lock/unlock in the absence of
  72. contention from readers is essentially mutex lock/unlock
  73. with a few simple checks make this rwlock implementation
  74. wr-lock optimized.
  75. */
  76. rwlock->active_writer = true;
  77. #ifdef SAFE_MUTEX
  78. rwlock->writer_thread = my_thread_self();
  79. #endif
  80. return 0;
  81. }
  82. int rw_pr_unlock(rw_pr_lock_t *rwlock) {
  83. if (rwlock->active_writer) {
  84. /* We are unlocking wr-lock. */
  85. #ifdef SAFE_MUTEX
  86. rwlock->writer_thread = 0;
  87. #endif
  88. rwlock->active_writer = false;
  89. if (rwlock->writers_waiting_readers) {
  90. /*
  91. Avoid expensive cond signal in case when there is no contention
  92. or it is wr-only.
  93. Note that from view point of performance it would be better to
  94. signal on the condition variable after unlocking mutex (as it
  95. reduces number of contex switches).
  96. Unfortunately this would mean that such rwlock can't be safely
  97. used by MDL subsystem, which relies on the fact that it is OK
  98. to destroy rwlock once it is in unlocked state.
  99. */
  100. native_cond_signal(&rwlock->no_active_readers);
  101. }
  102. native_mutex_unlock(&rwlock->lock);
  103. } else {
  104. /* We are unlocking rd-lock. */
  105. native_mutex_lock(&rwlock->lock);
  106. rwlock->active_readers--;
  107. if (rwlock->active_readers == 0 && rwlock->writers_waiting_readers) {
  108. /*
  109. If we are last reader and there are waiting
  110. writers wake them up.
  111. */
  112. native_cond_signal(&rwlock->no_active_readers);
  113. }
  114. native_mutex_unlock(&rwlock->lock);
  115. }
  116. return 0;
  117. }