summaryrefslogtreecommitdiffhomepage
path: root/src/signal.go
blob: 2cefad460fd34e3ddfb5cfecfc6c6a04f5ea5885 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
}