/Users/brunogarcia/projects/bitcoin-core-dev/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 | 0 | CThreadInterrupt::CThreadInterrupt() : flag(false) {} |
11 | | |
12 | | bool CThreadInterrupt::interrupted() const |
13 | 0 | { |
14 | 0 | return flag.load(std::memory_order_acquire); |
15 | 0 | } |
16 | | |
17 | | CThreadInterrupt::operator bool() const |
18 | 0 | { |
19 | 0 | return interrupted(); |
20 | 0 | } |
21 | | |
22 | | void CThreadInterrupt::reset() |
23 | 0 | { |
24 | 0 | flag.store(false, std::memory_order_release); |
25 | 0 | } |
26 | | |
27 | | void CThreadInterrupt::operator()() |
28 | 2 | { |
29 | 2 | { |
30 | 2 | LOCK(mut); Line | Count | Source | 259 | 2 | #define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__) Line | Count | Source | 11 | 2 | #define UNIQUE_NAME(name) PASTE2(name, __COUNTER__) Line | Count | Source | 9 | 2 | #define PASTE2(x, y) PASTE(x, y) Line | Count | Source | 8 | 2 | #define PASTE(x, y) x ## y |
|
|
|
|
31 | 2 | flag.store(true, std::memory_order_release); |
32 | 2 | } |
33 | 2 | cond.notify_all(); |
34 | 2 | } |
35 | | |
36 | | bool CThreadInterrupt::sleep_for(Clock::duration rel_time) |
37 | 0 | { |
38 | 0 | WAIT_LOCK(mut, lock); Line | Count | Source | 265 | 0 | #define WAIT_LOCK(cs, name) UniqueLock name(LOCK_ARGS(cs)) Line | Count | Source | 263 | 0 | #define LOCK_ARGS(cs) MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__ |
|
|
39 | 0 | return !cond.wait_for(lock, rel_time, [this]() { return flag.load(std::memory_order_acquire); }); |
40 | 0 | } |