700 |
|
* stale pointer that is now off the list. |
701 |
|
*/ |
702 |
|
final Node<E> pred(Node<E> p) { |
703 |
< |
Node<E> q = p.prev; |
704 |
< |
return (p == q) ? last() : q; |
703 |
> |
if (p == (p = p.prev)) |
704 |
> |
p = last(); |
705 |
> |
return p; |
706 |
|
} |
707 |
|
|
708 |
|
/** |
872 |
|
} |
873 |
|
|
874 |
|
public E peekFirst() { |
875 |
< |
for (Node<E> p = first(); p != null; p = succ(p)) { |
876 |
< |
E item = p.item; |
877 |
< |
if (item != null) |
878 |
< |
return item; |
875 |
> |
restart: for (;;) { |
876 |
> |
E item; |
877 |
> |
Node<E> first = first(), p = first; |
878 |
> |
while ((item = p.item) == null) { |
879 |
> |
if (p == (p = p.next)) continue restart; |
880 |
> |
if (p == null) |
881 |
> |
break; |
882 |
> |
} |
883 |
> |
// recheck for linearizability |
884 |
> |
if (first.prev != null) continue restart; |
885 |
> |
return item; |
886 |
|
} |
879 |
– |
return null; |
887 |
|
} |
888 |
|
|
889 |
|
public E peekLast() { |
890 |
< |
for (Node<E> p = last(); p != null; p = pred(p)) { |
891 |
< |
E item = p.item; |
892 |
< |
if (item != null) |
893 |
< |
return item; |
890 |
> |
restart: for (;;) { |
891 |
> |
E item; |
892 |
> |
Node<E> last = last(), p = last; |
893 |
> |
while ((item = p.item) == null) { |
894 |
> |
if (p == (p = p.prev)) continue restart; |
895 |
> |
if (p == null) |
896 |
> |
break; |
897 |
> |
} |
898 |
> |
// recheck for linearizability |
899 |
> |
if (last.next != null) continue restart; |
900 |
> |
return item; |
901 |
|
} |
888 |
– |
return null; |
902 |
|
} |
903 |
|
|
904 |
|
/** |
916 |
|
} |
917 |
|
|
918 |
|
public E pollFirst() { |
919 |
< |
for (Node<E> p = first(); p != null; p = succ(p)) { |
920 |
< |
E item = p.item; |
921 |
< |
if (item != null && p.casItem(item, null)) { |
922 |
< |
unlink(p); |
923 |
< |
return item; |
919 |
> |
restart: for (;;) { |
920 |
> |
for (Node<E> first = first(), p = first;;) { |
921 |
> |
final E item; |
922 |
> |
if ((item = p.item) != null) { |
923 |
> |
// recheck for linearizability |
924 |
> |
if (first.prev != null) continue restart; |
925 |
> |
if (p.casItem(item, null)) { |
926 |
> |
unlink(p); |
927 |
> |
return item; |
928 |
> |
} |
929 |
> |
} |
930 |
> |
if (p == (p = p.next)) continue restart; |
931 |
> |
if (p == null) { |
932 |
> |
if (first.prev != null) continue restart; |
933 |
> |
return null; |
934 |
> |
} |
935 |
|
} |
936 |
|
} |
913 |
– |
return null; |
937 |
|
} |
938 |
|
|
939 |
|
public E pollLast() { |
940 |
< |
for (Node<E> p = last(); p != null; p = pred(p)) { |
941 |
< |
E item = p.item; |
942 |
< |
if (item != null && p.casItem(item, null)) { |
943 |
< |
unlink(p); |
944 |
< |
return item; |
940 |
> |
restart: for (;;) { |
941 |
> |
for (Node<E> last = last(), p = last;;) { |
942 |
> |
final E item; |
943 |
> |
if ((item = p.item) != null) { |
944 |
> |
// recheck for linearizability |
945 |
> |
if (last.next != null) continue restart; |
946 |
> |
if (p.casItem(item, null)) { |
947 |
> |
unlink(p); |
948 |
> |
return item; |
949 |
> |
} |
950 |
> |
} |
951 |
> |
if (p == (p = p.prev)) continue restart; |
952 |
> |
if (p == null) { |
953 |
> |
if (last.next != null) continue restart; |
954 |
> |
return null; |
955 |
> |
} |
956 |
|
} |
957 |
|
} |
924 |
– |
return null; |
958 |
|
} |
959 |
|
|
960 |
|
/** |