mirror of
https://git.zx2c4.com/wireguard-go
synced 2024-11-15 01:05:15 +01:00
6db41d5a26
- Begin move away from global timer state. - Made logging format more consistent
67 lines
848 B
Go
67 lines
848 B
Go
package main
|
|
|
|
func signalSend(s chan<- struct{}) {
|
|
select {
|
|
case s <- struct{}{}:
|
|
default:
|
|
}
|
|
}
|
|
|
|
type Signal struct {
|
|
enabled AtomicBool
|
|
C chan struct{}
|
|
}
|
|
|
|
func NewSignal() (s Signal) {
|
|
s.C = make(chan struct{}, 1)
|
|
s.Enable()
|
|
return
|
|
}
|
|
|
|
func (s *Signal) Close() {
|
|
close(s.C)
|
|
}
|
|
|
|
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() {
|
|
if s.enabled.Get() {
|
|
close(s.C)
|
|
}
|
|
}
|
|
|
|
/* Wait for the signal
|
|
*/
|
|
func (s Signal) Wait() chan struct{} {
|
|
return s.C
|
|
}
|