Dequeue will return the op with the highest priority; block if queue is empty; returns the zero value of T if queue is closed.
()
| 109 | // Dequeue will return the op with the highest priority; block if queue is |
| 110 | // empty; returns the zero value of T if queue is closed. |
| 111 | func (pq *PriorityQueue[T]) Dequeue() T { |
| 112 | pq.lock.Lock() |
| 113 | defer pq.lock.Unlock() |
| 114 | |
| 115 | for len(pq.queue) == 0 && !pq.closing && !pq.closed { |
| 116 | pq.cond.Wait() |
| 117 | } |
| 118 | |
| 119 | if len(pq.queue) == 0 && (pq.closing || pq.closed) { |
| 120 | pq.closed = true |
| 121 | var zero T |
| 122 | return zero |
| 123 | } |
| 124 | |
| 125 | op := heap.Pop(&pq.queue).(T) |
| 126 | delete(pq.hit, op.Key()) |
| 127 | if pq.lengthGauge != nil { |
| 128 | pq.lengthGauge.Dec() |
| 129 | } |
| 130 | return op |
| 131 | } |