X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2Ffibers%2FTimedMutex-inl.h;h=1c64a0ebb0646fcda77413ade6af8d2fca8a791f;hp=26e523c73f0f1463deb380986732ee20d5884aa1;hb=214b26f334d1a06515211918fc4a760ae50e6a33;hpb=2b06a7168bbcbb11384350d918767ab1804b9cdc diff --git a/folly/fibers/TimedMutex-inl.h b/folly/fibers/TimedMutex-inl.h index 26e523c7..1c64a0eb 100644 --- a/folly/fibers/TimedMutex-inl.h +++ b/folly/fibers/TimedMutex-inl.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -61,9 +61,9 @@ TimedMutex::LockResult TimedMutex::lockHelper(WaitFunc&& waitFunc) { auto lockStolen = [&] { std::lock_guard lg(lock_); - auto lockStolen = notifiedFiber_ != &waiter; + auto stolen = notifiedFiber_ != &waiter; notifiedFiber_ = nullptr; - return lockStolen; + return stolen; }(); if (lockStolen) { @@ -151,11 +151,11 @@ inline void TimedMutex::unlock() { template void TimedRWMutex::read_lock() { - pthread_spin_lock(&lock_); + lock_.lock(); if (state_ == State::WRITE_LOCKED) { MutexWaiter waiter; read_waiters_.push_back(waiter); - pthread_spin_unlock(&lock_); + lock_.unlock(); waiter.baton.wait(); assert(state_ == State::READ_LOCKED); return; @@ -166,18 +166,18 @@ void TimedRWMutex::read_lock() { assert(read_waiters_.empty()); state_ = State::READ_LOCKED; readers_ += 1; - pthread_spin_unlock(&lock_); + lock_.unlock(); } template template bool TimedRWMutex::timed_read_lock( const std::chrono::duration& duration) { - pthread_spin_lock(&lock_); + lock_.lock(); if (state_ == State::WRITE_LOCKED) { MutexWaiter waiter; read_waiters_.push_back(waiter); - pthread_spin_unlock(&lock_); + lock_.unlock(); if (!waiter.baton.timed_wait(duration)) { // We timed out. Two cases: @@ -185,13 +185,13 @@ bool TimedRWMutex::timed_read_lock( // 2. We're not in the waiter list anymore. This could happen if the baton // times out but the mutex is unlocked before we reach this code. In // this case we'll pretend we got the lock on time. - pthread_spin_lock(&lock_); + lock_.lock(); if (waiter.hook.is_linked()) { read_waiters_.erase(read_waiters_.iterator_to(waiter)); - pthread_spin_unlock(&lock_); + lock_.unlock(); return false; } - pthread_spin_unlock(&lock_); + lock_.unlock(); } return true; } @@ -201,13 +201,13 @@ bool TimedRWMutex::timed_read_lock( assert(read_waiters_.empty()); state_ = State::READ_LOCKED; readers_ += 1; - pthread_spin_unlock(&lock_); + lock_.unlock(); return true; } template bool TimedRWMutex::try_read_lock() { - pthread_spin_lock(&lock_); + lock_.lock(); if (state_ != State::WRITE_LOCKED) { assert( (state_ == State::UNLOCKED && readers_ == 0) || @@ -215,25 +215,25 @@ bool TimedRWMutex::try_read_lock() { assert(read_waiters_.empty()); state_ = State::READ_LOCKED; readers_ += 1; - pthread_spin_unlock(&lock_); + lock_.unlock(); return true; } - pthread_spin_unlock(&lock_); + lock_.unlock(); return false; } template void TimedRWMutex::write_lock() { - pthread_spin_lock(&lock_); + lock_.lock(); if (state_ == State::UNLOCKED) { verify_unlocked_properties(); state_ = State::WRITE_LOCKED; - pthread_spin_unlock(&lock_); + lock_.unlock(); return; } MutexWaiter waiter; write_waiters_.push_back(waiter); - pthread_spin_unlock(&lock_); + lock_.unlock(); waiter.baton.wait(); } @@ -241,16 +241,16 @@ template template bool TimedRWMutex::timed_write_lock( const std::chrono::duration& duration) { - pthread_spin_lock(&lock_); + lock_.lock(); if (state_ == State::UNLOCKED) { verify_unlocked_properties(); state_ = State::WRITE_LOCKED; - pthread_spin_unlock(&lock_); + lock_.unlock(); return true; } MutexWaiter waiter; write_waiters_.push_back(waiter); - pthread_spin_unlock(&lock_); + lock_.unlock(); if (!waiter.baton.timed_wait(duration)) { // We timed out. Two cases: @@ -258,13 +258,13 @@ bool TimedRWMutex::timed_write_lock( // 2. We're not in the waiter list anymore. This could happen if the baton // times out but the mutex is unlocked before we reach this code. In // this case we'll pretend we got the lock on time. - pthread_spin_lock(&lock_); + lock_.lock(); if (waiter.hook.is_linked()) { write_waiters_.erase(write_waiters_.iterator_to(waiter)); - pthread_spin_unlock(&lock_); + lock_.unlock(); return false; } - pthread_spin_unlock(&lock_); + lock_.unlock(); } assert(state_ == State::WRITE_LOCKED); return true; @@ -272,20 +272,20 @@ bool TimedRWMutex::timed_write_lock( template bool TimedRWMutex::try_write_lock() { - pthread_spin_lock(&lock_); + lock_.lock(); if (state_ == State::UNLOCKED) { verify_unlocked_properties(); state_ = State::WRITE_LOCKED; - pthread_spin_unlock(&lock_); + lock_.unlock(); return true; } - pthread_spin_unlock(&lock_); + lock_.unlock(); return false; } template void TimedRWMutex::unlock() { - pthread_spin_lock(&lock_); + lock_.lock(); assert(state_ != State::UNLOCKED); assert( (state_ == State::READ_LOCKED && readers_ > 0) || @@ -322,12 +322,12 @@ void TimedRWMutex::unlock() { } else { assert(state_ == State::READ_LOCKED); } - pthread_spin_unlock(&lock_); + lock_.unlock(); } template void TimedRWMutex::downgrade() { - pthread_spin_lock(&lock_); + lock_.lock(); assert(state_ == State::WRITE_LOCKED && readers_ == 0); state_ = State::READ_LOCKED; readers_ += 1; @@ -341,7 +341,7 @@ void TimedRWMutex::downgrade() { to_wake.baton.post(); } } - pthread_spin_unlock(&lock_); + lock_.unlock(); } } }