Skip to content

Commit

Permalink
Replace ScopedUnlock with scope_unlock
Browse files Browse the repository at this point in the history
  • Loading branch information
Bronek committed Sep 9, 2024
1 parent c1ad525 commit b68f842
Showing 1 changed file with 6 additions and 84 deletions.
90 changes: 6 additions & 84 deletions src/xrpld/app/ledger/detail/LedgerMaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@
#include <xrpl/basics/UptimeClock.h>
#include <xrpl/basics/contract.h>
#include <xrpl/basics/safe_cast.h>
#include <xrpl/basics/scope.h>
#include <xrpl/protocol/BuildInfo.h>
#include <xrpl/protocol/HashPrefix.h>
#include <xrpl/protocol/digest.h>
#include <xrpl/resource/Fees.h>

#include <algorithm>
#include <cassert>
#include <chrono>
Expand All @@ -60,86 +62,6 @@

namespace ripple {

namespace {

//==============================================================================
/**
Automatically unlocks and re-locks a unique_lock object.
This is the reverse of a std::unique_lock object - instead of locking the
mutex for the lifetime of this object, it unlocks it.
Make sure you don't try to unlock mutexes that aren't actually locked!
This is essentially a less-versatile boost::reverse_lock.
e.g. @code
std::mutex mut;
for (;;)
{
std::unique_lock myScopedLock{mut};
// mut is now locked
... do some stuff with it locked ..
while (xyz)
{
... do some stuff with it locked ..
ScopedUnlock unlocker{myScopedLock};
// mut is now unlocked for the remainder of this block,
// and re-locked at the end.
...do some stuff with it unlocked ...
} // mut gets locked here.
} // mut gets unlocked here
@endcode
*/
template <class MutexType>
class ScopedUnlock
{
std::unique_lock<MutexType>& lock_;

public:
/** Creates a ScopedUnlock.
As soon as it is created, this will unlock the unique_lock, and
when the ScopedLock object is deleted, the unique_lock will
be re-locked.
Make sure this object is created and deleted by the same thread,
otherwise there are no guarantees what will happen! Best just to use it
as a local stack object, rather than creating on the heap.
*/
explicit ScopedUnlock(std::unique_lock<MutexType>& lock) : lock_(lock)
{
assert(lock_.owns_lock());
lock_.unlock();
}

ScopedUnlock(ScopedUnlock const&) = delete;
ScopedUnlock&
operator=(ScopedUnlock const&) = delete;

/** Destructor.
The unique_lock will be locked after the destructor is called.
Make sure this object is created and deleted by the same thread,
otherwise there are no guarantees what will happen!
*/
~ScopedUnlock() noexcept(false)
{
lock_.lock();
}
};

} // namespace

// Don't catch up more than 100 ledgers (cannot exceed 256)
static constexpr int MAX_LEDGER_GAP{100};

Expand Down Expand Up @@ -1336,7 +1258,7 @@ LedgerMaster::findNewLedgersToPublish(
auto valLedger = mValidLedger.get();
std::uint32_t valSeq = valLedger->info().seq;

ScopedUnlock sul{sl};
scope_unlock sul{sl};
try
{
for (std::uint32_t seq = pubSeq; seq <= valSeq; ++seq)
Expand Down Expand Up @@ -1882,7 +1804,7 @@ LedgerMaster::fetchForHistory(
InboundLedger::Reason reason,
std::unique_lock<std::recursive_mutex>& sl)
{
ScopedUnlock sul{sl};
scope_unlock sul{sl};
if (auto hash = getLedgerHashForHistory(missing, reason))
{
assert(hash->isNonZero());
Expand Down Expand Up @@ -2052,7 +1974,7 @@ LedgerMaster::doAdvance(std::unique_lock<std::recursive_mutex>& sl)
for (auto const& ledger : pubLedgers)
{
{
ScopedUnlock sul{sl};
scope_unlock sul{sl};
JLOG(m_journal.debug())
<< "tryAdvance publishing seq " << ledger->info().seq;
setFullLedger(ledger, true, true);
Expand All @@ -2061,7 +1983,7 @@ LedgerMaster::doAdvance(std::unique_lock<std::recursive_mutex>& sl)
setPubLedger(ledger);

{
ScopedUnlock sul{sl};
scope_unlock sul{sl};
app_.getOPs().pubLedger(ledger);
}
}
Expand Down

0 comments on commit b68f842

Please sign in to comment.