2017-08-09 18:06:31 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
|
|
// Copyright (c) 2009-2016 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <threadinterrupt.h>
|
2017-11-08 21:28:35 +01:00
|
|
|
#include <sync.h>
|
2017-08-09 18:06:31 +02:00
|
|
|
|
2021-05-25 12:48:04 +02:00
|
|
|
CThreadInterrupt::CThreadInterrupt() : flag(false) {}
|
|
|
|
|
2017-08-09 18:06:31 +02:00
|
|
|
CThreadInterrupt::operator bool() const
|
|
|
|
{
|
|
|
|
return flag.load(std::memory_order_acquire);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CThreadInterrupt::reset()
|
|
|
|
{
|
|
|
|
flag.store(false, std::memory_order_release);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CThreadInterrupt::operator()()
|
|
|
|
{
|
|
|
|
{
|
2017-11-08 21:28:35 +01:00
|
|
|
LOCK(mut);
|
2017-08-09 18:06:31 +02:00
|
|
|
flag.store(true, std::memory_order_release);
|
|
|
|
}
|
|
|
|
cond.notify_all();
|
|
|
|
}
|
|
|
|
|
2022-07-26 16:02:13 +02:00
|
|
|
bool CThreadInterrupt::sleep_for(Clock::duration rel_time)
|
2017-08-09 18:06:31 +02:00
|
|
|
{
|
2017-11-08 21:28:35 +01:00
|
|
|
WAIT_LOCK(mut, lock);
|
2017-08-09 18:06:31 +02:00
|
|
|
return !cond.wait_for(lock, rel_time, [this]() { return flag.load(std::memory_order_acquire); });
|
|
|
|
}
|