diff --git a/src/net.h b/src/net.h index a017440c212..4cb4cb906e2 100644 --- a/src/net.h +++ b/src/net.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include diff --git a/src/semaphore_grant.h b/src/semaphore_grant.h new file mode 100644 index 00000000000..b1f815f269e --- /dev/null +++ b/src/semaphore_grant.h @@ -0,0 +1,93 @@ +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2009-present 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_SEMAPHORE_GRANT_H +#define BITCOIN_SEMAPHORE_GRANT_H + +#include + +/** RAII-style semaphore lock */ +template ::max()> +class CountingSemaphoreGrant +{ +private: + std::counting_semaphore* sem; + bool fHaveGrant; + +public: + void Acquire() noexcept + { + if (fHaveGrant) { + return; + } + sem->acquire(); + fHaveGrant = true; + } + + void Release() noexcept + { + if (!fHaveGrant) { + return; + } + sem->release(); + fHaveGrant = false; + } + + bool TryAcquire() noexcept + { + if (!fHaveGrant && sem->try_acquire()) { + fHaveGrant = true; + } + return fHaveGrant; + } + + // Disallow copy. + CountingSemaphoreGrant(const CountingSemaphoreGrant&) = delete; + CountingSemaphoreGrant& operator=(const CountingSemaphoreGrant&) = delete; + + // Allow move. + CountingSemaphoreGrant(CountingSemaphoreGrant&& other) noexcept + { + sem = other.sem; + fHaveGrant = other.fHaveGrant; + other.fHaveGrant = false; + other.sem = nullptr; + } + + CountingSemaphoreGrant& operator=(CountingSemaphoreGrant&& other) noexcept + { + Release(); + sem = other.sem; + fHaveGrant = other.fHaveGrant; + other.fHaveGrant = false; + other.sem = nullptr; + return *this; + } + + CountingSemaphoreGrant() noexcept : sem(nullptr), fHaveGrant(false) {} + + explicit CountingSemaphoreGrant(std::counting_semaphore& sema, bool fTry = false) noexcept : sem(&sema), fHaveGrant(false) + { + if (fTry) { + TryAcquire(); + } else { + Acquire(); + } + } + + ~CountingSemaphoreGrant() + { + Release(); + } + + explicit operator bool() const noexcept + { + return fHaveGrant; + } +}; + +using BinarySemaphoreGrant = CountingSemaphoreGrant<1>; + +#endif // BITCOIN_SEMAPHORE_GRANT_H diff --git a/src/sync.h b/src/sync.h index 4e5efca4490..6eb45f29884 100644 --- a/src/sync.h +++ b/src/sync.h @@ -16,7 +16,6 @@ #include #include -#include #include #include @@ -301,86 +300,4 @@ inline MutexType* MaybeCheckNotHeld(MutexType* m) LOCKS_EXCLUDED(m) LOCK_RETURNE //! gcc and the -Wreturn-stack-address flag in clang, both enabled by default. #define WITH_LOCK(cs, code) (MaybeCheckNotHeld(cs), [&]() -> decltype(auto) { LOCK(cs); code; }()) -/** RAII-style semaphore lock */ -template ::max()> -class CountingSemaphoreGrant -{ -private: - std::counting_semaphore* sem; - bool fHaveGrant; - -public: - void Acquire() noexcept - { - if (fHaveGrant) { - return; - } - sem->acquire(); - fHaveGrant = true; - } - - void Release() noexcept - { - if (!fHaveGrant) { - return; - } - sem->release(); - fHaveGrant = false; - } - - bool TryAcquire() noexcept - { - if (!fHaveGrant && sem->try_acquire()) { - fHaveGrant = true; - } - return fHaveGrant; - } - - // Disallow copy. - CountingSemaphoreGrant(const CountingSemaphoreGrant&) = delete; - CountingSemaphoreGrant& operator=(const CountingSemaphoreGrant&) = delete; - - // Allow move. - CountingSemaphoreGrant(CountingSemaphoreGrant&& other) noexcept - { - sem = other.sem; - fHaveGrant = other.fHaveGrant; - other.fHaveGrant = false; - other.sem = nullptr; - } - - CountingSemaphoreGrant& operator=(CountingSemaphoreGrant&& other) noexcept - { - Release(); - sem = other.sem; - fHaveGrant = other.fHaveGrant; - other.fHaveGrant = false; - other.sem = nullptr; - return *this; - } - - CountingSemaphoreGrant() noexcept : sem(nullptr), fHaveGrant(false) {} - - explicit CountingSemaphoreGrant(std::counting_semaphore& sema, bool fTry = false) noexcept : sem(&sema), fHaveGrant(false) - { - if (fTry) { - TryAcquire(); - } else { - Acquire(); - } - } - - ~CountingSemaphoreGrant() - { - Release(); - } - - explicit operator bool() const noexcept - { - return fHaveGrant; - } -}; - -using BinarySemaphoreGrant = CountingSemaphoreGrant<1>; - #endif // BITCOIN_SYNC_H diff --git a/src/wallet/sqlite.h b/src/wallet/sqlite.h index dda1fab204c..14ad38792c4 100644 --- a/src/wallet/sqlite.h +++ b/src/wallet/sqlite.h @@ -8,6 +8,8 @@ #include #include +#include + struct bilingual_str; struct sqlite3_stmt;