Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto 2 : // Copyright (c) 2009-2016 The Bitcoin Core developers 3 : // Distributed under the MIT software license, see the accompanying 4 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 : 6 : #include <util/threadinterrupt.h> 7 : #include <sync.h> 8 : 9 1792 : CThreadInterrupt::CThreadInterrupt() : flag(false) {} 10 : 11 13813 : CThreadInterrupt::operator bool() const 12 : { 13 13813 : return flag.load(std::memory_order_acquire); 14 : } 15 : 16 180 : void CThreadInterrupt::reset() 17 : { 18 180 : flag.store(false, std::memory_order_release); 19 180 : } 20 : 21 956 : void CThreadInterrupt::operator()() 22 : { 23 : { 24 956 : LOCK(mut); 25 956 : flag.store(true, std::memory_order_release); 26 956 : } 27 956 : cond.notify_all(); 28 956 : } 29 : 30 1825 : bool CThreadInterrupt::sleep_for(Clock::duration rel_time) 31 : { 32 1825 : WAIT_LOCK(mut, lock); 33 5475 : return !cond.wait_for(lock, rel_time, [this]() { return flag.load(std::memory_order_acquire); }); 34 1825 : }