threading: make CountingSemaphore/CountingSemaphoreGrant template types

This commit is contained in:
Cory Fields committed 2025-05-10 00:53:16 +00:00
1 parent e6ce5f9e78
commit 1acacfbad7
1 file changed
+9 -6
+9 -6
View File
@@ -16,6 +16,7 @@
#include <condition_variable> #include <condition_variable>
#include <mutex> #include <mutex>
#include <semaphore>
#include <string> #include <string>
#include <thread> #include <thread>
@@ -304,6 +305,7 @@ inline MutexType* MaybeCheckNotHeld(MutexType* m) LOCKS_EXCLUDED(m) LOCK_RETURNE
* *
* See https://en.wikipedia.org/wiki/Semaphore_(programming) * See https://en.wikipedia.org/wiki/Semaphore_(programming)
*/ */
template <std::ptrdiff_t LeastMaxValue = std::counting_semaphore<>::max()>
class CountingSemaphore class CountingSemaphore
{ {
private: private:
@@ -348,14 +350,15 @@ public:
} }
}; };
using BinarySemaphore = CountingSemaphore; using BinarySemaphore = CountingSemaphore<1>;
using Semaphore = CountingSemaphore; using Semaphore = CountingSemaphore<>;
/** RAII-style semaphore lock */ /** RAII-style semaphore lock */
template <std::ptrdiff_t LeastMaxValue = std::counting_semaphore<>::max()>
class CountingSemaphoreGrant class CountingSemaphoreGrant
{ {
private: private:
CountingSemaphore* sem; CountingSemaphore<LeastMaxValue>* sem;
bool fHaveGrant; bool fHaveGrant;
public: public:
@@ -410,7 +413,7 @@ public:
CountingSemaphoreGrant() noexcept : sem(nullptr), fHaveGrant(false) {} CountingSemaphoreGrant() noexcept : sem(nullptr), fHaveGrant(false) {}
explicit CountingSemaphoreGrant(CountingSemaphore& sema, bool fTry = false) noexcept : sem(&sema), fHaveGrant(false) explicit CountingSemaphoreGrant(CountingSemaphore<LeastMaxValue>& sema, bool fTry = false) noexcept : sem(&sema), fHaveGrant(false)
{ {
if (fTry) { if (fTry) {
TryAcquire(); TryAcquire();
@@ -430,7 +433,7 @@ public:
} }
}; };
using BinarySemaphoreGrant = CountingSemaphoreGrant; using BinarySemaphoreGrant = CountingSemaphoreGrant<1>;
using SemaphoreGrant = CountingSemaphoreGrant; using SemaphoreGrant = CountingSemaphoreGrant<>;
#endif // BITCOIN_SYNC_H #endif // BITCOIN_SYNC_H