No-Typo-Check: Not related to this change. Bug: 161477208 Change-Id: I99e4780f6855b7045aa0cd5a49c13d2d0d51ed64
61 lines
1.2 KiB
C++
61 lines
1.2 KiB
C++
// Copyright 2019 Google LLC. All Rights Reserved. This file and proprietary
|
|
// source code may only be used and distributed under the Widevine License
|
|
// Agreement.
|
|
|
|
#include "rw_lock.h"
|
|
|
|
#include "log.h"
|
|
|
|
namespace wvutil {
|
|
|
|
shared_mutex::~shared_mutex() {
|
|
if (reader_count_ > 0) {
|
|
LOGE("shared_mutex destroyed with active readers!");
|
|
}
|
|
if (has_writer_) {
|
|
LOGE("shared_mutex destroyed with an active writer!");
|
|
}
|
|
}
|
|
|
|
void shared_mutex::lock_shared() {
|
|
std::unique_lock<std::mutex> lock(mutex_);
|
|
|
|
while (has_writer_) {
|
|
condition_variable_.wait(lock);
|
|
}
|
|
|
|
++reader_count_;
|
|
}
|
|
|
|
void shared_mutex::unlock_shared() {
|
|
std::unique_lock<std::mutex> lock(mutex_);
|
|
|
|
--reader_count_;
|
|
|
|
if (reader_count_ == 0) {
|
|
condition_variable_.notify_all();
|
|
}
|
|
}
|
|
|
|
bool shared_mutex::lock_implementation(bool abort_if_unavailable) {
|
|
std::unique_lock<std::mutex> lock(mutex_);
|
|
|
|
while (reader_count_ > 0 || has_writer_) {
|
|
if (abort_if_unavailable) return false;
|
|
condition_variable_.wait(lock);
|
|
}
|
|
|
|
has_writer_ = true;
|
|
return true;
|
|
}
|
|
|
|
void shared_mutex::unlock() {
|
|
std::unique_lock<std::mutex> lock(mutex_);
|
|
|
|
has_writer_ = false;
|
|
|
|
condition_variable_.notify_all();
|
|
}
|
|
|
|
} // namespace wvutil
|