8166507: ConcurrentSkipListSet.clear() can leave the Set in an invalid state
authordl
Mon, 28 Nov 2016 23:39:54 -0800
changeset 42320 bfc781c5078f
parent 42319 0193886267c3
child 42321 a0abf857aaec
8166507: ConcurrentSkipListSet.clear() can leave the Set in an invalid state Reviewed-by: martin, smarks, psandoz
jdk/src/java.base/share/classes/java/util/concurrent/ConcurrentSkipListMap.java
--- a/jdk/src/java.base/share/classes/java/util/concurrent/ConcurrentSkipListMap.java	Mon Nov 28 23:36:11 2016 -0800
+++ b/jdk/src/java.base/share/classes/java/util/concurrent/ConcurrentSkipListMap.java	Mon Nov 28 23:39:54 2016 -0800
@@ -1650,7 +1650,24 @@
      * Removes all of the mappings from this map.
      */
     public void clear() {
-        initialize();
+        for (;;) {
+            Node<K,V> b, n;
+            HeadIndex<K,V> h = head, d = (HeadIndex<K,V>)h.down;
+            if (d != null)
+                casHead(h, d);            // remove levels
+            else if ((b = h.node) != null && (n = b.next) != null) {
+                Node<K,V> f = n.next;     // remove values
+                if (n == b.next) {
+                    Object v = n.value;
+                    if (v == null)
+                        n.helpDelete(b, f);
+                    else if (n.casValue(v, null) && n.appendMarker(f))
+                        b.casNext(n, f);
+                }
+            }
+            else
+                break;
+        }
     }
 
     /**