/* * 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.Finalizer} nodes * modeled by {@link java.util.concurrent.ConcurrentLinkedDeque}. */ final class FinalizerList { /** * A node from which the first node on list (that is, the unique node p * with p.prev == null && p.next != 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).next != tmp || tmp != head * - head is never gc-unlinked (but may be unlinked) * Non-invariants: * - head.item may or may not be null * - head may not be reachable from the first or last node, or from tail */ private volatile Finalizer head; /** * A node from which the last node on list (that is, the unique node p * with p.next == null && p.prev != 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.item may or may not be null * - tail may not be reachable from the first or last node, or from head */ private volatile Finalizer tail; private static final Finalizer PREV_TERMINATOR, NEXT_TERMINATOR; /** * Links newFinalizer as first or last element, depending on * specified boolean flag. */ void link(Finalizer newFinalizer, boolean first) { if (first) { linkFirst(newFinalizer); } else { linkLast(newFinalizer); } } /** * Links newFinalizer as first element. */ private void linkFirst(Finalizer newFinalizer) { restartFromHead: for (;;) for (Finalizer h = head, p = h, q;;) { if ((q = p.prev) != null && (q = (p = q).prev) != 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.next == p) // PREV_TERMINATOR continue restartFromHead; else { // p is first node newFinalizer.lazySetNext(p); // CAS piggyback if (p.casPrev(null, newFinalizer)) { // 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, newFinalizer); // Failure is OK. return; } // Lost CAS race to another thread; re-read prev } } } /** * Links newNode as last element. */ private void linkLast(Finalizer newFinalizer) { restartFromTail: for (;;) for (Finalizer t = tail, p = t, q;;) { if ((q = p.next) != null && (q = (p = q).next) != 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.prev == p) // NEXT_TERMINATOR continue restartFromTail; else { // p is last node newFinalizer.lazySetPrev(p); // CAS piggyback if (p.casNext(null, newFinalizer)) { // 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, newFinalizer); // 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(Finalizer x) { // assert x != null; // assert x.item == null; // assert x != PREV_TERMINATOR; // assert x != NEXT_TERMINATOR; final Finalizer prev = x.prev; final Finalizer next = x.next; 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. Finalizer activePred, activeSucc; boolean isFirst, isLast; int hops = 1; // Find active predecessor for (Finalizer p = prev; ; ++hops) { if (p.isAlive()) { activePred = p; isFirst = false; break; } Finalizer q = p.prev; if (q == null) { if (p.next == p) return; activePred = p; isFirst = true; break; } else if (p == q) return; else p = q; } // Find active successor for (Finalizer p = next; ; ++hops) { if (p.isAlive()) { activeSucc = p; isLast = false; break; } Finalizer q = p.next; if (q == null) { if (p.prev == 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.next == activeSucc) && (activeSucc.prev == activePred) && (isFirst ? activePred.prev == null : activePred.isAlive()) && (isLast ? activeSucc.next == null : activeSucc.isAlive())) { updateHead(); // Ensure x is not reachable from head updateTail(); // Ensure x is not reachable from tail // Finally, actually gc-unlink x.lazySetPrev(isFirst ? PREV_TERMINATOR : x); x.lazySetNext(isLast ? NEXT_TERMINATOR : x); } } } /** * Unlinks non-null first node. */ private void unlinkFirst(Finalizer first, Finalizer next) { // assert first != null; // assert next != null; // assert first.item == null; for (Finalizer o = null, p = next, q;;) { if (p.isAlive() || (q = p.next) == null) { if (o != null && p.prev != p && first.casNext(next, p)) { skipDeletedPredecessors(p); if (first.prev == null && (p.next == null || p.isAlive()) && p.prev == first) { updateHead(); // Ensure o is not reachable from head updateTail(); // Ensure o is not reachable from tail // Finally, actually gc-unlink o.lazySetNext(o); o.lazySetPrev(PREV_TERMINATOR); } } return; } else if (p == q) return; else { o = p; p = q; } } } /** * Unlinks non-null last node. */ private void unlinkLast(Finalizer last, Finalizer prev) { // assert last != null; // assert prev != null; // assert last.item == null; for (Finalizer o = null, p = prev, q;;) { if (p.isAlive() || (q = p.prev) == null) { if (o != null && p.next != p && last.casPrev(prev, p)) { skipDeletedSuccessors(p); if (last.next == null && (p.prev == null || p.isAlive()) && p.next == last) { updateHead(); // Ensure o is not reachable from head updateTail(); // Ensure o is not reachable from tail // Finally, actually gc-unlink o.lazySetPrev(o); o.lazySetNext(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. Finalizer h, p, q; restartFromHead: while ((h = head).isDeleted() && (p = h.prev) != null) { for (;;) { if ((q = p.prev) == null || (q = (p = q).prev) == 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. Finalizer t, p, q; restartFromTail: while ((t = tail).isDeleted() && (p = t.next) != null) { for (;;) { if ((q = p.next) == null || (q = (p = q).next) == 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(Finalizer x) { whileActive: do { Finalizer prev = x.prev; // assert prev != null; // assert x != NEXT_TERMINATOR; // assert x != PREV_TERMINATOR; Finalizer p = prev; findActive: for (;;) { if (p.isAlive()) break findActive; Finalizer q = p.prev; if (q == null) { if (p.next == p) continue whileActive; break findActive; } else if (p == q) continue whileActive; else p = q; } // found active CAS target if (prev == p || x.casPrev(prev, p)) return; } while (x.isAlive() || x.next == null); } private void skipDeletedSuccessors(Finalizer x) { whileActive: do { Finalizer next = x.next; // assert next != null; // assert x != NEXT_TERMINATOR; // assert x != PREV_TERMINATOR; Finalizer p = next; findActive: for (;;) { if (p.isAlive()) break findActive; Finalizer q = p.next; if (q == null) { if (p.prev == p) continue whileActive; break findActive; } else if (p == q) continue whileActive; else p = q; } // found active CAS target if (next == p || x.casNext(next, p)) return; } while (x.isAlive() || x.prev == null); } /** * Returns the successor of p, or the first node if p.next has been * linked to self, which will only be true if traversing with a * stale pointer that is now off the list. */ Finalizer succ(Finalizer p) { // TODO: should we skip deleted nodes here? Finalizer q = p.next; return (p == q) ? first() : q; } /** * Returns the predecessor of p, or the last node if p.prev has been * linked to self, which will only be true if traversing with a * stale pointer that is now off the list. */ Finalizer pred(Finalizer p) { Finalizer q = p.prev; return (p == q) ? last() : q; } /** * Returns the first node, the unique node p for which: * p.prev == null && p.next != p * The returned node may or may not be logically deleted. * Guarantees that head is set to the returned node. */ Finalizer first() { restartFromHead: for (;;) for (Finalizer h = head, p = h, q;;) { if ((q = p.prev) != null && (q = (p = q).prev) != 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.next == null && p.prev != p * The returned node may or may not be logically deleted. * Guarantees that tail is set to the returned node. */ Finalizer last() { restartFromTail: for (;;) for (Finalizer t = tail, p = t, q;;) { if ((q = p.next) != null && (q = (p = q).next) != 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. */ FinalizerList() { head = tail = new Finalizer(); } // Unsafe mechanics private boolean casHead(Finalizer cmp, Finalizer val) { return UNSAFE.compareAndSwapObject(this, HEAD, cmp, val); } private boolean casTail(Finalizer cmp, Finalizer 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(); PREV_TERMINATOR.next = PREV_TERMINATOR; NEXT_TERMINATOR = new Finalizer(); NEXT_TERMINATOR.prev = NEXT_TERMINATOR; try { UNSAFE = sun.misc.Unsafe.getUnsafe(); Class flc = FinalizerList.class; HEAD = UNSAFE.objectFieldOffset (flc.getDeclaredField("head")); TAIL = UNSAFE.objectFieldOffset (flc.getDeclaredField("tail")); } catch (Exception e) { throw new Error(e); } } }