/bitcoin/src/util/threadinterrupt.cpp
Line | Count | Source |
1 | | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | | // Copyright (c) 2009-2022 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 | | |
8 | | #include <sync.h> |
9 | | |
10 | 44.3k | CThreadInterrupt::CThreadInterrupt() : flag(false) {} |
11 | | |
12 | | CThreadInterrupt::operator bool() const |
13 | 78.1M | { |
14 | 78.1M | return flag.load(std::memory_order_acquire); |
15 | 78.1M | } |
16 | | |
17 | | void CThreadInterrupt::reset() |
18 | 22.1k | { |
19 | 22.1k | flag.store(false, std::memory_order_release); |
20 | 22.1k | } |
21 | | |
22 | | void CThreadInterrupt::operator()() |
23 | 66.5k | { |
24 | 66.5k | { |
25 | 66.5k | LOCK(mut); |
26 | 66.5k | flag.store(true, std::memory_order_release); |
27 | 66.5k | } |
28 | 66.5k | cond.notify_all(); |
29 | 66.5k | } |
30 | | |
31 | | bool CThreadInterrupt::sleep_for(Clock::duration rel_time) |
32 | 42.1k | { |
33 | 42.1k | WAIT_LOCK(mut, lock); |
34 | 84.2k | return !cond.wait_for(lock, rel_time, [this]() { return flag.load(std::memory_order_acquire); }); |
35 | 42.1k | } |