public E poll() { restartFromHead: for (; ; ) { for (Node<E> h = head, p = h, q; ; ) { E item = p.item; if (item != null && p.casItem(item, null)) { // Successful CAS is the linearization point // for item to be removed from this queue. if (p != h) // hop two nodes at a time updateHead(h, ((q = p.next) != null) ? q : p); return item; } else if ((q = p.next) == null) { updateHead(h, p); return null; } else if (p == q) continue restartFromHead; else p = q; } } }
/** * Returns the first live (non-deleted) node on list, or null if none. This is yet another variant * of poll/peek; here returning the first node, not element. We could make peek() a wrapper around * first(), but that would cost an extra volatile read of item, and the need to add a retry loop * to deal with the possibility of losing a race to a concurrent poll(). */ Node<E> first() { restartFromHead: for (; ; ) { for (Node<E> h = head, p = h, q; ; ) { boolean hasItem = (p.item != null); if (hasItem || (q = p.next) == null) { updateHead(h, p); return hasItem ? p : null; } else if (p == q) continue restartFromHead; else p = q; } } }
public E peek() { restartFromHead: for (; ; ) { for (Node<E> h = head, p = h, q; ; ) { E item = p.item; if (item != null || (q = p.next) == null) { updateHead(h, p); return item; } else if (p == q) continue restartFromHead; else p = q; } } }