1616
1717#include < condition_variable>
1818#include < mutex>
19+ #include < semaphore>
1920#include < string>
2021#include < thread>
2122
@@ -304,6 +305,7 @@ inline MutexType* MaybeCheckNotHeld(MutexType* m) LOCKS_EXCLUDED(m) LOCK_RETURNE
304305 *
305306 * See https://en.wikipedia.org/wiki/Semaphore_(programming)
306307 */
308+ template <std::ptrdiff_t LeastMaxValue = std::counting_semaphore<>::max()>
307309class CountingSemaphore
308310{
309311private:
@@ -348,14 +350,15 @@ class CountingSemaphore
348350 }
349351};
350352
351- using BinarySemaphore = CountingSemaphore;
352- using Semaphore = CountingSemaphore;
353+ using BinarySemaphore = CountingSemaphore< 1 > ;
354+ using Semaphore = CountingSemaphore<> ;
353355
354356/* * RAII-style semaphore lock */
357+ template <std::ptrdiff_t LeastMaxValue = std::counting_semaphore<>::max()>
355358class CountingSemaphoreGrant
356359{
357360private:
358- CountingSemaphore* sem;
361+ CountingSemaphore<LeastMaxValue> * sem;
359362 bool fHaveGrant ;
360363
361364public:
@@ -410,7 +413,7 @@ class CountingSemaphoreGrant
410413
411414 CountingSemaphoreGrant () noexcept : sem(nullptr ), fHaveGrant (false ) {}
412415
413- explicit CountingSemaphoreGrant (CountingSemaphore& sema, bool fTry = false ) noexcept : sem(&sema), fHaveGrant(false )
416+ explicit CountingSemaphoreGrant (CountingSemaphore<LeastMaxValue> & sema, bool fTry = false ) noexcept : sem(&sema), fHaveGrant(false )
414417 {
415418 if (fTry ) {
416419 TryAcquire ();
@@ -430,7 +433,7 @@ class CountingSemaphoreGrant
430433 }
431434};
432435
433- using BinarySemaphoreGrant = CountingSemaphoreGrant;
434- using SemaphoreGrant = CountingSemaphoreGrant;
436+ using BinarySemaphoreGrant = CountingSemaphoreGrant< 1 > ;
437+ using SemaphoreGrant = CountingSemaphoreGrant<> ;
435438
436439#endif // BITCOIN_SYNC_H
0 commit comments