--- /dev/null 2015-06-17 09:49:50.124106881 +0200 +++ new/src/java.base/share/classes/java/lang/ref/DLList.java 2015-06-22 11:04:03.571380561 +0200 @@ -0,0 +1,557 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * This file is available under and governed by the GNU General Public + * License version 2 only, as published by the Free Software Foundation. + * However, the following notice accompanied the original version of this + * file: + * + * Written by Doug Lea and Martin Buchholz with assistance from members of + * JCP JSR-166 Expert Group and released to the public domain, as explained + * at http://creativecommons.org/publicdomain/zero/1.0/ + */ + +package java.lang.ref; + +/** + * A concurrent doubly-linked list of {@link java.lang.ref.Reference} nodes + * modeled by {@link java.util.concurrent.ConcurrentLinkedDeque}. + */ +final class DLList { + + /** + * A node from which the first node on list (that is, the unique node p + * with p.getPrevDll() == null && p.getNextDll() != p) can be reached in O(1) time. + * Invariants: + * - the first node is always O(1) reachable from head via prev links + * - all live nodes are reachable from the first node via succ() + * - head != null + * - (tmp = head).getNextDll() != tmp || tmp != head + * - head is never gc-unlinked (but may be unlinked) + * Non-invariants: + * - head.isDeleted() may or may not be true + * - head may not be reachable from the first or last node, or from tail + */ + private volatile Reference head; + + /** + * A node from which the last node on list (that is, the unique node p + * with p.getNextDll() == null && p.getPrevDll() != p) can be reached in O(1) time. + * Invariants: + * - the last node is always O(1) reachable from tail via next links + * - all live nodes are reachable from the last node via pred() + * - tail != null + * - tail is never gc-unlinked (but may be unlinked) + * Non-invariants: + * - tail.isDeletedDll() may or may not be true + * - tail may not be reachable from the first or last node, or from head + */ + private volatile Reference tail; + + private static final Reference PREV_TERMINATOR, NEXT_TERMINATOR; + + /** + * Links newReference as first or last element, depending on + * specified boolean flag. + */ + void link(Reference newReference, boolean first) { + if (first) { + linkFirst(newReference); + } else { + linkLast(newReference); + } + } + + /** + * Links newReference as first element. + */ + private void linkFirst(Reference newReference) { + + restartFromHead: + for (;;) + for (Reference h = head, p = h, q;;) { + if ((q = p.getPrevDll()) != null && + (q = (p = q).getPrevDll()) != null) + // Check for head updates every other hop. + // If p == q, we are sure to follow head instead. + p = (h != (h = head)) ? h : q; + else if (p.getNextDll() == p) // PREV_TERMINATOR + continue restartFromHead; + else { + // p is first node + newReference.lazySetNextDll(p); // CAS piggyback + if (p.casPrevDll(null, newReference)) { + // Successful CAS is the linearization point + // for e to become an element of this deque, + // and for newNode to become "live". + if (p != h) // hop two nodes at a time + casHead(h, newReference); // Failure is OK. + return; + } + // Lost CAS race to another thread; re-read prev + } + } + } + + /** + * Links newReference as last element. + */ + private void linkLast(Reference newReference) { + + restartFromTail: + for (;;) + for (Reference t = tail, p = t, q;;) { + if ((q = p.getNextDll()) != null && + (q = (p = q).getNextDll()) != null) + // Check for tail updates every other hop. + // If p == q, we are sure to follow tail instead. + p = (t != (t = tail)) ? t : q; + else if (p.getPrevDll() == p) // NEXT_TERMINATOR + continue restartFromTail; + else { + // p is last node + newReference.lazySetPrevDll(p); // CAS piggyback + if (p.casNextDll(null, newReference)) { + // Successful CAS is the linearization point + // for e to become an element of this deque, + // and for newNode to become "live". + if (p != t) // hop two nodes at a time + casTail(t, newReference); // Failure is OK. + return; + } + // Lost CAS race to another thread; re-read next + } + } + } + + private static final int HOPS = 2; + + /** + * Unlinks non-null node x. + */ + void unlink(Reference x) { + // assert x != null; + // assert x.isDeletedDll(); + // assert x != PREV_TERMINATOR; + // assert x != NEXT_TERMINATOR; + + final Reference prev = x.getPrevDll(); + final Reference next = x.getNextDll(); + if (prev == null) { + unlinkFirst(x, next); + } else if (next == null) { + unlinkLast(x, prev); + } else { + // Unlink interior node. + // + // This is the common case, since a series of polls at the + // same end will be "interior" removes, except perhaps for + // the first one, since end nodes cannot be unlinked. + // + // At any time, all active nodes are mutually reachable by + // following a sequence of either next or prev pointers. + // + // Our strategy is to find the unique active predecessor + // and successor of x. Try to fix up their links so that + // they point to each other, leaving x unreachable from + // active nodes. If successful, and if x has no live + // predecessor/successor, we additionally try to gc-unlink, + // leaving active nodes unreachable from x, by rechecking + // that the status of predecessor and successor are + // unchanged and ensuring that x is not reachable from + // tail/head, before setting x's prev/next links to their + // logical approximate replacements, self/TERMINATOR. + Reference activePred, activeSucc; + boolean isFirst, isLast; + int hops = 1; + + // Find active predecessor + for (Reference p = prev; ; ++hops) { + if (!p.isDeletedDll()) { + activePred = p; + isFirst = false; + break; + } + Reference q = p.getPrevDll(); + if (q == null) { + if (p.getNextDll() == p) + return; + activePred = p; + isFirst = true; + break; + } + else if (p == q) + return; + else + p = q; + } + + // Find active successor + for (Reference p = next; ; ++hops) { + if (!p.isDeletedDll()) { + activeSucc = p; + isLast = false; + break; + } + Reference q = p.getNextDll(); + if (q == null) { + if (p.getPrevDll() == p) + return; + activeSucc = p; + isLast = true; + break; + } + else if (p == q) + return; + else + p = q; + } + + // TODO: better HOP heuristics + if (hops < HOPS + // always squeeze out interior deleted nodes + && (isFirst | isLast)) + return; + + // Squeeze out deleted nodes between activePred and + // activeSucc, including x. + skipDeletedSuccessors(activePred); + skipDeletedPredecessors(activeSucc); + + // Try to gc-unlink, if possible + if ((isFirst | isLast) && + + // Recheck expected state of predecessor and successor + (activePred.getNextDll() == activeSucc) && + (activeSucc.getPrevDll() == activePred) && + (isFirst ? activePred.getPrevDll() == null : !activePred.isDeletedDll()) && + (isLast ? activeSucc.getNextDll() == null : !activeSucc.isDeletedDll())) { + + updateHead(); // Ensure x is not reachable from head + updateTail(); // Ensure x is not reachable from tail + + // Finally, actually gc-unlink + x.lazySetPrevDll(isFirst ? PREV_TERMINATOR : x); + x.lazySetNextDll(isLast ? NEXT_TERMINATOR : x); + } + } + } + + /** + * Unlinks non-null first node. + */ + private void unlinkFirst(Reference first, Reference next) { + // assert first != null; + // assert next != null; + // assert first.isDeletedDll(); + for (Reference o = null, p = next, q;;) { + if (!p.isDeletedDll() || (q = p.getNextDll()) == null) { + if (o != null && p.getPrevDll() != p && first.casNextDll(next, p)) { + skipDeletedPredecessors(p); + if (first.getPrevDll() == null && + (p.getNextDll() == null || !p.isDeletedDll()) && + p.getPrevDll() == first) { + + updateHead(); // Ensure o is not reachable from head + updateTail(); // Ensure o is not reachable from tail + + // Finally, actually gc-unlink + o.lazySetNextDll(o); + o.lazySetPrevDll(PREV_TERMINATOR); + } + } + return; + } + else if (p == q) + return; + else { + o = p; + p = q; + } + } + } + + /** + * Unlinks non-null last node. + */ + private void unlinkLast(Reference last, Reference prev) { + // assert last != null; + // assert prev != null; + // assert last.isDeletedDll(); + for (Reference o = null, p = prev, q;;) { + if (!p.isDeletedDll() || (q = p.getPrevDll()) == null) { + if (o != null && p.getNextDll() != p && last.casPrevDll(prev, p)) { + skipDeletedSuccessors(p); + if (last.getNextDll() == null && + (p.getPrevDll() == null || !p.isDeletedDll()) && + p.getNextDll() == last) { + + updateHead(); // Ensure o is not reachable from head + updateTail(); // Ensure o is not reachable from tail + + // Finally, actually gc-unlink + o.lazySetPrevDll(o); + o.lazySetNextDll(NEXT_TERMINATOR); + } + } + return; + } + else if (p == q) + return; + else { + o = p; + p = q; + } + } + } + + /** + * Guarantees that any node which was unlinked before a call to + * this method will be unreachable from head after it returns. + * Does not guarantee to eliminate slack, only that head will + * point to a node that was active while this method was running. + */ + private void updateHead() { + // Either head already points to an active node, or we keep + // trying to cas it to the first node until it does. + Reference h, p, q; + restartFromHead: + while ((h = head).isDeletedDll() && (p = h.getPrevDll()) != null) { + for (;;) { + if ((q = p.getPrevDll()) == null || + (q = (p = q).getPrevDll()) == null) { + // It is possible that p is PREV_TERMINATOR, + // but if so, the CAS is guaranteed to fail. + if (casHead(h, p)) + return; + else + continue restartFromHead; + } + else if (h != head) + continue restartFromHead; + else + p = q; + } + } + } + + /** + * Guarantees that any node which was unlinked before a call to + * this method will be unreachable from tail after it returns. + * Does not guarantee to eliminate slack, only that tail will + * point to a node that was active while this method was running. + */ + private void updateTail() { + // Either tail already points to an active node, or we keep + // trying to cas it to the last node until it does. + Reference t, p, q; + restartFromTail: + while ((t = tail).isDeletedDll() && (p = t.getNextDll()) != null) { + for (;;) { + if ((q = p.getNextDll()) == null || + (q = (p = q).getNextDll()) == null) { + // It is possible that p is NEXT_TERMINATOR, + // but if so, the CAS is guaranteed to fail. + if (casTail(t, p)) + return; + else + continue restartFromTail; + } + else if (t != tail) + continue restartFromTail; + else + p = q; + } + } + } + + private void skipDeletedPredecessors(Reference x) { + whileActive: + do { + Reference prev = x.getPrevDll(); + // assert prev != null; + // assert x != NEXT_TERMINATOR; + // assert x != PREV_TERMINATOR; + Reference p = prev; + findActive: + for (;;) { + if (!p.isDeletedDll()) + break findActive; + Reference q = p.getPrevDll(); + if (q == null) { + if (p.getNextDll() == p) + continue whileActive; + break findActive; + } + else if (p == q) + continue whileActive; + else + p = q; + } + + // found active CAS target + if (prev == p || x.casPrevDll(prev, p)) + return; + + } while (!x.isDeletedDll() || x.getNextDll() == null); + } + + private void skipDeletedSuccessors(Reference x) { + whileActive: + do { + Reference next = x.getNextDll(); + // assert next != null; + // assert x != NEXT_TERMINATOR; + // assert x != PREV_TERMINATOR; + Reference p = next; + findActive: + for (;;) { + if (!p.isDeletedDll()) + break findActive; + Reference q = p.getNextDll(); + if (q == null) { + if (p.getPrevDll() == p) + continue whileActive; + break findActive; + } + else if (p == q) + continue whileActive; + else + p = q; + } + + // found active CAS target + if (next == p || x.casNextDll(next, p)) + return; + + } while (!x.isDeletedDll() || x.getPrevDll() == null); + } + + /** + * Returns the successor of p, or the first node if p.getNextDll() has been + * linked to self, which will only be true if traversing with a + * stale pointer that is now off the list. + */ + Reference succ(Reference p) { + // TODO: should we skip deleted nodes here? + Reference q = p.getNextDll(); + return (p == q) ? first() : q; + } + + /** + * Returns the predecessor of p, or the last node if p.getPrevDll() has been + * linked to self, which will only be true if traversing with a + * stale pointer that is now off the list. + */ + Reference pred(Reference p) { + Reference q = p.getPrevDll(); + return (p == q) ? last() : q; + } + + /** + * Returns the first node, the unique node p for which: + * p.getPrevDll() == null && p.getNextDll() != p + * The returned node may or may not be logically deleted. + * Guarantees that head is set to the returned node. + */ + Reference first() { + restartFromHead: + for (;;) + for (Reference h = head, p = h, q;;) { + if ((q = p.getPrevDll()) != null && + (q = (p = q).getPrevDll()) != null) + // Check for head updates every other hop. + // If p == q, we are sure to follow head instead. + p = (h != (h = head)) ? h : q; + else if (p == h + // It is possible that p is PREV_TERMINATOR, + // but if so, the CAS is guaranteed to fail. + || casHead(h, p)) + return p; + else + continue restartFromHead; + } + } + + /** + * Returns the last node, the unique node p for which: + * p.getNextDll() == null && p.getPrevDll() != p + * The returned node may or may not be logically deleted. + * Guarantees that tail is set to the returned node. + */ + Reference last() { + restartFromTail: + for (;;) + for (Reference t = tail, p = t, q;;) { + if ((q = p.getNextDll()) != null && + (q = (p = q).getNextDll()) != null) + // Check for tail updates every other hop. + // If p == q, we are sure to follow tail instead. + p = (t != (t = tail)) ? t : q; + else if (p == t + // It is possible that p is NEXT_TERMINATOR, + // but if so, the CAS is guaranteed to fail. + || casTail(t, p)) + return p; + else + continue restartFromTail; + } + } + + /** + * Constructs an empty list. + */ + DLList() { + head = tail = new Finalizer<>(false, false); + } + + // Unsafe mechanics + + private boolean casHead(Reference cmp, Reference val) { + return UNSAFE.compareAndSwapObject(this, HEAD, cmp, val); + } + + private boolean casTail(Reference cmp, Reference val) { + return UNSAFE.compareAndSwapObject(this, TAIL, cmp, val); + } + + private static final sun.misc.Unsafe UNSAFE; + private static final long HEAD; + private static final long TAIL; + static { + PREV_TERMINATOR = new Finalizer<>(false, true); + NEXT_TERMINATOR = new Finalizer<>(true, false); + try { + UNSAFE = sun.misc.Unsafe.getUnsafe(); + Class flc = DLList.class; + HEAD = UNSAFE.objectFieldOffset + (flc.getDeclaredField("head")); + TAIL = UNSAFE.objectFieldOffset + (flc.getDeclaredField("tail")); + } catch (Exception e) { + throw new Error(e); + } + } +}