Pop waits until an item is ready. If multiple items are ready, they are returned in the order given by Heap.data.lessFunc.
()
| 237 | // Pop waits until an item is ready. If multiple items are |
| 238 | // ready, they are returned in the order given by Heap.data.lessFunc. |
| 239 | func (h *Heap) Pop() (interface{}, error) { |
| 240 | h.lock.Lock() |
| 241 | defer h.lock.Unlock() |
| 242 | for len(h.data.queue) == 0 { |
| 243 | // When the queue is empty, invocation of Pop() is blocked until new item is enqueued. |
| 244 | // When Close() is called, the h.closed is set and the condition is broadcast, |
| 245 | // which causes this loop to continue and return from the Pop(). |
| 246 | if h.closed { |
| 247 | return nil, fmt.Errorf("heap is closed") |
| 248 | } |
| 249 | h.cond.Wait() |
| 250 | } |
| 251 | obj := heap.Pop(h.data) |
| 252 | if obj != nil { |
| 253 | return obj, nil |
| 254 | } else { |
| 255 | return nil, fmt.Errorf("object was removed from heap data") |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // List returns a list of all the items. |
| 260 | func (h *Heap) List() []interface{} { |