Skip to content

Commit 799df91

Browse files
committed
net: add CThreadInterrupt and InterruptibleSleep
1 parent 7325b15 commit 799df91

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ BITCOIN_CORE_H = \
138138
support/lockedpool.h \
139139
sync.h \
140140
threadsafety.h \
141+
threadinterrupt.h \
141142
timedata.h \
142143
torcontrol.h \
143144
txdb.h \
@@ -327,6 +328,7 @@ libbitcoin_util_a_SOURCES = \
327328
rpc/protocol.cpp \
328329
support/cleanse.cpp \
329330
sync.cpp \
331+
threadinterrupt.cpp \
330332
util.cpp \
331333
utilmoneystr.cpp \
332334
utilstrencodings.cpp \

src/threadinterrupt.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
}

src/threadinterrupt.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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

0 commit comments

Comments
 (0)