Move folly/Baton.h to folly/synchronization/
[folly.git] / folly / fibers / TimedMutex-inl.h
index a42c3bc00e2b9af64869ecd1f37fde4b70bc4c2f..de5621bd598e164ae38c7dae733e4d4fe9cd059f 100644 (file)
@@ -26,7 +26,7 @@ namespace fibers {
 
 template <typename WaitFunc>
 TimedMutex::LockResult TimedMutex::lockHelper(WaitFunc&& waitFunc) {
-  std::unique_lock<folly::SpinLock> lock(lock_);
+  std::unique_lock<folly::SpinLock> ulock(lock_);
   if (!locked_) {
     locked_ = true;
     return LockResult::SUCCESS;
@@ -53,7 +53,7 @@ TimedMutex::LockResult TimedMutex::lockHelper(WaitFunc&& waitFunc) {
     threadWaiters_.push_back(waiter);
   }
 
-  lock.unlock();
+  ulock.unlock();
 
   if (!waitFunc(waiter)) {
     return LockResult::TIMEOUT;
@@ -64,7 +64,9 @@ TimedMutex::LockResult TimedMutex::lockHelper(WaitFunc&& waitFunc) {
       std::lock_guard<folly::SpinLock> lg(lock_);
 
       auto stolen = notifiedFiber_ != &waiter;
-      notifiedFiber_ = nullptr;
+      if (!stolen) {
+        notifiedFiber_ = nullptr;
+      }
       return stolen;
     }();
 
@@ -153,11 +155,11 @@ inline void TimedMutex::unlock() {
 
 template <typename BatonType>
 void TimedRWMutex<BatonType>::read_lock() {
-  std::unique_lock<folly::SpinLock> lock{lock_};
+  std::unique_lock<folly::SpinLock> ulock{lock_};
   if (state_ == State::WRITE_LOCKED) {
     MutexWaiter waiter;
     read_waiters_.push_back(waiter);
-    lock.unlock();
+    ulock.unlock();
     waiter.baton.wait();
     assert(state_ == State::READ_LOCKED);
     return;
@@ -174,11 +176,11 @@ template <typename BatonType>
 template <typename Rep, typename Period>
 bool TimedRWMutex<BatonType>::timed_read_lock(
     const std::chrono::duration<Rep, Period>& duration) {
-  std::unique_lock<folly::SpinLock> lock{lock_};
+  std::unique_lock<folly::SpinLock> ulock{lock_};
   if (state_ == State::WRITE_LOCKED) {
     MutexWaiter waiter;
     read_waiters_.push_back(waiter);
-    lock.unlock();
+    ulock.unlock();
 
     if (!waiter.baton.timed_wait(duration)) {
       // We timed out. Two cases:
@@ -220,7 +222,7 @@ bool TimedRWMutex<BatonType>::try_read_lock() {
 
 template <typename BatonType>
 void TimedRWMutex<BatonType>::write_lock() {
-  std::unique_lock<folly::SpinLock> lock{lock_};
+  std::unique_lock<folly::SpinLock> ulock{lock_};
   if (state_ == State::UNLOCKED) {
     verify_unlocked_properties();
     state_ = State::WRITE_LOCKED;
@@ -228,7 +230,7 @@ void TimedRWMutex<BatonType>::write_lock() {
   }
   MutexWaiter waiter;
   write_waiters_.push_back(waiter);
-  lock.unlock();
+  ulock.unlock();
   waiter.baton.wait();
 }
 
@@ -236,7 +238,7 @@ template <typename BatonType>
 template <typename Rep, typename Period>
 bool TimedRWMutex<BatonType>::timed_write_lock(
     const std::chrono::duration<Rep, Period>& duration) {
-  std::unique_lock<folly::SpinLock> lock{lock_};
+  std::unique_lock<folly::SpinLock> ulock{lock_};
   if (state_ == State::UNLOCKED) {
     verify_unlocked_properties();
     state_ = State::WRITE_LOCKED;
@@ -244,7 +246,7 @@ bool TimedRWMutex<BatonType>::timed_write_lock(
   }
   MutexWaiter waiter;
   write_waiters_.push_back(waiter);
-  lock.unlock();
+  ulock.unlock();
 
   if (!waiter.baton.timed_wait(duration)) {
     // We timed out. Two cases:
@@ -331,5 +333,5 @@ void TimedRWMutex<BatonType>::downgrade() {
     }
   }
 }
-}
-}
+} // namespace fibers
+} // namespace folly