16
16
17
17
#include < condition_variable>
18
18
#include < mutex>
19
+ #include < semaphore>
19
20
#include < string>
20
21
#include < thread>
21
22
@@ -304,6 +305,7 @@ inline MutexType* MaybeCheckNotHeld(MutexType* m) LOCKS_EXCLUDED(m) LOCK_RETURNE
304
305
*
305
306
* See https://en.wikipedia.org/wiki/Semaphore_(programming)
306
307
*/
308
+ template <std::ptrdiff_t LeastMaxValue = std::counting_semaphore<>::max()>
307
309
class CountingSemaphore
308
310
{
309
311
private:
@@ -348,14 +350,15 @@ class CountingSemaphore
348
350
}
349
351
};
350
352
351
- using BinarySemaphore = CountingSemaphore;
352
- using Semaphore = CountingSemaphore;
353
+ using BinarySemaphore = CountingSemaphore< 1 > ;
354
+ using Semaphore = CountingSemaphore<> ;
353
355
354
356
/* * RAII-style semaphore lock */
357
+ template <std::ptrdiff_t LeastMaxValue = std::counting_semaphore<>::max()>
355
358
class CountingSemaphoreGrant
356
359
{
357
360
private:
358
- CountingSemaphore* sem;
361
+ CountingSemaphore<LeastMaxValue> * sem;
359
362
bool fHaveGrant ;
360
363
361
364
public:
@@ -410,7 +413,7 @@ class CountingSemaphoreGrant
410
413
411
414
CountingSemaphoreGrant () noexcept : sem(nullptr ), fHaveGrant (false ) {}
412
415
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 )
414
417
{
415
418
if (fTry ) {
416
419
TryAcquire ();
@@ -430,7 +433,7 @@ class CountingSemaphoreGrant
430
433
}
431
434
};
432
435
433
- using BinarySemaphoreGrant = CountingSemaphoreGrant;
434
- using SemaphoreGrant = CountingSemaphoreGrant;
436
+ using BinarySemaphoreGrant = CountingSemaphoreGrant< 1 > ;
437
+ using SemaphoreGrant = CountingSemaphoreGrant<> ;
435
438
436
439
#endif // BITCOIN_SYNC_H
0 commit comments