diff options
author | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2018-02-04 16:08:26 +0100 |
---|---|---|
committer | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2018-02-04 16:08:26 +0100 |
commit | a0f54cbe5ac2cd8b8296c2c57c30029dd349cff0 (patch) | |
tree | 64574090d79ff3899c5c18e5268e450028e4656b /signal.go | |
parent | 5871ec04deb8f4715cab37146940baa35c08cbee (diff) |
Align with go library layout
Diffstat (limited to 'signal.go')
-rw-r--r-- | signal.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/signal.go b/signal.go new file mode 100644 index 0000000..2cefad4 --- /dev/null +++ b/signal.go @@ -0,0 +1,53 @@ +package main + +type Signal struct { + enabled AtomicBool + C chan struct{} +} + +func NewSignal() (s Signal) { + s.C = make(chan struct{}, 1) + s.Enable() + return +} + +func (s *Signal) Disable() { + s.enabled.Set(false) + s.Clear() +} + +func (s *Signal) Enable() { + s.enabled.Set(true) +} + +/* Unblock exactly one listener + */ +func (s *Signal) Send() { + if s.enabled.Get() { + select { + case s.C <- struct{}{}: + default: + } + } +} + +/* Clear the signal if already fired + */ +func (s Signal) Clear() { + select { + case <-s.C: + default: + } +} + +/* Unblocks all listeners (forever) + */ +func (s Signal) Broadcast() { + close(s.C) +} + +/* Wait for the signal + */ +func (s Signal) Wait() chan struct{} { + return s.C +} |