changeset 12437:40d80eabd765

8166507: ConcurrentSkipListSet.clear() can leave the Set in an invalid state Reviewed-by: martin, smarks, psandoz
author dl
date Tue, 30 May 2017 16:27:58 -0700
parents 27e6134970ab
children 05cae03b99b2
files src/share/classes/java/util/concurrent/ConcurrentSkipListMap.java
diffstat 1 files changed, 18 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/concurrent/ConcurrentSkipListMap.java	Thu Jun 01 15:45:33 2017 +0100
+++ b/src/share/classes/java/util/concurrent/ConcurrentSkipListMap.java	Tue May 30 16:27:58 2017 -0700
@@ -1655,7 +1655,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;
+        }
     }
 
     /**