From e28926c7ef059ae9c0148e4ca363584ecda90e05 Mon Sep 17 00:00:00 2001 From: Mikhail Knyazhev Date: Sat, 3 Aug 2024 02:34:02 +0300 Subject: [PATCH] add Semaphore --- control/semaphore.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 control/semaphore.go diff --git a/control/semaphore.go b/control/semaphore.go new file mode 100644 index 0000000..4bba711 --- /dev/null +++ b/control/semaphore.go @@ -0,0 +1,25 @@ +package control + +type ( + Semaphore interface { + Acquire() + Release() + } + _semaphore struct { + c chan struct{} + } +) + +func NewSemaphore(count uint64) Semaphore { + return &_semaphore{ + c: make(chan struct{}, count), + } +} + +func (s *_semaphore) Acquire() { + s.c <- struct{}{} +} + +func (s *_semaphore) Release() { + <-s.c +}