/bitcoin/src/semaphore_grant.h
Line | Count | Source |
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 | 8.06k | { |
22 | 8.06k | if (fHaveGrant) { Branch (22:13): [True: 0, False: 8.06k]
|
23 | 0 | return; |
24 | 0 | } |
25 | 8.06k | sem->acquire(); |
26 | 8.06k | fHaveGrant = true; |
27 | 8.06k | } |
28 | | |
29 | | void Release() noexcept |
30 | 1.42M | { |
31 | 1.42M | if (!fHaveGrant) { Branch (31:13): [True: 666k, False: 760k]
|
32 | 666k | return; |
33 | 666k | } |
34 | 760k | sem->release(); |
35 | 760k | fHaveGrant = false; |
36 | 760k | } |
37 | | |
38 | | bool TryAcquire() noexcept |
39 | 2.74k | { |
40 | 2.74k | if (!fHaveGrant && sem->try_acquire()) { Branch (40:13): [True: 2.74k, False: 0]
Branch (40:28): [True: 2.74k, False: 0]
|
41 | 2.74k | fHaveGrant = true; |
42 | 2.74k | } |
43 | 2.74k | return fHaveGrant; |
44 | 2.74k | } |
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 | 0 | { |
53 | 0 | sem = other.sem; |
54 | 0 | fHaveGrant = other.fHaveGrant; |
55 | 0 | other.fHaveGrant = false; |
56 | 0 | other.sem = nullptr; |
57 | 0 | } |
58 | | |
59 | | CountingSemaphoreGrant& operator=(CountingSemaphoreGrant&& other) noexcept |
60 | 2.74k | { |
61 | 2.74k | Release(); |
62 | 2.74k | sem = other.sem; |
63 | 2.74k | fHaveGrant = other.fHaveGrant; |
64 | 2.74k | other.fHaveGrant = false; |
65 | 2.74k | other.sem = nullptr; |
66 | 2.74k | return *this; |
67 | 2.74k | } |
68 | | |
69 | 24.9k | CountingSemaphoreGrant() noexcept : sem(nullptr), fHaveGrant(false) {} |
70 | | |
71 | 10.8k | explicit CountingSemaphoreGrant(std::counting_semaphore<LeastMaxValue>& sema, bool fTry = false) noexcept : sem(&sema), fHaveGrant(false) |
72 | 10.8k | { |
73 | 10.8k | if (fTry) { Branch (73:13): [True: 2.74k, False: 8.07k]
|
74 | 2.74k | TryAcquire(); |
75 | 8.07k | } else { |
76 | 8.07k | Acquire(); |
77 | 8.07k | } |
78 | 10.8k | } |
79 | | |
80 | | ~CountingSemaphoreGrant() |
81 | 1.38M | { |
82 | 1.38M | Release(); |
83 | 1.38M | } |
84 | | |
85 | | explicit operator bool() const noexcept |
86 | 2.74k | { |
87 | 2.74k | return fHaveGrant; |
88 | 2.74k | } |
89 | | }; |
90 | | |
91 | | using BinarySemaphoreGrant = CountingSemaphoreGrant<1>; |
92 | | |
93 | | #endif // BITCOIN_SEMAPHORE_GRANT_H |