NewWaiter returns a new Waiter that wraps the given diode.
(d Diode, opts ...WaiterConfigOption)
| 28 | |
| 29 | // NewWaiter returns a new Waiter that wraps the given diode. |
| 30 | func NewWaiter(d Diode, opts ...WaiterConfigOption) *Waiter { |
| 31 | w := new(Waiter) |
| 32 | w.Diode = d |
| 33 | w.c = sync.NewCond(&w.mu) |
| 34 | w.ctx = context.Background() |
| 35 | |
| 36 | for _, opt := range opts { |
| 37 | opt(w) |
| 38 | } |
| 39 | |
| 40 | go func() { |
| 41 | <-w.ctx.Done() |
| 42 | |
| 43 | // Mutex is strictly necessary here to avoid a race in Next() (between |
| 44 | // w.isDone() and w.c.Wait()) and w.c.Broadcast() here. |
| 45 | w.mu.Lock() |
| 46 | w.c.Broadcast() |
| 47 | w.mu.Unlock() |
| 48 | }() |
| 49 | |
| 50 | return w |
| 51 | } |
| 52 | |
| 53 | // Set invokes the wrapped diode's Set with the given data and uses Broadcast |
| 54 | // to wake up any readers. |