Coverage Report

Created: 2025-06-10 13:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
31.0k
    {
22
31.0k
        if (fHaveGrant) {
  Branch (22:13): [True: 0, False: 31.0k]
23
0
            return;
24
0
        }
25
31.0k
        sem->acquire();
26
31.0k
        fHaveGrant = true;
27
31.0k
    }
28
29
    void Release() noexcept
30
227k
    {
31
227k
        if (!fHaveGrant) {
  Branch (31:13): [True: 140k, False: 86.4k]
32
140k
            return;
33
140k
        }
34
86.4k
        sem->release();
35
86.4k
        fHaveGrant = false;
36
86.4k
    }
37
38
    bool TryAcquire() noexcept
39
55.4k
    {
40
55.4k
        if (!fHaveGrant && sem->try_acquire()) {
  Branch (40:13): [True: 55.4k, False: 0]
  Branch (40:28): [True: 55.4k, False: 0]
41
55.4k
            fHaveGrant = true;
42
55.4k
        }
43
55.4k
        return fHaveGrant;
44
55.4k
    }
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
44.3k
    {
61
44.3k
        Release();
62
44.3k
        sem = other.sem;
63
44.3k
        fHaveGrant = other.fHaveGrant;
64
44.3k
        other.fHaveGrant = false;
65
44.3k
        other.sem = nullptr;
66
44.3k
        return *this;
67
44.3k
    }
68
69
88.7k
    CountingSemaphoreGrant() noexcept : sem(nullptr), fHaveGrant(false) {}
70
71
86.4k
    explicit CountingSemaphoreGrant(std::counting_semaphore<LeastMaxValue>& sema, bool fTry = false) noexcept : sem(&sema), fHaveGrant(false)
72
86.4k
    {
73
86.4k
        if (fTry) {
  Branch (73:13): [True: 55.4k, False: 31.0k]
74
55.4k
            TryAcquire();
75
55.4k
        } else {
76
31.0k
            Acquire();
77
31.0k
        }
78
86.4k
    }
79
80
    ~CountingSemaphoreGrant()
81
175k
    {
82
175k
        Release();
83
175k
    }
84
85
    explicit operator bool() const noexcept
86
55.4k
    {
87
55.4k
        return fHaveGrant;
88
55.4k
    }
89
};
90
91
using BinarySemaphoreGrant = CountingSemaphoreGrant<1>;
92
93
#endif // BITCOIN_SEMAPHORE_GRANT_H