template <typename BatonType>
void TimedRWMutex<BatonType>::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;
assert(read_waiters_.empty());
state_ = State::READ_LOCKED;
readers_ += 1;
- pthread_spin_unlock(&lock_);
+ lock_.unlock();
}
template <typename BatonType>
template <typename Rep, typename Period>
bool TimedRWMutex<BatonType>::timed_read_lock(
const std::chrono::duration<Rep, Period>& 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:
// 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;
}
assert(read_waiters_.empty());
state_ = State::READ_LOCKED;
readers_ += 1;
- pthread_spin_unlock(&lock_);
+ lock_.unlock();
return true;
}
template <typename BatonType>
bool TimedRWMutex<BatonType>::try_read_lock() {
- pthread_spin_lock(&lock_);
+ lock_.lock();
if (state_ != State::WRITE_LOCKED) {
assert(
(state_ == State::UNLOCKED && readers_ == 0) ||
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 <typename BatonType>
void TimedRWMutex<BatonType>::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();
}
template <typename Rep, typename Period>
bool TimedRWMutex<BatonType>::timed_write_lock(
const std::chrono::duration<Rep, Period>& 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:
// 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;
template <typename BatonType>
bool TimedRWMutex<BatonType>::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 <typename BatonType>
void TimedRWMutex<BatonType>::unlock() {
- pthread_spin_lock(&lock_);
+ lock_.lock();
assert(state_ != State::UNLOCKED);
assert(
(state_ == State::READ_LOCKED && readers_ > 0) ||
} else {
assert(state_ == State::READ_LOCKED);
}
- pthread_spin_unlock(&lock_);
+ lock_.unlock();
}
template <typename BatonType>
void TimedRWMutex<BatonType>::downgrade() {
- pthread_spin_lock(&lock_);
+ lock_.lock();
assert(state_ == State::WRITE_LOCKED && readers_ == 0);
state_ = State::READ_LOCKED;
readers_ += 1;
to_wake.baton.post();
}
}
- pthread_spin_unlock(&lock_);
+ lock_.unlock();
}
}
}