src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java
author darcy
Wed, 16 Oct 2019 16:55:52 -0700
changeset 58657 6252605fb005
parent 58138 1e4270f875ee
child 58679 9c3209ff7550
child 58892 35bac2745d04
permissions -rw-r--r--
8232230: Suppress warnings on non-serializable non-transient instance fields in java.util.concurrent Reviewed-by: martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 7518
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util.concurrent;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
    37
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
    38
import java.io.ObjectStreamField;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.Serializable;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    40
import java.lang.reflect.ParameterizedType;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    41
import java.lang.reflect.Type;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    42
import java.util.AbstractMap;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    43
import java.util.Arrays;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    44
import java.util.Collection;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    45
import java.util.Enumeration;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    46
import java.util.HashMap;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    47
import java.util.Hashtable;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    48
import java.util.Iterator;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    49
import java.util.Map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    50
import java.util.NoSuchElementException;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    51
import java.util.Set;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    52
import java.util.Spliterator;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    53
import java.util.concurrent.atomic.AtomicReference;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
    54
import java.util.concurrent.locks.LockSupport;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    55
import java.util.concurrent.locks.ReentrantLock;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    56
import java.util.function.BiConsumer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    57
import java.util.function.BiFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    58
import java.util.function.Consumer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    59
import java.util.function.DoubleBinaryOperator;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    60
import java.util.function.Function;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    61
import java.util.function.IntBinaryOperator;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    62
import java.util.function.LongBinaryOperator;
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
    63
import java.util.function.Predicate;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    64
import java.util.function.ToDoubleBiFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    65
import java.util.function.ToDoubleFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    66
import java.util.function.ToIntBiFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    67
import java.util.function.ToIntFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    68
import java.util.function.ToLongBiFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    69
import java.util.function.ToLongFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    70
import java.util.stream.Stream;
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
    71
import jdk.internal.misc.Unsafe;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * A hash table supporting full concurrency of retrievals and
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    75
 * high expected concurrency for updates. This class obeys the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * same functional specification as {@link java.util.Hashtable}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * includes versions of methods corresponding to each method of
