mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-25 16:10:33 +02:00
9c4dc597ddc66acfd58a945a5ab11f833731abba Use LOCK macros for non-recursive locks (Russell Yanofsky) 1382913e61f5db6ba849b1e261e8aefcd5a1ae68 Make LOCK, LOCK2, TRY_LOCK work with CWaitableCriticalSection (Russell Yanofsky) ba1f095aadf29bddb0bd8176d2e0b908f92a5623 MOVEONLY Move AnnotatedMixin declaration (Russell Yanofsky) 41b88e93375d57db12da923f45f87b9a2db8e730 Add unit test for DEBUG_LOCKORDER code (Russell Yanofsky) Pull request description: Make LOCK macros work with non-recursive mutexes, and use wherever possible for better deadlock detection. Also add unit test for DEBUG_LOCKORDER code. Tree-SHA512: 64ef209307f28ecd0813a283f15c6406138c6ffe7f6cbbd084161044db60e2c099a7d0d2edcd1c5e7770a115e9b931b486e86c9a777bdc96d2e8a9f4dc192942
46 lines
1.2 KiB
C++
46 lines
1.2 KiB
C++
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
// Copyright (c) 2009-2018 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#include <threadinterrupt.h>
|
|
|
|
#include <sync.h>
|
|
|
|
CThreadInterrupt::CThreadInterrupt() : flag(false) {}
|
|
|
|
CThreadInterrupt::operator bool() const
|
|
{
|
|
return flag.load(std::memory_order_acquire);
|
|
}
|
|
|
|
void CThreadInterrupt::reset()
|
|
{
|
|
flag.store(false, std::memory_order_release);
|
|
}
|
|
|
|
void CThreadInterrupt::operator()()
|
|
{
|
|
{
|
|
LOCK(mut);
|
|
flag.store(true, std::memory_order_release);
|
|
}
|
|
cond.notify_all();
|
|
}
|
|
|
|
bool CThreadInterrupt::sleep_for(std::chrono::milliseconds rel_time)
|
|
{
|
|
WAIT_LOCK(mut, lock);
|
|
return !cond.wait_for(lock, rel_time, [this]() { return flag.load(std::memory_order_acquire); });
|
|
}
|
|
|
|
bool CThreadInterrupt::sleep_for(std::chrono::seconds rel_time)
|
|
{
|
|
return sleep_for(std::chrono::duration_cast<std::chrono::milliseconds>(rel_time));
|
|
}
|
|
|
|
bool CThreadInterrupt::sleep_for(std::chrono::minutes rel_time)
|
|
{
|
|
return sleep_for(std::chrono::duration_cast<std::chrono::milliseconds>(rel_time));
|
|
}
|