Replace unique_lock with scoped_lock (#467)

Fixes #466
This commit is contained in:
cyan317
2023-01-10 17:50:53 +00:00
committed by GitHub
parent c51d696181
commit a377514287
8 changed files with 18 additions and 18 deletions

View File

@@ -246,7 +246,7 @@ public:
void void
updateRange(uint32_t newMax) updateRange(uint32_t newMax)
{ {
std::unique_lock lck(rngMtx_); std::scoped_lock lck(rngMtx_);
assert(!range || newMax >= range->maxSequence); assert(!range || newMax >= range->maxSequence);
if (!range) if (!range)
range = {newMax, newMax}; range = {newMax, newMax};

View File

@@ -37,7 +37,7 @@ SimpleCache::update(
return; return;
{ {
std::unique_lock lck{mtx_}; std::scoped_lock lck{mtx_};
if (seq > latestSeq_) if (seq > latestSeq_)
{ {
assert(seq == latestSeq_ + 1 || latestSeq_ == 0); assert(seq == latestSeq_ + 1 || latestSeq_ == 0);
@@ -125,7 +125,7 @@ SimpleCache::setFull()
return; return;
full_ = true; full_ = true;
std::unique_lock lck{mtx_}; std::scoped_lock lck{mtx_};
deletes_.clear(); deletes_.clear();
} }

View File

@@ -159,7 +159,7 @@ public:
std::optional<T> std::optional<T>
tryPop() tryPop()
{ {
std::unique_lock lck(m_); std::scoped_lock lck(m_);
if (queue_.empty()) if (queue_.empty())
return {}; return {};
T ret = std::move(queue_.front()); T ret = std::move(queue_.front());

View File

@@ -58,7 +58,7 @@ ForwardCache::freshen()
resp = {}; resp = {};
{ {
std::unique_lock lk(mtx_); std::scoped_lock lk(mtx_);
latestForwarded_[command] = resp; latestForwarded_[command] = resp;
} }
}); });
@@ -68,7 +68,7 @@ ForwardCache::freshen()
void void
ForwardCache::clear() ForwardCache::clear()
{ {
std::unique_lock lk(mtx_); std::scoped_lock lk(mtx_);
for (auto& cacheEntry : latestForwarded_) for (auto& cacheEntry : latestForwarded_)
latestForwarded_[cacheEntry.first] = {}; latestForwarded_[cacheEntry.first] = {};
} }

View File

@@ -174,7 +174,7 @@ private:
void void
setLastPublish() setLastPublish()
{ {
std::unique_lock lck(publishTimeMtx_); std::scoped_lock lck(publishTimeMtx_);
lastPublish_ = std::chrono::system_clock::now(); lastPublish_ = std::chrono::system_clock::now();
} }
@@ -186,7 +186,7 @@ private:
void void
setLastClose(std::chrono::time_point<ripple::NetClock> lastCloseTime) setLastClose(std::chrono::time_point<ripple::NetClock> lastCloseTime)
{ {
std::unique_lock lck(closeTimeMtx_); std::scoped_lock lck(closeTimeMtx_);
lastCloseTime_ = lastCloseTime; lastCloseTime_ = lastCloseTime;
} }

View File

@@ -30,7 +30,7 @@ Counters::initializeCounter(std::string const& method)
if (methodInfo_.count(method) == 0) if (methodInfo_.count(method) == 0)
{ {
lk.unlock(); lk.unlock();
std::unique_lock ulk(mutex_); std::scoped_lock ulk(mutex_);
// This calls the default constructor for methodInfo of the method. // This calls the default constructor for methodInfo of the method.
methodInfo_[method]; methodInfo_[method];

View File

@@ -497,7 +497,7 @@ SubscriptionManager::subscribeHelper(
CleanupFunction&& func) CleanupFunction&& func)
{ {
subs.subscribe(session); subs.subscribe(session);
std::unique_lock lk(cleanupMtx_); std::scoped_lock lk(cleanupMtx_);
cleanupFuncs_[session].push_back(std::move(func)); cleanupFuncs_[session].push_back(std::move(func));
} }
template <typename Key> template <typename Key>
@@ -509,14 +509,14 @@ SubscriptionManager::subscribeHelper(
CleanupFunction&& func) CleanupFunction&& func)
{ {
subs.subscribe(session, k); subs.subscribe(session, k);
std::unique_lock lk(cleanupMtx_); std::scoped_lock lk(cleanupMtx_);
cleanupFuncs_[session].push_back(std::move(func)); cleanupFuncs_[session].push_back(std::move(func));
} }
void void
SubscriptionManager::cleanup(std::shared_ptr<WsBase> session) SubscriptionManager::cleanup(std::shared_ptr<WsBase> session)
{ {
std::unique_lock lk(cleanupMtx_); std::scoped_lock lk(cleanupMtx_);
if (!cleanupFuncs_.contains(session)) if (!cleanupFuncs_.contains(session))
return; return;

View File

@@ -111,7 +111,7 @@ public:
return true; return true;
{ {
std::unique_lock lck(mtx_); std::scoped_lock lck(mtx_);
if (ipState_.find(ip) != ipState_.end()) if (ipState_.find(ip) != ipState_.end())
{ {
auto [transferedByte, requests] = ipState_.at(ip); auto [transferedByte, requests] = ipState_.at(ip);
@@ -149,7 +149,7 @@ public:
{ {
if (whitelist_.contains(ip)) if (whitelist_.contains(ip))
return; return;
std::unique_lock lck{mtx_}; std::scoped_lock lck{mtx_};
ipConnCount_[ip]++; ipConnCount_[ip]++;
} }
@@ -163,7 +163,7 @@ public:
{ {
if (whitelist_.contains(ip)) if (whitelist_.contains(ip))
return; return;
std::unique_lock lck{mtx_}; std::scoped_lock lck{mtx_};
assert(ipConnCount_[ip] > 0); assert(ipConnCount_[ip] > 0);
ipConnCount_[ip]--; ipConnCount_[ip]--;
if (ipConnCount_[ip] == 0) if (ipConnCount_[ip] == 0)
@@ -189,7 +189,7 @@ public:
return true; return true;
{ {
std::unique_lock lck(mtx_); std::scoped_lock lck(mtx_);
ipState_[ip].transferedByte += numObjects; ipState_[ip].transferedByte += numObjects;
} }
@@ -214,7 +214,7 @@ public:
return true; return true;
{ {
std::unique_lock lck(mtx_); std::scoped_lock lck(mtx_);
ipState_[ip].requestsCount++; ipState_[ip].requestsCount++;
} }
@@ -228,7 +228,7 @@ public:
void void
clear() noexcept override clear() noexcept override
{ {
std::unique_lock lck(mtx_); std::scoped_lock lck(mtx_);
ipState_.clear(); ipState_.clear();
} }