Skip to content

Commit 6f7052a

Browse files
committed
threading: semaphore: move CountingSemaphoreGrant to its own header
1 parent fd15469 commit 6f7052a

File tree

4 files changed

+96
-83
lines changed

4 files changed

+96
-83
lines changed

src/net.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <policy/feerate.h>
2525
#include <protocol.h>
2626
#include <random.h>
27+
#include <semaphore_grant.h>
2728
#include <span.h>
2829
#include <streams.h>
2930
#include <sync.h>

src/semaphore_grant.h

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-present 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+
#ifndef BITCOIN_SEMAPHORE_GRANT_H
7+
#define BITCOIN_SEMAPHORE_GRANT_H
8+
9+
#include <semaphore>
10+
11+
/** RAII-style semaphore lock */
12+
template <std::ptrdiff_t LeastMaxValue = std::counting_semaphore<>::max()>
13+
class CountingSemaphoreGrant
14+
{
15+
private:
16+
std::counting_semaphore<LeastMaxValue>* sem;
17+
bool fHaveGrant;
18+
19+
public:
20+
void Acquire() noexcept
21+
{
22+
if (fHaveGrant) {
23+
return;
24+
}
25+
sem->acquire();
26+
fHaveGrant = true;
27+
}
28+
29+
void Release() noexcept
30+
{
31+
if (!fHaveGrant) {
32+
return;
33+
}
34+
sem->release();
35+
fHaveGrant = false;
36+
}
37+
38+
bool TryAcquire() noexcept
39+
{
40+
if (!fHaveGrant && sem->try_acquire()) {
41+
fHaveGrant = true;
42+
}
43+
return fHaveGrant;
44+
}
45+
46+
// Disallow copy.
47+
CountingSemaphoreGrant(const CountingSemaphoreGrant&) = delete;
48+
CountingSemaphoreGrant& operator=(const CountingSemaphoreGrant&) = delete;
49+
50+
// Allow move.
51+
CountingSemaphoreGrant(CountingSemaphoreGrant&& other) noexcept
52+
{
53+
sem = other.sem;
54+
fHaveGrant = other.fHaveGrant;
55+
other.fHaveGrant = false;
56+
other.sem = nullptr;
57+
}
58+
59+
CountingSemaphoreGrant& operator=(CountingSemaphoreGrant&& other) noexcept
60+
{
61+
Release();
62+
sem = other.sem;
63+
fHaveGrant = other.fHaveGrant;
64+
other.fHaveGrant = false;
65+
other.sem = nullptr;
66+
return *this;
67+
}
68+
69+
CountingSemaphoreGrant() noexcept : sem(nullptr), fHaveGrant(false) {}
70+
71+
explicit CountingSemaphoreGrant(std::counting_semaphore<LeastMaxValue>& sema, bool fTry = false) noexcept : sem(&sema), fHaveGrant(false)
72+
{
73+
if (fTry) {
74+
TryAcquire();
75+
} else {
76+
Acquire();
77+
}
78+
}
79+
80+
~CountingSemaphoreGrant()
81+
{
82+
Release();
83+
}
84+
85+
explicit operator bool() const noexcept
86+
{
87+
return fHaveGrant;
88+
}
89+
};
90+
91+
using BinarySemaphoreGrant = CountingSemaphoreGrant<1>;
92+
93+
#endif // BITCOIN_SEMAPHORE_GRANT_H

src/sync.h

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
#include <condition_variable>
1818
#include <mutex>
19-
#include <semaphore>
2019
#include <string>
2120
#include <thread>
2221

@@ -301,86 +300,4 @@ inline MutexType* MaybeCheckNotHeld(MutexType* m) LOCKS_EXCLUDED(m) LOCK_RETURNE
301300
//! gcc and the -Wreturn-stack-address flag in clang, both enabled by default.
302301
#define WITH_LOCK(cs, code) (MaybeCheckNotHeld(cs), [&]() -> decltype(auto) { LOCK(cs); code; }())
303302

304-
/** RAII-style semaphore lock */
305-
template <std::ptrdiff_t LeastMaxValue = std::counting_semaphore<>::max()>
306-
class CountingSemaphoreGrant
307-
{
308-
private:
309-
std::counting_semaphore<LeastMaxValue>* sem;
310-
bool fHaveGrant;
311-
312-
public:
313-
void Acquire() noexcept
314-
{
315-
if (fHaveGrant) {
316-
return;
317-
}
318-
sem->acquire();
319-
fHaveGrant = true;
320-
}
321-
322-
void Release() noexcept
323-
{
324-
if (!fHaveGrant) {
325-
return;
326-
}
327-
sem->release();
328-
fHaveGrant = false;
329-
}
330-
331-
bool TryAcquire() noexcept
332-
{
333-
if (!fHaveGrant && sem->try_acquire()) {
334-
fHaveGrant = true;
335-
}
336-
return fHaveGrant;
337-
}
338-
339-
// Disallow copy.
340-
CountingSemaphoreGrant(const CountingSemaphoreGrant&) = delete;
341-
CountingSemaphoreGrant& operator=(const CountingSemaphoreGrant&) = delete;
342-
343-
// Allow move.
344-
CountingSemaphoreGrant(CountingSemaphoreGrant&& other) noexcept
345-
{
346-
sem = other.sem;
347-
fHaveGrant = other.fHaveGrant;
348-
other.fHaveGrant = false;
349-
other.sem = nullptr;
350-
}
351-
352-
CountingSemaphoreGrant& operator=(CountingSemaphoreGrant&& other) noexcept
353-
{
354-
Release();
355-
sem = other.sem;
356-
fHaveGrant = other.fHaveGrant;
357-
other.fHaveGrant = false;
358-
other.sem = nullptr;
359-
return *this;
360-
}
361-
362-
CountingSemaphoreGrant() noexcept : sem(nullptr), fHaveGrant(false) {}
363-
364-
explicit CountingSemaphoreGrant(std::counting_semaphore<LeastMaxValue>& sema, bool fTry = false) noexcept : sem(&sema), fHaveGrant(false)
365-
{
366-
if (fTry) {
367-
TryAcquire();
368-
} else {
369-
Acquire();
370-
}
371-
}
372-
373-
~CountingSemaphoreGrant()
374-
{
375-
Release();
376-
}
377-
378-
explicit operator bool() const noexcept
379-
{
380-
return fHaveGrant;
381-
}
382-
};
383-
384-
using BinarySemaphoreGrant = CountingSemaphoreGrant<1>;
385-
386303
#endif // BITCOIN_SYNC_H

src/wallet/sqlite.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <sync.h>
99
#include <wallet/db.h>
1010

11+
#include <semaphore>
12+
1113
struct bilingual_str;
1214

1315
struct sqlite3_stmt;

0 commit comments

Comments
 (0)