You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

lock.cc 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "minddata/dataset/util/lock.h"
  17. #include "minddata/dataset/util/log_adapter.h"
  18. namespace mindspore {
  19. namespace dataset {
  20. void SpinLock::Lock() {
  21. while (true) {
  22. int expected = kUnlocked;
  23. if (val_.compare_exchange_weak(expected, kLocked)) {
  24. break;
  25. }
  26. }
  27. }
  28. bool SpinLock::TryLock() {
  29. int expected = kUnlocked;
  30. return val_.compare_exchange_strong(expected, kLocked);
  31. }
  32. void SpinLock::Unlock() noexcept { val_.store(kUnlocked); }
  33. void RWLock::LockShared() {
  34. std::unique_lock<std::mutex> lck(mtx_);
  35. waiting_readers_ += 1;
  36. read_cv_.wait(lck, [this]() { return (waiting_writers_ == 0 && status_ >= 0); });
  37. waiting_readers_ -= 1;
  38. status_ += 1;
  39. }
  40. void RWLock::Unlock() noexcept {
  41. std::unique_lock<std::mutex> lck(mtx_);
  42. if (status_ == -1) {
  43. // I am the writer. By definition, no other writer nor reader.
  44. status_ = 0;
  45. } else if (status_ > 0) {
  46. // One less reader
  47. status_ -= 1;
  48. }
  49. // Wake up writer only if there is no reader.
  50. if (waiting_writers_ > 0) {
  51. if (status_ == 0) {
  52. write_cv_.notify_one();
  53. }
  54. } else {
  55. read_cv_.notify_all();
  56. }
  57. }
  58. void RWLock::Upgrade() {
  59. std::unique_lock<std::mutex> lck(mtx_);
  60. MS_ASSERT(status_);
  61. if (status_ == -1) {
  62. // I am a writer already.
  63. return;
  64. } else if (status_ == 1) {
  65. // If I am the only reader. Just change the status.
  66. status_ = -1;
  67. return;
  68. } else {
  69. // In all other cases, let of the shared lock and relock in exclusive.
  70. lck.unlock();
  71. this->Unlock();
  72. this->LockExclusive();
  73. }
  74. }
  75. void RWLock::Downgrade() {
  76. std::unique_lock<std::mutex> lck(mtx_);
  77. MS_ASSERT(status_);
  78. if (status_ == -1) {
  79. // If there are no other writers waiting, just change the status
  80. if (waiting_writers_ == 0) {
  81. status_ = 1;
  82. } else {
  83. // Otherwise just unlock and relock in shared
  84. lck.unlock();
  85. this->Unlock();
  86. this->LockShared();
  87. }
  88. } else if (status_ > 0) {
  89. return;
  90. }
  91. }
  92. SharedLock::SharedLock(RWLock *rw) : rw_(rw), ownlock_(false) {
  93. rw_->LockShared();
  94. ownlock_ = true;
  95. }
  96. SharedLock::~SharedLock() {
  97. if (ownlock_) {
  98. rw_->Unlock();
  99. ownlock_ = false;
  100. }
  101. rw_ = nullptr;
  102. }
  103. void SharedLock::Unlock() {
  104. MS_ASSERT(ownlock_ == true);
  105. rw_->Unlock();
  106. ownlock_ = false;
  107. }
  108. void SharedLock::Lock() {
  109. MS_ASSERT(ownlock_ == false);
  110. rw_->LockShared();
  111. ownlock_ = true;
  112. }
  113. void SharedLock::Upgrade() {
  114. MS_ASSERT(ownlock_ == true);
  115. rw_->Upgrade();
  116. }
  117. void SharedLock::Downgrade() {
  118. MS_ASSERT(ownlock_ == true);
  119. rw_->Downgrade();
  120. }
  121. UniqueLock::UniqueLock(RWLock *rw) : rw_(rw), ownlock_(false) {
  122. rw_->LockExclusive();
  123. ownlock_ = true;
  124. }
  125. UniqueLock::~UniqueLock() {
  126. if (ownlock_) {
  127. rw_->Unlock();
  128. ownlock_ = false;
  129. }
  130. rw_ = nullptr;
  131. }
  132. void UniqueLock::Unlock() {
  133. MS_ASSERT(ownlock_ == true);
  134. rw_->Unlock();
  135. ownlock_ = false;
  136. }
  137. void UniqueLock::Lock() {
  138. MS_ASSERT(ownlock_ == false);
  139. rw_->LockExclusive();
  140. ownlock_ = true;
  141. }
  142. LockGuard::LockGuard(SpinLock *lock) : lck_(lock), own_lock_(false) {
  143. lck_->Lock();
  144. own_lock_ = true;
  145. }
  146. LockGuard::~LockGuard() {
  147. if (own_lock_) {
  148. lck_->Unlock();
  149. own_lock_ = false;
  150. }
  151. lck_ = nullptr;
  152. }
  153. void LockGuard::Unlock() {
  154. MS_ASSERT(own_lock_);
  155. lck_->Unlock();
  156. own_lock_ = false;
  157. }
  158. void LockGuard::Lock() {
  159. MS_ASSERT(own_lock_ == false);
  160. lck_->Lock();
  161. own_lock_ = true;
  162. }
  163. } // namespace dataset
  164. } // namespace mindspore