File tree Expand file tree Collapse file tree 3 files changed +77
-0
lines changed Expand file tree Collapse file tree 3 files changed +77
-0
lines changed Original file line number Diff line number Diff line change @@ -138,6 +138,7 @@ BITCOIN_CORE_H = \
138
138
support/lockedpool.h \
139
139
sync.h \
140
140
threadsafety.h \
141
+ threadinterrupt.h \
141
142
timedata.h \
142
143
torcontrol.h \
143
144
txdb.h \
@@ -327,6 +328,7 @@ libbitcoin_util_a_SOURCES = \
327
328
rpc/protocol.cpp \
328
329
support/cleanse.cpp \
329
330
sync.cpp \
331
+ threadinterrupt.cpp \
330
332
util.cpp \
331
333
utilmoneystr.cpp \
332
334
utilstrencodings.cpp \
Original file line number Diff line number Diff line change
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 " threadinterrupt.h"
7
+
8
+ CThreadInterrupt::operator bool () const
9
+ {
10
+ return flag.load (std::memory_order_acquire);
11
+ }
12
+
13
+ void CThreadInterrupt::reset ()
14
+ {
15
+ flag.store (false , std::memory_order_release);
16
+ }
17
+
18
+ void CThreadInterrupt::operator ()()
19
+ {
20
+ {
21
+ std::unique_lock<std::mutex> lock (mut);
22
+ flag.store (true , std::memory_order_release);
23
+ }
24
+ cond.notify_all ();
25
+ }
26
+
27
+ bool CThreadInterrupt::sleep_for (std::chrono::milliseconds rel_time)
28
+ {
29
+ std::unique_lock<std::mutex> lock (mut);
30
+ return !cond.wait_for (lock, rel_time, [this ]() { return flag.load (std::memory_order_acquire); });
31
+ }
32
+
33
+ bool CThreadInterrupt::sleep_for (std::chrono::seconds rel_time)
34
+ {
35
+ return sleep_for (std::chrono::duration_cast<std::chrono::milliseconds>(rel_time));
36
+ }
37
+
38
+ bool CThreadInterrupt::sleep_for (std::chrono::minutes rel_time)
39
+ {
40
+ return sleep_for (std::chrono::duration_cast<std::chrono::milliseconds>(rel_time));
41
+ }
Original file line number Diff line number Diff line change
1
+ // Copyright (c) 2016 The Bitcoin Core developers
2
+ // Distributed under the MIT software license, see the accompanying
3
+ // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
+
5
+ #ifndef BITCOIN_THREADINTERRUPT_H
6
+ #define BITCOIN_THREADINTERRUPT_H
7
+
8
+ #include < atomic>
9
+ #include < chrono>
10
+ #include < condition_variable>
11
+ #include < mutex>
12
+
13
+ /*
14
+ A helper class for interruptible sleeps. Calling operator() will interrupt
15
+ any current sleep, and after that point operator bool() will return true
16
+ until reset.
17
+ */
18
+ class CThreadInterrupt
19
+ {
20
+ public:
21
+ explicit operator bool () const ;
22
+ void operator ()();
23
+ void reset ();
24
+ bool sleep_for (std::chrono::milliseconds rel_time);
25
+ bool sleep_for (std::chrono::seconds rel_time);
26
+ bool sleep_for (std::chrono::minutes rel_time);
27
+
28
+ private:
29
+ std::condition_variable cond;
30
+ std::mutex mut;
31
+ std::atomic<bool > flag;
32
+ };
33
+
34
+ #endif // BITCOIN_THREADINTERRUPT_H
You can’t perform that action at this time.
0 commit comments