Use lock_guard over unique_lock

This commit is contained in:
Miguel Portilla
2016-07-21 14:08:13 -04:00
committed by Nik Bougalis
parent 69b91065c5
commit b343b0468a
4 changed files with 16 additions and 21 deletions

View File

@@ -81,6 +81,7 @@
#include <beast/core/detail/ci_char_traits.hpp>
#include <boost/asio/signal_set.hpp>
#include <boost/optional.hpp>
#include <atomic>
#include <fstream>
namespace ripple {
@@ -250,11 +251,10 @@ private:
class io_latency_sampler
{
private:
std::mutex mutable m_mutex;
beast::insight::Event m_event;
beast::Journal m_journal;
beast::io_latency_probe <std::chrono::steady_clock> m_probe;
std::chrono::milliseconds m_lastSample;
std::atomic<std::chrono::milliseconds> lastSample_;
public:
io_latency_sampler (
@@ -265,6 +265,7 @@ private:
: m_event (ev)
, m_journal (journal)
, m_probe (interval, ios)
, lastSample_ {}
{
}
@@ -280,10 +281,7 @@ private:
using namespace std::chrono;
auto const ms (ceil <std::chrono::milliseconds> (elapsed));
{
std::unique_lock <std::mutex> lock (m_mutex);
m_lastSample = ms;
}
lastSample_ = ms;
if (ms.count() >= 10)
m_event.notify (ms);
@@ -297,8 +295,7 @@ private:
std::chrono::milliseconds
get () const
{
std::unique_lock <std::mutex> lock (m_mutex);
return m_lastSample;
return lastSample_.load();
}
void
@@ -595,8 +592,6 @@ public:
std::chrono::milliseconds getIOLatency () override
{
std::unique_lock <std::mutex> m_IOLatencyLock;
return m_io_latency_sampler.get ();
}

View File

@@ -95,7 +95,7 @@ public:
~DatabaseImp ()
{
{
std::unique_lock <std::mutex> lock (m_readLock);
std::lock_guard <std::mutex> lock (m_readLock);
m_readShut = true;
m_readCondVar.notify_all ();
m_readGenCondVar.notify_all ();
@@ -132,7 +132,7 @@ public:
{
// No. Post a read
std::unique_lock <std::mutex> lock (m_readLock);
std::lock_guard <std::mutex> lock (m_readLock);
if (m_readSet.insert (hash).second)
m_readCondVar.notify_one ();
}

View File

@@ -124,14 +124,14 @@ private:
void
add (std::shared_ptr<Child> const& child)
{
std::unique_lock<std::mutex> lock(mutex_);
std::lock_guard<std::mutex> lock(mutex_);
list_.emplace(child.get(), child);
}
void
remove (Child* child)
{
std::unique_lock<std::mutex> lock(mutex_);
std::lock_guard<std::mutex> lock(mutex_);
list_.erase(child);
if (list_.empty())
cond_.notify_one();
@@ -142,7 +142,7 @@ private:
{
std::vector<std::shared_ptr<Child>> v;
{
std::unique_lock<std::mutex> lock(mutex_);
std::lock_guard<std::mutex> lock(mutex_);
v.reserve(list_.size());
if (closed_)
return;

View File

@@ -44,7 +44,7 @@ SHAMapInnerNode::clone(std::uint32_t seq) const
p->mIsBranch = mIsBranch;
p->mFullBelowGen = mFullBelowGen;
p->mHashes = mHashes;
std::unique_lock <std::mutex> lock(childLock);
std::lock_guard <std::mutex> lock(childLock);
for (int i = 0; i < 16; ++i)
{
p->mChildren[i] = mChildren[i];
@@ -63,7 +63,7 @@ SHAMapInnerNodeV2::clone(std::uint32_t seq) const
p->mHashes = mHashes;
p->common_ = common_;
p->depth_ = depth_;
std::unique_lock <std::mutex> lock(childLock);
std::lock_guard <std::mutex> lock(childLock);
for (int i = 0; i < 16; ++i)
{
p->mChildren[i] = mChildren[i];
@@ -679,7 +679,7 @@ SHAMapInnerNode::getChildPointer (int branch)
assert (branch >= 0 && branch < 16);
assert (isInner());
std::unique_lock <std::mutex> lock (childLock);
std::lock_guard <std::mutex> lock (childLock);
return mChildren[branch].get ();
}
@@ -689,7 +689,7 @@ SHAMapInnerNode::getChild (int branch)
assert (branch >= 0 && branch < 16);
assert (isInner());
std::unique_lock <std::mutex> lock (childLock);
std::lock_guard <std::mutex> lock (childLock);
return mChildren[branch];
}
@@ -701,7 +701,7 @@ SHAMapInnerNode::canonicalizeChild(int branch, std::shared_ptr<SHAMapAbstractNod
assert (node);
assert (node->getNodeHash() == mHashes[branch]);
std::unique_lock <std::mutex> lock (childLock);
std::lock_guard <std::mutex> lock (childLock);
if (mChildren[branch])
{
// There is already a node hooked up, return it
@@ -725,7 +725,7 @@ SHAMapInnerNodeV2::canonicalizeChild(int branch, std::shared_ptr<SHAMapAbstractN
assert (node);
assert (node->getNodeHash() == mHashes[branch]);
std::unique_lock <std::mutex> lock (childLock);
std::lock_guard <std::mutex> lock (childLock);
if (mChildren[branch])
{
// There is already a node hooked up, return it