|
| 1 | +package conc |
| 2 | + |
| 3 | +import ( |
| 4 | + "solod.dev/so/c" |
| 5 | + "solod.dev/so/mem" |
| 6 | + "solod.dev/so/sync" |
| 7 | + "solod.dev/so/time" |
| 8 | +) |
| 9 | + |
| 10 | +// Status reports the outcome of a timed channel operation. |
| 11 | +type Status int |
| 12 | + |
| 13 | +const ( |
| 14 | + Ok Status = iota // the value was transferred |
| 15 | + Timeout // the deadline elapsed before a transfer |
| 16 | + Closed // the channel was closed |
| 17 | +) |
| 18 | + |
| 19 | +// Buffer is the non-generic engine behind a buffered [Chan]: |
| 20 | +// a thread-safe FIFO of items stored in a ring buffer. In most cases, |
| 21 | +// using [Chan] is more convenient. |
| 22 | +type Buffer struct { |
| 23 | + alloc mem.Allocator |
| 24 | + |
| 25 | + mu sync.Mutex |
| 26 | + notEmpty sync.Cond // signaled when an item becomes available |
| 27 | + notFull sync.Cond // signaled when a slot frees |
| 28 | + |
| 29 | + // buf is a ring buffer of items in the channel. |
| 30 | + // bhead and btail are indices into buf, |
| 31 | + // and bcount is the number of items buffered. |
| 32 | + buf []any |
| 33 | + bhead int |
| 34 | + btail int |
| 35 | + bcount int |
| 36 | + |
| 37 | + closed bool // true after Close |
| 38 | +} |
| 39 | + |
| 40 | +// NewBuffer creates a buffered channel holding up to size items. |
| 41 | +func NewBuffer(alloc mem.Allocator, size int) *Buffer { |
| 42 | + c.Assert(size > 0, "conc: buffered chan size must be > 0") |
| 43 | + |
| 44 | + ch := mem.Alloc[Buffer](alloc) |
| 45 | + ch.alloc = alloc |
| 46 | + ch.buf = mem.AllocSlice[any](alloc, size, size) |
| 47 | + ch.bhead, ch.btail, ch.bcount = 0, 0, 0 |
| 48 | + ch.closed = false |
| 49 | + |
| 50 | + ch.mu.Init() |
| 51 | + ch.notEmpty.Init(&ch.mu) |
| 52 | + ch.notFull.Init(&ch.mu) |
| 53 | + return ch |
| 54 | +} |
| 55 | + |
| 56 | +// Send stores v in the channel, blocking while the channel is full |
| 57 | +// (back-pressure). Panics if the channel is closed. |
| 58 | +func (ch *Buffer) Send(v any) { |
| 59 | + ch.mu.Lock() |
| 60 | + for ch.bfull() && !ch.closed { |
| 61 | + ch.notFull.Wait() |
| 62 | + } |
| 63 | + if ch.closed { |
| 64 | + ch.mu.Unlock() |
| 65 | + panic("conc: send on closed channel") |
| 66 | + } |
| 67 | + ch.bpush(v) |
| 68 | + ch.notEmpty.Signal() |
| 69 | + ch.mu.Unlock() |
| 70 | +} |
| 71 | + |
| 72 | +// SendTimeout stores v, waiting up to d for room if the buffer is full. A |
| 73 | +// zero or negative d makes it non-blocking. |
| 74 | +// |
| 75 | +// Returns Ok if the value was stored, Timeout if the deadline passed while |
| 76 | +// the buffer stayed full, or Closed if the channel is closed. |
| 77 | +func (ch *Buffer) SendTimeout(v any, d time.Duration) Status { |
| 78 | + deadline := time.Now().Add(d) |
| 79 | + ch.mu.Lock() |
| 80 | + timedOut := false |
| 81 | + for ch.bfull() && !ch.closed && !timedOut { |
| 82 | + dur := int64(time.Until(deadline)) |
| 83 | + timedOut = ch.notFull.WaitFor(dur) |
| 84 | + } |
| 85 | + if ch.closed { |
| 86 | + ch.mu.Unlock() |
| 87 | + return Closed |
| 88 | + } |
| 89 | + if ch.bfull() { |
| 90 | + // Still full: the deadline passed before a slot freed. |
| 91 | + ch.mu.Unlock() |
| 92 | + return Timeout |
| 93 | + } |
| 94 | + // A slot may have freed right at the deadline; store anyway. |
| 95 | + ch.bpush(v) |
| 96 | + ch.notEmpty.Signal() |
| 97 | + ch.mu.Unlock() |
| 98 | + return Ok |
| 99 | +} |
| 100 | + |
| 101 | +// Recv takes the next value from the channel. It reports whether a value was |
| 102 | +// received: false means the channel is closed and drained. |
| 103 | +func (ch *Buffer) Recv() (any, bool) { |
| 104 | + ch.mu.Lock() |
| 105 | + for ch.bempty() && !ch.closed { |
| 106 | + ch.notEmpty.Wait() |
| 107 | + } |
| 108 | + if ch.bempty() && ch.closed { |
| 109 | + ch.mu.Unlock() |
| 110 | + return nil, false |
| 111 | + } |
| 112 | + v := ch.bpop() |
| 113 | + ch.notFull.Signal() |
| 114 | + ch.mu.Unlock() |
| 115 | + return v, true |
| 116 | +} |
| 117 | + |
| 118 | +// RecvTimeout takes the next value, waiting up to d for one if the buffer is |
| 119 | +// empty. A zero or negative d makes it non-blocking. |
| 120 | +// |
| 121 | +// Returns the value with Ok, or nil with Timeout if the deadline passed while |
| 122 | +// the buffer stayed empty, or nil with Closed if the channel is closed and drained. |
| 123 | +func (ch *Buffer) RecvTimeout(d time.Duration) (any, Status) { |
| 124 | + deadline := time.Now().Add(d) |
| 125 | + ch.mu.Lock() |
| 126 | + timedOut := false |
| 127 | + for ch.bempty() && !ch.closed && !timedOut { |
| 128 | + dur := int64(time.Until(deadline)) |
| 129 | + timedOut = ch.notEmpty.WaitFor(dur) |
| 130 | + } |
| 131 | + if !ch.bempty() { |
| 132 | + // A value is available (possibly delivered right at the deadline), so |
| 133 | + // it wins over both close and timeout. |
| 134 | + v := ch.bpop() |
| 135 | + ch.notFull.Signal() |
| 136 | + ch.mu.Unlock() |
| 137 | + return v, Ok |
| 138 | + } |
| 139 | + if ch.closed { |
| 140 | + ch.mu.Unlock() |
| 141 | + return nil, Closed |
| 142 | + } |
| 143 | + ch.mu.Unlock() |
| 144 | + return nil, Timeout |
| 145 | +} |
| 146 | + |
| 147 | +// Close marks the channel closed. Subsequent sends panic; receivers drain any |
| 148 | +// buffered items and then return false. Closing a closed channel panics. |
| 149 | +func (ch *Buffer) Close() { |
| 150 | + ch.mu.Lock() |
| 151 | + if ch.closed { |
| 152 | + ch.mu.Unlock() |
| 153 | + panic("conc: close of closed channel") |
| 154 | + } |
| 155 | + ch.closed = true |
| 156 | + ch.notEmpty.Broadcast() |
| 157 | + ch.notFull.Broadcast() |
| 158 | + ch.mu.Unlock() |
| 159 | +} |
| 160 | + |
| 161 | +// bfull reports whether the ring buffer is at capacity. |
| 162 | +func (ch *Buffer) bfull() bool { return ch.bcount == len(ch.buf) } |
| 163 | + |
| 164 | +// bempty reports whether the ring buffer is empty. |
| 165 | +func (ch *Buffer) bempty() bool { return ch.bcount == 0 } |
| 166 | + |
| 167 | +// bpush appends v to the tail of the ring buffer. |
| 168 | +func (ch *Buffer) bpush(v any) { |
| 169 | + ch.buf[ch.btail] = v |
| 170 | + ch.btail = (ch.btail + 1) % len(ch.buf) |
| 171 | + ch.bcount++ |
| 172 | +} |
| 173 | + |
| 174 | +// bpop removes and returns the item at the head of the ring buffer. |
| 175 | +func (ch *Buffer) bpop() any { |
| 176 | + v := ch.buf[ch.bhead] |
| 177 | + ch.bhead = (ch.bhead + 1) % len(ch.buf) |
| 178 | + ch.bcount-- |
| 179 | + return v |
| 180 | +} |
| 181 | + |
| 182 | +// Free releases the channel's resources. The channel is unusable afterward. |
| 183 | +// Call it once fully done; a channel may be drained after Close. |
| 184 | +func (ch *Buffer) Free() { |
| 185 | + ch.mu.Free() |
| 186 | + ch.notEmpty.Free() |
| 187 | + ch.notFull.Free() |
| 188 | + mem.FreeSlice(ch.alloc, ch.buf) |
| 189 | + mem.Free(ch.alloc, ch) |
| 190 | +} |
0 commit comments