Skip to content

Commit 1acacfb

Browse files
committed
threading: make CountingSemaphore/CountingSemaphoreGrant template types
1 parent e6ce5f9 commit 1acacfb

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/sync.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
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()>
307309
class CountingSemaphore
308310
{
309311
private:
@@ -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()>
355358
class CountingSemaphoreGrant
356359
{
357360
private:
358-
CountingSemaphore* sem;
361+
CountingSemaphore<LeastMaxValue>* sem;
359362
bool fHaveGrant;
360363

361364
public:
@@ -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

Comments
 (0)