release releases capacity in the semaphore. If the semaphore capacity was reduced in between and as a result inFlight is greater than capacity, we don't wake up goroutines as they'd not get any capacity anyway.
()
| 243 | // If the semaphore capacity was reduced in between and as a result inFlight is greater |
| 244 | // than capacity, we don't wake up goroutines as they'd not get any capacity anyway. |
| 245 | func (s *semaphore) release() { |
| 246 | for { |
| 247 | old := s.state.Load() |
| 248 | capacity, in := unpack(old) |
| 249 | |
| 250 | if in == 0 { |
| 251 | panic("release and acquire are not paired") |
| 252 | } |
| 253 | |
| 254 | in-- |
| 255 | if s.state.CAS(old, pack(capacity, in)) { |
| 256 | if in < capacity { |
| 257 | select { |
| 258 | case s.queue <- struct{}{}: |
| 259 | default: |
| 260 | // We generate more wakeups than we might need as we don't know |
| 261 | // how many goroutines are waiting here. It is therefore okay |
| 262 | // to drop the poke on the floor here as this case would mean we |
| 263 | // have enough wakeups to wake up as many goroutines as this semaphore |
| 264 | // can take, which is guaranteed to be enough. |
| 265 | } |
| 266 | } |
| 267 | return |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // updateCapacity updates the capacity of the semaphore to the desired size. |
| 273 | func (s *semaphore) updateCapacity(size int) { |