]> Sergey Matveev's repositories - btrtrc.git/blob - internal/chansync/broadcast-cond.go.go
6d96d3c4e6e02ab6707e7c82c1a3ae0796b60400
[btrtrc.git] / internal / chansync / broadcast-cond.go.go
1 package chansync
2
3 import (
4         "github.com/anacrolix/sync"
5 )
6
7 // Can be used as zero-value. Due to the caller needing to bring their own synchronization, an
8 // eqiuvalent to "sync".Cond.Signal is not provided. BroadcastCond is intended to be selected on
9 // with other channels.
10 type BroadcastCond struct {
11         mu sync.Mutex
12         ch chan struct{}
13 }
14
15 func (me *BroadcastCond) Broadcast() {
16         me.mu.Lock()
17         defer me.mu.Unlock()
18         if me.ch != nil {
19                 close(me.ch)
20                 me.ch = nil
21         }
22 }
23
24 // Should be called before releasing locks on resources that might trigger subsequent Broadcasts.
25 // The channel is closed when the condition changes.
26 func (me *BroadcastCond) Signaled() Signaled {
27         me.mu.Lock()
28         defer me.mu.Unlock()
29         if me.ch == nil {
30                 me.ch = make(chan struct{})
31         }
32         return me.ch
33 }