17717
fe0b28a1a3bd 8015439: Minor/sync/cleanup of ConcurrentHashMap
chegar
parents: 16883
diff changeset
    78
 * {@code Hashtable}. However, even though all operations are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * thread-safe, retrieval operations do <em>not</em> entail locking,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * and there is <em>not</em> any support for locking the entire table
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * in a way that prevents all access.  This class is fully
17717
fe0b28a1a3bd 8015439: Minor/sync/cleanup of ConcurrentHashMap
chegar
parents: 16883
diff changeset
    82
 * interoperable with {@code Hashtable} in programs that rely on its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * thread safety but not on its synchronization details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    85
 * <p>Retrieval operations (including {@code get}) generally do not
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    86
 * block, so may overlap with update operations (including {@code put}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    87
 * and {@code remove}). Retrievals reflect the results of the most
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    88
 * recently <em>completed</em> update operations holding upon their
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    89
 * onset. (More formally, an update operation for a given key bears a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    90
 * <em>happens-before</em> relation with any (non-null) retrieval for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    91
 * that key reporting the updated value.)  For aggregate operations
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    92
 * such as {@code putAll} and {@code clear}, concurrent retrievals may
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    93
 * reflect insertion or removal of only some entries.  Similarly,
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
    94
 * Iterators, Spliterators and Enumerations return elements reflecting the
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
    95
 * state of the hash table at some point at or since the creation of the
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    96
 * iterator/enumeration.  They do <em>not</em> throw {@link
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
    97
 * java.util.ConcurrentModificationException ConcurrentModificationException}.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
    98
 * However, iterators are designed to be used by only one thread at a time.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
    99
 * Bear in mind that the results of aggregate status methods including
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
   100
 * {@code size}, {@code isEmpty}, and {@code containsValue} are typically
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
   101
 * useful only when a map is not undergoing concurrent updates in other threads.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   102
 * Otherwise the results of these methods reflect transient states
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   103
 * that may be adequate for monitoring or estimation purposes, but not
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   104
 * for program control.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   106
 * <p>The table is dynamically expanded when there are too many
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   107
 * collisions (i.e., keys that have distinct hash codes but fall into
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   108
 * the same slot modulo the table size), with the expected average
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   109
 * effect of maintaining roughly two bins per mapping (corresponding
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   110
 * to a 0.75 load factor threshold for resizing). There may be much
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   111
 * variance around this average as mappings are added and removed, but
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   112
 * overall, this maintains a commonly accepted time/space tradeoff for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   113
 * hash tables.  However, resizing this or any other kind of hash
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   114
 * table may be a relatively slow operation. When possible, it is a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   115
 * good idea to provide a size estimate as an optional {@code
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   116
 * initialCapacity} constructor argument. An additional optional
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   117
 * {@code loadFactor} constructor argument provides a further means of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   118
 * customizing initial table capacity by specifying the table density
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   119
 * to be used in calculating the amount of space to allocate for the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   120
 * given number of elements.  Also, for compatibility with previous
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   121
 * versions of this class, constructors may optionally specify an
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   122
 * expected {@code concurrencyLevel} as an additional hint for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   123
 * internal sizing.  Note that using many keys with exactly the same
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   124
 * {@code hashCode()} is a sure way to slow down performance of any
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   125
 * hash table. To ameliorate impact, when keys are {@link Comparable},
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   126
 * this class may use comparison order among keys to help break ties.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   127
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   128
 * <p>A {@link Set} projection of a ConcurrentHashMap may be created
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   129
 * (using {@link #newKeySet()} or {@link #newKeySet(int)}), or viewed
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   130
 * (using {@link #keySet(Object)} when only keys are of interest, and the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   131
 * mapped values are (perhaps transiently) not used or all take the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   132
 * same mapping value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   133
 *
24972
d3304c4e66b5 8044730: small errors in ConcurrentHashMap and LongAdder docs
smarks
parents: 21981
diff changeset
   134
 * <p>A ConcurrentHashMap can be used as a scalable frequency map (a
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   135
 * form of histogram or multiset) by using {@link
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   136
 * java.util.concurrent.atomic.LongAdder} values and initializing via
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   137
 * {@link #computeIfAbsent computeIfAbsent}. For example, to add a count
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   138
 * to a {@code ConcurrentHashMap<String,LongAdder> freqs}, you can use
24972
d3304c4e66b5 8044730: small errors in ConcurrentHashMap and LongAdder docs
smarks
parents: 21981
diff changeset
   139
 * {@code freqs.computeIfAbsent(key, k -> new LongAdder()).increment();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * <p>This class and its views and iterators implement all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * <em>optional</em> methods of the {@link Map} and {@link Iterator}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *
17717
fe0b28a1a3bd 8015439: Minor/sync/cleanup of ConcurrentHashMap
chegar
parents: 16883
diff changeset
   145
 * <p>Like {@link Hashtable} but unlike {@link HashMap}, this class
fe0b28a1a3bd 8015439: Minor/sync/cleanup of ConcurrentHashMap
chegar
parents: 16883
diff changeset
   146
 * does <em>not</em> allow {@code null} to be used as a key or value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 *
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   148
 * <p>ConcurrentHashMaps support a set of sequential and parallel bulk
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   149
 * operations that, unlike most {@link Stream} methods, are designed
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   150
 * to be safely, and often sensibly, applied even with maps that are
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   151
 * being concurrently updated by other threads; for example, when
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   152
 * computing a snapshot summary of the values in a shared registry.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   153
 * There are three kinds of operation, each with four forms, accepting
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   154
 * functions with keys, values, entries, and (key, value) pairs as
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   155
 * arguments and/or return values. Because the elements of a
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   156
 * ConcurrentHashMap are not ordered in any particular way, and may be
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   157
 * processed in different orders in different parallel executions, the
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   158
 * correctness of supplied functions should not depend on any
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   159
 * ordering, or on any other objects or values that may transiently
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   160
 * change while computation is in progress; and except for forEach
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   161
 * actions, should ideally be side-effect-free. Bulk operations on
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 45563
diff changeset
   162
 * {@link Map.Entry} objects do not support method {@code setValue}.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   163
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   164
 * <ul>
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   165
 * <li>forEach: Performs a given action on each element.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   166
 * A variant form applies a given transformation on each element
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   167
 * before performing the action.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   168
 *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   169
 * <li>search: Returns the first available non-null result of
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   170
 * applying a given function on each element; skipping further
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   171
 * search when a result is found.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   172
 *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   173
 * <li>reduce: Accumulates each element.  The supplied reduction
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   174
 * function cannot rely on ordering (more formally, it should be
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   175
 * both associative and commutative).  There are five variants:
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   176
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   177
 * <ul>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   178
 *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   179
 * <li>Plain reductions. (There is not a form of this method for
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   180
 * (key, value) function arguments since there is no corresponding
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   181
 * return type.)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   182
 *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   183
 * <li>Mapped reductions that accumulate the results of a given
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   184
 * function applied to each element.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   185
 *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   186
 * <li>Reductions to scalar doubles, longs, and ints, using a
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   187
 * given basis value.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   188
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   189
 * </ul>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   190
 * </ul>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   191
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   192
 * <p>These bulk operations accept a {@code parallelismThreshold}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   193
 * argument. Methods proceed sequentially if the current map size is
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   194
 * estimated to be less than the given threshold. Using a value of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   195
 * {@code Long.MAX_VALUE} suppresses all parallelism.  Using a value
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   196
 * of {@code 1} results in maximal parallelism by partitioning into
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   197
 * enough subtasks to fully utilize the {@link
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   198
 * ForkJoinPool#commonPool()} that is used for all parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   199
 * computations. Normally, you would initially choose one of these
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   200
 * extreme values, and then measure performance of using in-between
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   201
 * values that trade off overhead versus throughput.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   202
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   203
 * <p>The concurrency properties of bulk operations follow
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   204
 * from those of ConcurrentHashMap: Any non-null result returned
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   205
 * from {@code get(key)} and related access methods bears a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   206
 * happens-before relation with the associated insertion or
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   207
 * update.  The result of any bulk operation reflects the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   208
 * composition of these per-element relations (but is not
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   209
 * necessarily atomic with respect to the map as a whole unless it
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   210
 * is somehow known to be quiescent).  Conversely, because keys
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   211
 * and values in the map are never null, null serves as a reliable
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   212
 * atomic indicator of the current lack of any result.  To
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   213
 * maintain this property, null serves as an implicit basis for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   214
 * all non-scalar reduction operations. For the double, long, and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   215
 * int versions, the basis should be one that, when combined with
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   216
 * any other value, returns that other value (more formally, it
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   217
 * should be the identity element for the reduction). Most common
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   218
 * reductions have these properties; for example, computing a sum
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   219
 * with basis 0 or a minimum with basis MAX_VALUE.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   220
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   221
 * <p>Search and transformation functions provided as arguments
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   222
 * should similarly return null to indicate the lack of any result
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   223
 * (in which case it is not used). In the case of mapped
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   224
 * reductions, this also enables transformations to serve as
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   225
 * filters, returning null (or, in the case of primitive
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   226
 * specializations, the identity basis) if the element should not
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   227
 * be combined. You can create compound transformations and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   228
 * filterings by composing them yourself under this "null means
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   229
 * there is nothing there now" rule before using them in search or
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   230
 * reduce operations.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   231
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   232
 * <p>Methods accepting and/or returning Entry arguments maintain
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   233
 * key-value associations. They may be useful for example when
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   234
 * finding the key for the greatest value. Note that "plain" Entry
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   235
 * arguments can be supplied using {@code new
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   236
 * AbstractMap.SimpleEntry(k,v)}.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   237
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   238
 * <p>Bulk operations may complete abruptly, throwing an
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   239
 * exception encountered in the application of a supplied
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   240
 * function. Bear in mind when handling such exceptions that other
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   241
 * concurrently executing functions could also have thrown
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   242
 * exceptions, or would have done so if the first exception had
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   243
 * not occurred.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   244
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   245
 * <p>Speedups for parallel compared to sequential forms are common
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   246
 * but not guaranteed.  Parallel operations involving brief functions
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   247
 * on small maps may execute more slowly than sequential forms if the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   248
 * underlying work to parallelize the computation is more expensive
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   249
 * than the computation itself.  Similarly, parallelization may not
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   250
 * lead to much actual parallelism if all processors are busy
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   251
 * performing unrelated tasks.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   252
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   253
 * <p>All arguments to all task methods must be non-null.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   254
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 * <p>This class is a member of the
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 48541
diff changeset
   256
 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 * @param <K> the type of keys maintained by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 * @param <V> the type of mapped values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 */
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   264
public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   265
    implements ConcurrentMap<K,V>, Serializable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    private static final long serialVersionUID = 7249069246763182397L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /*
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   269
     * Overview:
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   270
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   271
     * The primary design goal of this hash table is to maintain
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   272
     * concurrent readability (typically method get(), but also
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   273
     * iterators and related methods) while minimizing update
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   274
     * contention. Secondary goals are to keep space consumption about
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   275
     * the same or better than java.util.HashMap, and to support high
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   276
     * initial insertion rates on an empty table by many threads.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   277
     *
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   278
     * This map usually acts as a binned (bucketed) hash table.  Each
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   279
     * key-value mapping is held in a Node.  Most nodes are instances
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   280
     * of the basic Node class with hash, key, value, and next
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   281
     * fields. However, various subclasses exist: TreeNodes are
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   282
     * arranged in balanced trees, not lists.  TreeBins hold the roots
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   283
     * of sets of TreeNodes. ForwardingNodes are placed at the heads
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   284
     * of bins during resizing. ReservationNodes are used as
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   285
     * placeholders while establishing values in computeIfAbsent and
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   286
     * related methods.  The types TreeBin, ForwardingNode, and
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   287
     * ReservationNode do not hold normal user keys, values, or
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   288
     * hashes, and are readily distinguishable during search etc
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   289
     * because they have negative hash fields and null key and value
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   290
     * fields. (These special nodes are either uncommon or transient,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   291
     * so the impact of carrying around some unused fields is
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   292
     * insignificant.)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   293
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   294
     * The table is lazily initialized to a power-of-two size upon the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   295
     * first insertion.  Each bin in the table normally contains a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   296
     * list of Nodes (most often, the list has only zero or one Node).
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   297
     * Table accesses require volatile/atomic reads, writes, and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   298
     * CASes.  Because there is no other way to arrange this without
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   299
     * adding further indirections, we use intrinsics
33674
566777f73c32 8140606: Update library code to use internal Unsafe
chegar
parents: 32991
diff changeset
   300
     * (jdk.internal.misc.Unsafe) operations.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   301
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   302
     * We use the top (sign) bit of Node hash fields for control
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   303
     * purposes -- it is available anyway because of addressing
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   304
     * constraints.  Nodes with negative hash fields are specially
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   305
     * handled or ignored in map methods.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   306
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   307
     * Insertion (via put or its variants) of the first node in an
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   308
     * empty bin is performed by just CASing it to the bin.  This is
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   309
     * by far the most common case for put operations under most
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   310
     * key/hash distributions.  Other update operations (insert,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   311
     * delete, and replace) require locks.  We do not want to waste
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   312
     * the space required to associate a distinct lock object with
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   313
     * each bin, so instead use the first node of a bin list itself as
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   314
     * a lock. Locking support for these locks relies on builtin
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   315
     * "synchronized" monitors.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   316
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   317
     * Using the first node of a list as a lock does not by itself
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   318
     * suffice though: When a node is locked, any update must first
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   319
     * validate that it is still the first node after locking it, and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   320
     * retry if not. Because new nodes are always appended to lists,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   321
     * once a node is first in a bin, it remains first until deleted
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   322
     * or the bin becomes invalidated (upon resizing).
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   323
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   324
     * The main disadvantage of per-bin locks is that other update
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   325
     * operations on other nodes in a bin list protected by the same
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   326
     * lock can stall, for example when user equals() or mapping
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   327
     * functions take a long time.  However, statistically, under
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   328
     * random hash codes, this is not a common problem.  Ideally, the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   329
     * frequency of nodes in bins follows a Poisson distribution
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   330
     * (http://en.wikipedia.org/wiki/Poisson_distribution) with a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   331
     * parameter of about 0.5 on average, given the resizing threshold
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   332
     * of 0.75, although with a large variance because of resizing
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   333
     * granularity. Ignoring variance, the expected occurrences of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   334
     * list size k are (exp(-0.5) * pow(0.5, k) / factorial(k)). The
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   335
     * first values are:
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   336
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   337
     * 0:    0.60653066
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   338
     * 1:    0.30326533
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   339
     * 2:    0.07581633
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   340
     * 3:    0.01263606
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   341
     * 4:    0.00157952
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   342
     * 5:    0.00015795
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   343
     * 6:    0.00001316
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   344
     * 7:    0.00000094
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   345
     * 8:    0.00000006
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   346
     * more: less than 1 in ten million
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   347
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   348
     * Lock contention probability for two threads accessing distinct
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   349
     * elements is roughly 1 / (8 * #elements) under random hashes.
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
   350
     *
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   351
     * Actual hash code distributions encountered in practice
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   352
     * sometimes deviate significantly from uniform randomness.  This
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   353
     * includes the case when N > (1<<30), so some keys MUST collide.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   354
     * Similarly for dumb or hostile usages in which multiple keys are
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   355
     * designed to have identical hash codes or ones that differs only
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   356
     * in masked-out high bits. So we use a secondary strategy that
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   357
     * applies when the number of nodes in a bin exceeds a
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   358
     * threshold. These TreeBins use a balanced tree to hold nodes (a
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   359
     * specialized form of red-black trees), bounding search time to
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   360
     * O(log N).  Each search step in a TreeBin is at least twice as
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   361
     * slow as in a regular list, but given that N cannot exceed
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   362
     * (1<<64) (before running out of addresses) this bounds search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   363
     * steps, lock hold times, etc, to reasonable constants (roughly
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   364
     * 100 nodes inspected per operation worst case) so long as keys
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   365
     * are Comparable (which is very common -- String, Long, etc).
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   366
     * TreeBin nodes (TreeNodes) also maintain the same "next"
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   367
     * traversal pointers as regular nodes, so can be traversed in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   368
     * iterators in the same way.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   369
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   370
     * The table is resized when occupancy exceeds a percentage
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   371
     * threshold (nominally, 0.75, but see below).  Any thread
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   372
     * noticing an overfull bin may assist in resizing after the
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   373
     * initiating thread allocates and sets up the replacement array.
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   374
     * However, rather than stalling, these other threads may proceed
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   375
     * with insertions etc.  The use of TreeBins shields us from the
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   376
     * worst case effects of overfilling while resizes are in
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   377
     * progress.  Resizing proceeds by transferring bins, one by one,
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   378
     * from the table to the next table. However, threads claim small
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   379
     * blocks of indices to transfer (via field transferIndex) before
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   380
     * doing so, reducing contention.  A generation stamp in field
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   381
     * sizeCtl ensures that resizings do not overlap. Because we are
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   382
     * using power-of-two expansion, the elements from each bin must
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   383
     * either stay at same index, or move with a power of two
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   384
     * offset. We eliminate unnecessary node creation by catching
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   385
     * cases where old nodes can be reused because their next fields
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   386
     * won't change.  On average, only about one-sixth of them need
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   387
     * cloning when a table doubles. The nodes they replace will be
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 52958
diff changeset
   388
     * garbage collectible as soon as they are no longer referenced by
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   389
     * any reader thread that may be in the midst of concurrently
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   390
     * traversing table.  Upon transfer, the old table bin contains
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   391
     * only a special forwarding node (with hash field "MOVED") that
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   392
     * contains the next table as its key. On encountering a
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   393
     * forwarding node, access and update operations restart, using
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   394
     * the new table.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   395
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   396
     * Each bin transfer requires its bin lock, which can stall
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   397
     * waiting for locks while resizing. However, because other
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   398
     * threads can join in and help resize rather than contend for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   399
     * locks, average aggregate waits become shorter as resizing
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   400
     * progresses.  The transfer operation must also ensure that all
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   401
     * accessible bins in both the old and new table are usable by any
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   402
     * traversal.  This is arranged in part by proceeding from the
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   403
     * last bin (table.length - 1) up towards the first.  Upon seeing
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   404
     * a forwarding node, traversals (see class Traverser) arrange to
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   405
     * move to the new table without revisiting nodes.  To ensure that
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   406
     * no intervening nodes are skipped even when moved out of order,
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   407
     * a stack (see class TableStack) is created on first encounter of
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   408
     * a forwarding node during a traversal, to maintain its place if
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   409
     * later processing the current table. The need for these
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   410
     * save/restore mechanics is relatively rare, but when one
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   411
     * forwarding node is encountered, typically many more will be.
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   412
     * So Traversers use a simple caching scheme to avoid creating so
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   413
     * many new TableStack nodes. (Thanks to Peter Levart for
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   414
     * suggesting use of a stack here.)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   415
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   416
     * The traversal scheme also applies to partial traversals of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   417
     * ranges of bins (via an alternate Traverser constructor)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   418
     * to support partitioned aggregate operations.  Also, read-only
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   419
     * operations give up if ever forwarded to a null table, which
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   420
     * provides support for shutdown-style clearing, which is also not
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   421
     * currently implemented.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   422
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   423
     * Lazy table initialization minimizes footprint until first use,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   424
     * and also avoids resizings when the first operation is from a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   425
     * putAll, constructor with map argument, or deserialization.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   426
     * These cases attempt to override the initial capacity settings,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   427
     * but harmlessly fail to take effect in cases of races.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   428
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   429
     * The element count is maintained using a specialization of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   430
     * LongAdder. We need to incorporate a specialization rather than
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   431
     * just use a LongAdder in order to access implicit
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   432
     * contention-sensing that leads to creation of multiple
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   433
     * CounterCells.  The counter mechanics avoid contention on
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   434
     * updates but can encounter cache thrashing if read too
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   435
     * frequently during concurrent access. To avoid reading so often,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   436
     * resizing under contention is attempted only upon adding to a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   437
     * bin already holding two or more nodes. Under uniform hash
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   438
     * distributions, the probability of this occurring at threshold
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   439
     * is around 13%, meaning that only about 1 in 8 puts check
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   440
     * threshold (and after resizing, many fewer do so).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   441
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   442
     * TreeBins use a special form of comparison for search and
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   443
     * related operations (which is the main reason we cannot use
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   444
     * existing collections such as TreeMaps). TreeBins contain
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   445
     * Comparable elements, but may contain others, as well as
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   446
     * elements that are Comparable but not necessarily Comparable for
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   447
     * the same T, so we cannot invoke compareTo among them. To handle
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   448
     * this, the tree is ordered primarily by hash value, then by
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   449
     * Comparable.compareTo order if applicable.  On lookup at a node,
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   450
     * if elements are not comparable or compare as 0 then both left
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   451
     * and right children may need to be searched in the case of tied
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   452
     * hash values. (This corresponds to the full list search that
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   453
     * would be necessary if all elements were non-Comparable and had
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   454
     * tied hashes.) On insertion, to keep a total ordering (or as
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   455
     * close as is required here) across rebalancings, we compare
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   456
     * classes and identityHashCodes as tie-breakers. The red-black
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   457
     * balancing code is updated from pre-jdk-collections
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   458
     * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   459
     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   460
     * Algorithms" (CLR).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   461
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   462
     * TreeBins also require an additional locking mechanism.  While
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   463
     * list traversal is always possible by readers even during
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   464
     * updates, tree traversal is not, mainly because of tree-rotations
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   465
     * that may change the root node and/or its linkages.  TreeBins
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   466
     * include a simple read-write lock mechanism parasitic on the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   467
     * main bin-synchronization strategy: Structural adjustments
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   468
     * associated with an insertion or removal are already bin-locked
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   469
     * (and so cannot conflict with other writers) but must wait for
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   470
     * ongoing readers to finish. Since there can be only one such
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   471
     * waiter, we use a simple scheme using a single "waiter" field to
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   472
     * block writers.  However, readers need never block.  If the root
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   473
     * lock is held, they proceed along the slow traversal path (via
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   474
     * next-pointers) until the lock becomes available or the list is
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   475
     * exhausted, whichever comes first. These cases are not fast, but
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   476
     * maximize aggregate expected throughput.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   477
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   478
     * Maintaining API and serialization compatibility with previous
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   479
     * versions of this class introduces several oddities. Mainly: We
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 30441
diff changeset
   480
     * leave untouched but unused constructor arguments referring to
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   481
     * concurrencyLevel. We accept a loadFactor constructor argument,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   482
     * but apply it only to initial table capacity (which is the only
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   483
     * time that we can guarantee to honor it.) We also declare an
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   484
     * unused "Segment" class that is instantiated in minimal form
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   485
     * only when serializing.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   486
     *
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   487
     * Also, solely for compatibility with previous versions of this
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   488
     * class, it extends AbstractMap, even though all of its methods
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   489
     * are overridden, so it is just useless baggage.
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   490
     *
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   491
     * This file is organized to make things a little easier to follow
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   492
     * while reading than they might otherwise: First the main static
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   493
     * declarations and utilities, then fields, then main public
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   494
     * methods (with a few factorings of multiple public methods into
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   495
     * internal ones), then sizing methods, trees, traversers, and
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   496
     * bulk operations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    /* ---------------- Constants -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   502
     * The largest possible table capacity.  This value must be
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   503
     * exactly 1<<30 to stay within Java array allocation and indexing
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   504
     * bounds for power of two table sizes, and is further required
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   505
     * because the top two bits of 32bit hash fields are used for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   506
     * control purposes.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   508
    private static final int MAXIMUM_CAPACITY = 1 << 30;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   509
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   510
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   511
     * The default initial table capacity.  Must be a power of 2
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   512
     * (i.e., at least 1) and at most MAXIMUM_CAPACITY.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   513
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   514
    private static final int DEFAULT_CAPACITY = 16;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   517
     * The largest possible (non-power of two) array size.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   518
     * Needed by toArray and related methods.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   520
    static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   523
     * The default concurrency level for this table. Unused but
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   524
     * defined for compatibility with previous versions of this class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   526
    private static final int DEFAULT_CONCURRENCY_LEVEL = 16;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   527
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   528
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   529
     * The load factor for this table. Overrides of this value in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   530
     * constructors affect only the initial table capacity.  The
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   531
     * actual floating point value isn't normally used -- it is
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   532
     * simpler to use expressions such as {@code n - (n >>> 2)} for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   533
     * the associated resizing threshold.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   534
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   535
    private static final float LOAD_FACTOR = 0.75f;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   538
     * The bin count threshold for using a tree rather than list for a
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   539
     * bin.  Bins are converted to trees when adding an element to a
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   540
     * bin with at least this many nodes. The value must be greater
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   541
     * than 2, and should be at least 8 to mesh with assumptions in
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   542
     * tree removal about conversion back to plain bins upon
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   543
     * shrinkage.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   545
    static final int TREEIFY_THRESHOLD = 8;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   546
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   547
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   548
     * The bin count threshold for untreeifying a (split) bin during a
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   549
     * resize operation. Should be less than TREEIFY_THRESHOLD, and at
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   550
     * most 6 to mesh with shrinkage detection under removal.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   551
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   552
    static final int UNTREEIFY_THRESHOLD = 6;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   553
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   554
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   555
     * The smallest table capacity for which bins may be treeified.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   556
     * (Otherwise the table is resized if too many nodes in a bin.)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   557
     * The value should be at least 4 * TREEIFY_THRESHOLD to avoid
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   558
     * conflicts between resizing and treeification thresholds.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   559
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   560
    static final int MIN_TREEIFY_CAPACITY = 64;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   563
     * Minimum number of rebinnings per transfer step. Ranges are
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   564
     * subdivided to allow multiple resizer threads.  This value
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   565
     * serves as a lower bound to avoid resizers encountering
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   566
     * excessive memory contention.  The value should be at least
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   567
     * DEFAULT_CAPACITY.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   568
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   569
    private static final int MIN_TRANSFER_STRIDE = 16;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   570
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   571
    /**
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   572
     * The number of bits used for generation stamp in sizeCtl.
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   573
     * Must be at least 6 for 32bit arrays.
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   574
     */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   575
    private static final int RESIZE_STAMP_BITS = 16;
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   576
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   577
    /**
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   578
     * The maximum number of threads that can help resize.
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   579
     * Must fit in 32 - RESIZE_STAMP_BITS bits.
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   580
     */
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   581
    private static final int MAX_RESIZERS = (1 << (32 - RESIZE_STAMP_BITS)) - 1;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   582
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   583
    /**
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   584
     * The bit shift for recording size stamp in sizeCtl.
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   585
     */
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   586
    private static final int RESIZE_STAMP_SHIFT = 32 - RESIZE_STAMP_BITS;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   587
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   588
    /*
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   589
     * Encodings for Node hash fields. See above for explanation.
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
   590
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   591
    static final int MOVED     = -1; // hash for forwarding nodes
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   592
    static final int TREEBIN   = -2; // hash for roots of trees
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   593
    static final int RESERVED  = -3; // hash for transient reservations
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   594
    static final int HASH_BITS = 0x7fffffff; // usable bits of normal node hash
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   595
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   596
    /** Number of CPUS, to place bounds on some sizings */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   597
    static final int NCPU = Runtime.getRuntime().availableProcessors();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   598
34340
3b7a5a01c627 8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents: 33674
diff changeset
   599
    /**
3b7a5a01c627 8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents: 33674
diff changeset
   600
     * Serialized pseudo-fields, provided only for jdk7 compatibility.
3b7a5a01c627 8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents: 33674
diff changeset
   601
     * @serialField segments Segment[]
3b7a5a01c627 8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents: 33674
diff changeset
   602
     *   The segments, each of which is a specialized hash table.
3b7a5a01c627 8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents: 33674
diff changeset
   603
     * @serialField segmentMask int
3b7a5a01c627 8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents: 33674
diff changeset
   604
     *   Mask value for indexing into segments. The upper bits of a
3b7a5a01c627 8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents: 33674
diff changeset
   605
     *   key's hash code are used to choose the segment.
3b7a5a01c627 8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents: 33674
diff changeset
   606
     * @serialField segmentShift int
3b7a5a01c627 8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents: 33674
diff changeset
   607
     *   Shift value for indexing within segments.
3b7a5a01c627 8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents: 33674
diff changeset
   608
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   609
    private static final ObjectStreamField[] serialPersistentFields = {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   610
        new ObjectStreamField("segments", Segment[].class),
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   611
        new ObjectStreamField("segmentMask", Integer.TYPE),
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   612
        new ObjectStreamField("segmentShift", Integer.TYPE),
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   613
    };
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
   614
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   615
    /* ---------------- Nodes -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   616
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
   617
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   618
     * Key-value entry.  This class is never exported out as a
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   619
     * user-mutable Map.Entry (i.e., one supporting setValue; see
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   620
     * MapEntry below), but can be used for read-only traversals used
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   621
     * in bulk tasks.  Subclasses of Node with a negative hash field
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   622
     * are special, and contain null keys and values (but are never
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   623
     * exported).  Otherwise, keys and vals are never null.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   624
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   625
    static class Node<K,V> implements Map.Entry<K,V> {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   626
        final int hash;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   627
        final K key;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   628
        volatile V val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   629
        volatile Node<K,V> next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   630
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
   631
        Node(int hash, K key, V val) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   632
            this.hash = hash;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   633
            this.key = key;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   634
            this.val = val;
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
   635
        }
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
   636
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
   637
        Node(int hash, K key, V val, Node<K,V> next) {
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
   638
            this(hash, key, val);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   639
            this.next = next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   640
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   641
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   642
        public final K getKey()     { return key; }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   643
        public final V getValue()   { return val; }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   644
        public final int hashCode() { return key.hashCode() ^ val.hashCode(); }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   645
        public final String toString() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   646
            return Helpers.mapEntryToString(key, val);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   647
        }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   648
        public final V setValue(V value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   649
            throw new UnsupportedOperationException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   650
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   651
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   652
        public final boolean equals(Object o) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   653
            Object k, v, u; Map.Entry<?,?> e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   654
            return ((o instanceof Map.Entry) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   655
                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   656
                    (v = e.getValue()) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   657
                    (k == key || k.equals(key)) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   658
                    (v == (u = val) || v.equals(u)));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   659
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   660
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   661
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   662
         * Virtualized support for map.get(); overridden in subclasses.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   663
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   664
        Node<K,V> find(int h, Object k) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   665
            Node<K,V> e = this;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   666
            if (k != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   667
                do {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   668
                    K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   669
                    if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   670
                        ((ek = e.key) == k || (ek != null && k.equals(ek))))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   671
                        return e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   672
                } while ((e = e.next) != null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   673
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   674
            return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   675
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   676
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   677
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   678
    /* ---------------- Static utilities -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   679
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   680
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   681
     * Spreads (XORs) higher bits of hash to lower and also forces top
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   682
     * bit to 0. Because the table uses power-of-two masking, sets of
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   683
     * hashes that vary only in bits above the current mask will
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   684
     * always collide. (Among known examples are sets of Float keys
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   685
     * holding consecutive whole numbers in small tables.)  So we
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   686
     * apply a transform that spreads the impact of higher bits
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   687
     * downward. There is a tradeoff between speed, utility, and
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   688
     * quality of bit-spreading. Because many common sets of hashes
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   689
     * are already reasonably distributed (so don't benefit from
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   690
     * spreading), and because we use trees to handle large sets of
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   691
     * collisions in bins, we just XOR some shifted bits in the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   692
     * cheapest possible way to reduce systematic lossage, as well as
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   693
     * to incorporate impact of the highest bits that would otherwise
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   694
     * never be used in index calculations because of table bounds.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   696
    static final int spread(int h) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   697
        return (h ^ (h >>> 16)) & HASH_BITS;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   698
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   699
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   700
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   701
     * Returns a power of two table size for the given desired capacity.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   702
     * See Hackers Delight, sec 3.2
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   703
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   704
    private static final int tableSizeFor(int c) {
50762
3c3ff151c75e 8202422: value of 'sizeCtl' in ConcurrentHashMap varies with the constructor called
dl
parents: 49937
diff changeset
   705
        int n = -1 >>> Integer.numberOfLeadingZeros(c - 1);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   706
        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   707
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   708
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   709
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   710
     * Returns x's Class if it is of the form "class C implements
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   711
     * Comparable<C>", else null.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   712
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   713
    static Class<?> comparableClassFor(Object x) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   714
        if (x instanceof Comparable) {
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47307
diff changeset
   715
            Class<?> c; Type[] ts, as; ParameterizedType p;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   716
            if ((c = x.getClass()) == String.class) // bypass checks
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   717
                return c;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   718
            if ((ts = c.getGenericInterfaces()) != null) {
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47307
diff changeset
   719
                for (Type t : ts) {
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47307
diff changeset
   720
                    if ((t instanceof ParameterizedType) &&
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   721
                        ((p = (ParameterizedType)t).getRawType() ==
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   722
                         Comparable.class) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   723
                        (as = p.getActualTypeArguments()) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   724
                        as.length == 1 && as[0] == c) // type arg is c
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   725
                        return c;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   726
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   727
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   728
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   729
        return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   730
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   731
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   732
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   733
     * Returns k.compareTo(x) if x matches kc (k's screened comparable
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   734
     * class), else 0.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   735
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   736
    @SuppressWarnings({"rawtypes","unchecked"}) // for cast to Comparable
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   737
    static int compareComparables(Class<?> kc, Object k, Object x) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   738
        return (x == null || x.getClass() != kc ? 0 :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   739
                ((Comparable)k).compareTo(x));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   740
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   741
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   742
    /* ---------------- Table element access -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   743
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   744
    /*
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
   745
     * Atomic access methods are used for table elements as well as
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   746
     * elements of in-progress next table while resizing.  All uses of
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   747
     * the tab arguments must be null checked by callers.  All callers
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   748
     * also paranoically precheck that tab's length is not zero (or an
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   749
     * equivalent check), thus ensuring that any index argument taking
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   750
     * the form of a hash value anded with (length - 1) is a valid
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   751
     * index.  Note that, to be correct wrt arbitrary concurrency
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   752
     * errors by users, these checks must operate on local variables,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   753
     * which accounts for some odd-looking inline assignments below.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   754
     * Note that calls to setTabAt always occur within locked regions,
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
   755
     * and so require only release ordering.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   756
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   757
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   758
    @SuppressWarnings("unchecked")
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   759
    static final <K,V> Node<K,V> tabAt(Node<K,V>[] tab, int i) {
52220
9c260a6b6471 8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents: 50762
diff changeset
   760
        return (Node<K,V>)U.getReferenceAcquire(tab, ((long)i << ASHIFT) + ABASE);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   761
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   762
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   763
    static final <K,V> boolean casTabAt(Node<K,V>[] tab, int i,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   764
                                        Node<K,V> c, Node<K,V> v) {
52220
9c260a6b6471 8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents: 50762
diff changeset
   765
        return U.compareAndSetReference(tab, ((long)i << ASHIFT) + ABASE, c, v);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   766
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   767
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   768
    static final <K,V> void setTabAt(Node<K,V>[] tab, int i, Node<K,V> v) {
52220
9c260a6b6471 8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents: 50762
diff changeset
   769
        U.putReferenceRelease(tab, ((long)i << ASHIFT) + ABASE, v);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   770
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    /* ---------------- Fields -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   775
     * The array of bins. Lazily initialized upon first insertion.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   776
     * Size is always a power of two. Accessed directly by iterators.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   777
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   778
    transient volatile Node<K,V>[] table;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   779
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   780
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   781
     * The next table to use; non-null only while resizing.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   782
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   783
    private transient volatile Node<K,V>[] nextTable;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   784
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   785
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   786
     * Base counter value, used mainly when there is no contention,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   787
     * but also as a fallback during table initialization
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   788
     * races. Updated via CAS.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   789
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   790
    private transient volatile long baseCount;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   791
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   792
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   793
     * Table initialization and resizing control.  When negative, the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   794
     * table is being initialized or resized: -1 for initialization,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   795
     * else -(1 + the number of active resizing threads).  Otherwise,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   796
     * when table is null, holds the initial table size to use upon
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   797
     * creation, or 0 for default. After initialization, holds the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   798
     * next element count value upon which to resize the table.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   799
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   800
    private transient volatile int sizeCtl;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   801
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   802
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   803
     * The next table index (plus one) to split while resizing.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   804
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   805
    private transient volatile int transferIndex;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   806
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   807
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   808
     * Spinlock (locked via CAS) used when resizing and/or creating CounterCells.
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 11279
diff changeset
   809
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   810
    private transient volatile int cellsBusy;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   811
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   812
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   813
     * Table of counter cells. When non-null, size is a power of 2.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   814
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   815
    private transient volatile CounterCell[] counterCells;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   816
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   817
    // views
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   818
    private transient KeySetView<K,V> keySet;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   819
    private transient ValuesView<K,V> values;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   820
    private transient EntrySetView<K,V> entrySet;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   821
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   822
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   823
    /* ---------------- Public operations -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   824
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   825
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   826
     * Creates a new, empty map with the default initial table size (16).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   827
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   828
    public ConcurrentHashMap() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   829
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   830
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   831
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   832
     * Creates a new, empty map with an initial table size
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   833
     * accommodating the specified number of elements without the need
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   834
     * to dynamically resize.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   835
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   836
     * @param initialCapacity The implementation performs internal
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   837
     * sizing to accommodate this many elements.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   838
     * @throws IllegalArgumentException if the initial capacity of
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   839
     * elements is negative
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   840
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   841
    public ConcurrentHashMap(int initialCapacity) {
50762
3c3ff151c75e 8202422: value of 'sizeCtl' in ConcurrentHashMap varies with the constructor called
dl
parents: 49937
diff changeset
   842
        this(initialCapacity, LOAD_FACTOR, 1);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   843
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   844
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   845
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   846
     * Creates a new map with the same mappings as the given map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   847
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   848
     * @param m the map
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   849
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   850
    public ConcurrentHashMap(Map<? extends K, ? extends V> m) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   851
        this.sizeCtl = DEFAULT_CAPACITY;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   852
        putAll(m);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   853
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   854
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   855
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   856
     * Creates a new, empty map with an initial table size based on
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   857
     * the given number of elements ({@code initialCapacity}) and
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   858
     * initial table density ({@code loadFactor}).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   859
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   860
     * @param initialCapacity the initial capacity. The implementation
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   861
     * performs internal sizing to accommodate this many elements,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   862
     * given the specified load factor.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   863
     * @param loadFactor the load factor (table density) for
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   864
     * establishing the initial table size
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   865
     * @throws IllegalArgumentException if the initial capacity of
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   866
     * elements is negative or the load factor is nonpositive
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   867
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   868
     * @since 1.6
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   869
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   870
    public ConcurrentHashMap(int initialCapacity, float loadFactor) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   871
        this(initialCapacity, loadFactor, 1);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   872
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   873
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 11279
diff changeset
   874
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   875
     * Creates a new, empty map with an initial table size based on
50762
3c3ff151c75e 8202422: value of 'sizeCtl' in ConcurrentHashMap varies with the constructor called
dl
parents: 49937
diff changeset
   876
     * the given number of elements ({@code initialCapacity}), initial
3c3ff151c75e 8202422: value of 'sizeCtl' in ConcurrentHashMap varies with the constructor called
dl
parents: 49937
diff changeset
   877
     * table density ({@code loadFactor}), and number of concurrently
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   878
     * updating threads ({@code concurrencyLevel}).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   879
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   880
     * @param initialCapacity the initial capacity. The implementation
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   881
     * performs internal sizing to accommodate this many elements,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   882
     * given the specified load factor.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   883
     * @param loadFactor the load factor (table density) for
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   884
     * establishing the initial table size
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   885
     * @param concurrencyLevel the estimated number of concurrently
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   886
     * updating threads. The implementation may use this value as
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   887
     * a sizing hint.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   888
     * @throws IllegalArgumentException if the initial capacity is
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   889
     * negative or the load factor or concurrencyLevel are
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   890
     * nonpositive
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   891
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   892
    public ConcurrentHashMap(int initialCapacity,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   893
                             float loadFactor, int concurrencyLevel) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   894
        if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   895
            throw new IllegalArgumentException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   896
        if (initialCapacity < concurrencyLevel)   // Use at least as many bins
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   897
            initialCapacity = concurrencyLevel;   // as estimated threads
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   898
        long size = (long)(1.0 + (long)initialCapacity / loadFactor);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   899
        int cap = (size >= (long)MAXIMUM_CAPACITY) ?
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   900
            MAXIMUM_CAPACITY : tableSizeFor((int)size);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   901
        this.sizeCtl = cap;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   902
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   903
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   904
    // Original (since JDK1.2) Map methods
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   905
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   906
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   907
     * {@inheritDoc}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   908
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   909
    public int size() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   910
        long n = sumCount();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   911
        return ((n < 0L) ? 0 :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   912
                (n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   913
                (int)n);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   914
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   915
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   916
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   917
     * {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   919
    public boolean isEmpty() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   920
        return sumCount() <= 0L; // ignore transient negative values
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   921
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   922
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   923
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   924
     * Returns the value to which the specified key is mapped,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   925
     * or {@code null} if this map contains no mapping for the key.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   926
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   927
     * <p>More formally, if this map contains a mapping from a key
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   928
     * {@code k} to a value {@code v} such that {@code key.equals(k)},
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   929
     * then this method returns {@code v}; otherwise it returns
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   930
     * {@code null}.  (There can be at most one such mapping.)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   931
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   932
     * @throws NullPointerException if the specified key is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   933
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   934
    public V get(Object key) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   935
        Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   936
        int h = spread(key.hashCode());
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   937
        if ((tab = table) != null && (n = tab.length) > 0 &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   938
            (e = tabAt(tab, (n - 1) & h)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   939
            if ((eh = e.hash) == h) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   940
                if ((ek = e.key) == key || (ek != null && key.equals(ek)))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   941
                    return e.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   942
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   943
            else if (eh < 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   944
                return (p = e.find(h, key)) != null ? p.val : null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   945
            while ((e = e.next) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   946
                if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   947
                    ((ek = e.key) == key || (ek != null && key.equals(ek))))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   948
                    return e.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   949
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   950
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   951
        return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   952
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   953
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   954
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   955
     * Tests if the specified object is a key in this table.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   956
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   957
     * @param  key possible key
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   958
     * @return {@code true} if and only if the specified object
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   959
     *         is a key in this table, as determined by the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   960
     *         {@code equals} method; {@code false} otherwise
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   961
     * @throws NullPointerException if the specified key is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   962
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   963
    public boolean containsKey(Object key) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   964
        return get(key) != null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   965
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   966
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   967
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   968
     * Returns {@code true} if this map maps one or more keys to the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   969
     * specified value. Note: This method may require a full traversal
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   970
     * of the map, and is much slower than method {@code containsKey}.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   971
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   972
     * @param value value whose presence in this map is to be tested
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   973
     * @return {@code true} if this map maps one or more keys to the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   974
     *         specified value
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   975
     * @throws NullPointerException if the specified value is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   976
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   977
    public boolean containsValue(Object value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   978
        if (value == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   979
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   980
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   981
        if ((t = table) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   982
            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   983
            for (Node<K,V> p; (p = it.advance()) != null; ) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   984
                V v;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   985
                if ((v = p.val) == value || (v != null && value.equals(v)))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   986
                    return true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   987
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   988
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   989
        return false;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   990
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   993
     * Maps the specified key to the specified value in this table.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   994
     * Neither the key nor the value can be null.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   995
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   996
     * <p>The value can be retrieved by calling the {@code get} method
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   997
     * with a key that is equal to the original key.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   998
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   999
     * @param key key with which the specified value is to be associated
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1000
     * @param value value to be associated with the specified key
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1001
     * @return the previous value associated with {@code key}, or
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1002
     *         {@code null} if there was no mapping for {@code key}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1003
     * @throws NullPointerException if the specified key or value is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1005
    public V put(K key, V value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1006
        return putVal(key, value, false);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1007
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1008
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1009
    /** Implementation for put and putIfAbsent */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1010
    final V putVal(K key, V value, boolean onlyIfAbsent) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1011
        if (key == null || value == null) throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1012
        int hash = spread(key.hashCode());
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1013
        int binCount = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1014
        for (Node<K,V>[] tab = table;;) {
39779
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  1015
            Node<K,V> f; int n, i, fh; K fk; V fv;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1016
            if (tab == null || (n = tab.length) == 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1017
                tab = initTable();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1018
            else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  1019
                if (casTabAt(tab, i, null, new Node<K,V>(hash, key, value)))
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1020
                    break;                   // no lock when adding to empty bin
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1021
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1022
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1023
                tab = helpTransfer(tab, f);
44588
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  1024
            else if (onlyIfAbsent // check first node without acquiring lock
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  1025
                     && fh == hash
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  1026
                     && ((fk = f.key) == key || (fk != null && key.equals(fk)))
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  1027
                     && (fv = f.val) != null)
39779
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  1028
                return fv;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1029
            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1030
                V oldVal = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1031
                synchronized (f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1032
                    if (tabAt(tab, i) == f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1033
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1034
                            binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1035
                            for (Node<K,V> e = f;; ++binCount) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1036
                                K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1037
                                if (e.hash == hash &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1038
                                    ((ek = e.key) == key ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1039
                                     (ek != null && key.equals(ek)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1040
                                    oldVal = e.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1041
                                    if (!onlyIfAbsent)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1042
                                        e.val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1043
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1044
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1045
                                Node<K,V> pred = e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1046
                                if ((e = e.next) == null) {
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  1047
                                    pred.next = new Node<K,V>(hash, key, value);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1048
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1049
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1050
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1051
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1052
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1053
                            Node<K,V> p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1054
                            binCount = 2;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1055
                            if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1056
                                                           value)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1057
                                oldVal = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1058
                                if (!onlyIfAbsent)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1059
                                    p.val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1060
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1061
                        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1062
                        else if (f instanceof ReservationNode)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1063
                            throw new IllegalStateException("Recursive update");
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1064
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1065
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1066
                if (binCount != 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1067
                    if (binCount >= TREEIFY_THRESHOLD)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1068
                        treeifyBin(tab, i);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1069
                    if (oldVal != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1070
                        return oldVal;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1071
                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1072
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1073
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1074
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1075
        addCount(1L, binCount);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1076
        return null;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1077
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1078
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1079
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1080
     * Copies all of the mappings from the specified map to this one.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1081
     * These mappings replace any mappings that this map had for any of the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1082
     * keys currently in the specified map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1083
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1084
     * @param m mappings to be stored in this map
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1085
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1086
    public void putAll(Map<? extends K, ? extends V> m) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1087
        tryPresize(m.size());
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1088
        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1089
            putVal(e.getKey(), e.getValue(), false);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1090
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1091
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1092
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1093
     * Removes the key (and its corresponding value) from this map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1094
     * This method does nothing if the key is not in the map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1095
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1096
     * @param  key the key that needs to be removed
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1097
     * @return the previous value associated with {@code key}, or
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1098
     *         {@code null} if there was no mapping for {@code key}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1099
     * @throws NullPointerException if the specified key is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1100
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1101
    public V remove(Object key) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1102
        return replaceNode(key, null, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1103
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1104
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1105
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1106
     * Implementation for the four public remove/replace methods:
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1107
     * Replaces node value with v, conditional upon match of cv if
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1108
     * non-null.  If resulting value is null, delete.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1109
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1110
    final V replaceNode(Object key, V value, Object cv) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1111
        int hash = spread(key.hashCode());
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1112
        for (Node<K,V>[] tab = table;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1113
            Node<K,V> f; int n, i, fh;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1114
            if (tab == null || (n = tab.length) == 0 ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1115
                (f = tabAt(tab, i = (n - 1) & hash)) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1116
                break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1117
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1118
                tab = helpTransfer(tab, f);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1119
            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1120
                V oldVal = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1121
                boolean validated = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1122
                synchronized (f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1123
                    if (tabAt(tab, i) == f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1124
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1125
                            validated = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1126
                            for (Node<K,V> e = f, pred = null;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1127
                                K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1128
                                if (e.hash == hash &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1129
                                    ((ek = e.key) == key ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1130
                                     (ek != null && key.equals(ek)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1131
                                    V ev = e.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1132
                                    if (cv == null || cv == ev ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1133
                                        (ev != null && cv.equals(ev))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1134
                                        oldVal = ev;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1135
                                        if (value != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1136
                                            e.val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1137
                                        else if (pred != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1138
                                            pred.next = e.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1139
                                        else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1140
                                            setTabAt(tab, i, e.next);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1141
                                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1142
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1143
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1144
                                pred = e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1145
                                if ((e = e.next) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1146
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1147
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1148
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1149
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1150
                            validated = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1151
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1152
                            TreeNode<K,V> r, p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1153
                            if ((r = t.root) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1154
                                (p = r.findTreeNode(hash, key, null)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1155
                                V pv = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1156
                                if (cv == null || cv == pv ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1157
                                    (pv != null && cv.equals(pv))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1158
                                    oldVal = pv;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1159
                                    if (value != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1160
                                        p.val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1161
                                    else if (t.removeTreeNode(p))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1162
                                        setTabAt(tab, i, untreeify(t.first));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1163
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1164
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1165
                        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1166
                        else if (f instanceof ReservationNode)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1167
                            throw new IllegalStateException("Recursive update");
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1168
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1169
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1170
                if (validated) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1171
                    if (oldVal != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1172
                        if (value == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1173
                            addCount(-1L, -1);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1174
                        return oldVal;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1175
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1176
                    break;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1177
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1178
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1179
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1180
        return null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1181
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1184
     * Removes all of the mappings from this map.
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  1185
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1186
    public void clear() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1187
        long delta = 0L; // negative number of deletions
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1188
        int i = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1189
        Node<K,V>[] tab = table;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1190
        while (tab != null && i < tab.length) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1191
            int fh;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1192
            Node<K,V> f = tabAt(tab, i);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1193
            if (f == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1194
                ++i;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1195
            else if ((fh = f.hash) == MOVED) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1196
                tab = helpTransfer(tab, f);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1197
                i = 0; // restart
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1198
            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1199
            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1200
                synchronized (f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1201
                    if (tabAt(tab, i) == f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1202
                        Node<K,V> p = (fh >= 0 ? f :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1203
                                       (f instanceof TreeBin) ?
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1204
                                       ((TreeBin<K,V>)f).first : null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1205
                        while (p != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1206
                            --delta;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1207
                            p = p.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1208
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1209
                        setTabAt(tab, i++, null);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1210
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1211
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1212
            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1213
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1214
        if (delta != 0L)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1215
            addCount(delta, -1);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1216
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1217
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1218
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1219
     * Returns a {@link Set} view of the keys contained in this map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1220
     * The set is backed by the map, so changes to the map are
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1221
     * reflected in the set, and vice-versa. The set supports element
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1222
     * removal, which removes the corresponding mapping from this map,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1223
     * via the {@code Iterator.remove}, {@code Set.remove},
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1224
     * {@code removeAll}, {@code retainAll}, and {@code clear}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1225
     * operations.  It does not support the {@code add} or
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1226
     * {@code addAll} operations.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1227
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1228
     * <p>The view's iterators and spliterators are
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1229
     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1230
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1231
     * <p>The view's {@code spliterator} reports {@link Spliterator#CONCURRENT},
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1232
     * {@link Spliterator#DISTINCT}, and {@link Spliterator#NONNULL}.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1233
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1234
     * @return the set view
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1235
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1236
    public KeySetView<K,V> keySet() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1237
        KeySetView<K,V> ks;
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36958
diff changeset
  1238
        if ((ks = keySet) != null) return ks;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36958
diff changeset
  1239
        return keySet = new KeySetView<K,V>(this, null);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1240
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1241
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1242
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1243
     * Returns a {@link Collection} view of the values contained in this map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1244
     * The collection is backed by the map, so changes to the map are
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1245
     * reflected in the collection, and vice-versa.  The collection
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1246
     * supports element removal, which removes the corresponding
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1247
     * mapping from this map, via the {@code Iterator.remove},
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1248
     * {@code Collection.remove}, {@code removeAll},
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1249
     * {@code retainAll}, and {@code clear} operations.  It does not
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1250
     * support the {@code add} or {@code addAll} operations.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1251
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1252
     * <p>The view's iterators and spliterators are
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1253
     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1254
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1255
     * <p>The view's {@code spliterator} reports {@link Spliterator#CONCURRENT}
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1256
     * and {@link Spliterator#NONNULL}.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1257
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1258
     * @return the collection view
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1259
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1260
    public Collection<V> values() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1261
        ValuesView<K,V> vs;
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36958
diff changeset
  1262
        if ((vs = values) != null) return vs;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36958
diff changeset
  1263
        return values = new ValuesView<K,V>(this);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1264
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1265
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1266
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1267
     * Returns a {@link Set} view of the mappings contained in this map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1268
     * The set is backed by the map, so changes to the map are
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1269
     * reflected in the set, and vice-versa.  The set supports element
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1270
     * removal, which removes the corresponding mapping from the map,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1271
     * via the {@code Iterator.remove}, {@code Set.remove},
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1272
     * {@code removeAll}, {@code retainAll}, and {@code clear}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1273
     * operations.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1274
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1275
     * <p>The view's iterators and spliterators are
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1276
     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1277
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1278
     * <p>The view's {@code spliterator} reports {@link Spliterator#CONCURRENT},
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1279
     * {@link Spliterator#DISTINCT}, and {@link Spliterator#NONNULL}.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1280
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1281
     * @return the set view
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1282
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1283
    public Set<Map.Entry<K,V>> entrySet() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1284
        EntrySetView<K,V> es;
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36958
diff changeset
  1285
        if ((es = entrySet) != null) return es;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36958
diff changeset
  1286
        return entrySet = new EntrySetView<K,V>(this);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1287
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1288
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1289
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1290
     * Returns the hash code value for this {@link Map}, i.e.,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1291
     * the sum of, for each key-value pair in the map,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1292
     * {@code key.hashCode() ^ value.hashCode()}.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1293
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1294
     * @return the hash code value for this map
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1295
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1296
    public int hashCode() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1297
        int h = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1298
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1299
        if ((t = table) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1300
            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1301
            for (Node<K,V> p; (p = it.advance()) != null; )
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1302
                h += p.key.hashCode() ^ p.val.hashCode();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1303
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1304
        return h;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1305
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1306
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1307
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1308
     * Returns a string representation of this map.  The string
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1309
     * representation consists of a list of key-value mappings (in no
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1310
     * particular order) enclosed in braces ("{@code {}}").  Adjacent
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1311
     * mappings are separated by the characters {@code ", "} (comma
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1312
     * and space).  Each key-value mapping is rendered as the key
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1313
     * followed by an equals sign ("{@code =}") followed by the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1314
     * associated value.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1315
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1316
     * @return a string representation of this map
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1317
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1318
    public String toString() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1319
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1320
        int f = (t = table) == null ? 0 : t.length;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1321
        Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1322
        StringBuilder sb = new StringBuilder();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1323
        sb.append('{');
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1324
        Node<K,V> p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1325
        if ((p = it.advance()) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1326
            for (;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1327
                K k = p.key;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1328
                V v = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1329
                sb.append(k == this ? "(this Map)" : k);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1330
                sb.append('=');
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1331
                sb.append(v == this ? "(this Map)" : v);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1332
                if ((p = it.advance()) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1333
                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1334
                sb.append(',').append(' ');
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1335
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1336
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1337
        return sb.append('}').toString();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1338
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1339
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1340
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1341
     * Compares the specified object with this map for equality.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1342
     * Returns {@code true} if the given object is a map with the same
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1343
     * mappings as this map.  This operation may return misleading
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1344
     * results if either map is concurrently modified during execution
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1345
     * of this method.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1346
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1347
     * @param o object to be compared for equality with this map
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1348
     * @return {@code true} if the specified object is equal to this map
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1349
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1350
    public boolean equals(Object o) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1351
        if (o != this) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1352
            if (!(o instanceof Map))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1353
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1354
            Map<?,?> m = (Map<?,?>) o;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1355
            Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1356
            int f = (t = table) == null ? 0 : t.length;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1357
            Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1358
            for (Node<K,V> p; (p = it.advance()) != null; ) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1359
                V val = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1360
                Object v = m.get(p.key);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1361
                if (v == null || (v != val && !v.equals(val)))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1362
                    return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1363
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1364
            for (Map.Entry<?,?> e : m.entrySet()) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1365
                Object mk, mv, v;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1366
                if ((mk = e.getKey()) == null ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1367
                    (mv = e.getValue()) == null ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1368
                    (v = get(mk)) == null ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1369
                    (mv != v && !mv.equals(v)))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1370
                    return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1371
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1372
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1373
        return true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1374
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1375
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1376
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1377
     * Stripped-down version of helper class used in previous version,
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1378
     * declared for the sake of serialization compatibility.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1379
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1380
    static class Segment<K,V> extends ReentrantLock implements Serializable {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1381
        private static final long serialVersionUID = 2249069246763182397L;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1382
        final float loadFactor;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1383
        Segment(float lf) { this.loadFactor = lf; }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1384
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1385
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1386
    /**
47307
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1387
     * Saves this map to a stream (that is, serializes it).
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1388
     *
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1389
     * @param s the stream
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  1390
     * @throws java.io.IOException if an I/O error occurs
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1391
     * @serialData
34340
3b7a5a01c627 8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents: 33674
diff changeset
  1392
     * the serialized fields, followed by the key (Object) and value
3b7a5a01c627 8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents: 33674
diff changeset
  1393
     * (Object) for each key-value mapping, followed by a null pair.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1394
     * The key-value mappings are emitted in no particular order.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1395
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1396
    private void writeObject(java.io.ObjectOutputStream s)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1397
        throws java.io.IOException {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1398
        // For serialization compatibility
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1399
        // Emulate segment calculation from previous version of this class
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1400
        int sshift = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1401
        int ssize = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1402
        while (ssize < DEFAULT_CONCURRENCY_LEVEL) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1403
            ++sshift;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1404
            ssize <<= 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1405
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1406
        int segmentShift = 32 - sshift;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1407
        int segmentMask = ssize - 1;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1408
        @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1409
        Segment<K,V>[] segments = (Segment<K,V>[])
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1410
            new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1411
        for (int i = 0; i < segments.length; ++i)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1412
            segments[i] = new Segment<K,V>(LOAD_FACTOR);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1413
        java.io.ObjectOutputStream.PutField streamFields = s.putFields();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1414
        streamFields.put("segments", segments);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1415
        streamFields.put("segmentShift", segmentShift);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1416
        streamFields.put("segmentMask", segmentMask);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1417
        s.writeFields();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1418
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1419
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1420
        if ((t = table) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1421
            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1422
            for (Node<K,V> p; (p = it.advance()) != null; ) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1423
                s.writeObject(p.key);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1424
                s.writeObject(p.val);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1425
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1426
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1427
        s.writeObject(null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1428
        s.writeObject(null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1429
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1430
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1431
    /**
47307
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1432
     * Reconstitutes this map from a stream (that is, deserializes it).
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1433
     * @param s the stream
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  1434
     * @throws ClassNotFoundException if the class of a serialized object
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  1435
     *         could not be found
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  1436
     * @throws java.io.IOException if an I/O error occurs
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1437
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1438
    private void readObject(java.io.ObjectInputStream s)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1439
        throws java.io.IOException, ClassNotFoundException {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1440
        /*
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1441
         * To improve performance in typical cases, we create nodes
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1442
         * while reading, then place in table once size is known.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1443
         * However, we must also validate uniqueness and deal with
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1444
         * overpopulated bins while doing so, which requires
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1445
         * specialized versions of putVal mechanics.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1446
         */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1447
        sizeCtl = -1; // force exclusion for table construction
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1448
        s.defaultReadObject();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1449
        long size = 0L;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1450
        Node<K,V> p = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1451
        for (;;) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1452
            @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1453
            K k = (K) s.readObject();
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1454
            @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1455
            V v = (V) s.readObject();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1456
            if (k != null && v != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1457
                p = new Node<K,V>(spread(k.hashCode()), k, v, p);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1458
                ++size;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1459
            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1460
            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1461
                break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1462
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1463
        if (size == 0L)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1464
            sizeCtl = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1465
        else {
50762
3c3ff151c75e 8202422: value of 'sizeCtl' in ConcurrentHashMap varies with the constructor called
dl
parents: 49937
diff changeset
  1466
            long ts = (long)(1.0 + size / LOAD_FACTOR);
3c3ff151c75e 8202422: value of 'sizeCtl' in ConcurrentHashMap varies with the constructor called
dl
parents: 49937
diff changeset
  1467
            int n = (ts >= (long)MAXIMUM_CAPACITY) ?
3c3ff151c75e 8202422: value of 'sizeCtl' in ConcurrentHashMap varies with the constructor called
dl
parents: 49937
diff changeset
  1468
                MAXIMUM_CAPACITY : tableSizeFor((int)ts);
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1469
            @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1470
            Node<K,V>[] tab = (Node<K,V>[])new Node<?,?>[n];
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1471
            int mask = n - 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1472
            long added = 0L;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1473
            while (p != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1474
                boolean insertAtFront;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1475
                Node<K,V> next = p.next, first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1476
                int h = p.hash, j = h & mask;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1477
                if ((first = tabAt(tab, j)) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1478
                    insertAtFront = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1479
                else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1480
                    K k = p.key;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1481
                    if (first.hash < 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1482
                        TreeBin<K,V> t = (TreeBin<K,V>)first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1483
                        if (t.putTreeVal(h, k, p.val) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1484
                            ++added;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1485
                        insertAtFront = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1486
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1487
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1488
                        int binCount = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1489
                        insertAtFront = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1490
                        Node<K,V> q; K qk;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1491
                        for (q = first; q != null; q = q.next) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1492
                            if (q.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1493
                                ((qk = q.key) == k ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1494
                                 (qk != null && k.equals(qk)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1495
                                insertAtFront = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1496
                                break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1497
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1498
                            ++binCount;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1499
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1500
                        if (insertAtFront && binCount >= TREEIFY_THRESHOLD) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1501
                            insertAtFront = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1502
                            ++added;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1503
                            p.next = first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1504
                            TreeNode<K,V> hd = null, tl = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1505
                            for (q = p; q != null; q = q.next) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1506
                                TreeNode<K,V> t = new TreeNode<K,V>
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1507
                                    (q.hash, q.key, q.val, null, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1508
                                if ((t.prev = tl) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1509
                                    hd = t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1510
                                else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1511
                                    tl.next = t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1512
                                tl = t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1513
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1514
                            setTabAt(tab, j, new TreeBin<K,V>(hd));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1515
                        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1516
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1517
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1518
                if (insertAtFront) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1519
                    ++added;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1520
                    p.next = first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1521
                    setTabAt(tab, j, p);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1522
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1523
                p = next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1524
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1525
            table = tab;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1526
            sizeCtl = n - (n >>> 2);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1527
            baseCount = added;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1528
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1529
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1530
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1531
    // ConcurrentMap methods
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1532
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1533
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1534
     * {@inheritDoc}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1535
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1536
     * @return the previous value associated with the specified key,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1537
     *         or {@code null} if there was no mapping for the key
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1538
     * @throws NullPointerException if the specified key or value is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1539
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1540
    public V putIfAbsent(K key, V value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1541
        return putVal(key, value, true);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1542
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1543
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1544
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1545
     * {@inheritDoc}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1546
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1547
     * @throws NullPointerException if the specified key is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1548
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1549
    public boolean remove(Object key, Object value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1550
        if (key == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1551
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1552
        return value != null && replaceNode(key, null, value) != null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1553
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1554
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1555
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1556
     * {@inheritDoc}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1557
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1558
     * @throws NullPointerException if any of the arguments are null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1559
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1560
    public boolean replace(K key, V oldValue, V newValue) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1561
        if (key == null || oldValue == null || newValue == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1562
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1563
        return replaceNode(key, newValue, oldValue) != null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1564
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1565
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1566
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1567
     * {@inheritDoc}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1568
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1569
     * @return the previous value associated with the specified key,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1570
     *         or {@code null} if there was no mapping for the key
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1571
     * @throws NullPointerException if the specified key or value is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1572
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1573
    public V replace(K key, V value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1574
        if (key == null || value == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1575
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1576
        return replaceNode(key, value, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1577
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1578
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1579
    // Overrides of JDK8+ Map extension method defaults
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1580
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1581
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1582
     * Returns the value to which the specified key is mapped, or the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1583
     * given default value if this map contains no mapping for the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1584
     * key.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1585
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1586
     * @param key the key whose associated value is to be returned
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1587
     * @param defaultValue the value to return if this map contains
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1588
     * no mapping for the given key
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1589
     * @return the mapping for the key, if present; else the default value
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1590
     * @throws NullPointerException if the specified key is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1591
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1592
    public V getOrDefault(Object key, V defaultValue) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1593
        V v;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1594
        return (v = get(key)) == null ? defaultValue : v;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1595
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1596
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1597
    public void forEach(BiConsumer<? super K, ? super V> action) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1598
        if (action == null) throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1599
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1600
        if ((t = table) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1601
            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1602
            for (Node<K,V> p; (p = it.advance()) != null; ) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1603
                action.accept(p.key, p.val);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1604
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1605
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1606
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1607
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1608
    public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1609
        if (function == null) throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1610
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1611
        if ((t = table) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1612
            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1613
            for (Node<K,V> p; (p = it.advance()) != null; ) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1614
                V oldValue = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1615
                for (K key = p.key;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1616
                    V newValue = function.apply(key, oldValue);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1617
                    if (newValue == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1618
                        throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1619
                    if (replaceNode(key, newValue, oldValue) != null ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1620
                        (oldValue = get(key)) == null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1621
                        break;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1622
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1623
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1624
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1625
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1626
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1627
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1628
     * Helper method for EntrySetView.removeIf.
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1629
     */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1630
    boolean removeEntryIf(Predicate<? super Entry<K,V>> function) {
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1631
        if (function == null) throw new NullPointerException();
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1632
        Node<K,V>[] t;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1633
        boolean removed = false;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1634
        if ((t = table) != null) {
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1635
            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1636
            for (Node<K,V> p; (p = it.advance()) != null; ) {
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1637
                K k = p.key;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1638
                V v = p.val;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1639
                Map.Entry<K,V> e = new AbstractMap.SimpleImmutableEntry<>(k, v);
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1640
                if (function.test(e) && replaceNode(k, null, v) != null)
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1641
                    removed = true;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1642
            }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1643
        }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1644
        return removed;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1645
    }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1646
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1647
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1648
     * Helper method for ValuesView.removeIf.
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1649
     */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1650
    boolean removeValueIf(Predicate<? super V> function) {
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1651
        if (function == null) throw new NullPointerException();
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1652
        Node<K,V>[] t;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1653
        boolean removed = false;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1654
        if ((t = table) != null) {
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1655
            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1656
            for (Node<K,V> p; (p = it.advance()) != null; ) {
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1657
                K k = p.key;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1658
                V v = p.val;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1659
                if (function.test(v) && replaceNode(k, null, v) != null)
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1660
                    removed = true;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1661
            }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1662
        }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1663
        return removed;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1664
    }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1665
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1666
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1667
     * If the specified key is not already associated with a value,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1668
     * attempts to compute its value using the given mapping function
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1669
     * and enters it into this map unless {@code null}.  The entire
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1670
     * method invocation is performed atomically, so the function is
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1671
     * applied at most once per key.  Some attempted update operations
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1672
     * on this map by other threads may be blocked while computation
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1673
     * is in progress, so the computation should be short and simple,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1674
     * and must not attempt to update any other mappings of this map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1675
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1676
     * @param key key with which the specified value is to be associated
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1677
     * @param mappingFunction the function to compute a value
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1678
     * @return the current (existing or computed) value associated with
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1679
     *         the specified key, or null if the computed value is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1680
     * @throws NullPointerException if the specified key or mappingFunction
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1681
     *         is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1682
     * @throws IllegalStateException if the computation detectably
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1683
     *         attempts a recursive update to this map that would
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1684
     *         otherwise never complete
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1685
     * @throws RuntimeException or Error if the mappingFunction does so,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1686
     *         in which case the mapping is left unestablished
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1687
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1688
    public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1689
        if (key == null || mappingFunction == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1690
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1691
        int h = spread(key.hashCode());
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1692
        V val = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1693
        int binCount = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1694
        for (Node<K,V>[] tab = table;;) {
39779
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  1695
            Node<K,V> f; int n, i, fh; K fk; V fv;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1696
            if (tab == null || (n = tab.length) == 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1697
                tab = initTable();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1698
            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1699
                Node<K,V> r = new ReservationNode<K,V>();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1700
                synchronized (r) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1701
                    if (casTabAt(tab, i, null, r)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1702
                        binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1703
                        Node<K,V> node = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1704
                        try {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1705
                            if ((val = mappingFunction.apply(key)) != null)
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  1706
                                node = new Node<K,V>(h, key, val);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1707
                        } finally {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1708
                            setTabAt(tab, i, node);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1709
                        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1710
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1711
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1712
                if (binCount != 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1713
                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1714
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1715
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1716
                tab = helpTransfer(tab, f);
44588
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  1717
            else if (fh == h    // check first node without acquiring lock
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  1718
                     && ((fk = f.key) == key || (fk != null && key.equals(fk)))
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  1719
                     && (fv = f.val) != null)
39779
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  1720
                return fv;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1721
            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1722
                boolean added = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1723
                synchronized (f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1724
                    if (tabAt(tab, i) == f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1725
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1726
                            binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1727
                            for (Node<K,V> e = f;; ++binCount) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1728
                                K ek;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1729
                                if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1730
                                    ((ek = e.key) == key ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1731
                                     (ek != null && key.equals(ek)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1732
                                    val = e.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1733
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1734
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1735
                                Node<K,V> pred = e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1736
                                if ((e = e.next) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1737
                                    if ((val = mappingFunction.apply(key)) != null) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1738
                                        if (pred.next != null)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1739
                                            throw new IllegalStateException("Recursive update");
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1740
                                        added = true;
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  1741
                                        pred.next = new Node<K,V>(h, key, val);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1742
                                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1743
                                    break;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1744
                                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1745
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1746
                        }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1747
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1748
                            binCount = 2;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1749
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1750
                            TreeNode<K,V> r, p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1751
                            if ((r = t.root) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1752
                                (p = r.findTreeNode(h, key, null)) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1753
                                val = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1754
                            else if ((val = mappingFunction.apply(key)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1755
                                added = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1756
                                t.putTreeVal(h, key, val);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1757
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1758
                        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1759
                        else if (f instanceof ReservationNode)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1760
                            throw new IllegalStateException("Recursive update");
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1761
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1762
                }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1763
                if (binCount != 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1764
                    if (binCount >= TREEIFY_THRESHOLD)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1765
                        treeifyBin(tab, i);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1766
                    if (!added)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1767
                        return val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1768
                    break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1769
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1770
            }
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  1771
        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1772
        if (val != null)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1773
            addCount(1L, binCount);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1774
        return val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1775
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1776
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1777
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1778
     * If the value for the specified key is present, attempts to
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1779
     * compute a new mapping given the key and its current mapped
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1780
     * value.  The entire method invocation is performed atomically.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1781
     * Some attempted update operations on this map by other threads
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1782
     * may be blocked while computation is in progress, so the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1783
     * computation should be short and simple, and must not attempt to
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1784
     * update any other mappings of this map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1785
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1786
     * @param key key with which a value may be associated
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1787
     * @param remappingFunction the function to compute a value
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1788
     * @return the new value associated with the specified key, or null if none
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1789
     * @throws NullPointerException if the specified key or remappingFunction
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1790
     *         is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1791
     * @throws IllegalStateException if the computation detectably
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1792
     *         attempts a recursive update to this map that would
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1793
     *         otherwise never complete
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1794
     * @throws RuntimeException or Error if the remappingFunction does so,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1795
     *         in which case the mapping is unchanged
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1796
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1797
    public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1798
        if (key == null || remappingFunction == null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1799
            throw new NullPointerException();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1800
        int h = spread(key.hashCode());
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1801
        V val = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1802
        int delta = 0;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1803
        int binCount = 0;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1804
        for (Node<K,V>[] tab = table;;) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1805
            Node<K,V> f; int n, i, fh;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1806
            if (tab == null || (n = tab.length) == 0)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1807
                tab = initTable();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1808
            else if ((f = tabAt(tab, i = (n - 1) & h)) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1809
                break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1810
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1811
                tab = helpTransfer(tab, f);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1812
            else {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1813
                synchronized (f) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1814
                    if (tabAt(tab, i) == f) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1815
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1816
                            binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1817
                            for (Node<K,V> e = f, pred = null;; ++binCount) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1818
                                K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1819
                                if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1820
                                    ((ek = e.key) == key ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1821
                                     (ek != null && key.equals(ek)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1822
                                    val = remappingFunction.apply(key, e.val);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1823
                                    if (val != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1824
                                        e.val = val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1825
                                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1826
                                        delta = -1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1827
                                        Node<K,V> en = e.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1828
                                        if (pred != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1829
                                            pred.next = en;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1830
                                        else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1831
                                            setTabAt(tab, i, en);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1832
                                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1833
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1834
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1835
                                pred = e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1836
                                if ((e = e.next) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1837
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1838
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1839
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1840
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1841
                            binCount = 2;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1842
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1843
                            TreeNode<K,V> r, p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1844
                            if ((r = t.root) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1845
                                (p = r.findTreeNode(h, key, null)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1846
                                val = remappingFunction.apply(key, p.val);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1847
                                if (val != null)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1848
                                    p.val = val;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1849
                                else {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1850
                                    delta = -1;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1851
                                    if (t.removeTreeNode(p))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1852
                                        setTabAt(tab, i, untreeify(t.first));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1853
                                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1854
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1855
                        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1856
                        else if (f instanceof ReservationNode)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1857
                            throw new IllegalStateException("Recursive update");
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1858
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1859
                }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1860
                if (binCount != 0)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1861
                    break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1862
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1863
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1864
        if (delta != 0)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1865
            addCount((long)delta, binCount);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1866
        return val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1867
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1868
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1869
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1870
     * Attempts to compute a mapping for the specified key and its
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1871
     * current mapped value (or {@code null} if there is no current
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1872
     * mapping). The entire method invocation is performed atomically.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1873
     * Some attempted update operations on this map by other threads
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1874
     * may be blocked while computation is in progress, so the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1875
     * computation should be short and simple, and must not attempt to
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1876
     * update any other mappings of this Map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1877
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1878
     * @param key key with which the specified value is to be associated
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1879
     * @param remappingFunction the function to compute a value
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1880
     * @return the new value associated with the specified key, or null if none
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1881
     * @throws NullPointerException if the specified key or remappingFunction
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1882
     *         is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1883
     * @throws IllegalStateException if the computation detectably
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1884
     *         attempts a recursive update to this map that would
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1885
     *         otherwise never complete
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1886
     * @throws RuntimeException or Error if the remappingFunction does so,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1887
     *         in which case the mapping is unchanged
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1888
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1889
    public V compute(K key,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1890
                     BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1891
        if (key == null || remappingFunction == null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1892
            throw new NullPointerException();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1893
        int h = spread(key.hashCode());
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1894
        V val = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1895
        int delta = 0;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1896
        int binCount = 0;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1897
        for (Node<K,V>[] tab = table;;) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1898
            Node<K,V> f; int n, i, fh;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1899
            if (tab == null || (n = tab.length) == 0)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1900
                tab = initTable();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1901
            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1902
                Node<K,V> r = new ReservationNode<K,V>();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1903
                synchronized (r) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1904
                    if (casTabAt(tab, i, null, r)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1905
                        binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1906
                        Node<K,V> node = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1907
                        try {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1908
                            if ((val = remappingFunction.apply(key, null)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1909
                                delta = 1;
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  1910
                                node = new Node<K,V>(h, key, val);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1911
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1912
                        } finally {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1913
                            setTabAt(tab, i, node);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1914
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1915
                    }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1916
                }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1917
                if (binCount != 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1918
                    break;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1919
            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1920
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1921
                tab = helpTransfer(tab, f);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1922
            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1923
                synchronized (f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1924
                    if (tabAt(tab, i) == f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1925
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1926
                            binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1927
                            for (Node<K,V> e = f, pred = null;; ++binCount) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1928
                                K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1929
                                if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1930
                                    ((ek = e.key) == key ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1931
                                     (ek != null && key.equals(ek)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1932
                                    val = remappingFunction.apply(key, e.val);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1933
                                    if (val != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1934
                                        e.val = val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1935
                                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1936
                                        delta = -1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1937
                                        Node<K,V> en = e.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1938
                                        if (pred != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1939
                                            pred.next = en;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1940
                                        else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1941
                                            setTabAt(tab, i, en);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1942
                                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1943
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1944
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1945
                                pred = e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1946
                                if ((e = e.next) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1947
                                    val = remappingFunction.apply(key, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1948
                                    if (val != null) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1949
                                        if (pred.next != null)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1950
                                            throw new IllegalStateException("Recursive update");
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1951
                                        delta = 1;
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  1952
                                        pred.next = new Node<K,V>(h, key, val);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1953
                                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1954
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1955
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1956
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1957
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1958
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1959
                            binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1960
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1961
                            TreeNode<K,V> r, p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1962
                            if ((r = t.root) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1963
                                p = r.findTreeNode(h, key, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1964
                            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1965
                                p = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1966
                            V pv = (p == null) ? null : p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1967
                            val = remappingFunction.apply(key, pv);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1968
                            if (val != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1969
                                if (p != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1970
                                    p.val = val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1971
                                else {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1972
                                    delta = 1;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1973
                                    t.putTreeVal(h, key, val);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1974
                                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1975
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1976
                            else if (p != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1977
                                delta = -1;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1978
                                if (t.removeTreeNode(p))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1979
                                    setTabAt(tab, i, untreeify(t.first));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1980
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1981
                        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1982
                        else if (f instanceof ReservationNode)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1983
                            throw new IllegalStateException("Recursive update");
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1984
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1985
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1986
                if (binCount != 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1987
                    if (binCount >= TREEIFY_THRESHOLD)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1988
                        treeifyBin(tab, i);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1989
                    break;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1990
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1991
            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1992
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1993
        if (delta != 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1994
            addCount((long)delta, binCount);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1995
        return val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1996
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1997
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1998
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1999
     * If the specified key is not already associated with a
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2000
     * (non-null) value, associates it with the given value.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2001
     * Otherwise, replaces the value with the results of the given
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2002
     * remapping function, or removes if {@code null}. The entire
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2003
     * method invocation is performed atomically.  Some attempted
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2004
     * update operations on this map by other threads may be blocked
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2005
     * while computation is in progress, so the computation should be
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2006
     * short and simple, and must not attempt to update any other
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2007
     * mappings of this Map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2008
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2009
     * @param key key with which the specified value is to be associated
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2010
     * @param value the value to use if absent
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2011
     * @param remappingFunction the function to recompute a value if present
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2012
     * @return the new value associated with the specified key, or null if none
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2013
     * @throws NullPointerException if the specified key or the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2014
     *         remappingFunction is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2015
     * @throws RuntimeException or Error if the remappingFunction does so,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2016
     *         in which case the mapping is unchanged
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2017
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2018
    public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2019
        if (key == null || value == null || remappingFunction == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2020
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2021
        int h = spread(key.hashCode());
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2022
        V val = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2023
        int delta = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2024
        int binCount = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2025
        for (Node<K,V>[] tab = table;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2026
            Node<K,V> f; int n, i, fh;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2027
            if (tab == null || (n = tab.length) == 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2028
                tab = initTable();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2029
            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  2030
                if (casTabAt(tab, i, null, new Node<K,V>(h, key, value))) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2031
                    delta = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2032
                    val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2033
                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2034
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2035
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2036
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2037
                tab = helpTransfer(tab, f);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2038
            else {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2039
                synchronized (f) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2040
                    if (tabAt(tab, i) == f) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2041
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2042
                            binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2043
                            for (Node<K,V> e = f, pred = null;; ++binCount) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2044
                                K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2045
                                if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2046
                                    ((ek = e.key) == key ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2047
                                     (ek != null && key.equals(ek)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2048
                                    val = remappingFunction.apply(e.val, value);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2049
                                    if (val != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2050
                                        e.val = val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2051
                                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2052
                                        delta = -1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2053
                                        Node<K,V> en = e.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2054
                                        if (pred != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2055
                                            pred.next = en;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2056
                                        else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2057
                                            setTabAt(tab, i, en);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2058
                                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2059
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2060
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2061
                                pred = e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2062
                                if ((e = e.next) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2063
                                    delta = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2064
                                    val = value;
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  2065
                                    pred.next = new Node<K,V>(h, key, val);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2066
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2067
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2068
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2069
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2070
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2071
                            binCount = 2;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2072
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2073
                            TreeNode<K,V> r = t.root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2074
                            TreeNode<K,V> p = (r == null) ? null :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2075
                                r.findTreeNode(h, key, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2076
                            val = (p == null) ? value :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2077
                                remappingFunction.apply(p.val, value);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2078
                            if (val != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2079
                                if (p != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2080
                                    p.val = val;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2081
                                else {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2082
                                    delta = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2083
                                    t.putTreeVal(h, key, val);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2084
                                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2085
                            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2086
                            else if (p != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2087
                                delta = -1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2088
                                if (t.removeTreeNode(p))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2089
                                    setTabAt(tab, i, untreeify(t.first));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2090
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2091
                        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2092
                        else if (f instanceof ReservationNode)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2093
                            throw new IllegalStateException("Recursive update");
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2094
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2095
                }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2096
                if (binCount != 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2097
                    if (binCount >= TREEIFY_THRESHOLD)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2098
                        treeifyBin(tab, i);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2099
                    break;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2100
                }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2101
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2102
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2103
        if (delta != 0)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2104
            addCount((long)delta, binCount);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2105
        return val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2106
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2107
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2108
    // Hashtable legacy methods
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2109
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2110
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2111
     * Tests if some key maps into the specified value in this table.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2112
     *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2113
     * <p>Note that this method is identical in functionality to
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2114
     * {@link #containsValue(Object)}, and exists solely to ensure
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2115
     * full compatibility with class {@link java.util.Hashtable},
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2116
     * which supported this method prior to introduction of the
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2117
     * Java Collections Framework.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2118
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2119
     * @param  value a value to search for
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2120
     * @return {@code true} if and only if some key maps to the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2121
     *         {@code value} argument in this table as
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2122
     *         determined by the {@code equals} method;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2123
     *         {@code false} otherwise
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2124
     * @throws NullPointerException if the specified value is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2125
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2126
    public boolean contains(Object value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2127
        return containsValue(value);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2128
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2129
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2130
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2131
     * Returns an enumeration of the keys in this table.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2132
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2133
     * @return an enumeration of the keys in this table
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2134
     * @see #keySet()
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2135
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2136
    public Enumeration<K> keys() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2137
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2138
        int f = (t = table) == null ? 0 : t.length;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2139
        return new KeyIterator<K,V>(t, f, 0, f, this);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2140
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2141
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2142
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2143
     * Returns an enumeration of the values in this table.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2144
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2145
     * @return an enumeration of the values in this table
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2146
     * @see #values()
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2147
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2148
    public Enumeration<V> elements() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2149
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2150
        int f = (t = table) == null ? 0 : t.length;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2151
        return new ValueIterator<K,V>(t, f, 0, f, this);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2152
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2153
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2154
    // ConcurrentHashMap-only methods
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2155
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2156
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2157
     * Returns the number of mappings. This method should be used
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2158
     * instead of {@link #size} because a ConcurrentHashMap may
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2159
     * contain more mappings than can be represented as an int. The
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2160
     * value returned is an estimate; the actual count may differ if
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2161
     * there are concurrent insertions or removals.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2162
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2163
     * @return the number of mappings
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2164
     * @since 1.8
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2165
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2166
    public long mappingCount() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2167
        long n = sumCount();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2168
        return (n < 0L) ? 0L : n; // ignore transient negative values
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2169
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2170
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2171
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2172
     * Creates a new {@link Set} backed by a ConcurrentHashMap
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2173
     * from the given type to {@code Boolean.TRUE}.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2174
     *
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2175
     * @param <K> the element type of the returned set
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2176
     * @return the new set
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2177
     * @since 1.8
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2178
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2179
    public static <K> KeySetView<K,Boolean> newKeySet() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2180
        return new KeySetView<K,Boolean>
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2181
            (new ConcurrentHashMap<K,Boolean>(), Boolean.TRUE);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2182
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2183
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2184
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2185
     * Creates a new {@link Set} backed by a ConcurrentHashMap
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2186
     * from the given type to {@code Boolean.TRUE}.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2187
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2188
     * @param initialCapacity The implementation performs internal
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2189
     * sizing to accommodate this many elements.
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2190
     * @param <K> the element type of the returned set
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2191
     * @return the new set
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2192
     * @throws IllegalArgumentException if the initial capacity of
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2193
     * elements is negative
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2194
     * @since 1.8
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2195
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2196
    public static <K> KeySetView<K,Boolean> newKeySet(int initialCapacity) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2197
        return new KeySetView<K,Boolean>
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2198
            (new ConcurrentHashMap<K,Boolean>(initialCapacity), Boolean.TRUE);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2199
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2200
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2201
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2202
     * Returns a {@link Set} view of the keys in this map, using the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2203
     * given common mapped value for any additions (i.e., {@link
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2204
     * Collection#add} and {@link Collection#addAll(Collection)}).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2205
     * This is of course only appropriate if it is acceptable to use
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2206
     * the same value for all additions from this view.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2207
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2208
     * @param mappedValue the mapped value to use for any additions
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2209
     * @return the set view
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2210
     * @throws NullPointerException if the mappedValue is null
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2211
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2212
    public KeySetView<K,V> keySet(V mappedValue) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2213
        if (mappedValue == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2214
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2215
        return new KeySetView<K,V>(this, mappedValue);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2216
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2217
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2218
    /* ---------------- Special Nodes -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2219
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2220
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2221
     * A node inserted at head of bins during transfer operations.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2222
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2223
    static final class ForwardingNode<K,V> extends Node<K,V> {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2224
        final Node<K,V>[] nextTable;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2225
        ForwardingNode(Node<K,V>[] tab) {
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  2226
            super(MOVED, null, null);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2227
            this.nextTable = tab;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2228
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2229
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2230
        Node<K,V> find(int h, Object k) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2231
            // loop to avoid arbitrarily deep recursion on forwarding nodes
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2232
            outer: for (Node<K,V>[] tab = nextTable;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2233
                Node<K,V> e; int n;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2234
                if (k == null || tab == null || (n = tab.length) == 0 ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2235
                    (e = tabAt(tab, (n - 1) & h)) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2236
                    return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2237
                for (;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2238
                    int eh; K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2239
                    if ((eh = e.hash) == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2240
                        ((ek = e.key) == k || (ek != null && k.equals(ek))))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2241
                        return e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2242
                    if (eh < 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2243
                        if (e instanceof ForwardingNode) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2244
                            tab = ((ForwardingNode<K,V>)e).nextTable;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2245
                            continue outer;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2246
                        }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2247
                        else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2248
                            return e.find(h, k);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2249
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2250
                    if ((e = e.next) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2251
                        return null;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2252
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2253
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2254
        }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2255
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2256
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2257
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2258
     * A place-holder node used in computeIfAbsent and compute.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2259
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2260
    static final class ReservationNode<K,V> extends Node<K,V> {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2261
        ReservationNode() {
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  2262
            super(RESERVED, null, null);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2263
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2264
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2265
        Node<K,V> find(int h, Object k) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2266
            return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2267
        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2268
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2269
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2270
    /* ---------------- Table Initialization and Resizing -------------- */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2271
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2272
    /**
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2273
     * Returns the stamp bits for resizing a table of size n.
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2274
     * Must be negative when shifted left by RESIZE_STAMP_SHIFT.
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2275
     */
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2276
    static final int resizeStamp(int n) {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2277
        return Integer.numberOfLeadingZeros(n) | (1 << (RESIZE_STAMP_BITS - 1));
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2278
    }
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2279
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2280
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2281
     * Initializes table, using the size recorded in sizeCtl.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2282
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2283
    private final Node<K,V>[] initTable() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2284
        Node<K,V>[] tab; int sc;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2285
        while ((tab = table) == null || tab.length == 0) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2286
            if ((sc = sizeCtl) < 0)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2287
                Thread.yield(); // lost initialization race; just spin
45065
0c9f6b245cf1 8159995: Rename internal Unsafe.compare methods
psandoz
parents: 44926
diff changeset
  2288
            else if (U.compareAndSetInt(this, SIZECTL, sc, -1)) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2289
                try {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2290
                    if ((tab = table) == null || tab.length == 0) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2291
                        int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2292
                        @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2293
                        Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2294
                        table = tab = nt;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2295
                        sc = n - (n >>> 2);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2296
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2297
                } finally {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2298
                    sizeCtl = sc;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2299
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2300
                break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2301
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2302
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2303
        return tab;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2304
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2305
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2306
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2307
     * Adds to count, and if table is too small and not already
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2308
     * resizing, initiates transfer. If already resizing, helps
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2309
     * perform transfer if work is available.  Rechecks occupancy
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2310
     * after a transfer to see if another resize is already needed
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2311
     * because resizings are lagging additions.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2312
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2313
     * @param x the count to add
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2314
     * @param check if <0, don't check resize, if <= 1 only check if uncontended
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2315
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2316
    private final void addCount(long x, int check) {
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47307
diff changeset
  2317
        CounterCell[] cs; long b, s;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47307
diff changeset
  2318
        if ((cs = counterCells) != null ||
45065
0c9f6b245cf1 8159995: Rename internal Unsafe.compare methods
psandoz
parents: 44926
diff changeset
  2319
            !U.compareAndSetLong(this, BASECOUNT, b = baseCount, s = b + x)) {
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47307
diff changeset
  2320
            CounterCell c; long v; int m;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2321
            boolean uncontended = true;
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47307
diff changeset
  2322
            if (cs == null || (m = cs.length - 1) < 0 ||
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47307
diff changeset
  2323
                (c = cs[ThreadLocalRandom.getProbe() & m]) == null ||
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2324
                !(uncontended =
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47307
diff changeset
  2325
                  U.compareAndSetLong(c, CELLVALUE, v = c.value, v + x))) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2326
                fullAddCount(x, uncontended);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2327
                return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2328
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2329
            if (check <= 1)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2330
                return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2331
            s = sumCount();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2332
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2333
        if (check >= 0) {
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2334
            Node<K,V>[] tab, nt; int n, sc;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2335
            while (s >= (long)(sc = sizeCtl) && (tab = table) != null &&
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2336
                   (n = tab.length) < MAXIMUM_CAPACITY) {
52958
b4eaf570a588 8214427: probable bug in logic of ConcurrentHashMap.addCount()
dl
parents: 52730
diff changeset
  2337
                int rs = resizeStamp(n) << RESIZE_STAMP_SHIFT;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2338
                if (sc < 0) {
52958
b4eaf570a588 8214427: probable bug in logic of ConcurrentHashMap.addCount()
dl
parents: 52730
diff changeset
  2339
                    if (sc == rs + MAX_RESIZERS || sc == rs + 1 ||
b4eaf570a588 8214427: probable bug in logic of ConcurrentHashMap.addCount()
dl
parents: 52730
diff changeset
  2340
                        (nt = nextTable) == null || transferIndex <= 0)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2341
                        break;
45065
0c9f6b245cf1 8159995: Rename internal Unsafe.compare methods
psandoz
parents: 44926
diff changeset
  2342
                    if (U.compareAndSetInt(this, SIZECTL, sc, sc + 1))
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2343
                        transfer(tab, nt);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2344
                }
52958
b4eaf570a588 8214427: probable bug in logic of ConcurrentHashMap.addCount()
dl
parents: 52730
diff changeset
  2345
                else if (U.compareAndSetInt(this, SIZECTL, sc, rs + 2))
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2346
                    transfer(tab, null);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2347
                s = sumCount();
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2348
            }
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2349
        }
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2350
    }
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2351
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2352
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2353
     * Helps transfer if a resize is in progress.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2354
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2355
    final Node<K,V>[] helpTransfer(Node<K,V>[] tab, Node<K,V> f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2356
        Node<K,V>[] nextTab; int sc;
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2357
        if (tab != null && (f instanceof ForwardingNode) &&
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2358
            (nextTab = ((ForwardingNode<K,V>)f).nextTable) != null) {
52958
b4eaf570a588 8214427: probable bug in logic of ConcurrentHashMap.addCount()
dl
parents: 52730
diff changeset
  2359
            int rs = resizeStamp(tab.length) << RESIZE_STAMP_SHIFT;
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2360
            while (nextTab == nextTable && table == tab &&
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2361
                   (sc = sizeCtl) < 0) {
52958
b4eaf570a588 8214427: probable bug in logic of ConcurrentHashMap.addCount()
dl
parents: 52730
diff changeset
  2362
                if (sc == rs + MAX_RESIZERS || sc == rs + 1 ||
b4eaf570a588 8214427: probable bug in logic of ConcurrentHashMap.addCount()
dl
parents: 52730
diff changeset
  2363
                    transferIndex <= 0)
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2364
                    break;
45065
0c9f6b245cf1 8159995: Rename internal Unsafe.compare methods
psandoz
parents: 44926
diff changeset
  2365
                if (U.compareAndSetInt(this, SIZECTL, sc, sc + 1)) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2366
                    transfer(tab, nextTab);
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2367
                    break;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2368
                }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2369
            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2370
            return nextTab;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2371
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2372
        return table;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2373
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2374
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2375
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2376
     * Tries to presize table to accommodate the given number of elements.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2377
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2378
     * @param size number of elements (doesn't need to be perfectly accurate)
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2379
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2380
    private final void tryPresize(int size) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2381
        int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2382
            tableSizeFor(size + (size >>> 1) + 1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2383
        int sc;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2384
        while ((sc = sizeCtl) >= 0) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2385
            Node<K,V>[] tab = table; int n;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2386
            if (tab == null || (n = tab.length) == 0) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2387
                n = (sc > c) ? sc : c;
45065
0c9f6b245cf1 8159995: Rename internal Unsafe.compare methods
psandoz
parents: 44926
diff changeset
  2388
                if (U.compareAndSetInt(this, SIZECTL, sc, -1)) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2389
                    try {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2390
                        if (table == tab) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2391
                            @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2392
                            Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2393
                            table = nt;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2394
                            sc = n - (n >>> 2);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2395
                        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2396
                    } finally {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2397
                        sizeCtl = sc;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2398
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2399
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2400
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2401
            else if (c <= sc || n >= MAXIMUM_CAPACITY)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2402
                break;
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2403
            else if (tab == table) {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2404
                int rs = resizeStamp(n);
45065
0c9f6b245cf1 8159995: Rename internal Unsafe.compare methods
psandoz
parents: 44926
diff changeset
  2405
                if (U.compareAndSetInt(this, SIZECTL, sc,
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2406
                                        (rs << RESIZE_STAMP_SHIFT) + 2))
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2407
                    transfer(tab, null);
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2408
            }
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 11279
diff changeset
  2409
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2413
     * Moves and/or copies the nodes in each bin to new table. See
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2414
     * above for explanation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2416
    private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2417
        int n = tab.length, stride;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2418
        if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2419
            stride = MIN_TRANSFER_STRIDE; // subdivide range
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2420
        if (nextTab == null) {            // initiating
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2421
            try {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2422
                @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2423
                Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n << 1];
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2424
                nextTab = nt;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2425
            } catch (Throwable ex) {      // try to cope with OOME
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2426
                sizeCtl = Integer.MAX_VALUE;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2427
                return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2428
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2429
            nextTable = nextTab;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2430
            transferIndex = n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2432
        int nextn = nextTab.length;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2433
        ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2434
        boolean advance = true;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2435
        boolean finishing = false; // to ensure sweep before committing nextTab
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2436
        for (int i = 0, bound = 0;;) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2437
            Node<K,V> f; int fh;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2438
            while (advance) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2439
                int nextIndex, nextBound;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2440
                if (--i >= bound || finishing)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2441
                    advance = false;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2442
                else if ((nextIndex = transferIndex) <= 0) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2443
                    i = -1;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2444
                    advance = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2445
                }
45065
0c9f6b245cf1 8159995: Rename internal Unsafe.compare methods
psandoz
parents: 44926
diff changeset
  2446
                else if (U.compareAndSetInt
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2447
                         (this, TRANSFERINDEX, nextIndex,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2448
                          nextBound = (nextIndex > stride ?
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2449
                                       nextIndex - stride : 0))) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2450
                    bound = nextBound;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2451
                    i = nextIndex - 1;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2452
                    advance = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2453
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2454
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2455
            if (i < 0 || i >= n || i + n >= nextn) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2456
                int sc;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2457
                if (finishing) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2458
                    nextTable = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2459
                    table = nextTab;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2460
                    sizeCtl = (n << 1) - (n >>> 1);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2461
                    return;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2462
                }
45065
0c9f6b245cf1 8159995: Rename internal Unsafe.compare methods
psandoz
parents: 44926
diff changeset
  2463
                if (U.compareAndSetInt(this, SIZECTL, sc = sizeCtl, sc - 1)) {
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2464
                    if ((sc - 2) != resizeStamp(n) << RESIZE_STAMP_SHIFT)
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2465
                        return;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2466
                    finishing = advance = true;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2467
                    i = n; // recheck before commit
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2468
                }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2469
            }
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2470
            else if ((f = tabAt(tab, i)) == null)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2471
                advance = casTabAt(tab, i, null, fwd);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2472
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2473
                advance = true; // already processed
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2474
            else {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2475
                synchronized (f) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2476
                    if (tabAt(tab, i) == f) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2477
                        Node<K,V> ln, hn;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2478
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2479
                            int runBit = fh & n;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2480
                            Node<K,V> lastRun = f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2481
                            for (Node<K,V> p = f.next; p != null; p = p.next) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2482
                                int b = p.hash & n;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2483
                                if (b != runBit) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2484
                                    runBit = b;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2485
                                    lastRun = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2486
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2487
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2488
                            if (runBit == 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2489
                                ln = lastRun;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2490
                                hn = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
                            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2492
                            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2493
                                hn = lastRun;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2494
                                ln = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2495
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2496
                            for (Node<K,V> p = f; p != lastRun; p = p.next) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2497
                                int ph = p.hash; K pk = p.key; V pv = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2498
                                if ((ph & n) == 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2499
                                    ln = new Node<K,V>(ph, pk, pv, ln);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2500
                                else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2501
                                    hn = new Node<K,V>(ph, pk, pv, hn);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2502
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2503
                            setTabAt(nextTab, i, ln);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2504
                            setTabAt(nextTab, i + n, hn);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2505
                            setTabAt(tab, i, fwd);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2506
                            advance = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
                        }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2508
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2509
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2510
                            TreeNode<K,V> lo = null, loTail = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2511
                            TreeNode<K,V> hi = null, hiTail = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2512
                            int lc = 0, hc = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2513
                            for (Node<K,V> e = t.first; e != null; e = e.next) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2514
                                int h = e.hash;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2515
                                TreeNode<K,V> p = new TreeNode<K,V>
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2516
                                    (h, e.key, e.val, null, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2517
                                if ((h & n) == 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2518
                                    if ((p.prev = loTail) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2519
                                        lo = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2520
                                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2521
                                        loTail.next = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2522
                                    loTail = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2523
                                    ++lc;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2524
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2525
                                else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2526
                                    if ((p.prev = hiTail) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2527
                                        hi = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2528
                                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2529
                                        hiTail.next = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2530
                                    hiTail = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2531
                                    ++hc;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2532
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2533
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2534
                            ln = (lc <= UNTREEIFY_THRESHOLD) ? untreeify(lo) :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2535
                                (hc != 0) ? new TreeBin<K,V>(lo) : t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2536
                            hn = (hc <= UNTREEIFY_THRESHOLD) ? untreeify(hi) :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2537
                                (lc != 0) ? new TreeBin<K,V>(hi) : t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2538
                            setTabAt(nextTab, i, ln);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2539
                            setTabAt(nextTab, i + n, hn);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2540
                            setTabAt(tab, i, fwd);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2541
                            advance = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
                        }
52730
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 52220
diff changeset
  2543
                        else if (f instanceof ReservationNode)
345266000aba 8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents: 52220
diff changeset
  2544
                            throw new IllegalStateException("Recursive update");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
            }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2548
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2549
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2550
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2551
    /* ---------------- Counter support -------------- */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2552
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2553
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2554
     * A padded cell for distributing counts.  Adapted from LongAdder
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2555
     * and Striped64.  See their internal docs for explanation.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2556
     */
34369
b6df4cc80001 8140687: Move @Contended to the jdk.internal.vm.annotation package
chegar
parents: 33674
diff changeset
  2557
    @jdk.internal.vm.annotation.Contended static final class CounterCell {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2558
        volatile long value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2559
        CounterCell(long x) { value = x; }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2560
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2561
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2562
    final long sumCount() {
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47307
diff changeset
  2563
        CounterCell[] cs = counterCells;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2564
        long sum = baseCount;
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47307
diff changeset
  2565
        if (cs != null) {
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47307
diff changeset
  2566
            for (CounterCell c : cs)
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47307
diff changeset
  2567
                if (c != null)
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47307
diff changeset
  2568
                    sum += c.value;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2569
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2570
        return sum;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2571
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2572
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2573
    // See LongAdder version for explanation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2574
    private final void fullAddCount(long x, boolean wasUncontended) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2575
        int h;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2576
        if ((h = ThreadLocalRandom.getProbe()) == 0) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2577
            ThreadLocalRandom.localInit();      // force initialization
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2578
            h = ThreadLocalRandom.getProbe();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2579
            wasUncontended = true;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2580
        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2581
        boolean collide = false;                // True if last slot nonempty
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2582
        for (;;) {
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47307
diff changeset
  2583
            CounterCell[] cs; CounterCell c; int n; long v;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47307
diff changeset
  2584
            if ((cs = counterCells) != null && (n = cs.length) > 0) {
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47307
diff changeset
  2585
                if ((c = cs[(n - 1) & h]) == null) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2586
                    if (cellsBusy == 0) {            // Try to attach new Cell
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2587
                        CounterCell r = new CounterCell(x); // Optimistic create
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2588
                        if (cellsBusy == 0 &&
45065
0c9f6b245cf1 8159995: Rename internal Unsafe.compare methods
psandoz
parents: 44926
diff changeset
  2589
                            U.compareAndSetInt(this, CELLSBUSY, 0, 1)) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2590
                            boolean created = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2591
                            try {               // Recheck under lock
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2592
                                CounterCell[] rs; int m, j;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2593
                                if ((rs = counterCells) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2594
                                    (m = rs.length) > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2595
                                    rs[j = (m - 1) & h] == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2596
                                    rs[j] = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2597
                                    created = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2598
                                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2599
                            } finally {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2600
                                cellsBusy = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2601
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2602
                            if (created)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2603
                                break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2604
                            continue;           // Slot is now non-empty
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2605
                        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2606
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2607
                    collide = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2608
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2609
                else if (!wasUncontended)       // CAS already known to fail
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2610
                    wasUncontended = true;      // Continue after rehash
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47307
diff changeset
  2611
                else if (U.compareAndSetLong(c, CELLVALUE, v = c.value, v + x))
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2612
                    break;
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47307
diff changeset
  2613
                else if (counterCells != cs || n >= NCPU)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2614
                    collide = false;            // At max size or stale
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2615
                else if (!collide)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2616
                    collide = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2617
                else if (cellsBusy == 0 &&
45065
0c9f6b245cf1 8159995: Rename internal Unsafe.compare methods
psandoz
parents: 44926
diff changeset
  2618
                         U.compareAndSetInt(this, CELLSBUSY, 0, 1)) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2619
                    try {
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47307
diff changeset
  2620
                        if (counterCells == cs) // Expand table unless stale
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47307
diff changeset
  2621
                            counterCells = Arrays.copyOf(cs, n << 1);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2622
                    } finally {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2623
                        cellsBusy = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2624
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2625
                    collide = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2626
                    continue;                   // Retry with expanded table
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2627
                }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2628
                h = ThreadLocalRandom.advanceProbe(h);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2629
            }
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47307
diff changeset
  2630
            else if (cellsBusy == 0 && counterCells == cs &&
45065
0c9f6b245cf1 8159995: Rename internal Unsafe.compare methods
psandoz
parents: 44926
diff changeset
  2631
                     U.compareAndSetInt(this, CELLSBUSY, 0, 1)) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2632
                boolean init = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2633
                try {                           // Initialize table
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47307
diff changeset
  2634
                    if (counterCells == cs) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2635
                        CounterCell[] rs = new CounterCell[2];
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2636
                        rs[h & 1] = new CounterCell(x);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2637
                        counterCells = rs;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2638
                        init = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2639
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2640
                } finally {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2641
                    cellsBusy = 0;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2642
                }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2643
                if (init)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2644
                    break;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2645
            }
45065
0c9f6b245cf1 8159995: Rename internal Unsafe.compare methods
psandoz
parents: 44926
diff changeset
  2646
            else if (U.compareAndSetLong(this, BASECOUNT, v = baseCount, v + x))
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2647
                break;                          // Fall back on using base
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2648
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2649
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2650
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2651
    /* ---------------- Conversion from/to TreeBins -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2652
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2653
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2654
     * Replaces all linked nodes in bin at given index unless table is
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2655
     * too small, in which case resizes instead.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2656
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2657
    private final void treeifyBin(Node<K,V>[] tab, int index) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2658
        Node<K,V> b; int n;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2659
        if (tab != null) {
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2660
            if ((n = tab.length) < MIN_TREEIFY_CAPACITY)
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2661
                tryPresize(n << 1);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2662
            else if ((b = tabAt(tab, index)) != null && b.hash >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2663
                synchronized (b) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2664
                    if (tabAt(tab, index) == b) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2665
                        TreeNode<K,V> hd = null, tl = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2666
                        for (Node<K,V> e = b; e != null; e = e.next) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2667
                            TreeNode<K,V> p =
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2668
                                new TreeNode<K,V>(e.hash, e.key, e.val,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2669
                                                  null, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2670
                            if ((p.prev = tl) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2671
                                hd = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2672
                            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2673
                                tl.next = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2674
                            tl = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2675
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2676
                        setTabAt(tab, index, new TreeBin<K,V>(hd));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2677
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2678
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2679
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2680
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2681
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2682
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2683
    /**
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  2684
     * Returns a list of non-TreeNodes replacing those in given list.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2685
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2686
    static <K,V> Node<K,V> untreeify(Node<K,V> b) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2687
        Node<K,V> hd = null, tl = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2688
        for (Node<K,V> q = b; q != null; q = q.next) {
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  2689
            Node<K,V> p = new Node<K,V>(q.hash, q.key, q.val);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2690
            if (tl == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2691
                hd = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2692
            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2693
                tl.next = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2694
            tl = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2695
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2696
        return hd;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2697
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2698
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2699
    /* ---------------- TreeNodes -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2700
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2701
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2702
     * Nodes for use in TreeBins.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2703
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2704
    static final class TreeNode<K,V> extends Node<K,V> {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2705
        TreeNode<K,V> parent;  // red-black tree links
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2706
        TreeNode<K,V> left;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2707
        TreeNode<K,V> right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2708
        TreeNode<K,V> prev;    // needed to unlink next upon deletion
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2709
        boolean red;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2710
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2711
        TreeNode(int hash, K key, V val, Node<K,V> next,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2712
                 TreeNode<K,V> parent) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2713
            super(hash, key, val, next);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2714
            this.parent = parent;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2715
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2716
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2717
        Node<K,V> find(int h, Object k) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2718
            return findTreeNode(h, k, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2719
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2720
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2721
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2722
         * Returns the TreeNode (or null if not found) for the given key
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2723
         * starting at given root.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2724
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2725
        final TreeNode<K,V> findTreeNode(int h, Object k, Class<?> kc) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2726
            if (k != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2727
                TreeNode<K,V> p = this;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2728
                do {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2729
                    int ph, dir; K pk; TreeNode<K,V> q;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2730
                    TreeNode<K,V> pl = p.left, pr = p.right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2731
                    if ((ph = p.hash) > h)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2732
                        p = pl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2733
                    else if (ph < h)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2734
                        p = pr;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2735
                    else if ((pk = p.key) == k || (pk != null && k.equals(pk)))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2736
                        return p;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2737
                    else if (pl == null)
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2738
                        p = pr;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2739
                    else if (pr == null)
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2740
                        p = pl;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2741
                    else if ((kc != null ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2742
                              (kc = comparableClassFor(k)) != null) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2743
                             (dir = compareComparables(kc, k, pk)) != 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2744
                        p = (dir < 0) ? pl : pr;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2745
                    else if ((q = pr.findTreeNode(h, k, kc)) != null)
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2746
                        return q;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2747
                    else
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2748
                        p = pl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2749
                } while (p != null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2750
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2751
            return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2752
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2753
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2754
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2755
    /* ---------------- TreeBins -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2756
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2757
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2758
     * TreeNodes used at the heads of bins. TreeBins do not hold user
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2759
     * keys or values, but instead point to list of TreeNodes and
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2760
     * their root. They also maintain a parasitic read-write lock
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2761
     * forcing writers (who hold bin lock) to wait for readers (who do
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2762
     * not) to complete before tree restructuring operations.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2763
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2764
    static final class TreeBin<K,V> extends Node<K,V> {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2765
        TreeNode<K,V> root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2766
        volatile TreeNode<K,V> first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2767
        volatile Thread waiter;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2768
        volatile int lockState;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2769
        // values for lockState
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2770
        static final int WRITER = 1; // set while holding write lock
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2771
        static final int WAITER = 2; // set when waiting for write lock
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2772
        static final int READER = 4; // increment value for setting read lock
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2773
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2774
        /**
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2775
         * Tie-breaking utility for ordering insertions when equal
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2776
         * hashCodes and non-comparable. We don't require a total
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2777
         * order, just a consistent insertion rule to maintain
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2778
         * equivalence across rebalancings. Tie-breaking further than
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2779
         * necessary simplifies testing a bit.
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2780
         */
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2781
        static int tieBreakOrder(Object a, Object b) {
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2782
            int d;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2783
            if (a == null || b == null ||
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2784
                (d = a.getClass().getName().
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2785
                 compareTo(b.getClass().getName())) == 0)
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2786
                d = (System.identityHashCode(a) <= System.identityHashCode(b) ?
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2787
                     -1 : 1);
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2788
            return d;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2789
        }
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2790
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2791
        /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2792
         * Creates bin with initial set of nodes headed by b.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2793
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2794
        TreeBin(TreeNode<K,V> b) {
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  2795
            super(TREEBIN, null, null);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2796
            this.first = b;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2797
            TreeNode<K,V> r = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2798
            for (TreeNode<K,V> x = b, next; x != null; x = next) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2799
                next = (TreeNode<K,V>)x.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2800
                x.left = x.right = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2801
                if (r == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2802
                    x.parent = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2803
                    x.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2804
                    r = x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2805
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2806
                else {
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2807
                    K k = x.key;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2808
                    int h = x.hash;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2809
                    Class<?> kc = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2810
                    for (TreeNode<K,V> p = r;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2811
                        int dir, ph;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2812
                        K pk = p.key;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2813
                        if ((ph = p.hash) > h)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2814
                            dir = -1;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2815
                        else if (ph < h)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2816
                            dir = 1;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2817
                        else if ((kc == null &&
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2818
                                  (kc = comparableClassFor(k)) == null) ||
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2819
                                 (dir = compareComparables(kc, k, pk)) == 0)
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2820
                            dir = tieBreakOrder(k, pk);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2821
                        TreeNode<K,V> xp = p;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2822
                        if ((p = (dir <= 0) ? p.left : p.right) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2823
                            x.parent = xp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2824
                            if (dir <= 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2825
                                xp.left = x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2826
                            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2827
                                xp.right = x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2828
                            r = balanceInsertion(r, x);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2829
                            break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2830
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2831
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2832
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2833
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2834
            this.root = r;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2835
            assert checkInvariants(root);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2836
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2837
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2838
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2839
         * Acquires write lock for tree restructuring.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2840
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2841
        private final void lockRoot() {
45065
0c9f6b245cf1 8159995: Rename internal Unsafe.compare methods
psandoz
parents: 44926
diff changeset
  2842
            if (!U.compareAndSetInt(this, LOCKSTATE, 0, WRITER))
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2843
                contendedLock(); // offload to separate method
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2844
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2845
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2846
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2847
         * Releases write lock for tree restructuring.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2848
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2849
        private final void unlockRoot() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2850
            lockState = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2851
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2852
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2853
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2854
         * Possibly blocks awaiting root lock.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2855
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2856
        private final void contendedLock() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2857
            boolean waiting = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2858
            for (int s;;) {
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2859
                if (((s = lockState) & ~WAITER) == 0) {
45065
0c9f6b245cf1 8159995: Rename internal Unsafe.compare methods
psandoz
parents: 44926
diff changeset
  2860
                    if (U.compareAndSetInt(this, LOCKSTATE, s, WRITER)) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2861
                        if (waiting)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2862
                            waiter = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2863
                        return;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2864
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2865
                }
19413
56082c988815 8023104: ConcurrentHashMap typo
dl
parents: 19380
diff changeset
  2866
                else if ((s & WAITER) == 0) {
45065
0c9f6b245cf1 8159995: Rename internal Unsafe.compare methods
psandoz
parents: 44926
diff changeset
  2867
                    if (U.compareAndSetInt(this, LOCKSTATE, s, s | WAITER)) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2868
                        waiting = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2869
                        waiter = Thread.currentThread();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2870
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2871
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2872
                else if (waiting)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2873
                    LockSupport.park(this);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2874
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2875
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2876
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2877
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2878
         * Returns matching node or null if none. Tries to search
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2879
         * using tree comparisons from root, but continues linear
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2880
         * search when lock not available.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2881
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2882
        final Node<K,V> find(int h, Object k) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2883
            if (k != null) {
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2884
                for (Node<K,V> e = first; e != null; ) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2885
                    int s; K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2886
                    if (((s = lockState) & (WAITER|WRITER)) != 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2887
                        if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2888
                            ((ek = e.key) == k || (ek != null && k.equals(ek))))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2889
                            return e;
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2890
                        e = e.next;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2891
                    }
45065
0c9f6b245cf1 8159995: Rename internal Unsafe.compare methods
psandoz
parents: 44926
diff changeset
  2892
                    else if (U.compareAndSetInt(this, LOCKSTATE, s,
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2893
                                                 s + READER)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2894
                        TreeNode<K,V> r, p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2895
                        try {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2896
                            p = ((r = root) == null ? null :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2897
                                 r.findTreeNode(h, k, null));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2898
                        } finally {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2899
                            Thread w;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2900
                            if (U.getAndAddInt(this, LOCKSTATE, -READER) ==
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2901
                                (READER|WAITER) && (w = waiter) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2902
                                LockSupport.unpark(w);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2903
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2904
                        return p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2905
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2906
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2907
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2908
            return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2909
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2910
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2911
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2912
         * Finds or adds a node.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2913
         * @return null if added
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2914
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2915
        final TreeNode<K,V> putTreeVal(int h, K k, V v) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2916
            Class<?> kc = null;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2917
            boolean searched = false;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2918
            for (TreeNode<K,V> p = root;;) {
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2919
                int dir, ph; K pk;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2920
                if (p == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2921
                    first = root = new TreeNode<K,V>(h, k, v, null, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2922
                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2923
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2924
                else if ((ph = p.hash) > h)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2925
                    dir = -1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2926
                else if (ph < h)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2927
                    dir = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2928
                else if ((pk = p.key) == k || (pk != null && k.equals(pk)))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2929
                    return p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2930
                else if ((kc == null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2931
                          (kc = comparableClassFor(k)) == null) ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2932
                         (dir = compareComparables(kc, k, pk)) == 0) {
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2933
                    if (!searched) {
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2934
                        TreeNode<K,V> q, ch;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2935
                        searched = true;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2936
                        if (((ch = p.left) != null &&
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2937
                             (q = ch.findTreeNode(h, k, kc)) != null) ||
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2938
                            ((ch = p.right) != null &&
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2939
                             (q = ch.findTreeNode(h, k, kc)) != null))
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2940
                            return q;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2941
                    }
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2942
                    dir = tieBreakOrder(k, pk);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2943
                }
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2944
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2945
                TreeNode<K,V> xp = p;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2946
                if ((p = (dir <= 0) ? p.left : p.right) == null) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2947
                    TreeNode<K,V> x, f = first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2948
                    first = x = new TreeNode<K,V>(h, k, v, f, xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2949
                    if (f != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2950
                        f.prev = x;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2951
                    if (dir <= 0)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2952
                        xp.left = x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2953
                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2954
                        xp.right = x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2955
                    if (!xp.red)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2956
                        x.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2957
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2958
                        lockRoot();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2959
                        try {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2960
                            root = balanceInsertion(root, x);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2961
                        } finally {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2962
                            unlockRoot();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2963
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2964
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2965
                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2966
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2967
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2968
            assert checkInvariants(root);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2969
            return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2970
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2971
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2972
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2973
         * Removes the given node, that must be present before this
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2974
         * call.  This is messier than typical red-black deletion code
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2975
         * because we cannot swap the contents of an interior node
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2976
         * with a leaf successor that is pinned by "next" pointers
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2977
         * that are accessible independently of lock. So instead we
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2978
         * swap the tree linkages.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2979
         *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2980
         * @return true if now too small, so should be untreeified
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2981
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2982
        final boolean removeTreeNode(TreeNode<K,V> p) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2983
            TreeNode<K,V> next = (TreeNode<K,V>)p.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2984
            TreeNode<K,V> pred = p.prev;  // unlink traversal pointers
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2985
            TreeNode<K,V> r, rl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2986
            if (pred == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2987
                first = next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2988
            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2989
                pred.next = next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2990
            if (next != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2991
                next.prev = pred;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2992
            if (first == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2993
                root = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2994
                return true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2995
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2996
            if ((r = root) == null || r.right == null || // too small
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2997
                (rl = r.left) == null || rl.left == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2998
                return true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2999
            lockRoot();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3000
            try {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3001
                TreeNode<K,V> replacement;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3002
                TreeNode<K,V> pl = p.left;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3003
                TreeNode<K,V> pr = p.right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3004
                if (pl != null && pr != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3005
                    TreeNode<K,V> s = pr, sl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3006
                    while ((sl = s.left) != null) // find successor
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3007
                        s = sl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3008
                    boolean c = s.red; s.red = p.red; p.red = c; // swap colors
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3009
                    TreeNode<K,V> sr = s.right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3010
                    TreeNode<K,V> pp = p.parent;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3011
                    if (s == pr) { // p was s's direct parent
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3012
                        p.parent = s;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3013
                        s.right = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3014
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3015
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3016
                        TreeNode<K,V> sp = s.parent;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3017
                        if ((p.parent = sp) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3018
                            if (s == sp.left)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3019
                                sp.left = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3020
                            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3021
                                sp.right = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3022
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3023
                        if ((s.right = pr) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3024
                            pr.parent = s;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3025
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3026
                    p.left = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3027
                    if ((p.right = sr) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3028
                        sr.parent = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3029
                    if ((s.left = pl) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3030
                        pl.parent = s;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3031
                    if ((s.parent = pp) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3032
                        r = s;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3033
                    else if (p == pp.left)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3034
                        pp.left = s;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3035
                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3036
                        pp.right = s;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3037
                    if (sr != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3038
                        replacement = sr;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3039
                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3040
                        replacement = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3041
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3042
                else if (pl != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3043
                    replacement = pl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3044
                else if (pr != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3045
                    replacement = pr;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3046
                else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3047
                    replacement = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3048
                if (replacement != p) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3049
                    TreeNode<K,V> pp = replacement.parent = p.parent;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3050
                    if (pp == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3051
                        r = replacement;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3052
                    else if (p == pp.left)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3053
                        pp.left = replacement;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3054
                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3055
                        pp.right = replacement;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3056
                    p.left = p.right = p.parent = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3057
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3058
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3059
                root = (p.red) ? r : balanceDeletion(r, replacement);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3060
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3061
                if (p == replacement) {  // detach pointers
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3062
                    TreeNode<K,V> pp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3063
                    if ((pp = p.parent) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3064
                        if (p == pp.left)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3065
                            pp.left = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3066
                        else if (p == pp.right)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3067
                            pp.right = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3068
                        p.parent = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3069
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3070
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3071
            } finally {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3072
                unlockRoot();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3073
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3074
            assert checkInvariants(root);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3075
            return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3076
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3077
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3078
        /* ------------------------------------------------------------ */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3079
        // Red-black tree methods, all adapted from CLR
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3080
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3081
        static <K,V> TreeNode<K,V> rotateLeft(TreeNode<K,V> root,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3082
                                              TreeNode<K,V> p) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3083
            TreeNode<K,V> r, pp, rl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3084
            if (p != null && (r = p.right) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3085
                if ((rl = p.right = r.left) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3086
                    rl.parent = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3087
                if ((pp = r.parent = p.parent) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3088
                    (root = r).red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3089
                else if (pp.left == p)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3090
                    pp.left = r;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3091
                else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3092
                    pp.right = r;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3093
                r.left = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3094
                p.parent = r;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3095
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3096
            return root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3097
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3098
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3099
        static <K,V> TreeNode<K,V> rotateRight(TreeNode<K,V> root,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3100
                                               TreeNode<K,V> p) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3101
            TreeNode<K,V> l, pp, lr;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3102
            if (p != null && (l = p.left) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3103
                if ((lr = p.left = l.right) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3104
                    lr.parent = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3105
                if ((pp = l.parent = p.parent) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3106
                    (root = l).red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3107
                else if (pp.right == p)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3108
                    pp.right = l;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3109
                else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3110
                    pp.left = l;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3111
                l.right = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3112
                p.parent = l;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3113
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3114
            return root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3115
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3116
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3117
        static <K,V> TreeNode<K,V> balanceInsertion(TreeNode<K,V> root,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3118
                                                    TreeNode<K,V> x) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3119
            x.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3120
            for (TreeNode<K,V> xp, xpp, xppl, xppr;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3121
                if ((xp = x.parent) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3122
                    x.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3123
                    return x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3124
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3125
                else if (!xp.red || (xpp = xp.parent) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3126
                    return root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3127
                if (xp == (xppl = xpp.left)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3128
                    if ((xppr = xpp.right) != null && xppr.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3129
                        xppr.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3130
                        xp.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3131
                        xpp.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3132
                        x = xpp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3133
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3134
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3135
                        if (x == xp.right) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3136
                            root = rotateLeft(root, x = xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3137
                            xpp = (xp = x.parent) == null ? null : xp.parent;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3138
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3139
                        if (xp != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3140
                            xp.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3141
                            if (xpp != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3142
                                xpp.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3143
                                root = rotateRight(root, xpp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3144
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3145
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3146
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3147
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3148
                else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3149
                    if (xppl != null && xppl.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3150
                        xppl.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3151
                        xp.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3152
                        xpp.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3153
                        x = xpp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3154
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3155
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3156
                        if (x == xp.left) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3157
                            root = rotateRight(root, x = xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3158
                            xpp = (xp = x.parent) == null ? null : xp.parent;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3159
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3160
                        if (xp != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3161
                            xp.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3162
                            if (xpp != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3163
                                xpp.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3164
                                root = rotateLeft(root, xpp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3165
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3166
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3167
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3168
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3169
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3170
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3171
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3172
        static <K,V> TreeNode<K,V> balanceDeletion(TreeNode<K,V> root,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3173
                                                   TreeNode<K,V> x) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3174
            for (TreeNode<K,V> xp, xpl, xpr;;) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3175
                if (x == null || x == root)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3176
                    return root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3177
                else if ((xp = x.parent) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3178
                    x.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3179
                    return x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3180
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3181
                else if (x.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3182
                    x.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3183
                    return root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3184
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3185
                else if ((xpl = xp.left) == x) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3186
                    if ((xpr = xp.right) != null && xpr.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3187
                        xpr.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3188
                        xp.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3189
                        root = rotateLeft(root, xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3190
                        xpr = (xp = x.parent) == null ? null : xp.right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3191
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3192
                    if (xpr == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3193
                        x = xp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3194
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3195
                        TreeNode<K,V> sl = xpr.left, sr = xpr.right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3196
                        if ((sr == null || !sr.red) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3197
                            (sl == null || !sl.red)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3198
                            xpr.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3199
                            x = xp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3200
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3201
                        else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3202
                            if (sr == null || !sr.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3203
                                if (sl != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3204
                                    sl.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3205
                                xpr.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3206
                                root = rotateRight(root, xpr);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3207
                                xpr = (xp = x.parent) == null ?
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3208
                                    null : xp.right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3209
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3210
                            if (xpr != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3211
                                xpr.red = (xp == null) ? false : xp.red;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3212
                                if ((sr = xpr.right) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3213
                                    sr.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3214
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3215
                            if (xp != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3216
                                xp.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3217
                                root = rotateLeft(root, xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3218
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3219
                            x = root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3220
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3221
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3222
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3223
                else { // symmetric
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3224
                    if (xpl != null && xpl.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3225
                        xpl.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3226
                        xp.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3227
                        root = rotateRight(root, xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3228
                        xpl = (xp = x.parent) == null ? null : xp.left;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3229
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3230
                    if (xpl == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3231
                        x = xp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3232
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3233
                        TreeNode<K,V> sl = xpl.left, sr = xpl.right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3234
                        if ((sl == null || !sl.red) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3235
                            (sr == null || !sr.red)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3236
                            xpl.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3237
                            x = xp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3238
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3239
                        else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3240
                            if (sl == null || !sl.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3241
                                if (sr != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3242
                                    sr.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3243
                                xpl.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3244
                                root = rotateLeft(root, xpl);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3245
                                xpl = (xp = x.parent) == null ?
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3246
                                    null : xp.left;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3247
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3248
                            if (xpl != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3249
                                xpl.red = (xp == null) ? false : xp.red;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3250
                                if ((sl = xpl.left) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3251
                                    sl.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3252
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3253
                            if (xp != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3254
                                xp.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3255
                                root = rotateRight(root, xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3256
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3257
                            x = root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3258
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3259
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3260
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3261
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3262
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3263
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3264
        /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3265
         * Checks invariants recursively for the tree of Nodes rooted at t.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3266
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3267
        static <K,V> boolean checkInvariants(TreeNode<K,V> t) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3268
            TreeNode<K,V> tp = t.parent, tl = t.left, tr = t.right,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3269
                tb = t.prev, tn = (TreeNode<K,V>)t.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3270
            if (tb != null && tb.next != t)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3271
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3272
            if (tn != null && tn.prev != t)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3273
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3274
            if (tp != null && t != tp.left && t != tp.right)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3275
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3276
            if (tl != null && (tl.parent != t || tl.hash > t.hash))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3277
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3278
            if (tr != null && (tr.parent != t || tr.hash < t.hash))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3279
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3280
            if (t.red && tl != null && tl.red && tr != null && tr.red)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3281
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3282
            if (tl != null && !checkInvariants(tl))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3283
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3284
            if (tr != null && !checkInvariants(tr))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3285
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3286
            return true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3287
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3288
46873
7ac2f551b0d6 8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents: 45563
diff changeset
  3289
        private static final long LOCKSTATE
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 52958
diff changeset
  3290
            = U.objectFieldOffset(TreeBin.class, "lockState");
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3291
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3292
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3293
    /* ----------------Table Traversal -------------- */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3294
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3295
    /**
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3296
     * Records the table, its length, and current traversal index for a
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3297
     * traverser that must process a region of a forwarded table before
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3298
     * proceeding with current table.
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3299
     */
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3300
    static final class TableStack<K,V> {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3301
        int length;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3302
        int index;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3303
        Node<K,V>[] tab;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3304
        TableStack<K,V> next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3305
    }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3306
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3307
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3308
     * Encapsulates traversal for methods such as containsValue; also
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3309
     * serves as a base class for other iterators and spliterators.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3310
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3311
     * Method advance visits once each still-valid node that was
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3312
     * reachable upon iterator construction. It might miss some that
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3313
     * were added to a bin after the bin was visited, which is OK wrt
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3314
     * consistency guarantees. Maintaining this property in the face
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3315
     * of possible ongoing resizes requires a fair amount of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3316
     * bookkeeping state that is difficult to optimize away amidst
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3317
     * volatile accesses.  Even so, traversal maintains reasonable
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3318
     * throughput.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3319
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3320
     * Normally, iteration proceeds bin-by-bin traversing lists.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3321
     * However, if the table has been resized, then all future steps
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3322
     * must traverse both the bin at the current index as well as at
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3323
     * (index + baseSize); and so on for further resizings. To
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3324
     * paranoically cope with potential sharing by users of iterators
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3325
     * across threads, iteration terminates if a bounds checks fails
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3326
     * for a table read.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3327
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3328
    static class Traverser<K,V> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3329
        Node<K,V>[] tab;        // current table; updated if resized
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3330
        Node<K,V> next;         // the next entry to use
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3331
        TableStack<K,V> stack, spare; // to save/restore on ForwardingNodes
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3332
        int index;              // index of bin to use next
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3333
        int baseIndex;          // current index of initial table
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3334
        int baseLimit;          // index bound for initial table
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3335
        final int baseSize;     // initial table size
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3336
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3337
        Traverser(Node<K,V>[] tab, int size, int index, int limit) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3338
            this.tab = tab;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3339
            this.baseSize = size;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3340
            this.baseIndex = this.index = index;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3341
            this.baseLimit = limit;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3342
            this.next = null;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3343
        }
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3344
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3345
        /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3346
         * Advances if possible, returning next valid node, or null if none.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
         */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3348
        final Node<K,V> advance() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3349
            Node<K,V> e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3350
            if ((e = next) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3351
                e = e.next;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3352
            for (;;) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3353
                Node<K,V>[] t; int i, n;  // must use locals in checks
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3354
                if (e != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3355
                    return next = e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3356
                if (baseIndex >= baseLimit || (t = tab) == null ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3357
                    (n = t.length) <= (i = index) || i < 0)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3358
                    return next = null;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3359
                if ((e = tabAt(t, i)) != null && e.hash < 0) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3360
                    if (e instanceof ForwardingNode) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3361
                        tab = ((ForwardingNode<K,V>)e).nextTable;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3362
                        e = null;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3363
                        pushState(t, i, n);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3364
                        continue;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3365
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3366
                    else if (e instanceof TreeBin)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3367
                        e = ((TreeBin<K,V>)e).first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3368
                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3369
                        e = null;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3370
                }
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3371
                if (stack != null)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3372
                    recoverState(n);
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3373
                else if ((index = i + baseSize) >= n)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3374
                    index = ++baseIndex; // visit upper slots if present
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
        }
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3377
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3378
        /**
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3379
         * Saves traversal state upon encountering a forwarding node.
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3380
         */
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3381
        private void pushState(Node<K,V>[] t, int i, int n) {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3382
            TableStack<K,V> s = spare;  // reuse if possible
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3383
            if (s != null)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3384
                spare = s.next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3385
            else
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3386
                s = new TableStack<K,V>();
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3387
            s.tab = t;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3388
            s.length = n;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3389
            s.index = i;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3390
            s.next = stack;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3391
            stack = s;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3392
        }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3393
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3394
        /**
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3395
         * Possibly pops traversal state.
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3396
         *
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3397
         * @param n length of current table
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3398
         */
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3399
        private void recoverState(int n) {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3400
            TableStack<K,V> s; int len;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3401
            while ((s = stack) != null && (index += (len = s.length)) >= n) {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3402
                n = len;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3403
                index = s.index;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3404
                tab = s.tab;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3405
                s.tab = null;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3406
                TableStack<K,V> next = s.next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3407
                s.next = spare; // save for reuse
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3408
                stack = next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3409
                spare = s;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3410
            }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3411
            if (s == null && (index += baseSize) >= n)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3412
                index = ++baseIndex;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3413
        }
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3414
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3416
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3417
     * Base of key, value, and entry Iterators. Adds fields to
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3418
     * Traverser to support iterator.remove.
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3419
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3420
    static class BaseIterator<K,V> extends Traverser<K,V> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3421
        final ConcurrentHashMap<K,V> map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3422
        Node<K,V> lastReturned;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3423
        BaseIterator(Node<K,V>[] tab, int size, int index, int limit,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3424
                    ConcurrentHashMap<K,V> map) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3425
            super(tab, size, index, limit);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3426
            this.map = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3427
            advance();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3428
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3429
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3430
        public final boolean hasNext() { return next != null; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3431
        public final boolean hasMoreElements() { return next != null; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3432
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3433
        public final void remove() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3434
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3435
            if ((p = lastReturned) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3436
                throw new IllegalStateException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3437
            lastReturned = null;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3438
            map.replaceNode(p.key, null, null);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3439
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3440
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3441
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3442
    static final class KeyIterator<K,V> extends BaseIterator<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3443
        implements Iterator<K>, Enumeration<K> {
44588
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  3444
        KeyIterator(Node<K,V>[] tab, int size, int index, int limit,
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3445
                    ConcurrentHashMap<K,V> map) {
44588
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  3446
            super(tab, size, index, limit, map);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3447
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3448
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3449
        public final K next() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3450
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3451
            if ((p = next) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3452
                throw new NoSuchElementException();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3453
            K k = p.key;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3454
            lastReturned = p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3455
            advance();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3456
            return k;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3458
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3459
        public final K nextElement() { return next(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3460
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3461
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3462
    static final class ValueIterator<K,V> extends BaseIterator<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3463
        implements Iterator<V>, Enumeration<V> {
44588
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  3464
        ValueIterator(Node<K,V>[] tab, int size, int index, int limit,
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3465
                      ConcurrentHashMap<K,V> map) {
44588
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  3466
            super(tab, size, index, limit, map);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3467
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3468
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3469
        public final V next() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3470
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3471
            if ((p = next) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3472
                throw new NoSuchElementException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3473
            V v = p.val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3474
            lastReturned = p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3475
            advance();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3476
            return v;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3477
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3478
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3479
        public final V nextElement() { return next(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3480
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3481
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3482
    static final class EntryIterator<K,V> extends BaseIterator<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3483
        implements Iterator<Map.Entry<K,V>> {
44588
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  3484
        EntryIterator(Node<K,V>[] tab, int size, int index, int limit,
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3485
                      ConcurrentHashMap<K,V> map) {
44588
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  3486
            super(tab, size, index, limit, map);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3487
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3488
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3489
        public final Map.Entry<K,V> next() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3490
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3491
            if ((p = next) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3492
                throw new NoSuchElementException();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3493
            K k = p.key;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3494
            V v = p.val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3495
            lastReturned = p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3496
            advance();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3497
            return new MapEntry<K,V>(k, v, map);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3498
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3501
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3502
     * Exported Entry for EntryIterator.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3503
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3504
    static final class MapEntry<K,V> implements Map.Entry<K,V> {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3505
        final K key; // non-null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3506
        V val;       // non-null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3507
        final ConcurrentHashMap<K,V> map;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3508
        MapEntry(K key, V val, ConcurrentHashMap<K,V> map) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3509
            this.key = key;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3510
            this.val = val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3511
            this.map = map;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3512
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3513
        public K getKey()        { return key; }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3514
        public V getValue()      { return val; }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3515
        public int hashCode()    { return key.hashCode() ^ val.hashCode(); }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3516
        public String toString() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3517
            return Helpers.mapEntryToString(key, val);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3518
        }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3519
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3520
        public boolean equals(Object o) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3521
            Object k, v; Map.Entry<?,?> e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3522
            return ((o instanceof Map.Entry) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3523
                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3524
                    (v = e.getValue()) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3525
                    (k == key || k.equals(key)) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3526
                    (v == val || v.equals(val)));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3527
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3528
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3529
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3530
         * Sets our entry's value and writes through to the map. The
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3531
         * value to return is somewhat arbitrary here. Since we do not
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3532
         * necessarily track asynchronous changes, the most recent
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3533
         * "previous" value could be different from what we return (or
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3534
         * could even have been removed, in which case the put will
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3535
         * re-establish). We do not and cannot guarantee more.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3536
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3537
        public V setValue(V value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3538
            if (value == null) throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3539
            V v = val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3540
            val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3541
            map.put(key, value);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3542
            return v;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3543
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3544
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3545
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3546
    static final class KeySpliterator<K,V> extends Traverser<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3547
        implements Spliterator<K> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3548
        long est;               // size estimate
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3549
        KeySpliterator(Node<K,V>[] tab, int size, int index, int limit,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3550
                       long est) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3551
            super(tab, size, index, limit);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3552
            this.est = est;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3553
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3554
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3555
        public KeySpliterator<K,V> trySplit() {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3556
            int i, f, h;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3557
            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3558
                new KeySpliterator<K,V>(tab, baseSize, baseLimit = h,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3559
                                        f, est >>>= 1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3560
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3561
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3562
        public void forEachRemaining(Consumer<? super K> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3563
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3564
            for (Node<K,V> p; (p = advance()) != null;)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3565
                action.accept(p.key);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3566
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3567
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3568
        public boolean tryAdvance(Consumer<? super K> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3569
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3570
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3571
            if ((p = advance()) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3572
                return false;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3573
            action.accept(p.key);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3574
            return true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3575
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3576
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3577
        public long estimateSize() { return est; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3578
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3579
        public int characteristics() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3580
            return Spliterator.DISTINCT | Spliterator.CONCURRENT |
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3581
                Spliterator.NONNULL;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3582
        }
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3583
    }
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3584
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3585
    static final class ValueSpliterator<K,V> extends Traverser<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3586
        implements Spliterator<V> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3587
        long est;               // size estimate
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3588
        ValueSpliterator(Node<K,V>[] tab, int size, int index, int limit,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3589
                         long est) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3590
            super(tab, size, index, limit);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3591
            this.est = est;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3592
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3593
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3594
        public ValueSpliterator<K,V> trySplit() {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3595
            int i, f, h;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3596
            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3597
                new ValueSpliterator<K,V>(tab, baseSize, baseLimit = h,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3598
                                          f, est >>>= 1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3599
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3600
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3601
        public void forEachRemaining(Consumer<? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3602
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3603
            for (Node<K,V> p; (p = advance()) != null;)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3604
                action.accept(p.val);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3605
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3606
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3607
        public boolean tryAdvance(Consumer<? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3608
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3609
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3610
            if ((p = advance()) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3611
                return false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3612
            action.accept(p.val);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3613
            return true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3614
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3615
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3616
        public long estimateSize() { return est; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3617
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3618
        public int characteristics() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3619
            return Spliterator.CONCURRENT | Spliterator.NONNULL;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3620
        }
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3621
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3623
    static final class EntrySpliterator<K,V> extends Traverser<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3624
        implements Spliterator<Map.Entry<K,V>> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3625
        final ConcurrentHashMap<K,V> map; // To export MapEntry
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3626
        long est;               // size estimate
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3627
        EntrySpliterator(Node<K,V>[] tab, int size, int index, int limit,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3628
                         long est, ConcurrentHashMap<K,V> map) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3629
            super(tab, size, index, limit);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3630
            this.map = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3631
            this.est = est;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3632
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3633
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3634
        public EntrySpliterator<K,V> trySplit() {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3635
            int i, f, h;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3636
            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3637
                new EntrySpliterator<K,V>(tab, baseSize, baseLimit = h,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3638
                                          f, est >>>= 1, map);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3639
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3640
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3641
        public void forEachRemaining(Consumer<? super Map.Entry<K,V>> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3642
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3643
            for (Node<K,V> p; (p = advance()) != null; )
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3644
                action.accept(new MapEntry<K,V>(p.key, p.val, map));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3645
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3646
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3647
        public boolean tryAdvance(Consumer<? super Map.Entry<K,V>> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3648
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3649
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3650
            if ((p = advance()) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3651
                return false;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3652
            action.accept(new MapEntry<K,V>(p.key, p.val, map));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3653
            return true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3654
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3655
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3656
        public long estimateSize() { return est; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3657
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3658
        public int characteristics() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3659
            return Spliterator.DISTINCT | Spliterator.CONCURRENT |
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3660
                Spliterator.NONNULL;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3661
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3662
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3663
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3664
    // Parallel bulk operations
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3665
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3666
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3667
     * Computes initial batch value for bulk tasks. The returned value
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3668
     * is approximately exp2 of the number of times (minus one) to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3669
     * split task by two before executing leaf action. This value is
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3670
     * faster to compute and more convenient to use as a guide to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3671
     * splitting than is the depth, since it is used while dividing by
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3672
     * two anyway.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3673
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3674
    final int batchFor(long b) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3675
        long n;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3676
        if (b == Long.MAX_VALUE || (n = sumCount()) <= 1L || n < b)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3677
            return 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3678
        int sp = ForkJoinPool.getCommonPoolParallelism() << 2; // slack of 4
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3679
        return (b <= 0L || (n /= b) >= sp) ? sp : (int)n;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3680
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3681
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3682
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3683
     * Performs the given action for each (key, value).
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3684
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3685
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3686
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3687
     * @param action the action
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3688
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3689
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3690
    public void forEach(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3691
                        BiConsumer<? super K,? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3692
        if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3693
        new ForEachMappingTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3694
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3695
             action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3696
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3697
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3698
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3699
     * Performs the given action for each non-null transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3700
     * of each (key, value).
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3701
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3702
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3703
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3704
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3705
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3706
     * which case the action is not applied)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3707
     * @param action the action
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  3708
     * @param <U> the return type of the transformer
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3709
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3710
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3711
    public <U> void forEach(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3712
                            BiFunction<? super K, ? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3713
                            Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3714
        if (transformer == null || action == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3715
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3716
        new ForEachTransformedMappingTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3717
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3718
             transformer, action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3719
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3720
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3721
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3722
     * Returns a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3723
     * function on each (key, value), or null if none.  Upon
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3724
     * success, further element processing is suppressed and the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3725
     * results of any other parallel invocations of the search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3726
     * function are ignored.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3727
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3728
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3729
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3730
     * @param searchFunction a function returning a non-null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3731
     * result on success, else null
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  3732
     * @param <U> the return type of the search function
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3733
     * @return a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3734
     * function on each (key, value), or null if none
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3735
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3736
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3737
    public <U> U search(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3738
                        BiFunction<? super K, ? super V, ? extends U> searchFunction) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3739
        if (searchFunction == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3740
        return new SearchMappingsTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3741
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3742
             searchFunction, new AtomicReference<U>()).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3743
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3744
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3745
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3746
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3747
     * of all (key, value) pairs using the given reducer to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3748
     * combine values, or null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3749
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3750
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3751
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3752
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3753
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3754
     * which case it is not combined)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3755
     * @param reducer a commutative associative combining function
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  3756
     * @param <U> the return type of the transformer
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3757
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3758
     * of all (key, value) pairs
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3759
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3760
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3761
    public <U> U reduce(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3762
                        BiFunction<? super K, ? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3763
                        BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3764
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3765
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3766
        return new MapReduceMappingsTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3767
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3768
             null, transformer, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3769
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3770
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3771
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3772
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3773
     * of all (key, value) pairs using the given reducer to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3774
     * combine values, and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3775
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3776
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3777
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3778
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3779
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3780
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3781
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3782
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3783
     * of all (key, value) pairs
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3784
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3785
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3786
    public double reduceToDouble(long parallelismThreshold,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3787
                                 ToDoubleBiFunction<? super K, ? super V> transformer,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3788
                                 double basis,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3789
                                 DoubleBinaryOperator reducer) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3790
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3791
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3792
        return new MapReduceMappingsToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3793
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3794
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3795
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3796
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3797
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3798
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3799
     * of all (key, value) pairs using the given reducer to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3800
     * combine values, and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3801
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3802
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3803
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3804
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3805
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3806
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3807
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3808
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3809
     * of all (key, value) pairs
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3810
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3811
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3812
    public long reduceToLong(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3813
                             ToLongBiFunction<? super K, ? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3814
                             long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3815
                             LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3816
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3817
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3818
        return new MapReduceMappingsToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3819
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3820
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3821
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3822
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3823
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3824
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3825
     * of all (key, value) pairs using the given reducer to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3826
     * combine values, and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3827
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3828
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3829
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3830
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3831
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3832
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3833
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3834
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3835
     * of all (key, value) pairs
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3836
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3837
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3838
    public int reduceToInt(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3839
                           ToIntBiFunction<? super K, ? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3840
                           int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3841
                           IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3842
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3843
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3844
        return new MapReduceMappingsToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3845
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3846
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3847
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3848
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3849
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3850
     * Performs the given action for each key.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3851
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3852
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3853
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3854
     * @param action the action
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3855
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3856
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3857
    public void forEachKey(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3858
                           Consumer<? super K> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3859
        if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3860
        new ForEachKeyTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3861
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3862
             action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3863
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3864
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3865
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3866
     * Performs the given action for each non-null transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3867
     * of each key.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3868
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3869
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3870
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3871
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3872
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3873
     * which case the action is not applied)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3874
     * @param action the action
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  3875
     * @param <U> the return type of the transformer
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3876
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3877
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3878
    public <U> void forEachKey(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3879
                               Function<? super K, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3880
                               Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3881
        if (transformer == null || action == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3882
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3883
        new ForEachTransformedKeyTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3884
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3885
             transformer, action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3886
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3887
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3888
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3889
     * Returns a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3890
     * function on each key, or null if none. Upon success,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3891
     * further element processing is suppressed and the results of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3892
     * any other parallel invocations of the search function are
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3893
     * ignored.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3894
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3895
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3896
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3897
     * @param searchFunction a function returning a non-null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3898
     * result on success, else null
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  3899
     * @param <U> the return type of the search function
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3900
     * @return a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3901
     * function on each key, or null if none
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3902
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3903
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3904
    public <U> U searchKeys(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3905
                            Function<? super K, ? extends U> searchFunction) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3906
        if (searchFunction == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3907
        return new SearchKeysTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3908
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3909
             searchFunction, new AtomicReference<U>()).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3910
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3911
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3912
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3913
     * Returns the result of accumulating all keys using the given
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3914
     * reducer to combine values, or null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3915
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3916
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3917
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3918
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3919
     * @return the result of accumulating all keys using the given
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3920
     * reducer to combine values, or null if none
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3921
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3922
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3923
    public K reduceKeys(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3924
                        BiFunction<? super K, ? super K, ? extends K> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3925
        if (reducer == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3926
        return new ReduceKeysTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3927
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3928
             null, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3929
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3930
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3931
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3932
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3933
     * of all keys using the given reducer to combine values, or
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3934
     * null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3935
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3936
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3937
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3938
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3939
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3940
     * which case it is not combined)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3941
     * @param reducer a commutative associative combining function
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  3942
     * @param <U> the return type of the transformer
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3943
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3944
     * of all keys
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3945
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3946
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3947
    public <U> U reduceKeys(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3948
                            Function<? super K, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3949
         BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3950
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3951
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3952
        return new MapReduceKeysTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3953
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3954
             null, transformer, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3955
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3956
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3957
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3958
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3959
     * of all keys using the given reducer to combine values, and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3960
     * the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3961
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3962
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3963
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3964
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3965
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3966
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3967
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3968
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3969
     * of all keys
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3970
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3971
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3972
    public double reduceKeysToDouble(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3973
                                     ToDoubleFunction<? super K> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3974
                                     double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3975
                                     DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3976
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3977
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3978
        return new MapReduceKeysToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3979
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3980
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3981
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3982
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3983
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3984
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3985
     * of all keys using the given reducer to combine values, and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3986
     * the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3987
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3988
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3989
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3990
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3991
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3992
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3993
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3994
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3995
     * of all keys
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3996
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3997
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3998
    public long reduceKeysToLong(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3999
                                 ToLongFunction<? super K> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4000
                                 long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4001
                                 LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4002
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4003
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4004
        return new MapReduceKeysToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4005
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4006
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4007
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4008
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4009
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4010
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4011
     * of all keys using the given reducer to combine values, and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4012
     * the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4013
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4014
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4015
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4016
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4017
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4018
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4019
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4020
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4021
     * of all keys
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4022
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4023
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4024
    public int reduceKeysToInt(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4025
                               ToIntFunction<? super K> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4026
                               int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4027
                               IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4028
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4029
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4030
        return new MapReduceKeysToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4031
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4032
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4033
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4034
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4035
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4036
     * Performs the given action for each value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4037
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4038
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4039
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4040
     * @param action the action
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4041
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4042
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4043
    public void forEachValue(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4044
                             Consumer<? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4045
        if (action == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4046
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4047
        new ForEachValueTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4048
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4049
             action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4050
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4051
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4052
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4053
     * Performs the given action for each non-null transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4054
     * of each value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4055
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4056
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4057
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4058
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4059
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4060
     * which case the action is not applied)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4061
     * @param action the action
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  4062
     * @param <U> the return type of the transformer
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4063
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4064
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4065
    public <U> void forEachValue(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4066
                                 Function<? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4067
                                 Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4068
        if (transformer == null || action == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4069
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4070
        new ForEachTransformedValueTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4071
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4072
             transformer, action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4073
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4074
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4075
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4076
     * Returns a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4077
     * function on each value, or null if none.  Upon success,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4078
     * further element processing is suppressed and the results of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4079
     * any other parallel invocations of the search function are
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4080
     * ignored.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4081
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4082
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4083
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4084
     * @param searchFunction a function returning a non-null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4085
     * result on success, else null
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  4086
     * @param <U> the return type of the search function
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4087
     * @return a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4088
     * function on each value, or null if none
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4089
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4090
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4091
    public <U> U searchValues(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4092
                              Function<? super V, ? extends U> searchFunction) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4093
        if (searchFunction == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4094
        return new SearchValuesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4095
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4096
             searchFunction, new AtomicReference<U>()).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4097
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4098
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4099
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4100
     * Returns the result of accumulating all values using the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4101
     * given reducer to combine values, or null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4102
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4103
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4104
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4105
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4106
     * @return the result of accumulating all values
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4107
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4108
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4109
    public V reduceValues(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4110
                          BiFunction<? super V, ? super V, ? extends V> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4111
        if (reducer == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4112
        return new ReduceValuesTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4113
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4114
             null, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4115
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4116
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4117
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4118
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4119
     * of all values using the given reducer to combine values, or
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4120
     * null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4121
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4122
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4123
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4124
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4125
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4126
     * which case it is not combined)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4127
     * @param reducer a commutative associative combining function
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  4128
     * @param <U> the return type of the transformer
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4129
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4130
     * of all values
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4131
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4132
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4133
    public <U> U reduceValues(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4134
                              Function<? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4135
                              BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4136
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4137
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4138
        return new MapReduceValuesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4139
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4140
             null, transformer, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4141
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4142
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4143
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4144
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4145
     * of all values using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4146
     * and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4147
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4148
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4149
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4150
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4151
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4152
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4153
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4154
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4155
     * of all values
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4156
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4157
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4158
    public double reduceValuesToDouble(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4159
                                       ToDoubleFunction<? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4160
                                       double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4161
                                       DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4162
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4163
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4164
        return new MapReduceValuesToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4165
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4166
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4167
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4168
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4169
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4170
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4171
     * of all values using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4172
     * and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4173
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4174
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4175
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4176
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4177
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4178
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4179
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4180
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4181
     * of all values
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4182
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4183
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4184
    public long reduceValuesToLong(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4185
                                   ToLongFunction<? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4186
                                   long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4187
                                   LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4188
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4189
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4190
        return new MapReduceValuesToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4191
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4192
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4193
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4194
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4195
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4196
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4197
     * of all values using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4198
     * and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4199
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4200
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4201
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4202
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4203
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4204
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4205
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4206
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4207
     * of all values
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4208
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4209
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4210
    public int reduceValuesToInt(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4211
                                 ToIntFunction<? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4212
                                 int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4213
                                 IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4214
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4215
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4216
        return new MapReduceValuesToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4217
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4218
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4219
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4220
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4221
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4222
     * Performs the given action for each entry.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4223
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4224
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4225
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4226
     * @param action the action
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4227
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4228
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4229
    public void forEachEntry(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4230
                             Consumer<? super Map.Entry<K,V>> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4231
        if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4232
        new ForEachEntryTask<K,V>(null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4233
                                  action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4234
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4235
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4236
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4237
     * Performs the given action for each non-null transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4238
     * of each entry.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4239
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4240
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4241
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4242
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4243
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4244
     * which case the action is not applied)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4245
     * @param action the action
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  4246
     * @param <U> the return type of the transformer
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4247
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4248
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4249
    public <U> void forEachEntry(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4250
                                 Function<Map.Entry<K,V>, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4251
                                 Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4252
        if (transformer == null || action == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4253
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4254
        new ForEachTransformedEntryTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4255
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4256
             transformer, action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4257
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4258
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4259
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4260
     * Returns a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4261
     * function on each entry, or null if none.  Upon success,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4262
     * further element processing is suppressed and the results of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4263
     * any other parallel invocations of the search function are
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4264
     * ignored.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4265
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4266
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4267
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4268
     * @param searchFunction a function returning a non-null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4269
     * result on success, else null
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  4270
     * @param <U> the return type of the search function
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4271
     * @return a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4272
     * function on each entry, or null if none
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4273
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4274
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4275
    public <U> U searchEntries(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4276
                               Function<Map.Entry<K,V>, ? extends U> searchFunction) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4277
        if (searchFunction == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4278
        return new SearchEntriesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4279
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4280
             searchFunction, new AtomicReference<U>()).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4281
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4282
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4283
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4284
     * Returns the result of accumulating all entries using the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4285
     * given reducer to combine values, or null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4286
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4287
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4288
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4289
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4290
     * @return the result of accumulating all entries
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4291
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4292
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4293
    public Map.Entry<K,V> reduceEntries(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4294
                                        BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4295
        if (reducer == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4296
        return new ReduceEntriesTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4297
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4298
             null, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4299
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4300
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4301
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4302
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4303
     * of all entries using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4304
     * or null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4305
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4306
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4307
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4308
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4309
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4310
     * which case it is not combined)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4311
     * @param reducer a commutative associative combining function
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  4312
     * @param <U> the return type of the transformer
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4313
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4314
     * of all entries
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4315
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4316
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4317
    public <U> U reduceEntries(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4318
                               Function<Map.Entry<K,V>, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4319
                               BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4320
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4321
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4322
        return new MapReduceEntriesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4323
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4324
             null, transformer, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4325
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4326
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4327
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4328
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4329
     * of all entries using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4330
     * and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4331
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4332
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4333
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4334
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4335
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4336
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4337
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4338
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4339
     * of all entries
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4340
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4341
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4342
    public double reduceEntriesToDouble(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4343
                                        ToDoubleFunction<Map.Entry<K,V>> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4344
                                        double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4345
                                        DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4346
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4347
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4348
        return new MapReduceEntriesToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4349
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4350
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4351
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4352
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4353
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4354
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4355
     * of all entries using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4356
     * and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4357
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4358
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4359
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4360
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4361
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4362
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4363
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4364
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4365
     * of all entries
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4366
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4367
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4368
    public long reduceEntriesToLong(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4369
                                    ToLongFunction<Map.Entry<K,V>> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4370
                                    long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4371
                                    LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4372
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4373
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4374
        return new MapReduceEntriesToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4375
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4376
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4377
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4378
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4379
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4380
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4381
     * of all entries using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4382
     * and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4383
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4384
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4385
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4386
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4387
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4388
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4389
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4390
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4391
     * of all entries
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4392
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4393
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4394
    public int reduceEntriesToInt(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4395
                                  ToIntFunction<Map.Entry<K,V>> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4396
                                  int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4397
                                  IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4398
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4399
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4400
        return new MapReduceEntriesToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4401
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4402
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4403
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4404
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4405
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4406
    /* ----------------Views -------------- */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4407
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4408
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4409
     * Base class for views.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4410
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4411
    abstract static class CollectionView<K,V,E>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4412
        implements Collection<E>, java.io.Serializable {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4413
        private static final long serialVersionUID = 7249069246763182397L;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4414
        final ConcurrentHashMap<K,V> map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4415
        CollectionView(ConcurrentHashMap<K,V> map)  { this.map = map; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4416
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4417
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4418
         * Returns the map backing this view.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4419
         *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4420
         * @return the map backing this view
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4421
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4422
        public ConcurrentHashMap<K,V> getMap() { return map; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4423
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4424
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4425
         * Removes all of the elements from this view, by removing all
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4426
         * the mappings from the map backing this view.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4427
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4428
        public final void clear()      { map.clear(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4429
        public final int size()        { return map.size(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4430
        public final boolean isEmpty() { return map.isEmpty(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4431
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4432
        // implementations below rely on concrete classes supplying these
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4433
        // abstract methods
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4434
        /**
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  4435
         * Returns an iterator over the elements in this collection.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  4436
         *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  4437
         * <p>The returned iterator is
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  4438
         * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  4439
         *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  4440
         * @return an iterator over the elements in this collection
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4441
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4442
        public abstract Iterator<E> iterator();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4443
        public abstract boolean contains(Object o);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4444
        public abstract boolean remove(Object o);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4445
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  4446
        private static final String OOME_MSG = "Required array size too large";
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4447
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4448
        public final Object[] toArray() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4449
            long sz = map.mappingCount();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4450
            if (sz > MAX_ARRAY_SIZE)
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  4451
                throw new OutOfMemoryError(OOME_MSG);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4452
            int n = (int)sz;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4453
            Object[] r = new Object[n];
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4454
            int i = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4455
            for (E e : this) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4456
                if (i == n) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4457
                    if (n >= MAX_ARRAY_SIZE)
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  4458
                        throw new OutOfMemoryError(OOME_MSG);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4459
                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4460
                        n = MAX_ARRAY_SIZE;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4461
                    else
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4462
                        n += (n >>> 1) + 1;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4463
                    r = Arrays.copyOf(r, n);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4464
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4465
                r[i++] = e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4466
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4467
            return (i == n) ? r : Arrays.copyOf(r, i);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4468
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4469
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4470
        @SuppressWarnings("unchecked")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4471
        public final <T> T[] toArray(T[] a) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4472
            long sz = map.mappingCount();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4473
            if (sz > MAX_ARRAY_SIZE)
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  4474
                throw new OutOfMemoryError(OOME_MSG);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4475
            int m = (int)sz;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4476
            T[] r = (a.length >= m) ? a :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4477
                (T[])java.lang.reflect.Array
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4478
                .newInstance(a.getClass().getComponentType(), m);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4479
            int n = r.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4480
            int i = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4481
            for (E e : this) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4482
                if (i == n) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4483
                    if (n >= MAX_ARRAY_SIZE)
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  4484
                        throw new OutOfMemoryError(OOME_MSG);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4485
                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4486
                        n = MAX_ARRAY_SIZE;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4487
                    else
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4488
                        n += (n >>> 1) + 1;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4489
                    r = Arrays.copyOf(r, n);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4490
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4491
                r[i++] = (T)e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4492
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4493
            if (a == r && i < n) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4494
                r[i] = null; // null-terminate
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4495
                return r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4496
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4497
            return (i == n) ? r : Arrays.copyOf(r, i);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4498
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4499
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4500
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4501
         * Returns a string representation of this collection.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4502
         * The string representation consists of the string representations
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4503
         * of the collection's elements in the order they are returned by
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4504
         * its iterator, enclosed in square brackets ({@code "[]"}).
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4505
         * Adjacent elements are separated by the characters {@code ", "}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4506
         * (comma and space).  Elements are converted to strings as by
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4507
         * {@link String#valueOf(Object)}.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4508
         *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4509
         * @return a string representation of this collection
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4510
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4511
        public final String toString() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4512
            StringBuilder sb = new StringBuilder();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4513
            sb.append('[');
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4514
            Iterator<E> it = iterator();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4515
            if (it.hasNext()) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4516
                for (;;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4517
                    Object e = it.next();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4518
                    sb.append(e == this ? "(this Collection)" : e);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4519
                    if (!it.hasNext())
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4520
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4521
                    sb.append(',').append(' ');
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4522
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4523
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4524
            return sb.append(']').toString();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4525
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4526
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4527
        public final boolean containsAll(Collection<?> c) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4528
            if (c != this) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4529
                for (Object e : c) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4530
                    if (e == null || !contains(e))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4531
                        return false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4532
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4533
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4534
            return true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4535
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4536
39779
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4537
        public boolean removeAll(Collection<?> c) {
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  4538
            if (c == null) throw new NullPointerException();
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4539
            boolean modified = false;
39779
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4540
            // Use (c instanceof Set) as a hint that lookup in c is as
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4541
            // efficient as this view
40547
51759e487197 8163353: NPE in ConcurrentHashMap.removeAll()
dl
parents: 39779
diff changeset
  4542
            Node<K,V>[] t;
51759e487197 8163353: NPE in ConcurrentHashMap.removeAll()
dl
parents: 39779
diff changeset
  4543
            if ((t = map.table) == null) {
51759e487197 8163353: NPE in ConcurrentHashMap.removeAll()
dl
parents: 39779
diff changeset
  4544
                return false;
51759e487197 8163353: NPE in ConcurrentHashMap.removeAll()
dl
parents: 39779
diff changeset
  4545
            } else if (c instanceof Set<?> && c.size() > t.length) {
39779
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4546
                for (Iterator<?> it = iterator(); it.hasNext(); ) {
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4547
                    if (c.contains(it.next())) {
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4548
                        it.remove();
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4549
                        modified = true;
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4550
                    }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4551
                }
39779
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4552
            } else {
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4553
                for (Object e : c)
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4554
                    modified |= remove(e);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4555
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4556
            return modified;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4557
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4558
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4559
        public final boolean retainAll(Collection<?> c) {
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  4560
            if (c == null) throw new NullPointerException();
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4561
            boolean modified = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4562
            for (Iterator<E> it = iterator(); it.hasNext();) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4563
                if (!c.contains(it.next())) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4564
                    it.remove();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4565
                    modified = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4566
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4567
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4568
            return modified;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4569
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4570
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4571
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4572
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4573
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4574
     * A view of a ConcurrentHashMap as a {@link Set} of keys, in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4575
     * which additions may optionally be enabled by mapping to a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4576
     * common value.  This class cannot be directly instantiated.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4577
     * See {@link #keySet() keySet()},
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4578
     * {@link #keySet(Object) keySet(V)},
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4579
     * {@link #newKeySet() newKeySet()},
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4580
     * {@link #newKeySet(int) newKeySet(int)}.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4581
     *
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4582
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4583
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4584
    public static class KeySetView<K,V> extends CollectionView<K,V,K>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4585
        implements Set<K>, java.io.Serializable {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4586
        private static final long serialVersionUID = 7249069246763182397L;
58657
6252605fb005 8232230: Suppress warnings on non-serializable non-transient instance fields in java.util.concurrent
darcy
parents: 58138
diff changeset
  4587
        @SuppressWarnings("serial") // Conditionally serializable
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4588
        private final V value;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4589
        KeySetView(ConcurrentHashMap<K,V> map, V value) {  // non-public
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4590
            super(map);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4591
            this.value = value;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4592
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4593
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4594
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4595
         * Returns the default mapped value for additions,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4596
         * or {@code null} if additions are not supported.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4597
         *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4598
         * @return the default mapped value for additions, or {@code null}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4599
         * if not supported
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4600
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4601
        public V getMappedValue() { return value; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4602
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4603
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4604
         * {@inheritDoc}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4605
         * @throws NullPointerException if the specified key is null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4606
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4607
        public boolean contains(Object o) { return map.containsKey(o); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4608
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4609
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4610
         * Removes the key from this map view, by removing the key (and its
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4611
         * corresponding value) from the backing map.  This method does
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4612
         * nothing if the key is not in the map.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4613
         *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4614
         * @param  o the key to be removed from the backing map
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4615
         * @return {@code true} if the backing map contained the specified key
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4616
         * @throws NullPointerException if the specified key is null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4617
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4618
        public boolean remove(Object o) { return map.remove(o) != null; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4619
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4620
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4621
         * @return an iterator over the keys of the backing map
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4622
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4623
        public Iterator<K> iterator() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4624
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4625
            ConcurrentHashMap<K,V> m = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4626
            int f = (t = m.table) == null ? 0 : t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4627
            return new KeyIterator<K,V>(t, f, 0, f, m);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4628
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4629
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4630
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4631
         * Adds the specified key to this set view by mapping the key to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4632
         * the default mapped value in the backing map, if defined.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4633
         *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4634
         * @param e key to be added
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4635
         * @return {@code true} if this set changed as a result of the call
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4636
         * @throws NullPointerException if the specified key is null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4637
         * @throws UnsupportedOperationException if no default mapped value
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4638
         * for additions was provided
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4639
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4640
        public boolean add(K e) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4641
            V v;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4642
            if ((v = value) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4643
                throw new UnsupportedOperationException();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4644
            return map.putVal(e, v, true) == null;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4645
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4646
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4647
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4648
         * Adds all of the elements in the specified collection to this set,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4649
         * as if by calling {@link #add} on each one.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4650
         *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4651
         * @param c the elements to be inserted into this set
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4652
         * @return {@code true} if this set changed as a result of the call
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4653
         * @throws NullPointerException if the collection or any of its
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4654
         * elements are {@code null}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4655
         * @throws UnsupportedOperationException if no default mapped value
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4656
         * for additions was provided
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4657
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4658
        public boolean addAll(Collection<? extends K> c) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4659
            boolean added = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4660
            V v;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4661
            if ((v = value) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4662
                throw new UnsupportedOperationException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4663
            for (K e : c) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4664
                if (map.putVal(e, v, true) == null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4665
                    added = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4666
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4667
            return added;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4668
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4669
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4670
        public int hashCode() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4671
            int h = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4672
            for (K e : this)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4673
                h += e.hashCode();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4674
            return h;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4675
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4676
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4677
        public boolean equals(Object o) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4678
            Set<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4679
            return ((o instanceof Set) &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4680
                    ((c = (Set<?>)o) == this ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4681
                     (containsAll(c) && c.containsAll(this))));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4682
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4683
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4684
        public Spliterator<K> spliterator() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4685
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4686
            ConcurrentHashMap<K,V> m = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4687
            long n = m.sumCount();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4688
            int f = (t = m.table) == null ? 0 : t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4689
            return new KeySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4690
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4691
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4692
        public void forEach(Consumer<? super K> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4693
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4694
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4695
            if ((t = map.table) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4696
                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4697
                for (Node<K,V> p; (p = it.advance()) != null; )
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4698
                    action.accept(p.key);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4699
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4700
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4701
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4702
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4703
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4704
     * A view of a ConcurrentHashMap as a {@link Collection} of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4705
     * values, in which additions are disabled. This class cannot be
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4706
     * directly instantiated. See {@link #values()}.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4707
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4708
    static final class ValuesView<K,V> extends CollectionView<K,V,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4709
        implements Collection<V>, java.io.Serializable {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4710
        private static final long serialVersionUID = 2249069246763182397L;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4711
        ValuesView(ConcurrentHashMap<K,V> map) { super(map); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4712
        public final boolean contains(Object o) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4713
            return map.containsValue(o);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4714
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4715
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4716
        public final boolean remove(Object o) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4717
            if (o != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4718
                for (Iterator<V> it = iterator(); it.hasNext();) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4719
                    if (o.equals(it.next())) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4720
                        it.remove();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4721
                        return true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4722
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4723
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4724
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4725
            return false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4726
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4727
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4728
        public final Iterator<V> iterator() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4729
            ConcurrentHashMap<K,V> m = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4730
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4731
            int f = (t = m.table) == null ? 0 : t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4732
            return new ValueIterator<K,V>(t, f, 0, f, m);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4733
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4734
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4735
        public final boolean add(V e) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4736
            throw new UnsupportedOperationException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4737
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4738
        public final boolean addAll(Collection<? extends V> c) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4739
            throw new UnsupportedOperationException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4740
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4741
39779
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4742
        @Override public boolean removeAll(Collection<?> c) {
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4743
            if (c == null) throw new NullPointerException();
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4744
            boolean modified = false;
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4745
            for (Iterator<V> it = iterator(); it.hasNext();) {
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4746
                if (c.contains(it.next())) {
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4747
                    it.remove();
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4748
                    modified = true;
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4749
                }
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4750
            }
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4751
            return modified;
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4752
        }
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4753
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  4754
        public boolean removeIf(Predicate<? super V> filter) {
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  4755
            return map.removeValueIf(filter);
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  4756
        }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  4757
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4758
        public Spliterator<V> spliterator() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4759
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4760
            ConcurrentHashMap<K,V> m = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4761
            long n = m.sumCount();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4762
            int f = (t = m.table) == null ? 0 : t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4763
            return new ValueSpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4764
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4765
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4766
        public void forEach(Consumer<? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4767
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4768
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4769
            if ((t = map.table) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4770
                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4771
                for (Node<K,V> p; (p = it.advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4772
                    action.accept(p.val);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4773
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4774
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4775
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4776
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4777
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4778
     * A view of a ConcurrentHashMap as a {@link Set} of (key, value)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4779
     * entries.  This class cannot be directly instantiated. See
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4780
     * {@link #entrySet()}.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4781
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4782
    static final class EntrySetView<K,V> extends CollectionView<K,V,Map.Entry<K,V>>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4783
        implements Set<Map.Entry<K,V>>, java.io.Serializable {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4784
        private static final long serialVersionUID = 2249069246763182397L;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4785
        EntrySetView(ConcurrentHashMap<K,V> map) { super(map); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4786
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4787
        public boolean contains(Object o) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4788
            Object k, v, r; Map.Entry<?,?> e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4789
            return ((o instanceof Map.Entry) &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4790
                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4791
                    (r = map.get(k)) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4792
                    (v = e.getValue()) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4793
                    (v == r || v.equals(r)));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4794
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4795
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4796
        public boolean remove(Object o) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4797
            Object k, v; Map.Entry<?,?> e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4798
            return ((o instanceof Map.Entry) &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4799
                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4800
                    (v = e.getValue()) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4801
                    map.remove(k, v));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4802
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4803
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4804
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4805
         * @return an iterator over the entries of the backing map
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4806
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4807
        public Iterator<Map.Entry<K,V>> iterator() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4808
            ConcurrentHashMap<K,V> m = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4809
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4810
            int f = (t = m.table) == null ? 0 : t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4811
            return new EntryIterator<K,V>(t, f, 0, f, m);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4812
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4813
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4814
        public boolean add(Entry<K,V> e) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4815
            return map.putVal(e.getKey(), e.getValue(), false) == null;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4816
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4817
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4818
        public boolean addAll(Collection<? extends Entry<K,V>> c) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4819
            boolean added = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4820
            for (Entry<K,V> e : c) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4821
                if (add(e))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4822
                    added = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4823
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4824
            return added;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4825
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4826
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  4827
        public boolean removeIf(Predicate<? super Entry<K,V>> filter) {
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  4828
            return map.removeEntryIf(filter);
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  4829
        }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  4830
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4831
        public final int hashCode() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4832
            int h = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4833
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4834
            if ((t = map.table) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4835
                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4836
                for (Node<K,V> p; (p = it.advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4837
                    h += p.hashCode();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4838
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4839
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4840
            return h;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4841
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4842
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4843
        public final boolean equals(Object o) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4844
            Set<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4845
            return ((o instanceof Set) &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4846
                    ((c = (Set<?>)o) == this ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4847
                     (containsAll(c) && c.containsAll(this))));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4848
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4849
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4850
        public Spliterator<Map.Entry<K,V>> spliterator() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4851
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4852
            ConcurrentHashMap<K,V> m = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4853
            long n = m.sumCount();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4854
            int f = (t = m.table) == null ? 0 : t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4855
            return new EntrySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n, m);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4856
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4857
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4858
        public void forEach(Consumer<? super Map.Entry<K,V>> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4859
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4860
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4861
            if ((t = map.table) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4862
                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4863
                for (Node<K,V> p; (p = it.advance()) != null; )
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4864
                    action.accept(new MapEntry<K,V>(p.key, p.val, map));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4865
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4866
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4867
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4868
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4869
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4870
    // -------------------------------------------------------
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4871
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4872
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4873
     * Base class for bulk tasks. Repeats some fields and code from
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4874
     * class Traverser, because we need to subclass CountedCompleter.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4875
     */
19380
22eefc44fad6 8022724: lint warnings in j.u.concurrent packages
dl
parents: 19036
diff changeset
  4876
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4877
    abstract static class BulkTask<K,V,R> extends CountedCompleter<R> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4878
        Node<K,V>[] tab;        // same as Traverser
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4879
        Node<K,V> next;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4880
        TableStack<K,V> stack, spare;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4881
        int index;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4882
        int baseIndex;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4883
        int baseLimit;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4884
        final int baseSize;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4885
        int batch;              // split control
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4886
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4887
        BulkTask(BulkTask<K,V,?> par, int b, int i, int f, Node<K,V>[] t) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4888
            super(par);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4889
            this.batch = b;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4890
            this.index = this.baseIndex = i;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4891
            if ((this.tab = t) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4892
                this.baseSize = this.baseLimit = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4893
            else if (par == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4894
                this.baseSize = this.baseLimit = t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4895
            else {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4896
                this.baseLimit = f;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4897
                this.baseSize = par.baseSize;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4898
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4899
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4900
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4901
        /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  4902
         * Same as Traverser version.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4903
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4904
        final Node<K,V> advance() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4905
            Node<K,V> e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4906
            if ((e = next) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4907
                e = e.next;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4908
            for (;;) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4909
                Node<K,V>[] t; int i, n;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4910
                if (e != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4911
                    return next = e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4912
                if (baseIndex >= baseLimit || (t = tab) == null ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4913
                    (n = t.length) <= (i = index) || i < 0)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4914
                    return next = null;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4915
                if ((e = tabAt(t, i)) != null && e.hash < 0) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4916
                    if (e instanceof ForwardingNode) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4917
                        tab = ((ForwardingNode<K,V>)e).nextTable;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4918
                        e = null;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4919
                        pushState(t, i, n);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4920
                        continue;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4921
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4922
                    else if (e instanceof TreeBin)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4923
                        e = ((TreeBin<K,V>)e).first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4924
                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4925
                        e = null;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4926
                }
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4927
                if (stack != null)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4928
                    recoverState(n);
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4929
                else if ((index = i + baseSize) >= n)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4930
                    index = ++baseIndex;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  4931
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4932
        }
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4933
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4934
        private void pushState(Node<K,V>[] t, int i, int n) {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4935
            TableStack<K,V> s = spare;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4936
            if (s != null)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4937
                spare = s.next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4938
            else
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4939
                s = new TableStack<K,V>();
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4940
            s.tab = t;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4941
            s.length = n;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4942
            s.index = i;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4943
            s.next = stack;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4944
            stack = s;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4945
        }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4946
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4947
        private void recoverState(int n) {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4948
            TableStack<K,V> s; int len;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4949
            while ((s = stack) != null && (index += (len = s.length)) >= n) {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4950
                n = len;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4951
                index = s.index;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4952
                tab = s.tab;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4953
                s.tab = null;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4954
                TableStack<K,V> next = s.next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4955
                s.next = spare; // save for reuse
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4956
                stack = next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4957
                spare = s;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4958
            }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4959
            if (s == null && (index += baseSize) >= n)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4960
                index = ++baseIndex;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4961
        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4962
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4963
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4964
    /*
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4965
     * Task classes. Coded in a regular but ugly format/style to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4966
     * simplify checks that each variant differs in the right way from
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4967
     * others. The null screenings exist because compilers cannot tell
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4968
     * that we've already null-checked task arguments, so we force
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4969
     * simplest hoisted bypass to help avoid convoluted traps.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4970
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4971
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4972
    static final class ForEachKeyTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4973
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4974
        final Consumer<? super K> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4975
        ForEachKeyTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4976
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4977
             Consumer<? super K> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4978
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4979
            this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4980
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4981
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4982
            final Consumer<? super K> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4983
            if ((action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4984
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4985
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4986
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4987
                    new ForEachKeyTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4988
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4989
                         action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4990
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4991
                for (Node<K,V> p; (p = advance()) != null;)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4992
                    action.accept(p.key);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4993
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4994
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4995
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4996
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4997
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4998
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4999
    static final class ForEachValueTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5000
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5001
        final Consumer<? super V> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5002
        ForEachValueTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5003
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5004
             Consumer<? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5005
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5006
            this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5007
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5008
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5009
            final Consumer<? super V> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5010
            if ((action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5011
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5012
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5013
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5014
                    new ForEachValueTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5015
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5016
                         action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5017
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5018
                for (Node<K,V> p; (p = advance()) != null;)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5019
                    action.accept(p.val);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5020
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5021
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5022
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5023
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5024
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5025
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5026
    static final class ForEachEntryTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5027
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5028
        final Consumer<? super Entry<K,V>> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5029
        ForEachEntryTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5030
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5031
             Consumer<? super Entry<K,V>> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5032
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5033
            this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5034
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5035
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5036
            final Consumer<? super Entry<K,V>> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5037
            if ((action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5038
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5039
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5040
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5041
                    new ForEachEntryTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5042
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5043
                         action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5044
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5045
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5046
                    action.accept(p);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5047
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5048
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5049
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5050
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5051
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5052
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5053
    static final class ForEachMappingTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5054
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5055
        final BiConsumer<? super K, ? super V> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5056
        ForEachMappingTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5057
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5058
             BiConsumer<? super K,? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5059
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5060
            this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5061
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5062
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5063
            final BiConsumer<? super K, ? super V> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5064
            if ((action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5065
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5066
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5067
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5068
                    new ForEachMappingTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5069
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5070
                         action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5071
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5072
                for (Node<K,V> p; (p = advance()) != null; )
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5073
                    action.accept(p.key, p.val);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5074
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5075
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5076
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5077
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5078
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5079
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5080
    static final class ForEachTransformedKeyTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5081
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5082
        final Function<? super K, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5083
        final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5084
        ForEachTransformedKeyTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5085
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5086
             Function<? super K, ? extends U> transformer, Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5087
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5088
            this.transformer = transformer; this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5089
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5090
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5091
            final Function<? super K, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5092
            final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5093
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5094
                (action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5095
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5096
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5097
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5098
                    new ForEachTransformedKeyTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5099
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5100
                         transformer, action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5101
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5102
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5103
                    U u;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5104
                    if ((u = transformer.apply(p.key)) != null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5105
                        action.accept(u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5106
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5107
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5108
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5109
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5110
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5111
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5112
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5113
    static final class ForEachTransformedValueTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5114
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5115
        final Function<? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5116
        final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5117
        ForEachTransformedValueTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5118
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5119
             Function<? super V, ? extends U> transformer, Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5120
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5121
            this.transformer = transformer; this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5122
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5123
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5124
            final Function<? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5125
            final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5126
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5127
                (action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5128
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5129
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5130
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5131
                    new ForEachTransformedValueTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5132
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5133
                         transformer, action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5134
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5135
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5136
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5137
                    if ((u = transformer.apply(p.val)) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5138
                        action.accept(u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5139
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5140
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5141
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5142
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5143
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5144
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5145
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5146
    static final class ForEachTransformedEntryTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5147
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5148
        final Function<Map.Entry<K,V>, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5149
        final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5150
        ForEachTransformedEntryTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5151
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5152
             Function<Map.Entry<K,V>, ? extends U> transformer, Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5153
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5154
            this.transformer = transformer; this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5155
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5156
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5157
            final Function<Map.Entry<K,V>, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5158
            final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5159
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5160
                (action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5161
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5162
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5163
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5164
                    new ForEachTransformedEntryTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5165
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5166
                         transformer, action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5167
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5168
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5169
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5170
                    if ((u = transformer.apply(p)) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5171
                        action.accept(u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5172
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5173
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5174
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5175
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5176
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5177
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5178
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5179
    static final class ForEachTransformedMappingTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5180
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5181
        final BiFunction<? super K, ? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5182
        final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5183
        ForEachTransformedMappingTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5184
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5185
             BiFunction<? super K, ? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5186
             Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5187
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5188
            this.transformer = transformer; this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5189
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5190
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5191
            final BiFunction<? super K, ? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5192
            final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5193
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5194
                (action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5195
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5196
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5197
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5198
                    new ForEachTransformedMappingTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5199
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5200
                         transformer, action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5201
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5202
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5203
                    U u;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5204
                    if ((u = transformer.apply(p.key, p.val)) != null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5205
                        action.accept(u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5206
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5207
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5208
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5209
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5210
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5211
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5212
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5213
    static final class SearchKeysTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5214
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5215
        final Function<? super K, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5216
        final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5217
        SearchKeysTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5218
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5219
             Function<? super K, ? extends U> searchFunction,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5220
             AtomicReference<U> result) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5221
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5222
            this.searchFunction = searchFunction; this.result = result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5223
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5224
        public final U getRawResult() { return result.get(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5225
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5226
            final Function<? super K, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5227
            final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5228
            if ((searchFunction = this.searchFunction) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5229
                (result = this.result) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5230
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5231
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5232
                    if (result.get() != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5233
                        return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5234
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5235
                    new SearchKeysTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5236
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5237
                         searchFunction, result).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5238
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5239
                while (result.get() == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5240
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5241
                    Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5242
                    if ((p = advance()) == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5243
                        propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5244
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5245
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5246
                    if ((u = searchFunction.apply(p.key)) != null) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5247
                        if (result.compareAndSet(null, u))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5248
                            quietlyCompleteRoot();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5249
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5250
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5251
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5252
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5253
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5254
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5255
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5256
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5257
    static final class SearchValuesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5258
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5259
        final Function<? super V, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5260
        final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5261
        SearchValuesTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5262
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5263
             Function<? super V, ? extends U> searchFunction,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5264
             AtomicReference<U> result) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5265
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5266
            this.searchFunction = searchFunction; this.result = result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5267
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5268
        public final U getRawResult() { return result.get(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5269
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5270
            final Function<? super V, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5271
            final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5272
            if ((searchFunction = this.searchFunction) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5273
                (result = this.result) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5274
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5275
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5276
                    if (result.get() != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5277
                        return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5278
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5279
                    new SearchValuesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5280
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5281
                         searchFunction, result).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5282
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5283
                while (result.get() == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5284
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5285
                    Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5286
                    if ((p = advance()) == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5287
                        propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5288
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5289
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5290
                    if ((u = searchFunction.apply(p.val)) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5291
                        if (result.compareAndSet(null, u))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5292
                            quietlyCompleteRoot();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5293
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5294
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5295
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5296
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5297
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5298
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5299
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5300
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5301
    static final class SearchEntriesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5302
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5303
        final Function<Entry<K,V>, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5304
        final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5305
        SearchEntriesTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5306
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5307
             Function<Entry<K,V>, ? extends U> searchFunction,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5308
             AtomicReference<U> result) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5309
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5310
            this.searchFunction = searchFunction; this.result = result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5311
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5312
        public final U getRawResult() { return result.get(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5313
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5314
            final Function<Entry<K,V>, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5315
            final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5316
            if ((searchFunction = this.searchFunction) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5317
                (result = this.result) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5318
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5319
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5320
                    if (result.get() != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5321
                        return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5322
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5323
                    new SearchEntriesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5324
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5325
                         searchFunction, result).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5326
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5327
                while (result.get() == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5328
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5329
                    Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5330
                    if ((p = advance()) == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5331
                        propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5332
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5333
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5334
                    if ((u = searchFunction.apply(p)) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5335
                        if (result.compareAndSet(null, u))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5336
                            quietlyCompleteRoot();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5337
                        return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5338
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5339
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5340
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5341
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5342
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5343
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5344
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5345
    static final class SearchMappingsTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5346
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5347
        final BiFunction<? super K, ? super V, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5348
        final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5349
        SearchMappingsTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5350
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5351
             BiFunction<? super K, ? super V, ? extends U> searchFunction,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5352
             AtomicReference<U> result) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5353
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5354
            this.searchFunction = searchFunction; this.result = result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5355
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5356
        public final U getRawResult() { return result.get(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5357
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5358
            final BiFunction<? super K, ? super V, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5359
            final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5360
            if ((searchFunction = this.searchFunction) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5361
                (result = this.result) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5362
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5363
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5364
                    if (result.get() != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5365
                        return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5366
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5367
                    new SearchMappingsTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5368
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5369
                         searchFunction, result).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5370
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5371
                while (result.get() == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5372
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5373
                    Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5374
                    if ((p = advance()) == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5375
                        propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5376
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5377
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5378
                    if ((u = searchFunction.apply(p.key, p.val)) != null) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5379
                        if (result.compareAndSet(null, u))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5380
                            quietlyCompleteRoot();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5381
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5382
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5383
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5384
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5385
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5386
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5387
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5388
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5389
    static final class ReduceKeysTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5390
        extends BulkTask<K,V,K> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5391
        final BiFunction<? super K, ? super K, ? extends K> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5392
        K result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5393
        ReduceKeysTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5394
        ReduceKeysTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5395
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5396
             ReduceKeysTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5397
             BiFunction<? super K, ? super K, ? extends K> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5398
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5399
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5400
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5401
        public final K getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5402
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5403
            final BiFunction<? super K, ? super K, ? extends K> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5404
            if ((reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5405
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5406
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5407
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5408
                    (rights = new ReduceKeysTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5409
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5410
                      rights, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5411
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5412
                K r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5413
                for (Node<K,V> p; (p = advance()) != null; ) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5414
                    K u = p.key;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5415
                    r = (r == null) ? u : u == null ? r : reducer.apply(r, u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5416
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5417
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5418
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5419
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5420
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5421
                    ReduceKeysTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5422
                        t = (ReduceKeysTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5423
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5424
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5425
                        K tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5426
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5427
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5428
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5429
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5430
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5431
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5432
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5433
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5434
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5435
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5436
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5437
    static final class ReduceValuesTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5438
        extends BulkTask<K,V,V> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5439
        final BiFunction<? super V, ? super V, ? extends V> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5440
        V result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5441
        ReduceValuesTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5442
        ReduceValuesTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5443
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5444
             ReduceValuesTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5445
             BiFunction<? super V, ? super V, ? extends V> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5446
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5447
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5448
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5449
        public final V getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5450
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5451
            final BiFunction<? super V, ? super V, ? extends V> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5452
            if ((reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5453
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5454
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5455
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5456
                    (rights = new ReduceValuesTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5457
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5458
                      rights, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5459
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5460
                V r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5461
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5462
                    V v = p.val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5463
                    r = (r == null) ? v : reducer.apply(r, v);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5464
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5465
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5466
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5467
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5468
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5469
                    ReduceValuesTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5470
                        t = (ReduceValuesTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5471
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5472
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5473
                        V tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5474
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5475
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5476
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5477
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5478
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5479
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5480
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5481
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5482
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5483
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5484
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5485
    static final class ReduceEntriesTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5486
        extends BulkTask<K,V,Map.Entry<K,V>> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5487
        final BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5488
        Map.Entry<K,V> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5489
        ReduceEntriesTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5490
        ReduceEntriesTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5491
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5492
             ReduceEntriesTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5493
             BiFunction<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5494
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5495
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5496
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5497
        public final Map.Entry<K,V> getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5498
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5499
            final BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5500
            if ((reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5501
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5502
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5503
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5504
                    (rights = new ReduceEntriesTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5505
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5506
                      rights, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5507
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5508
                Map.Entry<K,V> r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5509
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5510
                    r = (r == null) ? p : reducer.apply(r, p);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5511
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5512
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5513
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5514
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5515
                    ReduceEntriesTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5516
                        t = (ReduceEntriesTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5517
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5518
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5519
                        Map.Entry<K,V> tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5520
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5521
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5522
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5523
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5524
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5525
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5526
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5527
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5528
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5529
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5530
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5531
    static final class MapReduceKeysTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5532
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5533
        final Function<? super K, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5534
        final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5535
        U result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5536
        MapReduceKeysTask<K,V,U> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5537
        MapReduceKeysTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5538
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5539
             MapReduceKeysTask<K,V,U> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5540
             Function<? super K, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5541
             BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5542
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5543
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5544
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5545
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5546
        public final U getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5547
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5548
            final Function<? super K, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5549
            final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5550
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5551
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5552
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5553
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5554
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5555
                    (rights = new MapReduceKeysTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5556
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5557
                      rights, transformer, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5558
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5559
                U r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5560
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5561
                    U u;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5562
                    if ((u = transformer.apply(p.key)) != null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5563
                        r = (r == null) ? u : reducer.apply(r, u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5564
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5565
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5566
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5567
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5568
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5569
                    MapReduceKeysTask<K,V,U>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5570
                        t = (MapReduceKeysTask<K,V,U>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5571
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5572
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5573
                        U tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5574
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5575
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5576
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5577
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5578
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5579
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5580
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5581
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5582
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5583
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5584
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5585
    static final class MapReduceValuesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5586
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5587
        final Function<? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5588
        final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5589
        U result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5590
        MapReduceValuesTask<K,V,U> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5591
        MapReduceValuesTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5592
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5593
             MapReduceValuesTask<K,V,U> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5594
             Function<? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5595
             BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5596
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5597
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5598
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5599
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5600
        public final U getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5601
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5602
            final Function<? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5603
            final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5604
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5605
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5606
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5607
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5608
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5609
                    (rights = new MapReduceValuesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5610
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5611
                      rights, transformer, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5612
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5613
                U r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5614
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5615
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5616
                    if ((u = transformer.apply(p.val)) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5617
                        r = (r == null) ? u : reducer.apply(r, u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5618
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5619
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5620
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5621
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5622
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5623
                    MapReduceValuesTask<K,V,U>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5624
                        t = (MapReduceValuesTask<K,V,U>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5625
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5626
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5627
                        U tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5628
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5629
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5630
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5631
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5632
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5633
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5634
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5635
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5636
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5637
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5638
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5639
    static final class MapReduceEntriesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5640
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5641
        final Function<Map.Entry<K,V>, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5642
        final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5643
        U result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5644
        MapReduceEntriesTask<K,V,U> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5645
        MapReduceEntriesTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5646
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5647
             MapReduceEntriesTask<K,V,U> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5648
             Function<Map.Entry<K,V>, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5649
             BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5650
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5651
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5652
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5653
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5654
        public final U getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5655
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5656
            final Function<Map.Entry<K,V>, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5657
            final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5658
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5659
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5660
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5661
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5662
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5663
                    (rights = new MapReduceEntriesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5664
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5665
                      rights, transformer, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5666
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5667
                U r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5668
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5669
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5670
                    if ((u = transformer.apply(p)) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5671
                        r = (r == null) ? u : reducer.apply(r, u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5672
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5673
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5674
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5675
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5676
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5677
                    MapReduceEntriesTask<K,V,U>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5678
                        t = (MapReduceEntriesTask<K,V,U>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5679
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5680
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5681
                        U tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5682
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5683
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5684
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5685
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5686
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5687
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5688
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5689
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5690
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5691
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5692
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5693
    static final class MapReduceMappingsTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5694
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5695
        final BiFunction<? super K, ? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5696
        final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5697
        U result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5698
        MapReduceMappingsTask<K,V,U> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5699
        MapReduceMappingsTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5700
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5701
             MapReduceMappingsTask<K,V,U> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5702
             BiFunction<? super K, ? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5703
             BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5704
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5705
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5706
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5707
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5708
        public final U getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5709
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5710
            final BiFunction<? super K, ? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5711
            final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5712
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5713
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5714
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5715
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5716
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5717
                    (rights = new MapReduceMappingsTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5718
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5719
                      rights, transformer, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5720
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5721
                U r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5722
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5723
                    U u;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5724
                    if ((u = transformer.apply(p.key, p.val)) != null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5725
                        r = (r == null) ? u : reducer.apply(r, u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5726
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5727
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5728
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5729
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5730
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5731
                    MapReduceMappingsTask<K,V,U>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5732
                        t = (MapReduceMappingsTask<K,V,U>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5733
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5734
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5735
                        U tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5736
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5737
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5738
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5739
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5740
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5741
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5742
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5743
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5744
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5745
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5746
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5747
    static final class MapReduceKeysToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5748
        extends BulkTask<K,V,Double> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5749
        final ToDoubleFunction<? super K> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5750
        final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5751
        final double basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5752
        double result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5753
        MapReduceKeysToDoubleTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5754
        MapReduceKeysToDoubleTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5755
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5756
             MapReduceKeysToDoubleTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5757
             ToDoubleFunction<? super K> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5758
             double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5759
             DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5760
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5761
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5762
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5763
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5764
        public final Double getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5765
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5766
            final ToDoubleFunction<? super K> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5767
            final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5768
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5769
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5770
                double r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5771
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5772
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5773
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5774
                    (rights = new MapReduceKeysToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5775
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5776
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5777
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5778
                for (Node<K,V> p; (p = advance()) != null; )
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5779
                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5780
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5781
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5782
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5783
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5784
                    MapReduceKeysToDoubleTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5785
                        t = (MapReduceKeysToDoubleTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5786
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5787
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5788
                        t.result = reducer.applyAsDouble(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5789
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5790
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5791
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5792
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5793
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5794
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5795
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5796
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5797
    static final class MapReduceValuesToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5798
        extends BulkTask<K,V,Double> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5799
        final ToDoubleFunction<? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5800
        final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5801
        final double basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5802
        double result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5803
        MapReduceValuesToDoubleTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5804
        MapReduceValuesToDoubleTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5805
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5806
             MapReduceValuesToDoubleTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5807
             ToDoubleFunction<? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5808
             double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5809
             DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5810
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5811
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5812
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5813
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5814
        public final Double getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5815
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5816
            final ToDoubleFunction<? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5817
            final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5818
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5819
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5820
                double r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5821
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5822
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5823
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5824
                    (rights = new MapReduceValuesToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5825
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5826
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5827
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5828
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5829
                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.val));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5830
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5831
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5832
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5833
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5834
                    MapReduceValuesToDoubleTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5835
                        t = (MapReduceValuesToDoubleTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5836
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5837
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5838
                        t.result = reducer.applyAsDouble(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5839
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5840
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5841
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5842
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5843
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5844
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5845
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5846
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5847
    static final class MapReduceEntriesToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5848
        extends BulkTask<K,V,Double> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5849
        final ToDoubleFunction<Map.Entry<K,V>> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5850
        final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5851
        final double basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5852
        double result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5853
        MapReduceEntriesToDoubleTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5854
        MapReduceEntriesToDoubleTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5855
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5856
             MapReduceEntriesToDoubleTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5857
             ToDoubleFunction<Map.Entry<K,V>> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5858
             double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5859
             DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5860
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5861
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5862
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5863
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5864
        public final Double getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5865
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5866
            final ToDoubleFunction<Map.Entry<K,V>> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5867
            final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5868
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5869
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5870
                double r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5871
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5872
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5873
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5874
                    (rights = new MapReduceEntriesToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5875
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5876
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5877
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5878
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5879
                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5880
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5881
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5882
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5883
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5884
                    MapReduceEntriesToDoubleTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5885
                        t = (MapReduceEntriesToDoubleTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5886
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5887
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5888
                        t.result = reducer.applyAsDouble(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5889
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5890
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5891
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5892
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5893
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5894
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5895
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5896
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5897
    static final class MapReduceMappingsToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5898
        extends BulkTask<K,V,Double> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5899
        final ToDoubleBiFunction<? super K, ? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5900
        final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5901
        final double basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5902
        double result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5903
        MapReduceMappingsToDoubleTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5904
        MapReduceMappingsToDoubleTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5905
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5906
             MapReduceMappingsToDoubleTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5907
             ToDoubleBiFunction<? super K, ? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5908
             double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5909
             DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5910
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5911
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5912
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5913
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5914
        public final Double getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5915
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5916
            final ToDoubleBiFunction<? super K, ? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5917
            final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5918
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5919
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5920
                double r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5921
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5922
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5923
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5924
                    (rights = new MapReduceMappingsToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5925
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5926
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5927
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5928
                for (Node<K,V> p; (p = advance()) != null; )
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5929
                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key, p.val));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5930
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5931
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5932
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5933
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5934
                    MapReduceMappingsToDoubleTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5935
                        t = (MapReduceMappingsToDoubleTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5936
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5937
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5938
                        t.result = reducer.applyAsDouble(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5939
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5940
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5941
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5942
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5943
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5944
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5945
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5946
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5947
    static final class MapReduceKeysToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5948
        extends BulkTask<K,V,Long> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5949
        final ToLongFunction<? super K> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5950
        final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5951
        final long basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5952
        long result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5953
        MapReduceKeysToLongTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5954
        MapReduceKeysToLongTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5955
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5956
             MapReduceKeysToLongTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5957
             ToLongFunction<? super K> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5958
             long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5959
             LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5960
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5961
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5962
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5963
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5964
        public final Long getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5965
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5966
            final ToLongFunction<? super K> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5967
            final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5968
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5969
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5970
                long r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5971
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5972
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5973
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5974
                    (rights = new MapReduceKeysToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5975
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5976
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5977
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5978
                for (Node<K,V> p; (p = advance()) != null; )
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5979
                    r = reducer.applyAsLong(r, transformer.applyAsLong(p.key));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5980
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5981
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5982
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5983
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5984
                    MapReduceKeysToLongTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5985
                        t = (MapReduceKeysToLongTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5986
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5987
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5988
                        t.result = reducer.applyAsLong(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5989
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5990
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5991
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5992
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5993
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5994
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5995
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5996
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5997
    static final class MapReduceValuesToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5998
        extends BulkTask<K,V,Long> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5999
        final ToLongFunction<? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6000
        final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6001
        final long basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6002
        long result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6003
        MapReduceValuesToLongTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6004
        MapReduceValuesToLongTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6005
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6006
             MapReduceValuesToLongTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6007
             ToLongFunction<? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6008
             long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6009
             LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6010
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6011
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6012
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6013
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6014
        public final Long getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6015
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6016
            final ToLongFunction<? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6017
            final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6018
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6019
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6020
                long r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6021
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6022
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6023
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6024
                    (rights = new MapReduceValuesToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6025
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6026
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6027
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6028
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6029
                    r = reducer.applyAsLong(r, transformer.applyAsLong(p.val));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6030
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6031
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6032
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6033
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6034
                    MapReduceValuesToLongTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6035
                        t = (MapReduceValuesToLongTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6036
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6037
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6038
                        t.result = reducer.applyAsLong(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6039
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6040
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6041
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6042
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6043
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6044
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6045
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6046
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6047
    static final class MapReduceEntriesToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6048
        extends BulkTask<K,V,Long> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6049
        final ToLongFunction<Map.Entry<K,V>> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6050
        final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6051
        final long basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6052
        long result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6053
        MapReduceEntriesToLongTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6054
        MapReduceEntriesToLongTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6055
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6056
             MapReduceEntriesToLongTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6057
             ToLongFunction<Map.Entry<K,V>> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6058
             long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6059
             LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6060
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6061
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6062
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6063
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6064
        public final Long getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6065
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6066
            final ToLongFunction<Map.Entry<K,V>> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6067
            final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6068
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6069
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6070
                long r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6071
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6072
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6073
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6074
                    (rights = new MapReduceEntriesToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6075
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6076
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6077
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6078
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6079
                    r = reducer.applyAsLong(r, transformer.applyAsLong(p));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6080
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6081
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6082
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6083
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6084
                    MapReduceEntriesToLongTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6085
                        t = (MapReduceEntriesToLongTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6086
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6087
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6088
                        t.result = reducer.applyAsLong(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6089
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6090
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6091
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6092
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6093
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6094
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6095
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6096
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6097
    static final class MapReduceMappingsToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6098
        extends BulkTask<K,V,Long> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6099
        final ToLongBiFunction<? super K, ? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6100
        final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6101
        final long basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6102
        long result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6103
        MapReduceMappingsToLongTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6104
        MapReduceMappingsToLongTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6105
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6106
             MapReduceMappingsToLongTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6107
             ToLongBiFunction<? super K, ? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6108
             long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6109
             LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6110
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6111
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6112
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6113
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6114
        public final Long getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6115
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6116
            final ToLongBiFunction<? super K, ? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6117
            final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6118
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6119
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6120
                long r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6121
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6122
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6123
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6124
                    (rights = new MapReduceMappingsToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6125
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6126
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6127
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6128
                for (Node<K,V> p; (p = advance()) != null; )
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6129
                    r = reducer.applyAsLong(r, transformer.applyAsLong(p.key, p.val));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6130
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6131
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6132
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6133
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6134
                    MapReduceMappingsToLongTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6135
                        t = (MapReduceMappingsToLongTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6136
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6137
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6138
                        t.result = reducer.applyAsLong(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6139
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6140
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6141
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6142
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6143
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6144
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6145
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6146
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6147
    static final class MapReduceKeysToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6148
        extends BulkTask<K,V,Integer> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6149
        final ToIntFunction<? super K> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6150
        final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6151
        final int basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6152
        int result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6153
        MapReduceKeysToIntTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6154
        MapReduceKeysToIntTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6155
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6156
             MapReduceKeysToIntTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6157
             ToIntFunction<? super K> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6158
             int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6159
             IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6160
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6161
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6162
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6163
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6164
        public final Integer getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6165
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6166
            final ToIntFunction<? super K> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6167
            final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6168
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6169
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6170
                int r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6171
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6172
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6173
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6174
                    (rights = new MapReduceKeysToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6175
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6176
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6177
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6178
                for (Node<K,V> p; (p = advance()) != null; )
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6179
                    r = reducer.applyAsInt(r, transformer.applyAsInt(p.key));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6180
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6181
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6182
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6183
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6184
                    MapReduceKeysToIntTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6185
                        t = (MapReduceKeysToIntTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6186
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6187
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6188
                        t.result = reducer.applyAsInt(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6189
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6190
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6191
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6192
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6193
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6194
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6195
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6196
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6197
    static final class MapReduceValuesToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6198
        extends BulkTask<K,V,Integer> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6199
        final ToIntFunction<? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6200
        final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6201
        final int basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6202
        int result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6203
        MapReduceValuesToIntTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6204
        MapReduceValuesToIntTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6205
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6206
             MapReduceValuesToIntTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6207
             ToIntFunction<? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6208
             int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6209
             IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6210
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6211
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6212
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6213
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6214
        public final Integer getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6215
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6216
            final ToIntFunction<? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6217
            final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6218
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6219
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6220
                int r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6221
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6222
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6223
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6224
                    (rights = new MapReduceValuesToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6225
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6226
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6227
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6228
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6229
                    r = reducer.applyAsInt(r, transformer.applyAsInt(p.val));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6230
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6231
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6232
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6233
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6234
                    MapReduceValuesToIntTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6235
                        t = (MapReduceValuesToIntTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6236
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6237
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6238
                        t.result = reducer.applyAsInt(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6239
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6240
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6241
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6242
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6243
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6244
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6245
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6246
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6247
    static final class MapReduceEntriesToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6248
        extends BulkTask<K,V,Integer> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6249
        final ToIntFunction<Map.Entry<K,V>> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6250
        final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6251
        final int basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6252
        int result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6253
        MapReduceEntriesToIntTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6254
        MapReduceEntriesToIntTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6255
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6256
             MapReduceEntriesToIntTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6257
             ToIntFunction<Map.Entry<K,V>> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6258
             int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6259
             IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6260
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6261
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6262
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6263
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6264
        public final Integer getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6265
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6266
            final ToIntFunction<Map.Entry<K,V>> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6267
            final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6268
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6269
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6270
                int r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6271
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6272
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6273
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6274
                    (rights = new MapReduceEntriesToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6275
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6276
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6277
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6278
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6279
                    r = reducer.applyAsInt(r, transformer.applyAsInt(p));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6280
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6281
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6282
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6283
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6284
                    MapReduceEntriesToIntTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6285
                        t = (MapReduceEntriesToIntTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6286
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6287
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6288
                        t.result = reducer.applyAsInt(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6289
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6290
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6291
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6292
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6293
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6294
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6295
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6296
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6297
    static final class MapReduceMappingsToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6298
        extends BulkTask<K,V,Integer> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6299
        final ToIntBiFunction<? super K, ? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6300
        final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6301
        final int basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6302
        int result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6303
        MapReduceMappingsToIntTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6304
        MapReduceMappingsToIntTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6305
            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6306
             MapReduceMappingsToIntTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6307
             ToIntBiFunction<? super K, ? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6308
             int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6309
             IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6310
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6311
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6312
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6313
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6314
        public final Integer getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6315
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6316
            final ToIntBiFunction<? super K, ? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6317
            final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6318
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6319
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6320
                int r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6321
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6322
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6323
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6324
                    (rights = new MapReduceMappingsToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6325
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6326
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6327
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6328
                for (Node<K,V> p; (p = advance()) != null; )
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6329
                    r = reducer.applyAsInt(r, transformer.applyAsInt(p.key, p.val));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6330
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6331
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6332
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6333
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6334
                    MapReduceMappingsToIntTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6335
                        t = (MapReduceMappingsToIntTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6336
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6337
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6338
                        t.result = reducer.applyAsInt(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6339
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6340
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6341
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6342
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  6343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  6344
    }
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6345
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6346
    // Unsafe mechanics
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
  6347
    private static final Unsafe U = Unsafe.getUnsafe();
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 52958
diff changeset
  6348
    private static final long SIZECTL
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 52958
diff changeset
  6349
        = U.objectFieldOffset(ConcurrentHashMap.class, "sizeCtl");
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 52958
diff changeset
  6350
    private static final long TRANSFERINDEX
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 52958
diff changeset
  6351
        = U.objectFieldOffset(ConcurrentHashMap.class, "transferIndex");
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 52958
diff changeset
  6352
    private static final long BASECOUNT
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 52958
diff changeset
  6353
        = U.objectFieldOffset(ConcurrentHashMap.class, "baseCount");
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 52958
diff changeset
  6354
    private static final long CELLSBUSY
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 52958
diff changeset
  6355
        = U.objectFieldOffset(ConcurrentHashMap.class, "cellsBusy");
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 52958
diff changeset
  6356
    private static final long CELLVALUE
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 52958
diff changeset
  6357
        = U.objectFieldOffset(CounterCell.class, "value");
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 52958
diff changeset
  6358
    private static final int ABASE = U.arrayBaseOffset(Node[].class);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6359
    private static final int ASHIFT;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6360
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6361
    static {
46873
7ac2f551b0d6 8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents: 45563
diff changeset
  6362
        int scale = U.arrayIndexScale(Node[].class);
7ac2f551b0d6 8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents: 45563
diff changeset
  6363
        if ((scale & (scale - 1)) != 0)
49565
b5705ade8c8d 8197531: Miscellaneous changes imported from jsr166 CVS 2018-04
dl
parents: 49433
diff changeset
  6364
            throw new ExceptionInInitializerError("array index scale not a power of two");
46873
7ac2f551b0d6 8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents: 45563
diff changeset
  6365
        ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  6366
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  6367
        // Reduce the risk of rare disastrous classloading in first call to
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  6368
        // LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  6369
        Class<?> ensureLoaded = LockSupport.class;
49937
7fed08d588b5 8202373: Forcing eager initialization of CHM$ReservationNode avoids deoptimization
dl
parents: 49565
diff changeset
  6370
7fed08d588b5 8202373: Forcing eager initialization of CHM$ReservationNode avoids deoptimization
dl
parents: 49565
diff changeset
  6371
        // Eager class load observed to help JIT during startup
7fed08d588b5 8202373: Forcing eager initialization of CHM$ReservationNode avoids deoptimization
dl
parents: 49565
diff changeset
  6372
        ensureLoaded = ReservationNode.class;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6373
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  6374
}