From f76a0dec6d0ed95f30f63811da0d0adadb587313 Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Tue, 27 Feb 2018 00:20:32 +0100 Subject: [PATCH] util.Semaphore: initial commit --- util/semaphore.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 util/semaphore.go diff --git a/util/semaphore.go b/util/semaphore.go new file mode 100644 index 0000000..2a0d509 --- /dev/null +++ b/util/semaphore.go @@ -0,0 +1,17 @@ +package util + +type Semaphore struct { + c chan struct{} +} + +func NewSemaphore(cap int) Semaphore { + return Semaphore{make(chan struct{}, cap)} +} + +func (s Semaphore) Down() { + s.c <- struct{}{} +} + +func (s Semaphore) Up() { + <-s.c +}