diff options
author | fanquake <fanquake@gmail.com> | 2022-10-11 15:33:22 +0800 |
---|---|---|
committer | fanquake <fanquake@gmail.com> | 2022-11-01 10:14:49 +0000 |
commit | b89530483d39f6a6a777df590b87ba2fad8c8b60 (patch) | |
tree | 76db063ca13d758a6b34220af362fc1ef85c6e95 /src/util | |
parent | c041d8f2c950105cbba1a1280321ffb7f48316da (diff) | |
download | bitcoin-b89530483d39f6a6a777df590b87ba2fad8c8b60.tar.xz |
util: move threadinterrupt into util
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/sock.cpp | 2 | ||||
-rw-r--r-- | src/util/sock.h | 2 | ||||
-rw-r--r-- | src/util/threadinterrupt.cpp | 35 | ||||
-rw-r--r-- | src/util/threadinterrupt.h | 36 |
4 files changed, 73 insertions, 2 deletions
diff --git a/src/util/sock.cpp b/src/util/sock.cpp index 84ac2759fa..e3d30c24b3 100644 --- a/src/util/sock.cpp +++ b/src/util/sock.cpp @@ -4,11 +4,11 @@ #include <compat/compat.h> #include <logging.h> -#include <threadinterrupt.h> #include <tinyformat.h> #include <util/sock.h> #include <util/syserror.h> #include <util/system.h> +#include <util/threadinterrupt.h> #include <util/time.h> #include <memory> diff --git a/src/util/sock.h b/src/util/sock.h index 7912284904..a7347df8ee 100644 --- a/src/util/sock.h +++ b/src/util/sock.h @@ -6,7 +6,7 @@ #define BITCOIN_UTIL_SOCK_H #include <compat/compat.h> -#include <threadinterrupt.h> +#include <util/threadinterrupt.h> #include <util/time.h> #include <chrono> diff --git a/src/util/threadinterrupt.cpp b/src/util/threadinterrupt.cpp new file mode 100644 index 0000000000..70731d6f31 --- /dev/null +++ b/src/util/threadinterrupt.cpp @@ -0,0 +1,35 @@ +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2009-2018 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include <util/threadinterrupt.h> + +#include <sync.h> + +CThreadInterrupt::CThreadInterrupt() : flag(false) {} + +CThreadInterrupt::operator bool() const +{ + return flag.load(std::memory_order_acquire); +} + +void CThreadInterrupt::reset() +{ + flag.store(false, std::memory_order_release); +} + +void CThreadInterrupt::operator()() +{ + { + LOCK(mut); + flag.store(true, std::memory_order_release); + } + cond.notify_all(); +} + +bool CThreadInterrupt::sleep_for(Clock::duration rel_time) +{ + WAIT_LOCK(mut, lock); + return !cond.wait_for(lock, rel_time, [this]() { return flag.load(std::memory_order_acquire); }); +} diff --git a/src/util/threadinterrupt.h b/src/util/threadinterrupt.h new file mode 100644 index 0000000000..d95cbb9aba --- /dev/null +++ b/src/util/threadinterrupt.h @@ -0,0 +1,36 @@ +// Copyright (c) 2016-2021 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_UTIL_THREADINTERRUPT_H +#define BITCOIN_UTIL_THREADINTERRUPT_H + +#include <sync.h> +#include <threadsafety.h> + +#include <atomic> +#include <chrono> +#include <condition_variable> + +/* + A helper class for interruptible sleeps. Calling operator() will interrupt + any current sleep, and after that point operator bool() will return true + until reset. +*/ +class CThreadInterrupt +{ +public: + using Clock = std::chrono::steady_clock; + CThreadInterrupt(); + explicit operator bool() const; + void operator()() EXCLUSIVE_LOCKS_REQUIRED(!mut); + void reset(); + bool sleep_for(Clock::duration rel_time) EXCLUSIVE_LOCKS_REQUIRED(!mut); + +private: + std::condition_variable cond; + Mutex mut; + std::atomic<bool> flag; +}; + +#endif // BITCOIN_UTIL_THREADINTERRUPT_H |