WaitFor waits for the data to be ready at the given offset. nil means ok. It guarantees that the chunk at the given offset is ready to be read.
(off int64)
| 127 | // WaitFor waits for the data to be ready at the given offset. nil means ok. |
| 128 | // It guarantees that the chunk at the given offset is ready to be read. |
| 129 | func (ab *AsyncBuffer) WaitFor(off int64) error { |
| 130 | // Get initial cursor before first check |
| 131 | cursor := ab.chunkCond.Cursor() |
| 132 | |
| 133 | // In case we are trying to read data which would potentially hit the pause threshold, |
| 134 | // we need to unpause the reader ASAP. |
| 135 | if off >= PauseThreshold { |
| 136 | ab.paused.Release() |
| 137 | } |
| 138 | |
| 139 | for { |
| 140 | ok, err := ab.offsetAvailable(off) |
| 141 | if ok || err != nil { |
| 142 | return err |
| 143 | } |
| 144 | |
| 145 | // Wait for a Tick() to occur after our cursor. |
| 146 | // If a Tick() happened between offsetAvailable() and here, Wait() returns immediately. |
| 147 | // This prevents the deadlock where we miss a signal. |
| 148 | cursor = ab.chunkCond.Wait(cursor) |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // Wait waits for the reader to finish reading all data and returns |
| 153 | // the total length of the data read. |