threading: semaphore: move CountingSemaphoreGrant to its own header
This commit is contained in:
1 parent
fd15469892
commit
6f7052a7b9
4 files changed
+96
-83
No files matched your search
@@ -24,6 +24,7 @@
|
|||||||
#include <policy/feerate.h>
|
#include <policy/feerate.h>
|
||||||
#include <protocol.h>
|
#include <protocol.h>
|
||||||
#include <random.h>
|
#include <random.h>
|
||||||
|
#include <semaphore_grant.h>
|
||||||
#include <span.h>
|
#include <span.h>
|
||||||
#include <streams.h>
|
#include <streams.h>
|
||||||
#include <sync.h>
|
#include <sync.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 <semaphore>
|
||||||
|
|
||||||
|
/** RAII-style semaphore lock */
|
||||||
|
template <std::ptrdiff_t LeastMaxValue = std::counting_semaphore<>::max()>
|
||||||
|
class CountingSemaphoreGrant
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
std::counting_semaphore<LeastMaxValue>* 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<LeastMaxValue>& 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
|
||||||
-83
@@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <semaphore>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
@@ -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.
|
//! 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; }())
|
#define WITH_LOCK(cs, code) (MaybeCheckNotHeld(cs), [&]() -> decltype(auto) { LOCK(cs); code; }())
|
||||||
|
|
||||||
/** RAII-style semaphore lock */
|
|
||||||
template <std::ptrdiff_t LeastMaxValue = std::counting_semaphore<>::max()>
|
|
||||||
class CountingSemaphoreGrant
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
std::counting_semaphore<LeastMaxValue>* 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<LeastMaxValue>& 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
|
#endif // BITCOIN_SYNC_H
|
||||||
@@ -8,6 +8,8 @@
|
|||||||
#include <sync.h>
|
#include <sync.h>
|
||||||
#include <wallet/db.h>
|
#include <wallet/db.h>
|
||||||
|
|
||||||
|
#include <semaphore>
|
||||||
|
|
||||||
struct bilingual_str;
|
struct bilingual_str;
|
||||||
|
|
||||||
struct sqlite3_stmt;
|
struct sqlite3_stmt;
|
||||||
|
|||||||
Reference in new issue
Block a user