pop returns first job from the queue, and true. If queue is empty, pop blocks until there is a job (returns true), or until queue is closed (returns false). If queue was already closed, pop first returns all remaining elements from the queue (with true value), and only then returns false.
()
| 103 | // If queue is empty, pop blocks until there is a job (returns true), or until queue is closed (returns false). |
| 104 | // If queue was already closed, pop first returns all remaining elements from the queue (with true value), and only then returns false. |
| 105 | func (q *writeJobQueue) pop() (chunkWriteJob, bool) { |
| 106 | q.mtx.Lock() |
| 107 | defer q.mtx.Unlock() |
| 108 | |
| 109 | // wait until something is pushed to the queue, or queue is closed. |
| 110 | for q.size == 0 { |
| 111 | if q.closed { |
| 112 | return chunkWriteJob{}, false |
| 113 | } |
| 114 | |
| 115 | q.pushed.Wait() |
| 116 | } |
| 117 | |
| 118 | res := q.first.segment[q.first.nextRead] |
| 119 | q.first.segment[q.first.nextRead] = chunkWriteJob{} // clear just-read element |
| 120 | q.first.nextRead++ |
| 121 | q.size-- |
| 122 | |
| 123 | // If we have read all possible elements from first segment, we can drop it. |
| 124 | if q.first.nextRead >= q.segmentSize { |
| 125 | q.first = q.first.nextSegment |
| 126 | if q.first == nil { |
| 127 | q.last = nil |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | q.popped.Signal() |
| 132 | return res, true |
| 133 | } |
| 134 | |
| 135 | // length returns number of all jobs in the queue. |
| 136 | func (q *writeJobQueue) length() int { |