jdk/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java
author dl
Mon, 10 Apr 2017 13:46:16 -0700
changeset 44588 fd9d4d384425
parent 40547 51759e487197
child 44926 32eeee1c0abd
permissions -rw-r--r--
8176402: parameter name switcharoo in ConcurrentHashMap Reviewed-by: martin, psandoz
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
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   162
 * {@link java.util.Map.Entry} objects do not support method {@code
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   163
 * setValue}.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   164
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   165
 * <ul>
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   166
 * <li>forEach: Performs a given action on each element.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   167
 * 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
   168
 * before performing the action.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   169
 *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   170
 * <li>search: Returns the first available non-null result of
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   171
 * applying a given function on each element; skipping further
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   172
 * search when a result is found.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   173
 *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   174
 * <li>reduce: Accumulates each element.  The supplied reduction
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   175
 * function cannot rely on ordering (more formally, it should be
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   176
 * both associative and commutative).  There are five variants:
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   177
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   178
 * <ul>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   179
 *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   180
 * <li>Plain reductions. (There is not a form of this method for
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   181
 * (key, value) function arguments since there is no corresponding
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   182
 * return type.)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   183
 *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   184
 * <li>Mapped reductions that accumulate the results of a given
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   185
 * function applied to each element.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   186
 *
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   187
 * <li>Reductions to scalar doubles, longs, and ints, using a
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   188
 * given basis value.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   189
 *
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
 * </ul>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   192
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   193
 * <p>These bulk operations accept a {@code parallelismThreshold}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   194
 * argument. Methods proceed sequentially if the current map size is
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   195
 * estimated to be less than the given threshold. Using a value of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   196
 * {@code Long.MAX_VALUE} suppresses all parallelism.  Using a value
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   197
 * of {@code 1} results in maximal parallelism by partitioning into
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   198
 * enough subtasks to fully utilize the {@link
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   199
 * ForkJoinPool#commonPool()} that is used for all parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   200
 * computations. Normally, you would initially choose one of these
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   201
 * extreme values, and then measure performance of using in-between
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   202
 * values that trade off overhead versus throughput.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   203
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   204
 * <p>The concurrency properties of bulk operations follow
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   205
 * from those of ConcurrentHashMap: Any non-null result returned
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   206
 * from {@code get(key)} and related access methods bears a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   207
 * happens-before relation with the associated insertion or
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   208
 * update.  The result of any bulk operation reflects the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   209
 * composition of these per-element relations (but is not
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   210
 * necessarily atomic with respect to the map as a whole unless it
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   211
 * is somehow known to be quiescent).  Conversely, because keys
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   212
 * and values in the map are never null, null serves as a reliable
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   213
 * atomic indicator of the current lack of any result.  To
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   214
 * maintain this property, null serves as an implicit basis for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   215
 * all non-scalar reduction operations. For the double, long, and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   216
 * int versions, the basis should be one that, when combined with
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   217
 * any other value, returns that other value (more formally, it
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   218
 * should be the identity element for the reduction). Most common
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   219
 * reductions have these properties; for example, computing a sum
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   220
 * with basis 0 or a minimum with basis MAX_VALUE.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   221
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   222
 * <p>Search and transformation functions provided as arguments
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   223
 * should similarly return null to indicate the lack of any result
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   224
 * (in which case it is not used). In the case of mapped
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   225
 * reductions, this also enables transformations to serve as
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   226
 * filters, returning null (or, in the case of primitive
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   227
 * specializations, the identity basis) if the element should not
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   228
 * be combined. You can create compound transformations and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   229
 * filterings by composing them yourself under this "null means
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   230
 * there is nothing there now" rule before using them in search or
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   231
 * reduce operations.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   232
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   233
 * <p>Methods accepting and/or returning Entry arguments maintain
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   234
 * key-value associations. They may be useful for example when
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   235
 * finding the key for the greatest value. Note that "plain" Entry
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   236
 * arguments can be supplied using {@code new
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   237
 * AbstractMap.SimpleEntry(k,v)}.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   238
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   239
 * <p>Bulk operations may complete abruptly, throwing an
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   240
 * exception encountered in the application of a supplied
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   241
 * function. Bear in mind when handling such exceptions that other
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   242
 * concurrently executing functions could also have thrown
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   243
 * exceptions, or would have done so if the first exception had
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   244
 * not occurred.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   245
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   246
 * <p>Speedups for parallel compared to sequential forms are common
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   247
 * but not guaranteed.  Parallel operations involving brief functions
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   248
 * on small maps may execute more slowly than sequential forms if the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   249
 * underlying work to parallelize the computation is more expensive
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   250
 * than the computation itself.  Similarly, parallelization may not
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   251
 * lead to much actual parallelism if all processors are busy
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   252
 * performing unrelated tasks.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   253
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   254
 * <p>All arguments to all task methods must be non-null.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   255
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 * @param <K> the type of keys maintained by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 * @param <V> the type of mapped values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 */
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   265
public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   266
    implements ConcurrentMap<K,V>, Serializable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    private static final long serialVersionUID = 7249069246763182397L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /*
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   270
     * Overview:
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   271
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   272
     * The primary design goal of this hash table is to maintain
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   273
     * concurrent readability (typically method get(), but also
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   274
     * iterators and related methods) while minimizing update
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   275
     * contention. Secondary goals are to keep space consumption about
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   276
     * the same or better than java.util.HashMap, and to support high
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   277
     * initial insertion rates on an empty table by many threads.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   278
     *
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   279
     * 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
   280
     * 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
   281
     * 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
   282
     * fields. However, various subclasses exist: TreeNodes are
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   283
     * 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
   284
     * 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
   285
     * of bins during resizing. ReservationNodes are used as
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   286
     * placeholders while establishing values in computeIfAbsent and
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   287
     * related methods.  The types TreeBin, ForwardingNode, and
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   288
     * 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
   289
     * hashes, and are readily distinguishable during search etc
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   290
     * 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
   291
     * 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
   292
     * 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
   293
     * insignificant.)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   294
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   295
     * The table is lazily initialized to a power-of-two size upon the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   296
     * first insertion.  Each bin in the table normally contains a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   297
     * list of Nodes (most often, the list has only zero or one Node).
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   298
     * Table accesses require volatile/atomic reads, writes, and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   299
     * CASes.  Because there is no other way to arrange this without
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   300
     * adding further indirections, we use intrinsics
33674
566777f73c32 8140606: Update library code to use internal Unsafe
chegar
parents: 32991
diff changeset
   301
     * (jdk.internal.misc.Unsafe) operations.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   302
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   303
     * We use the top (sign) bit of Node hash fields for control
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   304
     * 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
   305
     * constraints.  Nodes with negative hash fields are specially
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   306
     * handled or ignored in map methods.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   307
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   308
     * Insertion (via put or its variants) of the first node in an
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   309
     * empty bin is performed by just CASing it to the bin.  This is
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   310
     * by far the most common case for put operations under most
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   311
     * key/hash distributions.  Other update operations (insert,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   312
     * delete, and replace) require locks.  We do not want to waste
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   313
     * the space required to associate a distinct lock object with
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   314
     * 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
   315
     * a lock. Locking support for these locks relies on builtin
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   316
     * "synchronized" monitors.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   317
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   318
     * 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
   319
     * suffice though: When a node is locked, any update must first
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   320
     * validate that it is still the first node after locking it, and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   321
     * retry if not. Because new nodes are always appended to lists,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   322
     * once a node is first in a bin, it remains first until deleted
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   323
     * or the bin becomes invalidated (upon resizing).
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   324
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   325
     * The main disadvantage of per-bin locks is that other update
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   326
     * operations on other nodes in a bin list protected by the same
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   327
     * lock can stall, for example when user equals() or mapping
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   328
     * functions take a long time.  However, statistically, under
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   329
     * random hash codes, this is not a common problem.  Ideally, the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   330
     * frequency of nodes in bins follows a Poisson distribution
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   331
     * (http://en.wikipedia.org/wiki/Poisson_distribution) with a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   332
     * parameter of about 0.5 on average, given the resizing threshold
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   333
     * of 0.75, although with a large variance because of resizing
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   334
     * granularity. Ignoring variance, the expected occurrences of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   335
     * 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
   336
     * first values are:
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   337
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   338
     * 0:    0.60653066
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   339
     * 1:    0.30326533
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   340
     * 2:    0.07581633
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   341
     * 3:    0.01263606
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   342
     * 4:    0.00157952
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   343
     * 5:    0.00015795
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   344
     * 6:    0.00001316
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   345
     * 7:    0.00000094
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   346
     * 8:    0.00000006
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   347
     * more: less than 1 in ten million
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   348
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   349
     * Lock contention probability for two threads accessing distinct
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   350
     * elements is roughly 1 / (8 * #elements) under random hashes.
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
   351
     *
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   352
     * Actual hash code distributions encountered in practice
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   353
     * sometimes deviate significantly from uniform randomness.  This
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   354
     * includes the case when N > (1<<30), so some keys MUST collide.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   355
     * 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
   356
     * 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
   357
     * 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
   358
     * 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
   359
     * 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
   360
     * 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
   361
     * 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
   362
     * slow as in a regular list, but given that N cannot exceed
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   363
     * (1<<64) (before running out of addresses) this bounds search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   364
     * steps, lock hold times, etc, to reasonable constants (roughly
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   365
     * 100 nodes inspected per operation worst case) so long as keys
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   366
     * are Comparable (which is very common -- String, Long, etc).
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   367
     * TreeBin nodes (TreeNodes) also maintain the same "next"
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   368
     * traversal pointers as regular nodes, so can be traversed in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   369
     * iterators in the same way.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   370
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   371
     * The table is resized when occupancy exceeds a percentage
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   372
     * threshold (nominally, 0.75, but see below).  Any thread
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   373
     * 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
   374
     * initiating thread allocates and sets up the replacement array.
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   375
     * However, rather than stalling, these other threads may proceed
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   376
     * 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
   377
     * worst case effects of overfilling while resizes are in
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   378
     * 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
   379
     * 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
   380
     * 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
   381
     * 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
   382
     * 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
   383
     * 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
   384
     * 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
   385
     * 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
   386
     * 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
   387
     * 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
   388
     * cloning when a table doubles. The nodes they replace will be
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   389
     * garbage collectable as soon as they are no longer referenced by
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   390
     * 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
   391
     * 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
   392
     * 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
   393
     * 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
   394
     * 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
   395
     * the new table.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   396
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   397
     * Each bin transfer requires its bin lock, which can stall
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   398
     * waiting for locks while resizing. However, because other
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   399
     * threads can join in and help resize rather than contend for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   400
     * locks, average aggregate waits become shorter as resizing
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   401
     * progresses.  The transfer operation must also ensure that all
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   402
     * 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
   403
     * 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
   404
     * 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
   405
     * a forwarding node, traversals (see class Traverser) arrange to
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   406
     * 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
   407
     * 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
   408
     * 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
   409
     * 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
   410
     * later processing the current table. The need for these
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   411
     * save/restore mechanics is relatively rare, but when one
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   412
     * forwarding node is encountered, typically many more will be.
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   413
     * 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
   414
     * many new TableStack nodes. (Thanks to Peter Levart for
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   415
     * suggesting use of a stack here.)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   416
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   417
     * The traversal scheme also applies to partial traversals of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   418
     * ranges of bins (via an alternate Traverser constructor)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   419
     * to support partitioned aggregate operations.  Also, read-only
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   420
     * operations give up if ever forwarded to a null table, which
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   421
     * provides support for shutdown-style clearing, which is also not
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   422
     * currently implemented.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   423
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   424
     * Lazy table initialization minimizes footprint until first use,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   425
     * and also avoids resizings when the first operation is from a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   426
     * putAll, constructor with map argument, or deserialization.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   427
     * These cases attempt to override the initial capacity settings,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   428
     * but harmlessly fail to take effect in cases of races.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   429
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   430
     * The element count is maintained using a specialization of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   431
     * LongAdder. We need to incorporate a specialization rather than
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   432
     * just use a LongAdder in order to access implicit
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   433
     * 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
   434
     * CounterCells.  The counter mechanics avoid contention on
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   435
     * updates but can encounter cache thrashing if read too
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   436
     * frequently during concurrent access. To avoid reading so often,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   437
     * resizing under contention is attempted only upon adding to a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   438
     * bin already holding two or more nodes. Under uniform hash
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   439
     * distributions, the probability of this occurring at threshold
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   440
     * 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
   441
     * threshold (and after resizing, many fewer do so).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   442
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   443
     * 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
   444
     * 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
   445
     * existing collections such as TreeMaps). TreeBins contain
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   446
     * Comparable elements, but may contain others, as well as
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   447
     * elements that are Comparable but not necessarily Comparable for
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   448
     * the same T, so we cannot invoke compareTo among them. To handle
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   449
     * this, the tree is ordered primarily by hash value, then by
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   450
     * Comparable.compareTo order if applicable.  On lookup at a node,
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   451
     * if elements are not comparable or compare as 0 then both left
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   452
     * 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
   453
     * hash values. (This corresponds to the full list search that
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   454
     * would be necessary if all elements were non-Comparable and had
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   455
     * tied hashes.) On insertion, to keep a total ordering (or as
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   456
     * close as is required here) across rebalancings, we compare
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   457
     * classes and identityHashCodes as tie-breakers. The red-black
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   458
     * 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
   459
     * (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
   460
     * 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
   461
     * Algorithms" (CLR).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   462
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   463
     * TreeBins also require an additional locking mechanism.  While
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   464
     * 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
   465
     * 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
   466
     * 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
   467
     * 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
   468
     * main bin-synchronization strategy: Structural adjustments
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   469
     * 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
   470
     * (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
   471
     * 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
   472
     * 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
   473
     * 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
   474
     * 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
   475
     * 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
   476
     * 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
   477
     * maximize aggregate expected throughput.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   478
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   479
     * Maintaining API and serialization compatibility with previous
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   480
     * versions of this class introduces several oddities. Mainly: We
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 30441
diff changeset
   481
     * leave untouched but unused constructor arguments referring to
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   482
     * concurrencyLevel. We accept a loadFactor constructor argument,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   483
     * but apply it only to initial table capacity (which is the only
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   484
     * time that we can guarantee to honor it.) We also declare an
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   485
     * unused "Segment" class that is instantiated in minimal form
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   486
     * only when serializing.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   487
     *
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   488
     * Also, solely for compatibility with previous versions of this
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   489
     * class, it extends AbstractMap, even though all of its methods
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   490
     * are overridden, so it is just useless baggage.
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   491
     *
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   492
     * 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
   493
     * 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
   494
     * declarations and utilities, then fields, then main public
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   495
     * 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
   496
     * internal ones), then sizing methods, trees, traversers, and
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   497
     * bulk operations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    /* ---------------- Constants -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   503
     * The largest possible table capacity.  This value must be
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   504
     * exactly 1<<30 to stay within Java array allocation and indexing
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   505
     * bounds for power of two table sizes, and is further required
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   506
     * because the top two bits of 32bit hash fields are used for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   507
     * control purposes.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   509
    private static final int MAXIMUM_CAPACITY = 1 << 30;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   510
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   511
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   512
     * The default initial table capacity.  Must be a power of 2
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   513
     * (i.e., at least 1) and at most MAXIMUM_CAPACITY.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   514
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   515
    private static final int DEFAULT_CAPACITY = 16;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   518
     * The largest possible (non-power of two) array size.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   519
     * Needed by toArray and related methods.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   521
    static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   524
     * The default concurrency level for this table. Unused but
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   525
     * defined for compatibility with previous versions of this class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   527
    private static final int DEFAULT_CONCURRENCY_LEVEL = 16;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   528
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   529
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   530
     * The load factor for this table. Overrides of this value in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   531
     * constructors affect only the initial table capacity.  The
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   532
     * actual floating point value isn't normally used -- it is
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   533
     * simpler to use expressions such as {@code n - (n >>> 2)} for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   534
     * the associated resizing threshold.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   535
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   536
    private static final float LOAD_FACTOR = 0.75f;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   539
     * 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
   540
     * 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
   541
     * 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
   542
     * 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
   543
     * 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
   544
     * shrinkage.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   546
    static final int TREEIFY_THRESHOLD = 8;
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
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   549
     * 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
   550
     * 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
   551
     * 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
   552
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   553
    static final int UNTREEIFY_THRESHOLD = 6;
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
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   556
     * 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
   557
     * (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
   558
     * 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
   559
     * conflicts between resizing and treeification thresholds.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   560
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   561
    static final int MIN_TREEIFY_CAPACITY = 64;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   564
     * Minimum number of rebinnings per transfer step. Ranges are
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   565
     * subdivided to allow multiple resizer threads.  This value
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   566
     * serves as a lower bound to avoid resizers encountering
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   567
     * excessive memory contention.  The value should be at least
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   568
     * DEFAULT_CAPACITY.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   569
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   570
    private static final int MIN_TRANSFER_STRIDE = 16;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   571
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   572
    /**
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   573
     * 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
   574
     * 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
   575
     */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   576
    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
   577
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   578
    /**
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   579
     * 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
   580
     * 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
   581
     */
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   582
    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
   583
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   584
    /**
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   585
     * 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
   586
     */
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   587
    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
   588
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   589
    /*
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   590
     * Encodings for Node hash fields. See above for explanation.
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
   591
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   592
    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
   593
    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
   594
    static final int RESERVED  = -3; // hash for transient reservations
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   595
    static final int HASH_BITS = 0x7fffffff; // usable bits of normal node hash
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   596
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   597
    /** Number of CPUS, to place bounds on some sizings */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   598
    static final int NCPU = Runtime.getRuntime().availableProcessors();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   599
34340
3b7a5a01c627 8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents: 33674
diff changeset
   600
    /**
3b7a5a01c627 8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents: 33674
diff changeset
   601
     * Serialized pseudo-fields, provided only for jdk7 compatibility.
3b7a5a01c627 8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents: 33674
diff changeset
   602
     * @serialField segments Segment[]
3b7a5a01c627 8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents: 33674
diff changeset
   603
     *   The segments, each of which is a specialized hash table.
3b7a5a01c627 8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents: 33674
diff changeset
   604
     * @serialField segmentMask int
3b7a5a01c627 8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents: 33674
diff changeset
   605
     *   Mask value for indexing into segments. The upper bits of a
3b7a5a01c627 8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents: 33674
diff changeset
   606
     *   key's hash code are used to choose the segment.
3b7a5a01c627 8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents: 33674
diff changeset
   607
     * @serialField segmentShift int
3b7a5a01c627 8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents: 33674
diff changeset
   608
     *   Shift value for indexing within segments.
3b7a5a01c627 8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents: 33674
diff changeset
   609
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   610
    private static final ObjectStreamField[] serialPersistentFields = {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   611
        new ObjectStreamField("segments", Segment[].class),
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   612
        new ObjectStreamField("segmentMask", Integer.TYPE),
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   613
        new ObjectStreamField("segmentShift", Integer.TYPE),
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   614
    };
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
   615
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   616
    /* ---------------- Nodes -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   617
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
   618
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   619
     * 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
   620
     * 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
   621
     * 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
   622
     * 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
   623
     * 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
   624
     * exported).  Otherwise, keys and vals are never null.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   625
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   626
    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
   627
        final int hash;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   628
        final K key;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   629
        volatile V val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   630
        volatile Node<K,V> next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   631
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
   632
        Node(int hash, K key, V val) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   633
            this.hash = hash;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   634
            this.key = key;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   635
            this.val = val;
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
   636
        }
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
   637
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
   638
        Node(int hash, K key, V val, Node<K,V> next) {
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
   639
            this(hash, key, val);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   640
            this.next = next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   641
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   642
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   643
        public final K getKey()     { return key; }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   644
        public final V getValue()   { return val; }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   645
        public final int hashCode() { return key.hashCode() ^ val.hashCode(); }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   646
        public final String toString() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   647
            return Helpers.mapEntryToString(key, val);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   648
        }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   649
        public final V setValue(V value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   650
            throw new UnsupportedOperationException();
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
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   653
        public final boolean equals(Object o) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   654
            Object k, v, u; Map.Entry<?,?> e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   655
            return ((o instanceof Map.Entry) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   656
                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   657
                    (v = e.getValue()) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   658
                    (k == key || k.equals(key)) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   659
                    (v == (u = val) || v.equals(u)));
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
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   663
         * Virtualized support for map.get(); overridden in subclasses.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   664
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   665
        Node<K,V> find(int h, Object k) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   666
            Node<K,V> e = this;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   667
            if (k != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   668
                do {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   669
                    K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   670
                    if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   671
                        ((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
   672
                        return e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   673
                } while ((e = e.next) != null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   674
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   675
            return null;
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
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   679
    /* ---------------- Static utilities -------------- */
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
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   682
     * 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
   683
     * 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
   684
     * 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
   685
     * 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
   686
     * 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
   687
     * 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
   688
     * 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
   689
     * 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
   690
     * 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
   691
     * 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
   692
     * 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
   693
     * 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
   694
     * 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
   695
     * never be used in index calculations because of table bounds.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   697
    static final int spread(int h) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   698
        return (h ^ (h >>> 16)) & HASH_BITS;
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
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   702
     * 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
   703
     * See Hackers Delight, sec 3.2
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   704
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   705
    private static final int tableSizeFor(int c) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   706
        int n = c - 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   707
        n |= n >>> 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   708
        n |= n >>> 2;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   709
        n |= n >>> 4;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   710
        n |= n >>> 8;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   711
        n |= n >>> 16;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   712
        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
   713
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   714
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   715
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   716
     * 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
   717
     * Comparable<C>", else null.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   718
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   719
    static Class<?> comparableClassFor(Object x) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   720
        if (x instanceof Comparable) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   721
            Class<?> c; Type[] ts, as; Type t; ParameterizedType p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   722
            if ((c = x.getClass()) == String.class) // bypass checks
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   723
                return c;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   724
            if ((ts = c.getGenericInterfaces()) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   725
                for (int i = 0; i < ts.length; ++i) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   726
                    if (((t = ts[i]) instanceof ParameterizedType) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   727
                        ((p = (ParameterizedType)t).getRawType() ==
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   728
                         Comparable.class) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   729
                        (as = p.getActualTypeArguments()) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   730
                        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
   731
                        return c;
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
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   734
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   735
        return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   736
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   737
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   738
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   739
     * 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
   740
     * class), else 0.
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
    @SuppressWarnings({"rawtypes","unchecked"}) // for cast to Comparable
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   743
    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
   744
        return (x == null || x.getClass() != kc ? 0 :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   745
                ((Comparable)k).compareTo(x));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   746
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   747
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   748
    /* ---------------- Table element access -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   749
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   750
    /*
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
   751
     * 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
   752
     * 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
   753
     * 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
   754
     * 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
   755
     * 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
   756
     * 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
   757
     * 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
   758
     * 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
   759
     * 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
   760
     * 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
   761
     * and so require only release ordering.
18802
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
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   764
    @SuppressWarnings("unchecked")
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   765
    static final <K,V> Node<K,V> tabAt(Node<K,V>[] tab, int i) {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
   766
        return (Node<K,V>)U.getObjectAcquire(tab, ((long)i << ASHIFT) + ABASE);
18802
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
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   769
    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
   770
                                        Node<K,V> c, Node<K,V> v) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   771
        return U.compareAndSwapObject(tab, ((long)i << ASHIFT) + ABASE, c, v);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   772
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   773
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   774
    static final <K,V> void setTabAt(Node<K,V>[] tab, int i, Node<K,V> v) {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
   775
        U.putObjectRelease(tab, ((long)i << ASHIFT) + ABASE, v);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   776
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    /* ---------------- Fields -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   781
     * The array of bins. Lazily initialized upon first insertion.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   782
     * Size is always a power of two. Accessed directly by iterators.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   783
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   784
    transient volatile Node<K,V>[] table;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   785
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   786
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   787
     * The next table to use; non-null only while resizing.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   788
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   789
    private transient volatile Node<K,V>[] nextTable;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   790
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   791
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   792
     * Base counter value, used mainly when there is no contention,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   793
     * but also as a fallback during table initialization
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   794
     * races. Updated via CAS.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   795
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   796
    private transient volatile long baseCount;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   797
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   798
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   799
     * Table initialization and resizing control.  When negative, the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   800
     * table is being initialized or resized: -1 for initialization,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   801
     * else -(1 + the number of active resizing threads).  Otherwise,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   802
     * when table is null, holds the initial table size to use upon
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   803
     * creation, or 0 for default. After initialization, holds the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   804
     * next element count value upon which to resize the table.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   805
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   806
    private transient volatile int sizeCtl;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   807
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   808
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   809
     * The next table index (plus one) to split while resizing.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   810
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   811
    private transient volatile int transferIndex;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   812
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   813
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   814
     * Spinlock (locked via CAS) used when resizing and/or creating CounterCells.
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 11279
diff changeset
   815
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   816
    private transient volatile int cellsBusy;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   817
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   818
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   819
     * Table of counter cells. When non-null, size is a power of 2.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   820
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   821
    private transient volatile CounterCell[] counterCells;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   822
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   823
    // views
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   824
    private transient KeySetView<K,V> keySet;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   825
    private transient ValuesView<K,V> values;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   826
    private transient EntrySetView<K,V> entrySet;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   827
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   828
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   829
    /* ---------------- Public operations -------------- */
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 the default initial table size (16).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   833
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   834
    public ConcurrentHashMap() {
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
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   837
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   838
     * 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
   839
     * 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
   840
     * to dynamically resize.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   841
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   842
     * @param initialCapacity The implementation performs internal
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   843
     * sizing to accommodate this many elements.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   844
     * @throws IllegalArgumentException if the initial capacity of
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   845
     * elements is negative
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   846
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   847
    public ConcurrentHashMap(int initialCapacity) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   848
        if (initialCapacity < 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   849
            throw new IllegalArgumentException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   850
        int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   851
                   MAXIMUM_CAPACITY :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   852
                   tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   853
        this.sizeCtl = cap;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   854
    }
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
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   857
     * 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
   858
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   859
     * @param m the map
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   860
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   861
    public ConcurrentHashMap(Map<? extends K, ? extends V> m) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   862
        this.sizeCtl = DEFAULT_CAPACITY;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   863
        putAll(m);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   864
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   865
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   866
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   867
     * 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
   868
     * the given number of elements ({@code initialCapacity}) and
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   869
     * initial table density ({@code loadFactor}).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   870
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   871
     * @param initialCapacity the initial capacity. The implementation
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   872
     * performs internal sizing to accommodate this many elements,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   873
     * given the specified load factor.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   874
     * @param loadFactor the load factor (table density) for
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   875
     * establishing the initial table size
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   876
     * @throws IllegalArgumentException if the initial capacity of
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   877
     * 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
   878
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   879
     * @since 1.6
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   880
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   881
    public ConcurrentHashMap(int initialCapacity, float loadFactor) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   882
        this(initialCapacity, loadFactor, 1);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   883
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   884
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 11279
diff changeset
   885
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   886
     * 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
   887
     * the given number of elements ({@code initialCapacity}), table
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   888
     * density ({@code loadFactor}), and number of concurrently
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   889
     * updating threads ({@code concurrencyLevel}).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   890
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   891
     * @param initialCapacity the initial capacity. The implementation
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   892
     * performs internal sizing to accommodate this many elements,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   893
     * given the specified load factor.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   894
     * @param loadFactor the load factor (table density) for
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   895
     * establishing the initial table size
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   896
     * @param concurrencyLevel the estimated number of concurrently
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   897
     * 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
   898
     * a sizing hint.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   899
     * @throws IllegalArgumentException if the initial capacity is
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   900
     * negative or the load factor or concurrencyLevel are
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   901
     * nonpositive
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
    public ConcurrentHashMap(int initialCapacity,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   904
                             float loadFactor, int concurrencyLevel) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   905
        if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   906
            throw new IllegalArgumentException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   907
        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
   908
            initialCapacity = concurrencyLevel;   // as estimated threads
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   909
        long size = (long)(1.0 + (long)initialCapacity / loadFactor);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   910
        int cap = (size >= (long)MAXIMUM_CAPACITY) ?
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   911
            MAXIMUM_CAPACITY : tableSizeFor((int)size);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   912
        this.sizeCtl = cap;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   913
    }
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
    // Original (since JDK1.2) Map methods
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
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   918
     * {@inheritDoc}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   919
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   920
    public int size() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   921
        long n = sumCount();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   922
        return ((n < 0L) ? 0 :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   923
                (n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   924
                (int)n);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   925
    }
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
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   928
     * {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   930
    public boolean isEmpty() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   931
        return sumCount() <= 0L; // ignore transient negative values
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   932
    }
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
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   935
     * 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
   936
     * 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
   937
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   938
     * <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
   939
     * {@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
   940
     * 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
   941
     * {@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
   942
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   943
     * @throws NullPointerException if the specified key is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   944
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   945
    public V get(Object key) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   946
        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
   947
        int h = spread(key.hashCode());
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   948
        if ((tab = table) != null && (n = tab.length) > 0 &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   949
            (e = tabAt(tab, (n - 1) & h)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   950
            if ((eh = e.hash) == h) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   951
                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
   952
                    return e.val;
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
            else if (eh < 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   955
                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
   956
            while ((e = e.next) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   957
                if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   958
                    ((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
   959
                    return e.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   960
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   961
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   962
        return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   963
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   964
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
     * 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
   967
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   968
     * @param  key possible key
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   969
     * @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
   970
     *         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
   971
     *         {@code equals} method; {@code false} otherwise
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   972
     * @throws NullPointerException if the specified key is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   973
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   974
    public boolean containsKey(Object key) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   975
        return get(key) != 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
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   978
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   979
     * 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
   980
     * 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
   981
     * 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
   982
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   983
     * @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
   984
     * @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
   985
     *         specified value
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   986
     * @throws NullPointerException if the specified value is null
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
    public boolean containsValue(Object value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   989
        if (value == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   990
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   991
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   992
        if ((t = table) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   993
            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
   994
            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
   995
                V v;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   996
                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
   997
                    return true;
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
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1000
        return false;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1001
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1004
     * 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
  1005
     * 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
  1006
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1007
     * <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
  1008
     * 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
  1009
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1010
     * @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
  1011
     * @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
  1012
     * @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
  1013
     *         {@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
  1014
     * @throws NullPointerException if the specified key or value is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1016
    public V put(K key, V value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1017
        return putVal(key, value, false);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1018
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1019
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1020
    /** Implementation for put and putIfAbsent */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1021
    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
  1022
        if (key == null || value == null) throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1023
        int hash = spread(key.hashCode());
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1024
        int binCount = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1025
        for (Node<K,V>[] tab = table;;) {
39779
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  1026
            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
  1027
            if (tab == null || (n = tab.length) == 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1028
                tab = initTable();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1029
            else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  1030
                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
  1031
                    break;                   // no lock when adding to empty bin
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1032
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1033
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1034
                tab = helpTransfer(tab, f);
44588
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  1035
            else if (onlyIfAbsent // check first node without acquiring lock
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  1036
                     && fh == hash
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  1037
                     && ((fk = f.key) == key || (fk != null && key.equals(fk)))
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  1038
                     && (fv = f.val) != null)
39779
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  1039
                return fv;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1040
            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1041
                V oldVal = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1042
                synchronized (f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1043
                    if (tabAt(tab, i) == f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1044
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1045
                            binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1046
                            for (Node<K,V> e = f;; ++binCount) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1047
                                K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1048
                                if (e.hash == hash &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1049
                                    ((ek = e.key) == key ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1050
                                     (ek != null && key.equals(ek)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1051
                                    oldVal = e.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1052
                                    if (!onlyIfAbsent)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1053
                                        e.val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1054
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1055
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1056
                                Node<K,V> pred = e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1057
                                if ((e = e.next) == null) {
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  1058
                                    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
  1059
                                    break;
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
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1062
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1063
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1064
                            Node<K,V> p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1065
                            binCount = 2;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1066
                            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
  1067
                                                           value)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1068
                                oldVal = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1069
                                if (!onlyIfAbsent)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1070
                                    p.val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1071
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1072
                        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1073
                        else if (f instanceof ReservationNode)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1074
                            throw new IllegalStateException("Recursive update");
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1075
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1076
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1077
                if (binCount != 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1078
                    if (binCount >= TREEIFY_THRESHOLD)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1079
                        treeifyBin(tab, i);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1080
                    if (oldVal != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1081
                        return oldVal;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1082
                    break;
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
            }
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
        addCount(1L, binCount);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1087
        return null;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1088
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1089
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1090
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1091
     * 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
  1092
     * 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
  1093
     * keys currently in the specified map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1094
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1095
     * @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
  1096
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1097
    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
  1098
        tryPresize(m.size());
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1099
        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
  1100
            putVal(e.getKey(), e.getValue(), false);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1101
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1102
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
     * 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
  1105
     * 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
  1106
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1107
     * @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
  1108
     * @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
  1109
     *         {@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
  1110
     * @throws NullPointerException if the specified key is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1111
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1112
    public V remove(Object key) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1113
        return replaceNode(key, null, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1114
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1115
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1116
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1117
     * Implementation for the four public remove/replace methods:
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1118
     * 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
  1119
     * non-null.  If resulting value is null, delete.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1120
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1121
    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
  1122
        int hash = spread(key.hashCode());
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1123
        for (Node<K,V>[] tab = table;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1124
            Node<K,V> f; int n, i, fh;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1125
            if (tab == null || (n = tab.length) == 0 ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1126
                (f = tabAt(tab, i = (n - 1) & hash)) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1127
                break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1128
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1129
                tab = helpTransfer(tab, f);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1130
            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1131
                V oldVal = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1132
                boolean validated = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1133
                synchronized (f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1134
                    if (tabAt(tab, i) == f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1135
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1136
                            validated = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1137
                            for (Node<K,V> e = f, pred = null;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1138
                                K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1139
                                if (e.hash == hash &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1140
                                    ((ek = e.key) == key ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1141
                                     (ek != null && key.equals(ek)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1142
                                    V ev = e.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1143
                                    if (cv == null || cv == ev ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1144
                                        (ev != null && cv.equals(ev))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1145
                                        oldVal = ev;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1146
                                        if (value != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1147
                                            e.val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1148
                                        else if (pred != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1149
                                            pred.next = e.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1150
                                        else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1151
                                            setTabAt(tab, i, e.next);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1152
                                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1153
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1154
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1155
                                pred = e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1156
                                if ((e = e.next) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1157
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1158
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1159
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1160
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1161
                            validated = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1162
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1163
                            TreeNode<K,V> r, p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1164
                            if ((r = t.root) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1165
                                (p = r.findTreeNode(hash, key, null)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1166
                                V pv = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1167
                                if (cv == null || cv == pv ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1168
                                    (pv != null && cv.equals(pv))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1169
                                    oldVal = pv;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1170
                                    if (value != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1171
                                        p.val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1172
                                    else if (t.removeTreeNode(p))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1173
                                        setTabAt(tab, i, untreeify(t.first));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1174
                                }
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
                        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1177
                        else if (f instanceof ReservationNode)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1178
                            throw new IllegalStateException("Recursive update");
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1179
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1180
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1181
                if (validated) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1182
                    if (oldVal != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1183
                        if (value == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1184
                            addCount(-1L, -1);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1185
                        return oldVal;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1186
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1187
                    break;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1188
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1189
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1190
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1191
        return null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1192
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1195
     * Removes all of the mappings from this map.
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  1196
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1197
    public void clear() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1198
        long delta = 0L; // negative number of deletions
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1199
        int i = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1200
        Node<K,V>[] tab = table;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1201
        while (tab != null && i < tab.length) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1202
            int fh;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1203
            Node<K,V> f = tabAt(tab, i);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1204
            if (f == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1205
                ++i;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1206
            else if ((fh = f.hash) == MOVED) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1207
                tab = helpTransfer(tab, f);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1208
                i = 0; // restart
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1209
            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1210
            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1211
                synchronized (f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1212
                    if (tabAt(tab, i) == f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1213
                        Node<K,V> p = (fh >= 0 ? f :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1214
                                       (f instanceof TreeBin) ?
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1215
                                       ((TreeBin<K,V>)f).first : null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1216
                        while (p != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1217
                            --delta;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1218
                            p = p.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1219
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1220
                        setTabAt(tab, i++, null);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1221
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1222
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1223
            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1224
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1225
        if (delta != 0L)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1226
            addCount(delta, -1);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1227
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1228
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1229
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1230
     * 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
  1231
     * 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
  1232
     * 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
  1233
     * 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
  1234
     * via the {@code Iterator.remove}, {@code Set.remove},
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1235
     * {@code removeAll}, {@code retainAll}, and {@code clear}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1236
     * 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
  1237
     * {@code addAll} operations.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1238
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1239
     * <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
  1240
     * <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
  1241
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1242
     * <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
  1243
     * {@link Spliterator#DISTINCT}, and {@link Spliterator#NONNULL}.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1244
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1245
     * @return the set view
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1246
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1247
    public KeySetView<K,V> keySet() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1248
        KeySetView<K,V> ks;
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36958
diff changeset
  1249
        if ((ks = keySet) != null) return ks;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36958
diff changeset
  1250
        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
  1251
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1252
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1253
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1254
     * 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
  1255
     * 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
  1256
     * 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
  1257
     * supports element removal, which removes the corresponding
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1258
     * 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
  1259
     * {@code Collection.remove}, {@code removeAll},
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1260
     * {@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
  1261
     * support the {@code add} or {@code addAll} operations.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1262
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1263
     * <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
  1264
     * <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
  1265
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1266
     * <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
  1267
     * and {@link Spliterator#NONNULL}.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1268
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1269
     * @return the collection view
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1270
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1271
    public Collection<V> values() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1272
        ValuesView<K,V> vs;
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36958
diff changeset
  1273
        if ((vs = values) != null) return vs;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36958
diff changeset
  1274
        return values = new ValuesView<K,V>(this);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1275
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1276
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1277
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1278
     * 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
  1279
     * 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
  1280
     * 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
  1281
     * 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
  1282
     * via the {@code Iterator.remove}, {@code Set.remove},
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1283
     * {@code removeAll}, {@code retainAll}, and {@code clear}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1284
     * operations.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1285
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1286
     * <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
  1287
     * <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
  1288
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1289
     * <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
  1290
     * {@link Spliterator#DISTINCT}, and {@link Spliterator#NONNULL}.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1291
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1292
     * @return the set view
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
    public Set<Map.Entry<K,V>> entrySet() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1295
        EntrySetView<K,V> es;
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36958
diff changeset
  1296
        if ((es = entrySet) != null) return es;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36958
diff changeset
  1297
        return entrySet = new EntrySetView<K,V>(this);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1298
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1299
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1300
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1301
     * 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
  1302
     * 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
  1303
     * {@code key.hashCode() ^ value.hashCode()}.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1304
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1305
     * @return the hash code value for this map
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
    public int hashCode() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1308
        int h = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1309
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1310
        if ((t = table) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1311
            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
  1312
            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
  1313
                h += p.key.hashCode() ^ p.val.hashCode();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1314
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1315
        return h;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1316
    }
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
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1319
     * 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
  1320
     * 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
  1321
     * particular order) enclosed in braces ("{@code {}}").  Adjacent
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1322
     * mappings are separated by the characters {@code ", "} (comma
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1323
     * 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
  1324
     * 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
  1325
     * associated value.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1326
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1327
     * @return a string representation of this map
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1328
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1329
    public String toString() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1330
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1331
        int f = (t = table) == null ? 0 : t.length;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1332
        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
  1333
        StringBuilder sb = new StringBuilder();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1334
        sb.append('{');
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1335
        Node<K,V> p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1336
        if ((p = it.advance()) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1337
            for (;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1338
                K k = p.key;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1339
                V v = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1340
                sb.append(k == this ? "(this Map)" : k);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1341
                sb.append('=');
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1342
                sb.append(v == this ? "(this Map)" : v);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1343
                if ((p = it.advance()) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1344
                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1345
                sb.append(',').append(' ');
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
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1348
        return sb.append('}').toString();
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
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1351
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1352
     * 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
  1353
     * 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
  1354
     * 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
  1355
     * 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
  1356
     * of this method.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1357
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1358
     * @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
  1359
     * @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
  1360
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1361
    public boolean equals(Object o) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1362
        if (o != this) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1363
            if (!(o instanceof Map))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1364
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1365
            Map<?,?> m = (Map<?,?>) o;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1366
            Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1367
            int f = (t = table) == null ? 0 : t.length;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1368
            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
  1369
            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
  1370
                V val = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1371
                Object v = m.get(p.key);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1372
                if (v == null || (v != val && !v.equals(val)))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1373
                    return false;
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
            for (Map.Entry<?,?> e : m.entrySet()) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1376
                Object mk, mv, v;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1377
                if ((mk = e.getKey()) == null ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1378
                    (mv = e.getValue()) == null ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1379
                    (v = get(mk)) == null ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1380
                    (mv != v && !mv.equals(v)))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1381
                    return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1382
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1383
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1384
        return true;
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
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1387
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1388
     * 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
  1389
     * declared for the sake of serialization compatibility.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1390
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1391
    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
  1392
        private static final long serialVersionUID = 2249069246763182397L;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1393
        final float loadFactor;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1394
        Segment(float lf) { this.loadFactor = lf; }
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
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1397
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1398
     * Saves the state of the {@code ConcurrentHashMap} instance to a
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1399
     * stream (i.e., serializes it).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1400
     * @param s the stream
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  1401
     * @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
  1402
     * @serialData
34340
3b7a5a01c627 8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents: 33674
diff changeset
  1403
     * the serialized fields, followed by the key (Object) and value
3b7a5a01c627 8143073: Improve documentation for ConcurrentHashMap serialized form
dl
parents: 33674
diff changeset
  1404
     * (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
  1405
     * 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
  1406
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1407
    private void writeObject(java.io.ObjectOutputStream s)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1408
        throws java.io.IOException {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1409
        // For serialization compatibility
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1410
        // 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
  1411
        int sshift = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1412
        int ssize = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1413
        while (ssize < DEFAULT_CONCURRENCY_LEVEL) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1414
            ++sshift;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1415
            ssize <<= 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1416
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1417
        int segmentShift = 32 - sshift;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1418
        int segmentMask = ssize - 1;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1419
        @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1420
        Segment<K,V>[] segments = (Segment<K,V>[])
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1421
            new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1422
        for (int i = 0; i < segments.length; ++i)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1423
            segments[i] = new Segment<K,V>(LOAD_FACTOR);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1424
        java.io.ObjectOutputStream.PutField streamFields = s.putFields();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1425
        streamFields.put("segments", segments);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1426
        streamFields.put("segmentShift", segmentShift);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1427
        streamFields.put("segmentMask", segmentMask);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1428
        s.writeFields();
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
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1431
        if ((t = table) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1432
            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
  1433
            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
  1434
                s.writeObject(p.key);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1435
                s.writeObject(p.val);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1436
            }
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
        s.writeObject(null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1439
        s.writeObject(null);
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
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1442
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1443
     * Reconstitutes the instance from a stream (that is, deserializes it).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1444
     * @param s the stream
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  1445
     * @throws ClassNotFoundException if the class of a serialized object
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  1446
     *         could not be found
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  1447
     * @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
  1448
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1449
    private void readObject(java.io.ObjectInputStream s)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1450
        throws java.io.IOException, ClassNotFoundException {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1451
        /*
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1452
         * 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
  1453
         * 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
  1454
         * 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
  1455
         * overpopulated bins while doing so, which requires
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1456
         * specialized versions of putVal mechanics.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1457
         */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1458
        sizeCtl = -1; // force exclusion for table construction
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1459
        s.defaultReadObject();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1460
        long size = 0L;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1461
        Node<K,V> p = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1462
        for (;;) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1463
            @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1464
            K k = (K) s.readObject();
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1465
            @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1466
            V v = (V) s.readObject();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1467
            if (k != null && v != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1468
                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
  1469
                ++size;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1470
            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1471
            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1472
                break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1473
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1474
        if (size == 0L)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1475
            sizeCtl = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1476
        else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1477
            int n;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1478
            if (size >= (long)(MAXIMUM_CAPACITY >>> 1))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1479
                n = MAXIMUM_CAPACITY;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1480
            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1481
                int sz = (int)size;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1482
                n = tableSizeFor(sz + (sz >>> 1) + 1);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1483
            }
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1484
            @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1485
            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
  1486
            int mask = n - 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1487
            long added = 0L;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1488
            while (p != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1489
                boolean insertAtFront;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1490
                Node<K,V> next = p.next, first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1491
                int h = p.hash, j = h & mask;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1492
                if ((first = tabAt(tab, j)) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1493
                    insertAtFront = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1494
                else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1495
                    K k = p.key;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1496
                    if (first.hash < 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1497
                        TreeBin<K,V> t = (TreeBin<K,V>)first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1498
                        if (t.putTreeVal(h, k, p.val) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1499
                            ++added;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1500
                        insertAtFront = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1501
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1502
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1503
                        int binCount = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1504
                        insertAtFront = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1505
                        Node<K,V> q; K qk;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1506
                        for (q = first; q != null; q = q.next) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1507
                            if (q.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1508
                                ((qk = q.key) == k ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1509
                                 (qk != null && k.equals(qk)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1510
                                insertAtFront = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1511
                                break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1512
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1513
                            ++binCount;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1514
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1515
                        if (insertAtFront && binCount >= TREEIFY_THRESHOLD) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1516
                            insertAtFront = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1517
                            ++added;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1518
                            p.next = first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1519
                            TreeNode<K,V> hd = null, tl = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1520
                            for (q = p; q != null; q = q.next) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1521
                                TreeNode<K,V> t = new TreeNode<K,V>
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1522
                                    (q.hash, q.key, q.val, null, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1523
                                if ((t.prev = tl) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1524
                                    hd = t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1525
                                else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1526
                                    tl.next = t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1527
                                tl = t;
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
                            setTabAt(tab, j, new TreeBin<K,V>(hd));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1530
                        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1531
                    }
18802
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
                if (insertAtFront) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1534
                    ++added;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1535
                    p.next = first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1536
                    setTabAt(tab, j, p);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1537
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1538
                p = next;
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
            table = tab;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1541
            sizeCtl = n - (n >>> 2);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1542
            baseCount = added;
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
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1546
    // ConcurrentMap methods
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1547
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
     * {@inheritDoc}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1550
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1551
     * @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
  1552
     *         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
  1553
     * @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
  1554
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1555
    public V putIfAbsent(K key, V value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1556
        return putVal(key, value, true);
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
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
     * {@inheritDoc}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1561
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1562
     * @throws NullPointerException if the specified key is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1563
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1564
    public boolean remove(Object key, Object value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1565
        if (key == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1566
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1567
        return value != null && replaceNode(key, null, value) != null;
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
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1570
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1571
     * {@inheritDoc}
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
     * @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
  1574
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1575
    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
  1576
        if (key == null || oldValue == null || newValue == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1577
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1578
        return replaceNode(key, newValue, oldValue) != null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1579
    }
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
     * {@inheritDoc}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1583
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1584
     * @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
  1585
     *         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
  1586
     * @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
  1587
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1588
    public V replace(K key, V value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1589
        if (key == null || value == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1590
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1591
        return replaceNode(key, value, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1592
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1593
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1594
    // Overrides of JDK8+ Map extension method defaults
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
     * 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
  1598
     * 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
  1599
     * key.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1600
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1601
     * @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
  1602
     * @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
  1603
     * no mapping for the given key
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1604
     * @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
  1605
     * @throws NullPointerException if the specified key is null
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
    public V getOrDefault(Object key, V defaultValue) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1608
        V v;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1609
        return (v = get(key)) == null ? defaultValue : v;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1610
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1611
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1612
    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
  1613
        if (action == null) throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1614
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1615
        if ((t = table) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1616
            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
  1617
            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
  1618
                action.accept(p.key, p.val);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1619
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1620
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1621
    }
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
    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
  1624
        if (function == null) throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1625
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1626
        if ((t = table) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1627
            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
  1628
            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
  1629
                V oldValue = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1630
                for (K key = p.key;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1631
                    V newValue = function.apply(key, oldValue);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1632
                    if (newValue == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1633
                        throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1634
                    if (replaceNode(key, newValue, oldValue) != null ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1635
                        (oldValue = get(key)) == null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1636
                        break;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1637
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1638
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1639
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1640
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1641
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1642
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1643
     * Helper method for EntrySetView.removeIf.
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1644
     */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1645
    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
  1646
        if (function == null) throw new NullPointerException();
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1647
        Node<K,V>[] t;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1648
        boolean removed = false;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1649
        if ((t = table) != null) {
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1650
            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
  1651
            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
  1652
                K k = p.key;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1653
                V v = p.val;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1654
                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
  1655
                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
  1656
                    removed = true;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1657
            }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1658
        }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1659
        return removed;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1660
    }
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
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1663
     * Helper method for ValuesView.removeIf.
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1664
     */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1665
    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
  1666
        if (function == null) throw new NullPointerException();
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1667
        Node<K,V>[] t;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1668
        boolean removed = false;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1669
        if ((t = table) != null) {
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1670
            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
  1671
            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
  1672
                K k = p.key;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1673
                V v = p.val;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1674
                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
  1675
                    removed = true;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1676
            }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1677
        }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1678
        return removed;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1679
    }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1680
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  1681
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1682
     * 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
  1683
     * 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
  1684
     * 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
  1685
     * 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
  1686
     * 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
  1687
     * 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
  1688
     * 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
  1689
     * 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
  1690
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1691
     * @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
  1692
     * @param mappingFunction the function to compute a value
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1693
     * @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
  1694
     *         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
  1695
     * @throws NullPointerException if the specified key or mappingFunction
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1696
     *         is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1697
     * @throws IllegalStateException if the computation detectably
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1698
     *         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
  1699
     *         otherwise never complete
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1700
     * @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
  1701
     *         in which case the mapping is left unestablished
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1702
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1703
    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
  1704
        if (key == null || mappingFunction == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1705
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1706
        int h = spread(key.hashCode());
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1707
        V val = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1708
        int binCount = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1709
        for (Node<K,V>[] tab = table;;) {
39779
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  1710
            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
  1711
            if (tab == null || (n = tab.length) == 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1712
                tab = initTable();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1713
            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
  1714
                Node<K,V> r = new ReservationNode<K,V>();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1715
                synchronized (r) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1716
                    if (casTabAt(tab, i, null, r)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1717
                        binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1718
                        Node<K,V> node = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1719
                        try {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1720
                            if ((val = mappingFunction.apply(key)) != null)
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  1721
                                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
  1722
                        } finally {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1723
                            setTabAt(tab, i, node);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1724
                        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1725
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1726
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1727
                if (binCount != 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1728
                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1729
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1730
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1731
                tab = helpTransfer(tab, f);
44588
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  1732
            else if (fh == h    // check first node without acquiring lock
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  1733
                     && ((fk = f.key) == key || (fk != null && key.equals(fk)))
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  1734
                     && (fv = f.val) != null)
39779
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  1735
                return fv;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1736
            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1737
                boolean added = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1738
                synchronized (f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1739
                    if (tabAt(tab, i) == f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1740
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1741
                            binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1742
                            for (Node<K,V> e = f;; ++binCount) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1743
                                K ek;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1744
                                if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1745
                                    ((ek = e.key) == key ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1746
                                     (ek != null && key.equals(ek)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1747
                                    val = e.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1748
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1749
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1750
                                Node<K,V> pred = e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1751
                                if ((e = e.next) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1752
                                    if ((val = mappingFunction.apply(key)) != null) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1753
                                        if (pred.next != null)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1754
                                            throw new IllegalStateException("Recursive update");
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1755
                                        added = true;
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  1756
                                        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
  1757
                                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1758
                                    break;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1759
                                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1760
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1761
                        }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1762
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1763
                            binCount = 2;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1764
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1765
                            TreeNode<K,V> r, p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1766
                            if ((r = t.root) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1767
                                (p = r.findTreeNode(h, key, null)) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1768
                                val = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1769
                            else if ((val = mappingFunction.apply(key)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1770
                                added = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1771
                                t.putTreeVal(h, key, val);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1772
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1773
                        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1774
                        else if (f instanceof ReservationNode)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1775
                            throw new IllegalStateException("Recursive update");
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1776
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1777
                }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1778
                if (binCount != 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1779
                    if (binCount >= TREEIFY_THRESHOLD)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1780
                        treeifyBin(tab, i);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1781
                    if (!added)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1782
                        return val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1783
                    break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1784
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1785
            }
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  1786
        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1787
        if (val != null)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1788
            addCount(1L, binCount);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1789
        return val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1790
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1791
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1792
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1793
     * 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
  1794
     * 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
  1795
     * value.  The entire method invocation is performed atomically.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1796
     * 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
  1797
     * 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
  1798
     * 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
  1799
     * update any other mappings of this map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1800
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1801
     * @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
  1802
     * @param remappingFunction the function to compute a value
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1803
     * @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
  1804
     * @throws NullPointerException if the specified key or remappingFunction
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1805
     *         is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1806
     * @throws IllegalStateException if the computation detectably
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1807
     *         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
  1808
     *         otherwise never complete
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1809
     * @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
  1810
     *         in which case the mapping is unchanged
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1811
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1812
    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
  1813
        if (key == null || remappingFunction == null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1814
            throw new NullPointerException();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1815
        int h = spread(key.hashCode());
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1816
        V val = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1817
        int delta = 0;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1818
        int binCount = 0;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1819
        for (Node<K,V>[] tab = table;;) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1820
            Node<K,V> f; int n, i, fh;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1821
            if (tab == null || (n = tab.length) == 0)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1822
                tab = initTable();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1823
            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
  1824
                break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1825
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1826
                tab = helpTransfer(tab, f);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1827
            else {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1828
                synchronized (f) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1829
                    if (tabAt(tab, i) == f) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1830
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1831
                            binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1832
                            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
  1833
                                K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1834
                                if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1835
                                    ((ek = e.key) == key ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1836
                                     (ek != null && key.equals(ek)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1837
                                    val = remappingFunction.apply(key, e.val);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1838
                                    if (val != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1839
                                        e.val = val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1840
                                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1841
                                        delta = -1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1842
                                        Node<K,V> en = e.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1843
                                        if (pred != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1844
                                            pred.next = en;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1845
                                        else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1846
                                            setTabAt(tab, i, en);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1847
                                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1848
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1849
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1850
                                pred = e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1851
                                if ((e = e.next) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1852
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1853
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1854
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1855
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1856
                            binCount = 2;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1857
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1858
                            TreeNode<K,V> r, p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1859
                            if ((r = t.root) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1860
                                (p = r.findTreeNode(h, key, null)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1861
                                val = remappingFunction.apply(key, p.val);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1862
                                if (val != null)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1863
                                    p.val = val;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1864
                                else {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1865
                                    delta = -1;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1866
                                    if (t.removeTreeNode(p))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1867
                                        setTabAt(tab, i, untreeify(t.first));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1868
                                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1869
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1870
                        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1871
                        else if (f instanceof ReservationNode)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1872
                            throw new IllegalStateException("Recursive update");
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1873
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1874
                }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1875
                if (binCount != 0)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1876
                    break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1877
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1878
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1879
        if (delta != 0)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1880
            addCount((long)delta, binCount);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1881
        return val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1882
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1883
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1884
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1885
     * 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
  1886
     * 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
  1887
     * mapping). The entire method invocation is performed atomically.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1888
     * 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
  1889
     * 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
  1890
     * 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
  1891
     * update any other mappings of this Map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1892
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1893
     * @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
  1894
     * @param remappingFunction the function to compute a value
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1895
     * @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
  1896
     * @throws NullPointerException if the specified key or remappingFunction
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1897
     *         is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1898
     * @throws IllegalStateException if the computation detectably
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1899
     *         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
  1900
     *         otherwise never complete
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1901
     * @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
  1902
     *         in which case the mapping is unchanged
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1903
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1904
    public V compute(K key,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1905
                     BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1906
        if (key == null || remappingFunction == null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1907
            throw new NullPointerException();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1908
        int h = spread(key.hashCode());
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1909
        V val = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1910
        int delta = 0;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1911
        int binCount = 0;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1912
        for (Node<K,V>[] tab = table;;) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1913
            Node<K,V> f; int n, i, fh;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1914
            if (tab == null || (n = tab.length) == 0)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1915
                tab = initTable();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1916
            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
  1917
                Node<K,V> r = new ReservationNode<K,V>();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1918
                synchronized (r) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1919
                    if (casTabAt(tab, i, null, r)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1920
                        binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1921
                        Node<K,V> node = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1922
                        try {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1923
                            if ((val = remappingFunction.apply(key, null)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1924
                                delta = 1;
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  1925
                                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
  1926
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1927
                        } finally {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1928
                            setTabAt(tab, i, node);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1929
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1930
                    }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1931
                }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1932
                if (binCount != 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1933
                    break;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1934
            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1935
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1936
                tab = helpTransfer(tab, f);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1937
            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1938
                synchronized (f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1939
                    if (tabAt(tab, i) == f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1940
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1941
                            binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1942
                            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
  1943
                                K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1944
                                if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1945
                                    ((ek = e.key) == key ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1946
                                     (ek != null && key.equals(ek)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1947
                                    val = remappingFunction.apply(key, e.val);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1948
                                    if (val != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1949
                                        e.val = val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1950
                                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1951
                                        delta = -1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1952
                                        Node<K,V> en = e.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1953
                                        if (pred != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1954
                                            pred.next = en;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1955
                                        else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1956
                                            setTabAt(tab, i, en);
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
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1959
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1960
                                pred = e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1961
                                if ((e = e.next) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1962
                                    val = remappingFunction.apply(key, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1963
                                    if (val != null) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1964
                                        if (pred.next != null)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1965
                                            throw new IllegalStateException("Recursive update");
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1966
                                        delta = 1;
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  1967
                                        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
  1968
                                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1969
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1970
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1971
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1972
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1973
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1974
                            binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1975
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1976
                            TreeNode<K,V> r, p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1977
                            if ((r = t.root) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1978
                                p = r.findTreeNode(h, key, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1979
                            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1980
                                p = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1981
                            V pv = (p == null) ? null : p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1982
                            val = remappingFunction.apply(key, pv);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1983
                            if (val != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1984
                                if (p != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1985
                                    p.val = val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1986
                                else {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1987
                                    delta = 1;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1988
                                    t.putTreeVal(h, key, val);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1989
                                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1990
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1991
                            else if (p != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1992
                                delta = -1;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1993
                                if (t.removeTreeNode(p))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1994
                                    setTabAt(tab, i, untreeify(t.first));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1995
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1996
                        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1997
                        else if (f instanceof ReservationNode)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  1998
                            throw new IllegalStateException("Recursive update");
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1999
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2000
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2001
                if (binCount != 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2002
                    if (binCount >= TREEIFY_THRESHOLD)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2003
                        treeifyBin(tab, i);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2004
                    break;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2005
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2006
            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2007
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2008
        if (delta != 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2009
            addCount((long)delta, binCount);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2010
        return val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2011
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2012
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2013
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2014
     * 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
  2015
     * (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
  2016
     * 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
  2017
     * 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
  2018
     * method invocation is performed atomically.  Some attempted
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2019
     * 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
  2020
     * 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
  2021
     * 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
  2022
     * mappings of this Map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2023
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2024
     * @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
  2025
     * @param value the value to use if absent
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2026
     * @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
  2027
     * @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
  2028
     * @throws NullPointerException if the specified key or the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2029
     *         remappingFunction is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2030
     * @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
  2031
     *         in which case the mapping is unchanged
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2032
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2033
    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
  2034
        if (key == null || value == null || remappingFunction == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2035
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2036
        int h = spread(key.hashCode());
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2037
        V val = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2038
        int delta = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2039
        int binCount = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2040
        for (Node<K,V>[] tab = table;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2041
            Node<K,V> f; int n, i, fh;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2042
            if (tab == null || (n = tab.length) == 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2043
                tab = initTable();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2044
            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  2045
                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
  2046
                    delta = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2047
                    val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2048
                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2049
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2050
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2051
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2052
                tab = helpTransfer(tab, f);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2053
            else {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2054
                synchronized (f) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2055
                    if (tabAt(tab, i) == f) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2056
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2057
                            binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2058
                            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
  2059
                                K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2060
                                if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2061
                                    ((ek = e.key) == key ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2062
                                     (ek != null && key.equals(ek)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2063
                                    val = remappingFunction.apply(e.val, value);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2064
                                    if (val != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2065
                                        e.val = val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2066
                                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2067
                                        delta = -1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2068
                                        Node<K,V> en = e.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2069
                                        if (pred != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2070
                                            pred.next = en;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2071
                                        else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2072
                                            setTabAt(tab, i, en);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2073
                                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2074
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2075
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2076
                                pred = e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2077
                                if ((e = e.next) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2078
                                    delta = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2079
                                    val = value;
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  2080
                                    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
  2081
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2082
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2083
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2084
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2085
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2086
                            binCount = 2;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2087
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2088
                            TreeNode<K,V> r = t.root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2089
                            TreeNode<K,V> p = (r == null) ? null :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2090
                                r.findTreeNode(h, key, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2091
                            val = (p == null) ? value :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2092
                                remappingFunction.apply(p.val, value);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2093
                            if (val != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2094
                                if (p != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2095
                                    p.val = val;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2096
                                else {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2097
                                    delta = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2098
                                    t.putTreeVal(h, key, val);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2099
                                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2100
                            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2101
                            else if (p != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2102
                                delta = -1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2103
                                if (t.removeTreeNode(p))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2104
                                    setTabAt(tab, i, untreeify(t.first));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2105
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2106
                        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2107
                        else if (f instanceof ReservationNode)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2108
                            throw new IllegalStateException("Recursive update");
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2109
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2110
                }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2111
                if (binCount != 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2112
                    if (binCount >= TREEIFY_THRESHOLD)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2113
                        treeifyBin(tab, i);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2114
                    break;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2115
                }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2116
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2117
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2118
        if (delta != 0)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2119
            addCount((long)delta, binCount);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2120
        return val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2121
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2122
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2123
    // Hashtable legacy methods
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2124
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2125
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2126
     * 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
  2127
     *
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2128
     * <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
  2129
     * {@link #containsValue(Object)}, and exists solely to ensure
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2130
     * full compatibility with class {@link java.util.Hashtable},
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2131
     * which supported this method prior to introduction of the
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2132
     * Java Collections Framework.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2133
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2134
     * @param  value a value to search for
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2135
     * @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
  2136
     *         {@code value} argument in this table as
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2137
     *         determined by the {@code equals} method;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2138
     *         {@code false} otherwise
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2139
     * @throws NullPointerException if the specified value is null
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
    public boolean contains(Object value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2142
        return containsValue(value);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2143
    }
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
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2146
     * 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
  2147
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2148
     * @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
  2149
     * @see #keySet()
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2150
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2151
    public Enumeration<K> keys() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2152
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2153
        int f = (t = table) == null ? 0 : t.length;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2154
        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
  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
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2158
     * 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
  2159
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2160
     * @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
  2161
     * @see #values()
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
    public Enumeration<V> elements() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2164
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2165
        int f = (t = table) == null ? 0 : t.length;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2166
        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
  2167
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2168
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2169
    // ConcurrentHashMap-only methods
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
     * 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
  2173
     * instead of {@link #size} because a ConcurrentHashMap may
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2174
     * 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
  2175
     * 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
  2176
     * there are concurrent insertions or removals.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2177
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2178
     * @return the number of mappings
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2179
     * @since 1.8
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2180
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2181
    public long mappingCount() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2182
        long n = sumCount();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2183
        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
  2184
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2185
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2186
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2187
     * 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
  2188
     * from the given type to {@code Boolean.TRUE}.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2189
     *
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2190
     * @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
  2191
     * @return the new set
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2192
     * @since 1.8
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2193
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2194
    public static <K> KeySetView<K,Boolean> newKeySet() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2195
        return new KeySetView<K,Boolean>
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2196
            (new ConcurrentHashMap<K,Boolean>(), Boolean.TRUE);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2197
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2198
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2199
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2200
     * 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
  2201
     * from the given type to {@code Boolean.TRUE}.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2202
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2203
     * @param initialCapacity The implementation performs internal
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2204
     * sizing to accommodate this many elements.
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2205
     * @param <K> the element type of the returned set
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2206
     * @return the new set
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2207
     * @throws IllegalArgumentException if the initial capacity of
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2208
     * elements is negative
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2209
     * @since 1.8
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2210
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2211
    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
  2212
        return new KeySetView<K,Boolean>
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2213
            (new ConcurrentHashMap<K,Boolean>(initialCapacity), Boolean.TRUE);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2214
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2215
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
     * 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
  2218
     * 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
  2219
     * Collection#add} and {@link Collection#addAll(Collection)}).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2220
     * 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
  2221
     * 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
  2222
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2223
     * @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
  2224
     * @return the set view
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2225
     * @throws NullPointerException if the mappedValue is null
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2226
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2227
    public KeySetView<K,V> keySet(V mappedValue) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2228
        if (mappedValue == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2229
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2230
        return new KeySetView<K,V>(this, mappedValue);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2231
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2232
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2233
    /* ---------------- Special Nodes -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2234
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2235
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2236
     * 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
  2237
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2238
    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
  2239
        final Node<K,V>[] nextTable;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2240
        ForwardingNode(Node<K,V>[] tab) {
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  2241
            super(MOVED, null, null);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2242
            this.nextTable = tab;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2243
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2244
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2245
        Node<K,V> find(int h, Object k) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2246
            // 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
  2247
            outer: for (Node<K,V>[] tab = nextTable;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2248
                Node<K,V> e; int n;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2249
                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
  2250
                    (e = tabAt(tab, (n - 1) & h)) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2251
                    return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2252
                for (;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2253
                    int eh; K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2254
                    if ((eh = e.hash) == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2255
                        ((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
  2256
                        return e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2257
                    if (eh < 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2258
                        if (e instanceof ForwardingNode) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2259
                            tab = ((ForwardingNode<K,V>)e).nextTable;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2260
                            continue outer;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2261
                        }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2262
                        else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2263
                            return e.find(h, k);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2264
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2265
                    if ((e = e.next) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2266
                        return null;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2267
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2268
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2269
        }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2270
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2271
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2272
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2273
     * 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
  2274
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2275
    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
  2276
        ReservationNode() {
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  2277
            super(RESERVED, null, null);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2278
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2279
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2280
        Node<K,V> find(int h, Object k) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2281
            return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2282
        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2283
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2284
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2285
    /* ---------------- Table Initialization and Resizing -------------- */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2286
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2287
    /**
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2288
     * 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
  2289
     * 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
  2290
     */
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2291
    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
  2292
        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
  2293
    }
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2294
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2295
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2296
     * Initializes table, using the size recorded in sizeCtl.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2297
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2298
    private final Node<K,V>[] initTable() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2299
        Node<K,V>[] tab; int sc;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2300
        while ((tab = table) == null || tab.length == 0) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2301
            if ((sc = sizeCtl) < 0)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2302
                Thread.yield(); // lost initialization race; just spin
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2303
            else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2304
                try {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2305
                    if ((tab = table) == null || tab.length == 0) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2306
                        int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2307
                        @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2308
                        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
  2309
                        table = tab = nt;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2310
                        sc = n - (n >>> 2);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2311
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2312
                } finally {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2313
                    sizeCtl = sc;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2314
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2315
                break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2316
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2317
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2318
        return tab;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2319
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2320
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2321
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2322
     * Adds to count, and if table is too small and not already
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2323
     * resizing, initiates transfer. If already resizing, helps
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2324
     * perform transfer if work is available.  Rechecks occupancy
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2325
     * after a transfer to see if another resize is already needed
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2326
     * because resizings are lagging additions.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2327
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2328
     * @param x the count to add
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2329
     * @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
  2330
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2331
    private final void addCount(long x, int check) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2332
        CounterCell[] as; long b, s;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2333
        if ((as = counterCells) != null ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2334
            !U.compareAndSwapLong(this, BASECOUNT, b = baseCount, s = b + x)) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2335
            CounterCell a; long v; int m;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2336
            boolean uncontended = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2337
            if (as == null || (m = as.length - 1) < 0 ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2338
                (a = as[ThreadLocalRandom.getProbe() & m]) == null ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2339
                !(uncontended =
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2340
                  U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2341
                fullAddCount(x, uncontended);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2342
                return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2343
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2344
            if (check <= 1)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2345
                return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2346
            s = sumCount();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2347
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2348
        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
  2349
            Node<K,V>[] tab, nt; int n, sc;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2350
            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
  2351
                   (n = tab.length) < MAXIMUM_CAPACITY) {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2352
                int rs = resizeStamp(n);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2353
                if (sc < 0) {
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2354
                    if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2355
                        sc == rs + MAX_RESIZERS || (nt = nextTable) == null ||
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2356
                        transferIndex <= 0)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2357
                        break;
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2358
                    if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1))
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2359
                        transfer(tab, nt);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2360
                }
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2361
                else if (U.compareAndSwapInt(this, SIZECTL, sc,
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2362
                                             (rs << RESIZE_STAMP_SHIFT) + 2))
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2363
                    transfer(tab, null);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2364
                s = sumCount();
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2365
            }
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2366
        }
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2367
    }
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2368
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2369
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2370
     * Helps transfer if a resize is in progress.
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
    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
  2373
        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
  2374
        if (tab != null && (f instanceof ForwardingNode) &&
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2375
            (nextTab = ((ForwardingNode<K,V>)f).nextTable) != null) {
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2376
            int rs = resizeStamp(tab.length);
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2377
            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
  2378
                   (sc = sizeCtl) < 0) {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2379
                if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2380
                    sc == rs + MAX_RESIZERS || transferIndex <= 0)
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2381
                    break;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2382
                if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1)) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2383
                    transfer(tab, nextTab);
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2384
                    break;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2385
                }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2386
            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2387
            return nextTab;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2388
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2389
        return table;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2390
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2391
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2392
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2393
     * Tries to presize table to accommodate the given number of elements.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2394
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2395
     * @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
  2396
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2397
    private final void tryPresize(int size) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2398
        int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2399
            tableSizeFor(size + (size >>> 1) + 1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2400
        int sc;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2401
        while ((sc = sizeCtl) >= 0) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2402
            Node<K,V>[] tab = table; int n;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2403
            if (tab == null || (n = tab.length) == 0) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2404
                n = (sc > c) ? sc : c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2405
                if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2406
                    try {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2407
                        if (table == tab) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2408
                            @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2409
                            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
  2410
                            table = nt;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2411
                            sc = n - (n >>> 2);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2412
                        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2413
                    } finally {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2414
                        sizeCtl = sc;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2415
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2416
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2417
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2418
            else if (c <= sc || n >= MAXIMUM_CAPACITY)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2419
                break;
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2420
            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
  2421
                int rs = resizeStamp(n);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2422
                if (U.compareAndSwapInt(this, SIZECTL, sc,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2423
                                        (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
  2424
                    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
  2425
            }
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 11279
diff changeset
  2426
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2430
     * Moves and/or copies the nodes in each bin to new table. See
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2431
     * above for explanation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2433
    private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2434
        int n = tab.length, stride;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2435
        if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2436
            stride = MIN_TRANSFER_STRIDE; // subdivide range
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2437
        if (nextTab == null) {            // initiating
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2438
            try {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2439
                @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2440
                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
  2441
                nextTab = nt;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2442
            } catch (Throwable ex) {      // try to cope with OOME
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2443
                sizeCtl = Integer.MAX_VALUE;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2444
                return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2445
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2446
            nextTable = nextTab;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2447
            transferIndex = n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2449
        int nextn = nextTab.length;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2450
        ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2451
        boolean advance = true;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2452
        boolean finishing = false; // to ensure sweep before committing nextTab
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2453
        for (int i = 0, bound = 0;;) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2454
            Node<K,V> f; int fh;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2455
            while (advance) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2456
                int nextIndex, nextBound;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2457
                if (--i >= bound || finishing)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2458
                    advance = false;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2459
                else if ((nextIndex = transferIndex) <= 0) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2460
                    i = -1;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2461
                    advance = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2462
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2463
                else if (U.compareAndSwapInt
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2464
                         (this, TRANSFERINDEX, nextIndex,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2465
                          nextBound = (nextIndex > stride ?
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2466
                                       nextIndex - stride : 0))) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2467
                    bound = nextBound;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2468
                    i = nextIndex - 1;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2469
                    advance = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2470
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2471
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2472
            if (i < 0 || i >= n || i + n >= nextn) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2473
                int sc;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2474
                if (finishing) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2475
                    nextTable = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2476
                    table = nextTab;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2477
                    sizeCtl = (n << 1) - (n >>> 1);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2478
                    return;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2479
                }
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2480
                if (U.compareAndSwapInt(this, SIZECTL, sc = sizeCtl, sc - 1)) {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2481
                    if ((sc - 2) != resizeStamp(n) << RESIZE_STAMP_SHIFT)
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2482
                        return;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2483
                    finishing = advance = true;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2484
                    i = n; // recheck before commit
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2485
                }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2486
            }
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2487
            else if ((f = tabAt(tab, i)) == null)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2488
                advance = casTabAt(tab, i, null, fwd);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2489
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2490
                advance = true; // already processed
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2491
            else {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2492
                synchronized (f) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2493
                    if (tabAt(tab, i) == f) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2494
                        Node<K,V> ln, hn;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2495
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2496
                            int runBit = fh & n;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2497
                            Node<K,V> lastRun = f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2498
                            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
  2499
                                int b = p.hash & n;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2500
                                if (b != runBit) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2501
                                    runBit = b;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2502
                                    lastRun = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2503
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2504
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2505
                            if (runBit == 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2506
                                ln = lastRun;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2507
                                hn = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
                            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2509
                            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2510
                                hn = lastRun;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2511
                                ln = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2512
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2513
                            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
  2514
                                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
  2515
                                if ((ph & n) == 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2516
                                    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
  2517
                                else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2518
                                    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
  2519
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2520
                            setTabAt(nextTab, i, ln);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2521
                            setTabAt(nextTab, i + n, hn);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2522
                            setTabAt(tab, i, fwd);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2523
                            advance = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
                        }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2525
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2526
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2527
                            TreeNode<K,V> lo = null, loTail = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2528
                            TreeNode<K,V> hi = null, hiTail = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2529
                            int lc = 0, hc = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2530
                            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
  2531
                                int h = e.hash;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2532
                                TreeNode<K,V> p = new TreeNode<K,V>
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2533
                                    (h, e.key, e.val, null, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2534
                                if ((h & n) == 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2535
                                    if ((p.prev = loTail) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2536
                                        lo = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2537
                                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2538
                                        loTail.next = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2539
                                    loTail = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2540
                                    ++lc;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2541
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2542
                                else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2543
                                    if ((p.prev = hiTail) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2544
                                        hi = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2545
                                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2546
                                        hiTail.next = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2547
                                    hiTail = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2548
                                    ++hc;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2549
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2550
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2551
                            ln = (lc <= UNTREEIFY_THRESHOLD) ? untreeify(lo) :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2552
                                (hc != 0) ? new TreeBin<K,V>(lo) : t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2553
                            hn = (hc <= UNTREEIFY_THRESHOLD) ? untreeify(hi) :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2554
                                (lc != 0) ? new TreeBin<K,V>(hi) : t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2555
                            setTabAt(nextTab, i, ln);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2556
                            setTabAt(nextTab, i + n, hn);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2557
                            setTabAt(tab, i, fwd);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2558
                            advance = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
            }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2563
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2564
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2565
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2566
    /* ---------------- Counter support -------------- */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2567
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2568
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2569
     * 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
  2570
     * and Striped64.  See their internal docs for explanation.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2571
     */
34369
b6df4cc80001 8140687: Move @Contended to the jdk.internal.vm.annotation package
chegar
parents: 33674
diff changeset
  2572
    @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
  2573
        volatile long value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2574
        CounterCell(long x) { value = x; }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2575
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2576
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2577
    final long sumCount() {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2578
        CounterCell[] as = counterCells; CounterCell a;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2579
        long sum = baseCount;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2580
        if (as != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2581
            for (int i = 0; i < as.length; ++i) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2582
                if ((a = as[i]) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2583
                    sum += a.value;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2584
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2585
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2586
        return sum;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2587
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2588
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2589
    // See LongAdder version for explanation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2590
    private final void fullAddCount(long x, boolean wasUncontended) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2591
        int h;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2592
        if ((h = ThreadLocalRandom.getProbe()) == 0) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2593
            ThreadLocalRandom.localInit();      // force initialization
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2594
            h = ThreadLocalRandom.getProbe();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2595
            wasUncontended = true;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2596
        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2597
        boolean collide = false;                // True if last slot nonempty
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2598
        for (;;) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2599
            CounterCell[] as; CounterCell a; int n; long v;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2600
            if ((as = counterCells) != null && (n = as.length) > 0) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2601
                if ((a = as[(n - 1) & h]) == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2602
                    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
  2603
                        CounterCell r = new CounterCell(x); // Optimistic create
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2604
                        if (cellsBusy == 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2605
                            U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2606
                            boolean created = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2607
                            try {               // Recheck under lock
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2608
                                CounterCell[] rs; int m, j;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2609
                                if ((rs = counterCells) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2610
                                    (m = rs.length) > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2611
                                    rs[j = (m - 1) & h] == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2612
                                    rs[j] = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2613
                                    created = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2614
                                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2615
                            } finally {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2616
                                cellsBusy = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2617
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2618
                            if (created)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2619
                                break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2620
                            continue;           // Slot is now non-empty
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2621
                        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2622
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2623
                    collide = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2624
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2625
                else if (!wasUncontended)       // CAS already known to fail
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2626
                    wasUncontended = true;      // Continue after rehash
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2627
                else if (U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2628
                    break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2629
                else if (counterCells != as || n >= NCPU)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2630
                    collide = false;            // At max size or stale
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2631
                else if (!collide)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2632
                    collide = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2633
                else if (cellsBusy == 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2634
                         U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2635
                    try {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2636
                        if (counterCells == as) {// Expand table unless stale
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2637
                            CounterCell[] rs = new CounterCell[n << 1];
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2638
                            for (int i = 0; i < n; ++i)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2639
                                rs[i] = as[i];
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2640
                            counterCells = rs;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2641
                        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2642
                    } finally {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2643
                        cellsBusy = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2644
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2645
                    collide = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2646
                    continue;                   // Retry with expanded table
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2647
                }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2648
                h = ThreadLocalRandom.advanceProbe(h);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2649
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2650
            else if (cellsBusy == 0 && counterCells == as &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2651
                     U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2652
                boolean init = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2653
                try {                           // Initialize table
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2654
                    if (counterCells == as) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2655
                        CounterCell[] rs = new CounterCell[2];
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2656
                        rs[h & 1] = new CounterCell(x);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2657
                        counterCells = rs;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2658
                        init = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2659
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2660
                } finally {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2661
                    cellsBusy = 0;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2662
                }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2663
                if (init)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2664
                    break;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2665
            }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2666
            else if (U.compareAndSwapLong(this, BASECOUNT, v = baseCount, v + x))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2667
                break;                          // Fall back on using base
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2668
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2669
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2670
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2671
    /* ---------------- Conversion from/to TreeBins -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2672
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2673
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2674
     * 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
  2675
     * too small, in which case resizes instead.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2676
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2677
    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
  2678
        Node<K,V> b; int n;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2679
        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
  2680
            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
  2681
                tryPresize(n << 1);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2682
            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
  2683
                synchronized (b) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2684
                    if (tabAt(tab, index) == b) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2685
                        TreeNode<K,V> hd = null, tl = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2686
                        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
  2687
                            TreeNode<K,V> p =
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2688
                                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
  2689
                                                  null, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2690
                            if ((p.prev = 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
                        setTabAt(tab, index, new TreeBin<K,V>(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
            }
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
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2702
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2703
    /**
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  2704
     * 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
  2705
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2706
    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
  2707
        Node<K,V> hd = null, tl = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2708
        for (Node<K,V> q = b; q != null; q = q.next) {
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  2709
            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
  2710
            if (tl == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2711
                hd = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2712
            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2713
                tl.next = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2714
            tl = p;
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
        return hd;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2717
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2718
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2719
    /* ---------------- TreeNodes -------------- */
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
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2722
     * Nodes for use in TreeBins.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2723
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2724
    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
  2725
        TreeNode<K,V> parent;  // red-black tree links
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2726
        TreeNode<K,V> left;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2727
        TreeNode<K,V> right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2728
        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
  2729
        boolean red;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2730
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2731
        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
  2732
                 TreeNode<K,V> parent) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2733
            super(hash, key, val, next);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2734
            this.parent = parent;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2735
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2736
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2737
        Node<K,V> find(int h, Object k) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2738
            return findTreeNode(h, k, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2739
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2740
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2741
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2742
         * 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
  2743
         * starting at given root.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2744
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2745
        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
  2746
            if (k != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2747
                TreeNode<K,V> p = this;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2748
                do {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2749
                    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
  2750
                    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
  2751
                    if ((ph = p.hash) > h)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2752
                        p = pl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2753
                    else if (ph < h)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2754
                        p = pr;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2755
                    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
  2756
                        return p;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2757
                    else if (pl == null)
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2758
                        p = pr;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2759
                    else if (pr == null)
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2760
                        p = pl;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2761
                    else if ((kc != null ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2762
                              (kc = comparableClassFor(k)) != null) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2763
                             (dir = compareComparables(kc, k, pk)) != 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2764
                        p = (dir < 0) ? pl : pr;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2765
                    else if ((q = pr.findTreeNode(h, k, kc)) != null)
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2766
                        return q;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2767
                    else
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2768
                        p = pl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2769
                } while (p != null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2770
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2771
            return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2772
        }
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
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2775
    /* ---------------- TreeBins -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2776
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2777
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2778
     * 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
  2779
     * 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
  2780
     * 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
  2781
     * 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
  2782
     * not) to complete before tree restructuring operations.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2783
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2784
    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
  2785
        TreeNode<K,V> root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2786
        volatile TreeNode<K,V> first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2787
        volatile Thread waiter;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2788
        volatile int lockState;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2789
        // values for lockState
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2790
        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
  2791
        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
  2792
        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
  2793
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2794
        /**
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2795
         * Tie-breaking utility for ordering insertions when equal
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2796
         * hashCodes and non-comparable. We don't require a total
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2797
         * order, just a consistent insertion rule to maintain
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2798
         * equivalence across rebalancings. Tie-breaking further than
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2799
         * necessary simplifies testing a bit.
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2800
         */
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2801
        static int tieBreakOrder(Object a, Object b) {
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2802
            int d;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2803
            if (a == null || b == null ||
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2804
                (d = a.getClass().getName().
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2805
                 compareTo(b.getClass().getName())) == 0)
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2806
                d = (System.identityHashCode(a) <= System.identityHashCode(b) ?
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2807
                     -1 : 1);
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2808
            return d;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2809
        }
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2810
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2811
        /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2812
         * 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
  2813
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2814
        TreeBin(TreeNode<K,V> b) {
36958
8a320111d9fe 8151579: Optimize ConcurrentHashMap.Node
dl
parents: 34376
diff changeset
  2815
            super(TREEBIN, null, null);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2816
            this.first = b;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2817
            TreeNode<K,V> r = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2818
            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
  2819
                next = (TreeNode<K,V>)x.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2820
                x.left = x.right = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2821
                if (r == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2822
                    x.parent = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2823
                    x.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2824
                    r = x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2825
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2826
                else {
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2827
                    K k = x.key;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2828
                    int h = x.hash;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2829
                    Class<?> kc = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2830
                    for (TreeNode<K,V> p = r;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2831
                        int dir, ph;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2832
                        K pk = p.key;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2833
                        if ((ph = p.hash) > h)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2834
                            dir = -1;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2835
                        else if (ph < h)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2836
                            dir = 1;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2837
                        else if ((kc == null &&
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2838
                                  (kc = comparableClassFor(k)) == null) ||
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2839
                                 (dir = compareComparables(kc, k, pk)) == 0)
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2840
                            dir = tieBreakOrder(k, pk);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2841
                        TreeNode<K,V> xp = p;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2842
                        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
  2843
                            x.parent = xp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2844
                            if (dir <= 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2845
                                xp.left = x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2846
                            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2847
                                xp.right = x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2848
                            r = balanceInsertion(r, x);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2849
                            break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2850
                        }
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
            this.root = r;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2855
            assert checkInvariants(root);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2856
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2857
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2858
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2859
         * Acquires write lock for tree restructuring.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2860
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2861
        private final void lockRoot() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2862
            if (!U.compareAndSwapInt(this, LOCKSTATE, 0, WRITER))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2863
                contendedLock(); // offload to separate method
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
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2866
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2867
         * Releases write lock for tree restructuring.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2868
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2869
        private final void unlockRoot() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2870
            lockState = 0;
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
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2873
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2874
         * Possibly blocks awaiting root lock.
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
        private final void contendedLock() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2877
            boolean waiting = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2878
            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
  2879
                if (((s = lockState) & ~WAITER) == 0) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2880
                    if (U.compareAndSwapInt(this, LOCKSTATE, s, WRITER)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2881
                        if (waiting)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2882
                            waiter = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2883
                        return;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2884
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2885
                }
19413
56082c988815 8023104: ConcurrentHashMap typo
dl
parents: 19380
diff changeset
  2886
                else if ((s & WAITER) == 0) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2887
                    if (U.compareAndSwapInt(this, LOCKSTATE, s, s | WAITER)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2888
                        waiting = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2889
                        waiter = Thread.currentThread();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2890
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2891
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2892
                else if (waiting)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2893
                    LockSupport.park(this);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2894
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2895
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2896
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2897
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2898
         * 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
  2899
         * using tree comparisons from root, but continues linear
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2900
         * search when lock not available.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2901
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2902
        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
  2903
            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
  2904
                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
  2905
                    int s; K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2906
                    if (((s = lockState) & (WAITER|WRITER)) != 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2907
                        if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2908
                            ((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
  2909
                            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
  2910
                        e = e.next;
18802
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
                    else if (U.compareAndSwapInt(this, LOCKSTATE, s,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2913
                                                 s + READER)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2914
                        TreeNode<K,V> r, p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2915
                        try {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2916
                            p = ((r = root) == null ? null :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2917
                                 r.findTreeNode(h, k, null));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2918
                        } finally {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2919
                            Thread w;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2920
                            if (U.getAndAddInt(this, LOCKSTATE, -READER) ==
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2921
                                (READER|WAITER) && (w = waiter) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2922
                                LockSupport.unpark(w);
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
                        return p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2925
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2926
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2927
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2928
            return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2929
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2930
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2931
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2932
         * Finds or adds a node.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2933
         * @return null if added
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2934
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2935
        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
  2936
            Class<?> kc = null;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2937
            boolean searched = false;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2938
            for (TreeNode<K,V> p = root;;) {
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2939
                int dir, ph; K pk;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2940
                if (p == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2941
                    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
  2942
                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2943
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2944
                else if ((ph = p.hash) > h)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2945
                    dir = -1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2946
                else if (ph < h)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2947
                    dir = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2948
                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
  2949
                    return p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2950
                else if ((kc == null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2951
                          (kc = comparableClassFor(k)) == null) ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2952
                         (dir = compareComparables(kc, k, pk)) == 0) {
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2953
                    if (!searched) {
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2954
                        TreeNode<K,V> q, ch;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2955
                        searched = true;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2956
                        if (((ch = p.left) != null &&
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2957
                             (q = ch.findTreeNode(h, k, kc)) != null) ||
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2958
                            ((ch = p.right) != null &&
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2959
                             (q = ch.findTreeNode(h, k, kc)) != null))
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2960
                            return q;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2961
                    }
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2962
                    dir = tieBreakOrder(k, pk);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2963
                }
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2964
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2965
                TreeNode<K,V> xp = p;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2966
                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
  2967
                    TreeNode<K,V> x, f = first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2968
                    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
  2969
                    if (f != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2970
                        f.prev = x;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2971
                    if (dir <= 0)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2972
                        xp.left = x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2973
                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2974
                        xp.right = x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2975
                    if (!xp.red)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2976
                        x.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2977
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2978
                        lockRoot();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2979
                        try {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2980
                            root = balanceInsertion(root, x);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2981
                        } finally {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2982
                            unlockRoot();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2983
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2984
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2985
                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2986
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2987
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2988
            assert checkInvariants(root);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2989
            return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2990
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2991
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2992
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2993
         * 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
  2994
         * 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
  2995
         * 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
  2996
         * 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
  2997
         * 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
  2998
         * swap the tree linkages.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2999
         *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3000
         * @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
  3001
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3002
        final boolean removeTreeNode(TreeNode<K,V> p) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3003
            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
  3004
            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
  3005
            TreeNode<K,V> r, rl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3006
            if (pred == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3007
                first = next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3008
            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3009
                pred.next = next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3010
            if (next != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3011
                next.prev = pred;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3012
            if (first == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3013
                root = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3014
                return true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3015
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3016
            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
  3017
                (rl = r.left) == null || rl.left == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3018
                return true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3019
            lockRoot();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3020
            try {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3021
                TreeNode<K,V> replacement;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3022
                TreeNode<K,V> pl = p.left;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3023
                TreeNode<K,V> pr = p.right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3024
                if (pl != null && pr != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3025
                    TreeNode<K,V> s = pr, sl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3026
                    while ((sl = s.left) != null) // find successor
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3027
                        s = sl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3028
                    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
  3029
                    TreeNode<K,V> sr = s.right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3030
                    TreeNode<K,V> pp = p.parent;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3031
                    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
  3032
                        p.parent = s;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3033
                        s.right = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3034
                    }
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
                        TreeNode<K,V> sp = s.parent;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3037
                        if ((p.parent = sp) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3038
                            if (s == sp.left)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3039
                                sp.left = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3040
                            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3041
                                sp.right = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3042
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3043
                        if ((s.right = pr) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3044
                            pr.parent = s;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3045
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3046
                    p.left = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3047
                    if ((p.right = sr) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3048
                        sr.parent = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3049
                    if ((s.left = pl) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3050
                        pl.parent = s;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3051
                    if ((s.parent = pp) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3052
                        r = s;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3053
                    else if (p == pp.left)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3054
                        pp.left = s;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3055
                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3056
                        pp.right = s;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3057
                    if (sr != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3058
                        replacement = sr;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3059
                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3060
                        replacement = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3061
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3062
                else if (pl != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3063
                    replacement = pl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3064
                else if (pr != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3065
                    replacement = pr;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3066
                else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3067
                    replacement = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3068
                if (replacement != p) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3069
                    TreeNode<K,V> pp = replacement.parent = p.parent;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3070
                    if (pp == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3071
                        r = replacement;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3072
                    else if (p == pp.left)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3073
                        pp.left = replacement;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3074
                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3075
                        pp.right = replacement;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3076
                    p.left = p.right = p.parent = null;
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
                root = (p.red) ? r : balanceDeletion(r, replacement);
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
                if (p == replacement) {  // detach pointers
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3082
                    TreeNode<K,V> pp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3083
                    if ((pp = p.parent) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3084
                        if (p == pp.left)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3085
                            pp.left = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3086
                        else if (p == pp.right)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3087
                            pp.right = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3088
                        p.parent = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3089
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3090
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3091
            } finally {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3092
                unlockRoot();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3093
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3094
            assert checkInvariants(root);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3095
            return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3096
        }
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
        // Red-black tree methods, all adapted from CLR
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3100
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3101
        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
  3102
                                              TreeNode<K,V> p) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3103
            TreeNode<K,V> r, pp, rl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3104
            if (p != null && (r = p.right) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3105
                if ((rl = p.right = r.left) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3106
                    rl.parent = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3107
                if ((pp = r.parent = p.parent) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3108
                    (root = r).red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3109
                else if (pp.left == p)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3110
                    pp.left = r;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3111
                else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3112
                    pp.right = r;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3113
                r.left = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3114
                p.parent = r;
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
            return root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3117
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3118
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3119
        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
  3120
                                               TreeNode<K,V> p) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3121
            TreeNode<K,V> l, pp, lr;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3122
            if (p != null && (l = p.left) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3123
                if ((lr = p.left = l.right) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3124
                    lr.parent = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3125
                if ((pp = l.parent = p.parent) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3126
                    (root = l).red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3127
                else if (pp.right == p)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3128
                    pp.right = l;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3129
                else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3130
                    pp.left = l;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3131
                l.right = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3132
                p.parent = l;
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
            return root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3135
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3136
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3137
        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
  3138
                                                    TreeNode<K,V> x) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3139
            x.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3140
            for (TreeNode<K,V> xp, xpp, xppl, xppr;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3141
                if ((xp = x.parent) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3142
                    x.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3143
                    return x;
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
                else if (!xp.red || (xpp = xp.parent) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3146
                    return root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3147
                if (xp == (xppl = xpp.left)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3148
                    if ((xppr = xpp.right) != null && xppr.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3149
                        xppr.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3150
                        xp.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3151
                        xpp.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3152
                        x = xpp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3153
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3154
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3155
                        if (x == xp.right) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3156
                            root = rotateLeft(root, x = xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3157
                            xpp = (xp = x.parent) == null ? null : xp.parent;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3158
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3159
                        if (xp != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3160
                            xp.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3161
                            if (xpp != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3162
                                xpp.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3163
                                root = rotateRight(root, xpp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3164
                            }
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
                else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3169
                    if (xppl != null && xppl.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3170
                        xppl.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3171
                        xp.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3172
                        xpp.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3173
                        x = xpp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3174
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3175
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3176
                        if (x == xp.left) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3177
                            root = rotateRight(root, x = xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3178
                            xpp = (xp = x.parent) == null ? null : xp.parent;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3179
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3180
                        if (xp != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3181
                            xp.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3182
                            if (xpp != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3183
                                xpp.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3184
                                root = rotateLeft(root, xpp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3185
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3186
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3187
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3188
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3189
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3190
        }
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
        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
  3193
                                                   TreeNode<K,V> x) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3194
            for (TreeNode<K,V> xp, xpl, xpr;;) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3195
                if (x == null || x == root)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3196
                    return root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3197
                else if ((xp = x.parent) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3198
                    x.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3199
                    return x;
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 if (x.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3202
                    x.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3203
                    return root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3204
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3205
                else if ((xpl = xp.left) == x) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3206
                    if ((xpr = xp.right) != null && xpr.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3207
                        xpr.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3208
                        xp.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3209
                        root = rotateLeft(root, xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3210
                        xpr = (xp = x.parent) == null ? null : xp.right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3211
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3212
                    if (xpr == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3213
                        x = xp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3214
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3215
                        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
  3216
                        if ((sr == null || !sr.red) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3217
                            (sl == null || !sl.red)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3218
                            xpr.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3219
                            x = xp;
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
                        else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3222
                            if (sr == null || !sr.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3223
                                if (sl != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3224
                                    sl.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3225
                                xpr.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3226
                                root = rotateRight(root, xpr);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3227
                                xpr = (xp = x.parent) == null ?
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3228
                                    null : xp.right;
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 (xpr != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3231
                                xpr.red = (xp == null) ? false : xp.red;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3232
                                if ((sr = xpr.right) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3233
                                    sr.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3234
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3235
                            if (xp != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3236
                                xp.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3237
                                root = rotateLeft(root, 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
                            x = root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3240
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3241
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3242
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3243
                else { // symmetric
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3244
                    if (xpl != null && xpl.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3245
                        xpl.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3246
                        xp.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3247
                        root = rotateRight(root, xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3248
                        xpl = (xp = x.parent) == null ? null : xp.left;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3249
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3250
                    if (xpl == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3251
                        x = xp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3252
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3253
                        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
  3254
                        if ((sl == null || !sl.red) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3255
                            (sr == null || !sr.red)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3256
                            xpl.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3257
                            x = xp;
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
                        else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3260
                            if (sl == null || !sl.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3261
                                if (sr != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3262
                                    sr.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3263
                                xpl.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3264
                                root = rotateLeft(root, xpl);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3265
                                xpl = (xp = x.parent) == null ?
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3266
                                    null : xp.left;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3267
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3268
                            if (xpl != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3269
                                xpl.red = (xp == null) ? false : xp.red;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3270
                                if ((sl = xpl.left) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3271
                                    sl.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3272
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3273
                            if (xp != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3274
                                xp.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3275
                                root = rotateRight(root, xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3276
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3277
                            x = root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3278
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3279
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3280
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3281
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3282
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3283
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3284
        /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3285
         * 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
  3286
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3287
        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
  3288
            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
  3289
                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
  3290
            if (tb != null && tb.next != t)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3291
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3292
            if (tn != null && tn.prev != t)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3293
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3294
            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
  3295
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3296
            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
  3297
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3298
            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
  3299
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3300
            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
  3301
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3302
            if (tl != null && !checkInvariants(tl))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3303
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3304
            if (tr != null && !checkInvariants(tr))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3305
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3306
            return true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3307
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3308
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
  3309
        private static final Unsafe U = Unsafe.getUnsafe();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3310
        private static final long LOCKSTATE;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3311
        static {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3312
            try {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3313
                LOCKSTATE = U.objectFieldOffset
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3314
                    (TreeBin.class.getDeclaredField("lockState"));
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3315
            } catch (ReflectiveOperationException e) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3316
                throw new Error(e);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3317
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3318
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3319
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3320
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3321
    /* ----------------Table Traversal -------------- */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3322
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3323
    /**
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3324
     * 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
  3325
     * 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
  3326
     * proceeding with current table.
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3327
     */
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3328
    static final class TableStack<K,V> {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3329
        int length;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3330
        int index;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3331
        Node<K,V>[] tab;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3332
        TableStack<K,V> next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3333
    }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3334
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3335
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3336
     * Encapsulates traversal for methods such as containsValue; also
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3337
     * serves as a base class for other iterators and spliterators.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3338
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3339
     * Method advance visits once each still-valid node that was
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3340
     * reachable upon iterator construction. It might miss some that
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3341
     * 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
  3342
     * consistency guarantees. Maintaining this property in the face
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3343
     * of possible ongoing resizes requires a fair amount of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3344
     * bookkeeping state that is difficult to optimize away amidst
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3345
     * volatile accesses.  Even so, traversal maintains reasonable
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3346
     * throughput.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3347
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3348
     * Normally, iteration proceeds bin-by-bin traversing lists.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3349
     * However, if the table has been resized, then all future steps
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3350
     * must traverse both the bin at the current index as well as at
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3351
     * (index + baseSize); and so on for further resizings. To
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3352
     * paranoically cope with potential sharing by users of iterators
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3353
     * across threads, iteration terminates if a bounds checks fails
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3354
     * for a table read.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3355
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3356
    static class Traverser<K,V> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3357
        Node<K,V>[] tab;        // current table; updated if resized
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3358
        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
  3359
        TableStack<K,V> stack, spare; // to save/restore on ForwardingNodes
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3360
        int index;              // index of bin to use next
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3361
        int baseIndex;          // current index of initial table
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3362
        int baseLimit;          // index bound for initial table
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3363
        final int baseSize;     // initial table size
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3364
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3365
        Traverser(Node<K,V>[] tab, int size, int index, int limit) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3366
            this.tab = tab;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3367
            this.baseSize = size;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3368
            this.baseIndex = this.index = index;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3369
            this.baseLimit = limit;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3370
            this.next = null;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3371
        }
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3372
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3373
        /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3374
         * Advances if possible, returning next valid node, or null if none.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
         */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3376
        final Node<K,V> advance() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3377
            Node<K,V> e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3378
            if ((e = next) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3379
                e = e.next;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3380
            for (;;) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3381
                Node<K,V>[] t; int i, n;  // must use locals in checks
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3382
                if (e != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3383
                    return next = e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3384
                if (baseIndex >= baseLimit || (t = tab) == null ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3385
                    (n = t.length) <= (i = index) || i < 0)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3386
                    return next = null;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3387
                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
  3388
                    if (e instanceof ForwardingNode) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3389
                        tab = ((ForwardingNode<K,V>)e).nextTable;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3390
                        e = null;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3391
                        pushState(t, i, n);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3392
                        continue;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3393
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3394
                    else if (e instanceof TreeBin)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3395
                        e = ((TreeBin<K,V>)e).first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3396
                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3397
                        e = null;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3398
                }
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3399
                if (stack != null)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3400
                    recoverState(n);
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3401
                else if ((index = i + baseSize) >= n)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3402
                    index = ++baseIndex; // visit upper slots if present
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
        }
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3405
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3406
        /**
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3407
         * Saves traversal state upon encountering a forwarding node.
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3408
         */
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3409
        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
  3410
            TableStack<K,V> s = spare;  // reuse if possible
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3411
            if (s != null)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3412
                spare = s.next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3413
            else
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3414
                s = new TableStack<K,V>();
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3415
            s.tab = t;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3416
            s.length = n;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3417
            s.index = i;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3418
            s.next = stack;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3419
            stack = s;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3420
        }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3421
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3422
        /**
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3423
         * Possibly pops traversal state.
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3424
         *
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3425
         * @param n length of current table
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3426
         */
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3427
        private void recoverState(int n) {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3428
            TableStack<K,V> s; int len;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3429
            while ((s = stack) != null && (index += (len = s.length)) >= n) {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3430
                n = len;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3431
                index = s.index;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3432
                tab = s.tab;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3433
                s.tab = null;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3434
                TableStack<K,V> next = s.next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3435
                s.next = spare; // save for reuse
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3436
                stack = next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3437
                spare = s;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3438
            }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3439
            if (s == null && (index += baseSize) >= n)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3440
                index = ++baseIndex;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3441
        }
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3442
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3444
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3445
     * 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
  3446
     * Traverser to support iterator.remove.
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3447
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3448
    static class BaseIterator<K,V> extends Traverser<K,V> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3449
        final ConcurrentHashMap<K,V> map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3450
        Node<K,V> lastReturned;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3451
        BaseIterator(Node<K,V>[] tab, int size, int index, int limit,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3452
                    ConcurrentHashMap<K,V> map) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3453
            super(tab, size, index, limit);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3454
            this.map = map;
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
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3457
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3458
        public final boolean hasNext() { return next != null; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3459
        public final boolean hasMoreElements() { return next != null; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3460
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3461
        public final void remove() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3462
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3463
            if ((p = lastReturned) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3464
                throw new IllegalStateException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3465
            lastReturned = null;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3466
            map.replaceNode(p.key, null, null);
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
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3470
    static final class KeyIterator<K,V> extends BaseIterator<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3471
        implements Iterator<K>, Enumeration<K> {
44588
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  3472
        KeyIterator(Node<K,V>[] tab, int size, int index, int limit,
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3473
                    ConcurrentHashMap<K,V> map) {
44588
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  3474
            super(tab, size, index, limit, map);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3475
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3476
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3477
        public final K next() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3478
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3479
            if ((p = next) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3480
                throw new NoSuchElementException();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3481
            K k = p.key;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3482
            lastReturned = p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3483
            advance();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3484
            return k;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3486
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3487
        public final K nextElement() { return next(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3488
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3489
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3490
    static final class ValueIterator<K,V> extends BaseIterator<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3491
        implements Iterator<V>, Enumeration<V> {
44588
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  3492
        ValueIterator(Node<K,V>[] tab, int size, int index, int limit,
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3493
                      ConcurrentHashMap<K,V> map) {
44588
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  3494
            super(tab, size, index, limit, map);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3495
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3496
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3497
        public final V next() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3498
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3499
            if ((p = next) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3500
                throw new NoSuchElementException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3501
            V v = p.val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3502
            lastReturned = p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3503
            advance();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3504
            return v;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3505
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3506
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3507
        public final V nextElement() { return next(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3508
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3509
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3510
    static final class EntryIterator<K,V> extends BaseIterator<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3511
        implements Iterator<Map.Entry<K,V>> {
44588
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  3512
        EntryIterator(Node<K,V>[] tab, int size, int index, int limit,
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3513
                      ConcurrentHashMap<K,V> map) {
44588
fd9d4d384425 8176402: parameter name switcharoo in ConcurrentHashMap
dl
parents: 40547
diff changeset
  3514
            super(tab, size, index, limit, map);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3515
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3516
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3517
        public final Map.Entry<K,V> next() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3518
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3519
            if ((p = next) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3520
                throw new NoSuchElementException();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3521
            K k = p.key;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3522
            V v = p.val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3523
            lastReturned = p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3524
            advance();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3525
            return new MapEntry<K,V>(k, v, map);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3526
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3529
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3530
     * Exported Entry for EntryIterator.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3531
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3532
    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
  3533
        final K key; // non-null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3534
        V val;       // non-null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3535
        final ConcurrentHashMap<K,V> map;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3536
        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
  3537
            this.key = key;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3538
            this.val = val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3539
            this.map = map;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3540
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3541
        public K getKey()        { return key; }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3542
        public V getValue()      { return val; }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3543
        public int hashCode()    { return key.hashCode() ^ val.hashCode(); }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3544
        public String toString() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3545
            return Helpers.mapEntryToString(key, val);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3546
        }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3547
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3548
        public boolean equals(Object o) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3549
            Object k, v; Map.Entry<?,?> e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3550
            return ((o instanceof Map.Entry) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3551
                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3552
                    (v = e.getValue()) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3553
                    (k == key || k.equals(key)) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3554
                    (v == val || v.equals(val)));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3555
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3556
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3557
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3558
         * 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
  3559
         * 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
  3560
         * necessarily track asynchronous changes, the most recent
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3561
         * "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
  3562
         * 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
  3563
         * 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
  3564
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3565
        public V setValue(V value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3566
            if (value == null) throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3567
            V v = val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3568
            val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3569
            map.put(key, value);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3570
            return v;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3571
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3572
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3573
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3574
    static final class KeySpliterator<K,V> extends Traverser<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3575
        implements Spliterator<K> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3576
        long est;               // size estimate
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3577
        KeySpliterator(Node<K,V>[] tab, int size, int index, int limit,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3578
                       long est) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3579
            super(tab, size, index, limit);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3580
            this.est = est;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3581
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3582
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3583
        public KeySpliterator<K,V> trySplit() {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3584
            int i, f, h;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3585
            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3586
                new KeySpliterator<K,V>(tab, baseSize, baseLimit = h,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3587
                                        f, est >>>= 1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3588
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3589
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3590
        public void forEachRemaining(Consumer<? super K> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3591
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3592
            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
  3593
                action.accept(p.key);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3594
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3595
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3596
        public boolean tryAdvance(Consumer<? super K> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3597
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3598
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3599
            if ((p = advance()) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3600
                return false;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3601
            action.accept(p.key);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3602
            return true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3603
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3604
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3605
        public long estimateSize() { return est; }
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 int characteristics() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3608
            return Spliterator.DISTINCT | Spliterator.CONCURRENT |
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3609
                Spliterator.NONNULL;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3610
        }
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3611
    }
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3612
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3613
    static final class ValueSpliterator<K,V> extends Traverser<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3614
        implements Spliterator<V> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3615
        long est;               // size estimate
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3616
        ValueSpliterator(Node<K,V>[] tab, int size, int index, int limit,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3617
                         long est) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3618
            super(tab, size, index, limit);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3619
            this.est = est;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3620
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3621
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3622
        public ValueSpliterator<K,V> trySplit() {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3623
            int i, f, h;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3624
            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3625
                new ValueSpliterator<K,V>(tab, baseSize, baseLimit = h,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3626
                                          f, est >>>= 1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3627
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3628
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3629
        public void forEachRemaining(Consumer<? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3630
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3631
            for (Node<K,V> p; (p = advance()) != null;)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3632
                action.accept(p.val);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3633
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3634
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3635
        public boolean tryAdvance(Consumer<? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3636
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3637
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3638
            if ((p = advance()) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3639
                return false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3640
            action.accept(p.val);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3641
            return true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3642
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3643
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3644
        public long estimateSize() { return est; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3645
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3646
        public int characteristics() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3647
            return Spliterator.CONCURRENT | Spliterator.NONNULL;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3648
        }
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3649
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3650
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3651
    static final class EntrySpliterator<K,V> extends Traverser<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3652
        implements Spliterator<Map.Entry<K,V>> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3653
        final ConcurrentHashMap<K,V> map; // To export MapEntry
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3654
        long est;               // size estimate
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3655
        EntrySpliterator(Node<K,V>[] tab, int size, int index, int limit,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3656
                         long est, ConcurrentHashMap<K,V> map) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3657
            super(tab, size, index, limit);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3658
            this.map = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3659
            this.est = est;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3660
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3661
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3662
        public EntrySpliterator<K,V> trySplit() {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3663
            int i, f, h;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3664
            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3665
                new EntrySpliterator<K,V>(tab, baseSize, baseLimit = h,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3666
                                          f, est >>>= 1, map);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3667
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3668
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3669
        public void forEachRemaining(Consumer<? super Map.Entry<K,V>> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3670
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3671
            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
  3672
                action.accept(new MapEntry<K,V>(p.key, p.val, map));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3673
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3674
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3675
        public boolean tryAdvance(Consumer<? super Map.Entry<K,V>> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3676
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3677
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3678
            if ((p = advance()) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3679
                return false;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3680
            action.accept(new MapEntry<K,V>(p.key, p.val, map));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3681
            return true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3682
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3683
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3684
        public long estimateSize() { return est; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3685
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3686
        public int characteristics() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3687
            return Spliterator.DISTINCT | Spliterator.CONCURRENT |
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3688
                Spliterator.NONNULL;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3689
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3690
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3691
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3692
    // Parallel bulk operations
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3693
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3694
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3695
     * Computes initial batch value for bulk tasks. The returned value
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3696
     * is approximately exp2 of the number of times (minus one) to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3697
     * split task by two before executing leaf action. This value is
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3698
     * faster to compute and more convenient to use as a guide to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3699
     * splitting than is the depth, since it is used while dividing by
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3700
     * two anyway.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3701
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3702
    final int batchFor(long b) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3703
        long n;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3704
        if (b == Long.MAX_VALUE || (n = sumCount()) <= 1L || n < b)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3705
            return 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3706
        int sp = ForkJoinPool.getCommonPoolParallelism() << 2; // slack of 4
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3707
        return (b <= 0L || (n /= b) >= sp) ? sp : (int)n;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3708
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3709
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3710
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3711
     * Performs the given action for each (key, value).
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3712
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3713
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3714
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3715
     * @param action the action
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3716
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3717
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3718
    public void forEach(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3719
                        BiConsumer<? super K,? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3720
        if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3721
        new ForEachMappingTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3722
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3723
             action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3724
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3725
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3726
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3727
     * Performs the given action for each non-null transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3728
     * of each (key, value).
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3729
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3730
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3731
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3732
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3733
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3734
     * which case the action is not applied)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3735
     * @param action the action
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  3736
     * @param <U> the return type of the transformer
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3737
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3738
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3739
    public <U> void forEach(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3740
                            BiFunction<? super K, ? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3741
                            Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3742
        if (transformer == null || action == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3743
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3744
        new ForEachTransformedMappingTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3745
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3746
             transformer, action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3747
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3748
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3749
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3750
     * Returns a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3751
     * function on each (key, value), or null if none.  Upon
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3752
     * success, further element processing is suppressed and the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3753
     * results of any other parallel invocations of the search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3754
     * function are ignored.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3755
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3756
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3757
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3758
     * @param searchFunction a function returning a non-null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3759
     * result on success, else null
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  3760
     * @param <U> the return type of the search function
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3761
     * @return a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3762
     * 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
  3763
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3764
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3765
    public <U> U search(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3766
                        BiFunction<? super K, ? super V, ? extends U> searchFunction) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3767
        if (searchFunction == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3768
        return new SearchMappingsTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3769
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3770
             searchFunction, new AtomicReference<U>()).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3771
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3772
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3773
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3774
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3775
     * of all (key, value) pairs using the given reducer to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3776
     * combine values, or null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3777
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3778
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3779
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3780
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3781
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3782
     * which case it is not combined)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3783
     * @param reducer a commutative associative combining function
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  3784
     * @param <U> the return type of the transformer
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3785
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3786
     * of all (key, value) pairs
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3787
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3788
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3789
    public <U> U reduce(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3790
                        BiFunction<? super K, ? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3791
                        BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3792
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3793
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3794
        return new MapReduceMappingsTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3795
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3796
             null, transformer, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3797
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3798
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3799
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3800
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3801
     * of all (key, value) pairs using the given reducer to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3802
     * combine values, and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3803
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3804
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3805
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3806
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3807
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3808
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3809
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3810
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3811
     * of all (key, value) pairs
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3812
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3813
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3814
    public double reduceToDouble(long parallelismThreshold,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3815
                                 ToDoubleBiFunction<? super K, ? super V> transformer,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3816
                                 double basis,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3817
                                 DoubleBinaryOperator reducer) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3818
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3819
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3820
        return new MapReduceMappingsToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3821
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3822
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3823
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3824
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3825
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3826
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3827
     * of all (key, value) pairs using the given reducer to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3828
     * combine values, and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3829
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3830
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3831
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3832
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3833
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3834
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3835
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3836
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3837
     * of all (key, value) pairs
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3838
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3839
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3840
    public long reduceToLong(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3841
                             ToLongBiFunction<? super K, ? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3842
                             long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3843
                             LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3844
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3845
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3846
        return new MapReduceMappingsToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3847
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3848
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3849
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3850
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3851
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3852
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3853
     * of all (key, value) pairs using the given reducer to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3854
     * combine values, and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3855
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3856
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3857
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3858
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3859
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3860
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3861
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3862
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3863
     * of all (key, value) pairs
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3864
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3865
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3866
    public int reduceToInt(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3867
                           ToIntBiFunction<? super K, ? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3868
                           int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3869
                           IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3870
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3871
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3872
        return new MapReduceMappingsToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3873
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3874
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3875
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3876
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3877
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3878
     * Performs the given action for each key.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3879
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3880
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3881
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3882
     * @param action the action
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3883
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3884
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3885
    public void forEachKey(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3886
                           Consumer<? super K> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3887
        if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3888
        new ForEachKeyTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3889
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3890
             action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3891
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3892
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3893
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3894
     * Performs the given action for each non-null transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3895
     * of each key.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3896
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3897
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3898
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3899
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3900
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3901
     * which case the action is not applied)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3902
     * @param action the action
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  3903
     * @param <U> the return type of the transformer
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3904
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3905
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3906
    public <U> void forEachKey(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3907
                               Function<? super K, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3908
                               Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3909
        if (transformer == null || action == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3910
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3911
        new ForEachTransformedKeyTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3912
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3913
             transformer, action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3914
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3915
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3916
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3917
     * Returns a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3918
     * function on each key, or null if none. Upon success,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3919
     * further element processing is suppressed and the results of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3920
     * any other parallel invocations of the search function are
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3921
     * ignored.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3922
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3923
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3924
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3925
     * @param searchFunction a function returning a non-null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3926
     * result on success, else null
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  3927
     * @param <U> the return type of the search function
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3928
     * @return a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3929
     * function on each key, or null if none
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3930
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3931
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3932
    public <U> U searchKeys(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3933
                            Function<? super K, ? extends U> searchFunction) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3934
        if (searchFunction == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3935
        return new SearchKeysTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3936
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3937
             searchFunction, new AtomicReference<U>()).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3938
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3939
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3940
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3941
     * Returns the result of accumulating all keys using the given
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3942
     * reducer to combine values, or null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3943
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3944
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3945
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3946
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3947
     * @return the result of accumulating all keys using the given
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3948
     * reducer to combine values, or null if none
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3949
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3950
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3951
    public K reduceKeys(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3952
                        BiFunction<? super K, ? super K, ? extends K> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3953
        if (reducer == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3954
        return new ReduceKeysTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3955
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3956
             null, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3957
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3958
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3959
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3960
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3961
     * of all keys using the given reducer to combine values, or
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3962
     * null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3963
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3964
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3965
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3966
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3967
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3968
     * which case it is not combined)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3969
     * @param reducer a commutative associative combining function
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  3970
     * @param <U> the return type of the transformer
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3971
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3972
     * of all keys
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3973
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3974
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3975
    public <U> U reduceKeys(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3976
                            Function<? super K, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3977
         BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3978
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3979
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3980
        return new MapReduceKeysTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3981
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3982
             null, transformer, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3983
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3984
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3985
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3986
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3987
     * of all keys using the given reducer to combine values, and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3988
     * the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3989
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3990
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3991
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3992
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3993
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3994
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3995
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3996
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3997
     * of all keys
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3998
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3999
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4000
    public double reduceKeysToDouble(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4001
                                     ToDoubleFunction<? super K> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4002
                                     double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4003
                                     DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4004
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4005
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4006
        return new MapReduceKeysToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4007
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4008
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4009
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4010
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4011
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4012
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4013
     * of all keys using the given reducer to combine values, and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4014
     * the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4015
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4016
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4017
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4018
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4019
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4020
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4021
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4022
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4023
     * of all keys
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4024
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4025
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4026
    public long reduceKeysToLong(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4027
                                 ToLongFunction<? super K> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4028
                                 long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4029
                                 LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4030
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4031
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4032
        return new MapReduceKeysToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4033
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4034
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4035
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4036
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4037
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4038
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4039
     * of all keys using the given reducer to combine values, and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4040
     * the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4041
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4042
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4043
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4044
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4045
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4046
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4047
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4048
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4049
     * of all keys
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4050
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4051
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4052
    public int reduceKeysToInt(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4053
                               ToIntFunction<? super K> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4054
                               int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4055
                               IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4056
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4057
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4058
        return new MapReduceKeysToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4059
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4060
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4061
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4062
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4063
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4064
     * Performs the given action for each value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4065
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4066
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4067
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4068
     * @param action the action
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4069
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4070
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4071
    public void forEachValue(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4072
                             Consumer<? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4073
        if (action == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4074
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4075
        new ForEachValueTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4076
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4077
             action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4078
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4079
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4080
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4081
     * Performs the given action for each non-null transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4082
     * of each value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4083
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4084
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4085
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4086
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4087
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4088
     * which case the action is not applied)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4089
     * @param action the action
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  4090
     * @param <U> the return type of the transformer
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4091
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4092
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4093
    public <U> void forEachValue(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4094
                                 Function<? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4095
                                 Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4096
        if (transformer == null || action == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4097
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4098
        new ForEachTransformedValueTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4099
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4100
             transformer, action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4101
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4102
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4103
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4104
     * Returns a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4105
     * function on each value, or null if none.  Upon success,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4106
     * further element processing is suppressed and the results of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4107
     * any other parallel invocations of the search function are
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4108
     * ignored.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4109
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4110
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4111
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4112
     * @param searchFunction a function returning a non-null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4113
     * result on success, else null
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  4114
     * @param <U> the return type of the search function
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4115
     * @return a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4116
     * function on each value, or null if none
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4117
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4118
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4119
    public <U> U searchValues(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4120
                              Function<? super V, ? extends U> searchFunction) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4121
        if (searchFunction == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4122
        return new SearchValuesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4123
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4124
             searchFunction, new AtomicReference<U>()).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4125
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4126
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4127
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4128
     * Returns the result of accumulating all values using the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4129
     * given reducer to combine values, or null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4130
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4131
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4132
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4133
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4134
     * @return the result of accumulating all values
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4135
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4136
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4137
    public V reduceValues(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4138
                          BiFunction<? super V, ? super V, ? extends V> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4139
        if (reducer == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4140
        return new ReduceValuesTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4141
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4142
             null, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4143
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4144
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4145
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4146
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4147
     * of all values using the given reducer to combine values, or
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4148
     * null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4149
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4150
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4151
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4152
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4153
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4154
     * which case it is not combined)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4155
     * @param reducer a commutative associative combining function
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  4156
     * @param <U> the return type of the transformer
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4157
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4158
     * of all values
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4159
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4160
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4161
    public <U> U reduceValues(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4162
                              Function<? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4163
                              BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4164
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4165
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4166
        return new MapReduceValuesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4167
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4168
             null, transformer, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4169
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4170
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4171
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4172
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4173
     * of all values using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4174
     * and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4175
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4176
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4177
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4178
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4179
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4180
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4181
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4182
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4183
     * of all values
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4184
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4185
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4186
    public double reduceValuesToDouble(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4187
                                       ToDoubleFunction<? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4188
                                       double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4189
                                       DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4190
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4191
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4192
        return new MapReduceValuesToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4193
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4194
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4195
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4196
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4197
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4198
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4199
     * of all values using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4200
     * and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4201
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4202
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4203
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4204
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4205
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4206
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4207
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4208
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4209
     * of all values
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4210
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4211
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4212
    public long reduceValuesToLong(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4213
                                   ToLongFunction<? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4214
                                   long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4215
                                   LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4216
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4217
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4218
        return new MapReduceValuesToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4219
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4220
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4221
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4222
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4223
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4224
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4225
     * of all values using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4226
     * and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4227
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4228
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4229
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4230
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4231
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4232
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4233
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4234
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4235
     * of all values
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4236
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4237
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4238
    public int reduceValuesToInt(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4239
                                 ToIntFunction<? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4240
                                 int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4241
                                 IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4242
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4243
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4244
        return new MapReduceValuesToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4245
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4246
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4247
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4248
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4249
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4250
     * Performs the given action for each entry.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4251
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4252
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4253
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4254
     * @param action the action
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4255
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4256
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4257
    public void forEachEntry(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4258
                             Consumer<? super Map.Entry<K,V>> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4259
        if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4260
        new ForEachEntryTask<K,V>(null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4261
                                  action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4262
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4263
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4264
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4265
     * Performs the given action for each non-null transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4266
     * of each entry.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4267
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4268
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4269
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4270
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4271
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4272
     * which case the action is not applied)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4273
     * @param action the action
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  4274
     * @param <U> the return type of the transformer
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4275
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4276
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4277
    public <U> void forEachEntry(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4278
                                 Function<Map.Entry<K,V>, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4279
                                 Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4280
        if (transformer == null || action == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4281
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4282
        new ForEachTransformedEntryTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4283
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4284
             transformer, action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4285
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4286
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4287
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4288
     * Returns a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4289
     * function on each entry, or null if none.  Upon success,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4290
     * further element processing is suppressed and the results of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4291
     * any other parallel invocations of the search function are
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4292
     * ignored.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4293
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4294
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4295
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4296
     * @param searchFunction a function returning a non-null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4297
     * result on success, else null
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  4298
     * @param <U> the return type of the search function
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4299
     * @return a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4300
     * function on each entry, or null if none
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4301
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4302
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4303
    public <U> U searchEntries(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4304
                               Function<Map.Entry<K,V>, ? extends U> searchFunction) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4305
        if (searchFunction == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4306
        return new SearchEntriesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4307
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4308
             searchFunction, new AtomicReference<U>()).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4309
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4310
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4311
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4312
     * Returns the result of accumulating all entries using the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4313
     * given reducer to combine values, or null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4314
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4315
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4316
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4317
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4318
     * @return the result of accumulating all entries
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4319
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4320
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4321
    public Map.Entry<K,V> reduceEntries(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4322
                                        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
  4323
        if (reducer == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4324
        return new ReduceEntriesTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4325
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4326
             null, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4327
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4328
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4329
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4330
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4331
     * of all entries using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4332
     * or null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4333
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4334
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4335
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4336
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4337
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4338
     * which case it is not combined)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4339
     * @param reducer a commutative associative combining function
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  4340
     * @param <U> the return type of the transformer
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4341
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4342
     * of all entries
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4343
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4344
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4345
    public <U> U reduceEntries(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4346
                               Function<Map.Entry<K,V>, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4347
                               BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4348
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4349
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4350
        return new MapReduceEntriesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4351
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4352
             null, transformer, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4353
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4354
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4355
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4356
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4357
     * of all entries using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4358
     * and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4359
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4360
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4361
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4362
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4363
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4364
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4365
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4366
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4367
     * of all entries
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4368
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4369
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4370
    public double reduceEntriesToDouble(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4371
                                        ToDoubleFunction<Map.Entry<K,V>> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4372
                                        double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4373
                                        DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4374
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4375
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4376
        return new MapReduceEntriesToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4377
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4378
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4379
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4380
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4381
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4382
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4383
     * of all entries using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4384
     * and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4385
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4386
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4387
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4388
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4389
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4390
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4391
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4392
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4393
     * of all entries
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4394
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4395
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4396
    public long reduceEntriesToLong(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4397
                                    ToLongFunction<Map.Entry<K,V>> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4398
                                    long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4399
                                    LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4400
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4401
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4402
        return new MapReduceEntriesToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4403
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4404
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4405
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4406
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4407
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4408
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4409
     * of all entries using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4410
     * and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4411
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4412
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4413
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4414
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4415
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4416
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4417
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4418
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4419
     * of all entries
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4420
     * @since 1.8
17945
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 int reduceEntriesToInt(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4423
                                  ToIntFunction<Map.Entry<K,V>> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4424
                                  int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4425
                                  IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4426
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4427
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4428
        return new MapReduceEntriesToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4429
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4430
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4431
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4432
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4433
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4434
    /* ----------------Views -------------- */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4435
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4436
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4437
     * Base class for views.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4438
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4439
    abstract static class CollectionView<K,V,E>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4440
        implements Collection<E>, java.io.Serializable {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4441
        private static final long serialVersionUID = 7249069246763182397L;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4442
        final ConcurrentHashMap<K,V> map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4443
        CollectionView(ConcurrentHashMap<K,V> map)  { this.map = map; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4444
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4445
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4446
         * Returns the map backing this view.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4447
         *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4448
         * @return the map backing this view
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4449
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4450
        public ConcurrentHashMap<K,V> getMap() { return map; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4451
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4452
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4453
         * Removes all of the elements from this view, by removing all
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4454
         * the mappings from the map backing this view.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4455
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4456
        public final void clear()      { map.clear(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4457
        public final int size()        { return map.size(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4458
        public final boolean isEmpty() { return map.isEmpty(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4459
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4460
        // implementations below rely on concrete classes supplying these
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4461
        // abstract methods
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4462
        /**
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  4463
         * 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
  4464
         *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  4465
         * <p>The returned iterator is
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  4466
         * <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
  4467
         *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  4468
         * @return an iterator over the elements in this collection
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4469
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4470
        public abstract Iterator<E> iterator();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4471
        public abstract boolean contains(Object o);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4472
        public abstract boolean remove(Object o);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4473
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  4474
        private static final String OOME_MSG = "Required array size too large";
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4475
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4476
        public final Object[] toArray() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4477
            long sz = map.mappingCount();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4478
            if (sz > MAX_ARRAY_SIZE)
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  4479
                throw new OutOfMemoryError(OOME_MSG);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4480
            int n = (int)sz;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4481
            Object[] r = new Object[n];
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4482
            int i = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4483
            for (E e : this) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4484
                if (i == n) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4485
                    if (n >= MAX_ARRAY_SIZE)
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  4486
                        throw new OutOfMemoryError(OOME_MSG);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4487
                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4488
                        n = MAX_ARRAY_SIZE;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4489
                    else
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4490
                        n += (n >>> 1) + 1;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4491
                    r = Arrays.copyOf(r, n);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4492
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4493
                r[i++] = e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4494
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4495
            return (i == n) ? r : Arrays.copyOf(r, i);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4496
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4497
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4498
        @SuppressWarnings("unchecked")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4499
        public final <T> T[] toArray(T[] a) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4500
            long sz = map.mappingCount();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4501
            if (sz > MAX_ARRAY_SIZE)
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  4502
                throw new OutOfMemoryError(OOME_MSG);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4503
            int m = (int)sz;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4504
            T[] r = (a.length >= m) ? a :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4505
                (T[])java.lang.reflect.Array
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4506
                .newInstance(a.getClass().getComponentType(), m);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4507
            int n = r.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4508
            int i = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4509
            for (E e : this) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4510
                if (i == n) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4511
                    if (n >= MAX_ARRAY_SIZE)
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  4512
                        throw new OutOfMemoryError(OOME_MSG);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4513
                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4514
                        n = MAX_ARRAY_SIZE;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4515
                    else
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4516
                        n += (n >>> 1) + 1;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4517
                    r = Arrays.copyOf(r, n);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4518
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4519
                r[i++] = (T)e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4520
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4521
            if (a == r && i < n) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4522
                r[i] = null; // null-terminate
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4523
                return r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4524
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4525
            return (i == n) ? r : Arrays.copyOf(r, i);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4526
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4527
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4528
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4529
         * Returns a string representation of this collection.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4530
         * The string representation consists of the string representations
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4531
         * of the collection's elements in the order they are returned by
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4532
         * its iterator, enclosed in square brackets ({@code "[]"}).
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4533
         * Adjacent elements are separated by the characters {@code ", "}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4534
         * (comma and space).  Elements are converted to strings as by
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4535
         * {@link String#valueOf(Object)}.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4536
         *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4537
         * @return a string representation of this collection
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4538
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4539
        public final String toString() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4540
            StringBuilder sb = new StringBuilder();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4541
            sb.append('[');
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4542
            Iterator<E> it = iterator();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4543
            if (it.hasNext()) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4544
                for (;;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4545
                    Object e = it.next();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4546
                    sb.append(e == this ? "(this Collection)" : e);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4547
                    if (!it.hasNext())
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4548
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4549
                    sb.append(',').append(' ');
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4550
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4551
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4552
            return sb.append(']').toString();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4553
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4554
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4555
        public final boolean containsAll(Collection<?> c) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4556
            if (c != this) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4557
                for (Object e : c) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4558
                    if (e == null || !contains(e))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4559
                        return false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4560
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4561
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4562
            return true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4563
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4564
39779
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4565
        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
  4566
            if (c == null) throw new NullPointerException();
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4567
            boolean modified = false;
39779
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4568
            // Use (c instanceof Set) as a hint that lookup in c is as
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4569
            // efficient as this view
40547
51759e487197 8163353: NPE in ConcurrentHashMap.removeAll()
dl
parents: 39779
diff changeset
  4570
            Node<K,V>[] t;
51759e487197 8163353: NPE in ConcurrentHashMap.removeAll()
dl
parents: 39779
diff changeset
  4571
            if ((t = map.table) == null) {
51759e487197 8163353: NPE in ConcurrentHashMap.removeAll()
dl
parents: 39779
diff changeset
  4572
                return false;
51759e487197 8163353: NPE in ConcurrentHashMap.removeAll()
dl
parents: 39779
diff changeset
  4573
            } else if (c instanceof Set<?> && c.size() > t.length) {
39779
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4574
                for (Iterator<?> it = iterator(); it.hasNext(); ) {
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4575
                    if (c.contains(it.next())) {
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4576
                        it.remove();
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4577
                        modified = true;
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4578
                    }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4579
                }
39779
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4580
            } else {
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4581
                for (Object e : c)
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4582
                    modified |= remove(e);
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
            return modified;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4585
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4586
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4587
        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
  4588
            if (c == null) throw new NullPointerException();
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4589
            boolean modified = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4590
            for (Iterator<E> it = iterator(); it.hasNext();) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4591
                if (!c.contains(it.next())) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4592
                    it.remove();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4593
                    modified = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4594
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4595
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4596
            return modified;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4597
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4598
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4599
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4600
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4601
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4602
     * A view of a ConcurrentHashMap as a {@link Set} of keys, in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4603
     * which additions may optionally be enabled by mapping to a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4604
     * common value.  This class cannot be directly instantiated.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4605
     * See {@link #keySet() keySet()},
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4606
     * {@link #keySet(Object) keySet(V)},
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4607
     * {@link #newKeySet() newKeySet()},
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4608
     * {@link #newKeySet(int) newKeySet(int)}.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4609
     *
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4610
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4611
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4612
    public static class KeySetView<K,V> extends CollectionView<K,V,K>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4613
        implements Set<K>, java.io.Serializable {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4614
        private static final long serialVersionUID = 7249069246763182397L;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4615
        private final V value;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4616
        KeySetView(ConcurrentHashMap<K,V> map, V value) {  // non-public
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4617
            super(map);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4618
            this.value = value;
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
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4622
         * Returns the default mapped value for additions,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4623
         * or {@code null} if additions are not supported.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4624
         *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4625
         * @return the default mapped value for additions, or {@code null}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4626
         * if not supported
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4627
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4628
        public V getMappedValue() { return value; }
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
         * {@inheritDoc}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4632
         * @throws NullPointerException if the specified key is null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4633
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4634
        public boolean contains(Object o) { return map.containsKey(o); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4635
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4636
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4637
         * Removes the key from this map view, by removing the key (and its
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4638
         * corresponding value) from the backing map.  This method does
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4639
         * nothing if the key is not in the map.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4640
         *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4641
         * @param  o the key to be removed from the backing map
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4642
         * @return {@code true} if the backing map contained the specified key
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4643
         * @throws NullPointerException if the specified key is null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4644
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4645
        public boolean remove(Object o) { return map.remove(o) != null; }
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
         * @return an iterator over the keys of the backing map
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4649
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4650
        public Iterator<K> iterator() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4651
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4652
            ConcurrentHashMap<K,V> m = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4653
            int f = (t = m.table) == null ? 0 : t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4654
            return new KeyIterator<K,V>(t, f, 0, f, m);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4655
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4656
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4657
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4658
         * Adds the specified key to this set view by mapping the key to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4659
         * the default mapped value in the backing map, if defined.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4660
         *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4661
         * @param e key to be added
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4662
         * @return {@code true} if this set changed as a result of the call
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4663
         * @throws NullPointerException if the specified key is null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4664
         * @throws UnsupportedOperationException if no default mapped value
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4665
         * for additions was provided
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4666
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4667
        public boolean add(K e) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4668
            V v;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4669
            if ((v = value) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4670
                throw new UnsupportedOperationException();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4671
            return map.putVal(e, v, true) == null;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4672
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4673
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4674
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4675
         * Adds all of the elements in the specified collection to this set,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4676
         * as if by calling {@link #add} on each one.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4677
         *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4678
         * @param c the elements to be inserted into this set
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4679
         * @return {@code true} if this set changed as a result of the call
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4680
         * @throws NullPointerException if the collection or any of its
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4681
         * elements are {@code null}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4682
         * @throws UnsupportedOperationException if no default mapped value
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4683
         * for additions was provided
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4684
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4685
        public boolean addAll(Collection<? extends K> c) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4686
            boolean added = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4687
            V v;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4688
            if ((v = value) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4689
                throw new UnsupportedOperationException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4690
            for (K e : c) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4691
                if (map.putVal(e, v, true) == null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4692
                    added = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4693
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4694
            return added;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4695
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4696
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4697
        public int hashCode() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4698
            int h = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4699
            for (K e : this)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4700
                h += e.hashCode();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4701
            return h;
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
        public boolean equals(Object o) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4705
            Set<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4706
            return ((o instanceof Set) &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4707
                    ((c = (Set<?>)o) == this ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4708
                     (containsAll(c) && c.containsAll(this))));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4709
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4710
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4711
        public Spliterator<K> spliterator() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4712
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4713
            ConcurrentHashMap<K,V> m = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4714
            long n = m.sumCount();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4715
            int f = (t = m.table) == null ? 0 : t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4716
            return new KeySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4717
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4718
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4719
        public void forEach(Consumer<? super K> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4720
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4721
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4722
            if ((t = map.table) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4723
                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
  4724
                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
  4725
                    action.accept(p.key);
17945
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
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4729
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4730
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4731
     * A view of a ConcurrentHashMap as a {@link Collection} of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4732
     * values, in which additions are disabled. This class cannot be
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4733
     * directly instantiated. See {@link #values()}.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4734
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4735
    static final class ValuesView<K,V> extends CollectionView<K,V,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4736
        implements Collection<V>, java.io.Serializable {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4737
        private static final long serialVersionUID = 2249069246763182397L;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4738
        ValuesView(ConcurrentHashMap<K,V> map) { super(map); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4739
        public final boolean contains(Object o) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4740
            return map.containsValue(o);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4741
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4742
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4743
        public final boolean remove(Object o) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4744
            if (o != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4745
                for (Iterator<V> it = iterator(); it.hasNext();) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4746
                    if (o.equals(it.next())) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4747
                        it.remove();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4748
                        return true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4749
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4750
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4751
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4752
            return false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4753
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4754
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4755
        public final Iterator<V> iterator() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4756
            ConcurrentHashMap<K,V> m = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4757
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4758
            int f = (t = m.table) == null ? 0 : t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4759
            return new ValueIterator<K,V>(t, f, 0, f, m);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4760
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4761
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4762
        public final boolean add(V e) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4763
            throw new UnsupportedOperationException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4764
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4765
        public final boolean addAll(Collection<? extends V> c) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4766
            throw new UnsupportedOperationException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4767
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4768
39779
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4769
        @Override public boolean removeAll(Collection<?> c) {
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4770
            if (c == null) throw new NullPointerException();
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4771
            boolean modified = false;
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4772
            for (Iterator<V> it = iterator(); it.hasNext();) {
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4773
                if (c.contains(it.next())) {
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4774
                    it.remove();
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4775
                    modified = true;
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4776
                }
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4777
            }
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4778
            return modified;
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4779
        }
4666307d3155 8160751: Optimize ConcurrentHashMap.keySet().removeAll
dl
parents: 39725
diff changeset
  4780
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  4781
        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
  4782
            return map.removeValueIf(filter);
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  4783
        }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  4784
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4785
        public Spliterator<V> spliterator() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4786
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4787
            ConcurrentHashMap<K,V> m = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4788
            long n = m.sumCount();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4789
            int f = (t = m.table) == null ? 0 : t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4790
            return new ValueSpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4791
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4792
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4793
        public void forEach(Consumer<? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4794
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4795
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4796
            if ((t = map.table) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4797
                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
  4798
                for (Node<K,V> p; (p = it.advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4799
                    action.accept(p.val);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4800
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4801
        }
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
     * A view of a ConcurrentHashMap as a {@link Set} of (key, value)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4806
     * entries.  This class cannot be directly instantiated. See
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4807
     * {@link #entrySet()}.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4808
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4809
    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
  4810
        implements Set<Map.Entry<K,V>>, java.io.Serializable {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4811
        private static final long serialVersionUID = 2249069246763182397L;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4812
        EntrySetView(ConcurrentHashMap<K,V> map) { super(map); }
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 contains(Object o) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4815
            Object k, v, r; Map.Entry<?,?> e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4816
            return ((o instanceof Map.Entry) &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4817
                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4818
                    (r = map.get(k)) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4819
                    (v = e.getValue()) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4820
                    (v == r || v.equals(r)));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4821
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4822
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4823
        public boolean remove(Object o) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4824
            Object k, v; Map.Entry<?,?> e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4825
            return ((o instanceof Map.Entry) &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4826
                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4827
                    (v = e.getValue()) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4828
                    map.remove(k, v));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4829
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4830
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4831
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4832
         * @return an iterator over the entries of the backing map
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4833
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4834
        public Iterator<Map.Entry<K,V>> iterator() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4835
            ConcurrentHashMap<K,V> m = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4836
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4837
            int f = (t = m.table) == null ? 0 : t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4838
            return new EntryIterator<K,V>(t, f, 0, f, m);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4839
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4840
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4841
        public boolean add(Entry<K,V> e) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4842
            return map.putVal(e.getKey(), e.getValue(), false) == null;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4843
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4844
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4845
        public boolean addAll(Collection<? extends Entry<K,V>> c) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4846
            boolean added = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4847
            for (Entry<K,V> e : c) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4848
                if (add(e))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4849
                    added = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4850
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4851
            return added;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4852
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4853
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  4854
        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
  4855
            return map.removeEntryIf(filter);
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  4856
        }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  4857
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4858
        public final int hashCode() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4859
            int h = 0;
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; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4864
                    h += p.hashCode();
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
            return h;
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
        public final boolean equals(Object o) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4871
            Set<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4872
            return ((o instanceof Set) &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4873
                    ((c = (Set<?>)o) == this ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4874
                     (containsAll(c) && c.containsAll(this))));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4875
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4876
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4877
        public Spliterator<Map.Entry<K,V>> spliterator() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4878
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4879
            ConcurrentHashMap<K,V> m = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4880
            long n = m.sumCount();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4881
            int f = (t = m.table) == null ? 0 : t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4882
            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
  4883
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4884
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4885
        public void forEach(Consumer<? super Map.Entry<K,V>> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4886
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4887
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4888
            if ((t = map.table) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4889
                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
  4890
                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
  4891
                    action.accept(new MapEntry<K,V>(p.key, p.val, map));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4892
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4893
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4894
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4895
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4896
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4897
    // -------------------------------------------------------
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
     * Base class for bulk tasks. Repeats some fields and code from
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4901
     * class Traverser, because we need to subclass CountedCompleter.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4902
     */
19380
22eefc44fad6 8022724: lint warnings in j.u.concurrent packages
dl
parents: 19036
diff changeset
  4903
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4904
    abstract static class BulkTask<K,V,R> extends CountedCompleter<R> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4905
        Node<K,V>[] tab;        // same as Traverser
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4906
        Node<K,V> next;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4907
        TableStack<K,V> stack, spare;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4908
        int index;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4909
        int baseIndex;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4910
        int baseLimit;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4911
        final int baseSize;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4912
        int batch;              // split control
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4913
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4914
        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
  4915
            super(par);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4916
            this.batch = b;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4917
            this.index = this.baseIndex = i;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4918
            if ((this.tab = t) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4919
                this.baseSize = this.baseLimit = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4920
            else if (par == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4921
                this.baseSize = this.baseLimit = t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4922
            else {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4923
                this.baseLimit = f;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4924
                this.baseSize = par.baseSize;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4925
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4926
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4927
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4928
        /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  4929
         * Same as Traverser version.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4930
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4931
        final Node<K,V> advance() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4932
            Node<K,V> e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4933
            if ((e = next) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4934
                e = e.next;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4935
            for (;;) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4936
                Node<K,V>[] t; int i, n;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4937
                if (e != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4938
                    return next = e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4939
                if (baseIndex >= baseLimit || (t = tab) == null ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4940
                    (n = t.length) <= (i = index) || i < 0)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4941
                    return next = null;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4942
                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
  4943
                    if (e instanceof ForwardingNode) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4944
                        tab = ((ForwardingNode<K,V>)e).nextTable;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4945
                        e = null;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4946
                        pushState(t, i, n);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4947
                        continue;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4948
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4949
                    else if (e instanceof TreeBin)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4950
                        e = ((TreeBin<K,V>)e).first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4951
                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4952
                        e = null;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4953
                }
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4954
                if (stack != null)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4955
                    recoverState(n);
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4956
                else if ((index = i + baseSize) >= n)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4957
                    index = ++baseIndex;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  4958
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4959
        }
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4960
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4961
        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
  4962
            TableStack<K,V> s = spare;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4963
            if (s != null)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4964
                spare = s.next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4965
            else
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4966
                s = new TableStack<K,V>();
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4967
            s.tab = t;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4968
            s.length = n;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4969
            s.index = i;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4970
            s.next = stack;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4971
            stack = s;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4972
        }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4973
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4974
        private void recoverState(int n) {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4975
            TableStack<K,V> s; int len;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4976
            while ((s = stack) != null && (index += (len = s.length)) >= n) {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4977
                n = len;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4978
                index = s.index;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4979
                tab = s.tab;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4980
                s.tab = null;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4981
                TableStack<K,V> next = s.next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4982
                s.next = spare; // save for reuse
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4983
                stack = next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4984
                spare = s;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4985
            }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4986
            if (s == null && (index += baseSize) >= n)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4987
                index = ++baseIndex;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4988
        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4989
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4990
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4991
    /*
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4992
     * Task classes. Coded in a regular but ugly format/style to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4993
     * simplify checks that each variant differs in the right way from
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4994
     * others. The null screenings exist because compilers cannot tell
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4995
     * that we've already null-checked task arguments, so we force
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4996
     * simplest hoisted bypass to help avoid convoluted traps.
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 ForEachKeyTask<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 K> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5002
        ForEachKeyTask
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 K> 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 K> 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 ForEachKeyTask<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;)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5019
                    action.accept(p.key);
17945
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 ForEachValueTask<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 V> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5029
        ForEachValueTask
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 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 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 ForEachValueTask<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.val);
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 ForEachEntryTask<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 Consumer<? super Entry<K,V>> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5056
        ForEachEntryTask
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
             Consumer<? super Entry<K,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 Consumer<? super Entry<K,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 ForEachEntryTask<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; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5073
                    action.accept(p);
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 ForEachMappingTask<K,V>
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 BiConsumer<? super K, ? super V> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5083
        ForEachMappingTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5084
            (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
  5085
             BiConsumer<? super K,? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5086
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5087
            this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5088
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5089
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5090
            final BiConsumer<? super K, ? super V> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5091
            if ((action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5092
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5093
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5094
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5095
                    new ForEachMappingTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5096
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5097
                         action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5098
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5099
                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
  5100
                    action.accept(p.key, p.val);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5101
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5102
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5103
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5104
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5105
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5106
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5107
    static final class ForEachTransformedKeyTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5108
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5109
        final Function<? super K, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5110
        final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5111
        ForEachTransformedKeyTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5112
            (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
  5113
             Function<? super K, ? extends U> transformer, Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5114
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5115
            this.transformer = transformer; this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5116
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5117
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5118
            final Function<? super K, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5119
            final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5120
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5121
                (action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5122
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5123
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5124
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5125
                    new ForEachTransformedKeyTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5126
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5127
                         transformer, action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5128
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5129
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5130
                    U u;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5131
                    if ((u = transformer.apply(p.key)) != null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5132
                        action.accept(u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5133
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5134
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5135
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5136
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5137
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5138
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5139
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5140
    static final class ForEachTransformedValueTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5141
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5142
        final Function<? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5143
        final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5144
        ForEachTransformedValueTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5145
            (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
  5146
             Function<? super V, ? extends U> transformer, Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5147
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5148
            this.transformer = transformer; this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5149
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5150
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5151
            final Function<? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5152
            final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5153
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5154
                (action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5155
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5156
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5157
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5158
                    new ForEachTransformedValueTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5159
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5160
                         transformer, action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5161
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5162
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5163
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5164
                    if ((u = transformer.apply(p.val)) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5165
                        action.accept(u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5166
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5167
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5168
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5169
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5170
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5171
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5172
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5173
    static final class ForEachTransformedEntryTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5174
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5175
        final Function<Map.Entry<K,V>, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5176
        final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5177
        ForEachTransformedEntryTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5178
            (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
  5179
             Function<Map.Entry<K,V>, ? extends U> transformer, Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5180
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5181
            this.transformer = transformer; this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5182
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5183
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5184
            final Function<Map.Entry<K,V>, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5185
            final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5186
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5187
                (action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5188
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5189
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5190
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5191
                    new ForEachTransformedEntryTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5192
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5193
                         transformer, action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5194
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5195
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5196
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5197
                    if ((u = transformer.apply(p)) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5198
                        action.accept(u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5199
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5200
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5201
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5202
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5203
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5204
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5205
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5206
    static final class ForEachTransformedMappingTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5207
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5208
        final BiFunction<? super K, ? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5209
        final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5210
        ForEachTransformedMappingTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5211
            (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
  5212
             BiFunction<? super K, ? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5213
             Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5214
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5215
            this.transformer = transformer; this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5216
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5217
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5218
            final BiFunction<? super K, ? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5219
            final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5220
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5221
                (action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5222
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5223
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5224
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5225
                    new ForEachTransformedMappingTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5226
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5227
                         transformer, action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5228
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5229
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5230
                    U u;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5231
                    if ((u = transformer.apply(p.key, p.val)) != null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5232
                        action.accept(u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5233
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5234
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5235
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5236
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5237
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5238
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5239
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5240
    static final class SearchKeysTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5241
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5242
        final Function<? super K, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5243
        final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5244
        SearchKeysTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5245
            (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
  5246
             Function<? super K, ? extends U> searchFunction,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5247
             AtomicReference<U> result) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5248
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5249
            this.searchFunction = searchFunction; this.result = result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5250
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5251
        public final U getRawResult() { return result.get(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5252
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5253
            final Function<? super K, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5254
            final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5255
            if ((searchFunction = this.searchFunction) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5256
                (result = this.result) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5257
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5258
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5259
                    if (result.get() != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5260
                        return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5261
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5262
                    new SearchKeysTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5263
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5264
                         searchFunction, result).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5265
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5266
                while (result.get() == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5267
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5268
                    Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5269
                    if ((p = advance()) == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5270
                        propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5271
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5272
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5273
                    if ((u = searchFunction.apply(p.key)) != null) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5274
                        if (result.compareAndSet(null, u))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5275
                            quietlyCompleteRoot();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5276
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5277
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5278
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5279
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5280
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5281
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5282
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5283
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5284
    static final class SearchValuesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5285
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5286
        final Function<? super V, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5287
        final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5288
        SearchValuesTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5289
            (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
  5290
             Function<? super V, ? extends U> searchFunction,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5291
             AtomicReference<U> result) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5292
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5293
            this.searchFunction = searchFunction; this.result = result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5294
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5295
        public final U getRawResult() { return result.get(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5296
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5297
            final Function<? super V, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5298
            final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5299
            if ((searchFunction = this.searchFunction) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5300
                (result = this.result) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5301
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5302
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5303
                    if (result.get() != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5304
                        return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5305
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5306
                    new SearchValuesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5307
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5308
                         searchFunction, result).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5309
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5310
                while (result.get() == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5311
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5312
                    Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5313
                    if ((p = advance()) == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5314
                        propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5315
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5316
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5317
                    if ((u = searchFunction.apply(p.val)) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5318
                        if (result.compareAndSet(null, u))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5319
                            quietlyCompleteRoot();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5320
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5321
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5322
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5323
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5324
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5325
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5326
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5327
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5328
    static final class SearchEntriesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5329
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5330
        final Function<Entry<K,V>, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5331
        final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5332
        SearchEntriesTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5333
            (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
  5334
             Function<Entry<K,V>, ? extends U> searchFunction,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5335
             AtomicReference<U> result) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5336
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5337
            this.searchFunction = searchFunction; this.result = result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5338
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5339
        public final U getRawResult() { return result.get(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5340
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5341
            final Function<Entry<K,V>, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5342
            final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5343
            if ((searchFunction = this.searchFunction) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5344
                (result = this.result) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5345
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5346
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5347
                    if (result.get() != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5348
                        return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5349
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5350
                    new SearchEntriesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5351
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5352
                         searchFunction, result).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5353
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5354
                while (result.get() == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5355
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5356
                    Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5357
                    if ((p = advance()) == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5358
                        propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5359
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5360
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5361
                    if ((u = searchFunction.apply(p)) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5362
                        if (result.compareAndSet(null, u))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5363
                            quietlyCompleteRoot();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5364
                        return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5365
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5366
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5367
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5368
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5369
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5370
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5371
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5372
    static final class SearchMappingsTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5373
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5374
        final BiFunction<? super K, ? super V, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5375
        final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5376
        SearchMappingsTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5377
            (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
  5378
             BiFunction<? super K, ? super V, ? extends U> searchFunction,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5379
             AtomicReference<U> result) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5380
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5381
            this.searchFunction = searchFunction; this.result = result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5382
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5383
        public final U getRawResult() { return result.get(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5384
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5385
            final BiFunction<? super K, ? super V, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5386
            final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5387
            if ((searchFunction = this.searchFunction) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5388
                (result = this.result) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5389
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5390
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5391
                    if (result.get() != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5392
                        return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5393
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5394
                    new SearchMappingsTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5395
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5396
                         searchFunction, result).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5397
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5398
                while (result.get() == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5399
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5400
                    Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5401
                    if ((p = advance()) == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5402
                        propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5403
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5404
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5405
                    if ((u = searchFunction.apply(p.key, p.val)) != null) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5406
                        if (result.compareAndSet(null, u))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5407
                            quietlyCompleteRoot();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5408
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5409
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5410
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5411
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5412
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5413
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5414
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5415
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5416
    static final class ReduceKeysTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5417
        extends BulkTask<K,V,K> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5418
        final BiFunction<? super K, ? super K, ? extends K> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5419
        K result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5420
        ReduceKeysTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5421
        ReduceKeysTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5422
            (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
  5423
             ReduceKeysTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5424
             BiFunction<? super K, ? super K, ? extends K> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5425
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5426
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5427
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5428
        public final K getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5429
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5430
            final BiFunction<? super K, ? super K, ? extends K> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5431
            if ((reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5432
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5433
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5434
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5435
                    (rights = new ReduceKeysTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5436
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5437
                      rights, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5438
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5439
                K r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5440
                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
  5441
                    K u = p.key;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5442
                    r = (r == null) ? u : u == null ? r : reducer.apply(r, u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5443
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5444
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5445
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5446
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5447
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5448
                    ReduceKeysTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5449
                        t = (ReduceKeysTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5450
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5451
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5452
                        K tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5453
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5454
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5455
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5456
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5457
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5458
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5459
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5460
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5461
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5462
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5463
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5464
    static final class ReduceValuesTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5465
        extends BulkTask<K,V,V> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5466
        final BiFunction<? super V, ? super V, ? extends V> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5467
        V result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5468
        ReduceValuesTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5469
        ReduceValuesTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5470
            (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
  5471
             ReduceValuesTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5472
             BiFunction<? super V, ? super V, ? extends V> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5473
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5474
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5475
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5476
        public final V getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5477
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5478
            final BiFunction<? super V, ? super V, ? extends V> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5479
            if ((reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5480
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5481
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5482
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5483
                    (rights = new ReduceValuesTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5484
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5485
                      rights, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5486
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5487
                V r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5488
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5489
                    V v = p.val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5490
                    r = (r == null) ? v : reducer.apply(r, v);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5491
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5492
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5493
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5494
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5495
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5496
                    ReduceValuesTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5497
                        t = (ReduceValuesTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5498
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5499
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5500
                        V tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5501
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5502
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5503
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5504
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5505
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5506
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5507
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5508
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5509
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5510
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5511
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5512
    static final class ReduceEntriesTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5513
        extends BulkTask<K,V,Map.Entry<K,V>> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5514
        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
  5515
        Map.Entry<K,V> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5516
        ReduceEntriesTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5517
        ReduceEntriesTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5518
            (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
  5519
             ReduceEntriesTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5520
             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
  5521
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5522
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5523
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5524
        public final Map.Entry<K,V> getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5525
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5526
            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
  5527
            if ((reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5528
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5529
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5530
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5531
                    (rights = new ReduceEntriesTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5532
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5533
                      rights, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5534
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5535
                Map.Entry<K,V> r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5536
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5537
                    r = (r == null) ? p : reducer.apply(r, p);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5538
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5539
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5540
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5541
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5542
                    ReduceEntriesTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5543
                        t = (ReduceEntriesTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5544
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5545
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5546
                        Map.Entry<K,V> tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5547
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5548
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5549
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5550
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5551
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5552
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5553
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5554
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5555
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5556
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5557
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5558
    static final class MapReduceKeysTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5559
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5560
        final Function<? super K, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5561
        final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5562
        U result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5563
        MapReduceKeysTask<K,V,U> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5564
        MapReduceKeysTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5565
            (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
  5566
             MapReduceKeysTask<K,V,U> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5567
             Function<? super K, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5568
             BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5569
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5570
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5571
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5572
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5573
        public final U getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5574
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5575
            final Function<? super K, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5576
            final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5577
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5578
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5579
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5580
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5581
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5582
                    (rights = new MapReduceKeysTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5583
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5584
                      rights, transformer, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5585
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5586
                U r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5587
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5588
                    U u;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5589
                    if ((u = transformer.apply(p.key)) != null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5590
                        r = (r == null) ? u : reducer.apply(r, u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5591
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5592
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5593
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5594
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5595
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5596
                    MapReduceKeysTask<K,V,U>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5597
                        t = (MapReduceKeysTask<K,V,U>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5598
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5599
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5600
                        U tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5601
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5602
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5603
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5604
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5605
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5606
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5607
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5608
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5609
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5610
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5611
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5612
    static final class MapReduceValuesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5613
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5614
        final Function<? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5615
        final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5616
        U result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5617
        MapReduceValuesTask<K,V,U> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5618
        MapReduceValuesTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5619
            (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
  5620
             MapReduceValuesTask<K,V,U> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5621
             Function<? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5622
             BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5623
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5624
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5625
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5626
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5627
        public final U getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5628
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5629
            final Function<? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5630
            final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5631
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5632
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5633
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5634
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5635
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5636
                    (rights = new MapReduceValuesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5637
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5638
                      rights, transformer, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5639
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5640
                U r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5641
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5642
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5643
                    if ((u = transformer.apply(p.val)) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5644
                        r = (r == null) ? u : reducer.apply(r, u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5645
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5646
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5647
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5648
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5649
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5650
                    MapReduceValuesTask<K,V,U>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5651
                        t = (MapReduceValuesTask<K,V,U>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5652
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5653
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5654
                        U tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5655
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5656
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5657
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5658
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5659
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5660
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5661
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5662
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5663
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5664
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5665
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5666
    static final class MapReduceEntriesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5667
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5668
        final Function<Map.Entry<K,V>, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5669
        final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5670
        U result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5671
        MapReduceEntriesTask<K,V,U> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5672
        MapReduceEntriesTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5673
            (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
  5674
             MapReduceEntriesTask<K,V,U> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5675
             Function<Map.Entry<K,V>, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5676
             BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5677
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5678
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5679
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5680
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5681
        public final U getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5682
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5683
            final Function<Map.Entry<K,V>, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5684
            final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5685
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5686
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5687
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5688
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5689
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5690
                    (rights = new MapReduceEntriesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5691
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5692
                      rights, transformer, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5693
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5694
                U r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5695
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5696
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5697
                    if ((u = transformer.apply(p)) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5698
                        r = (r == null) ? u : reducer.apply(r, u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5699
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5700
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5701
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5702
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5703
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5704
                    MapReduceEntriesTask<K,V,U>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5705
                        t = (MapReduceEntriesTask<K,V,U>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5706
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5707
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5708
                        U tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5709
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5710
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5711
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5712
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5713
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5714
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5715
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5716
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5717
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5718
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5719
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5720
    static final class MapReduceMappingsTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5721
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5722
        final BiFunction<? super K, ? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5723
        final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5724
        U result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5725
        MapReduceMappingsTask<K,V,U> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5726
        MapReduceMappingsTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5727
            (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
  5728
             MapReduceMappingsTask<K,V,U> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5729
             BiFunction<? super K, ? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5730
             BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5731
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5732
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5733
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5734
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5735
        public final U getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5736
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5737
            final BiFunction<? super K, ? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5738
            final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5739
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5740
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5741
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5742
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5743
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5744
                    (rights = new MapReduceMappingsTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5745
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5746
                      rights, transformer, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5747
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5748
                U r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5749
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5750
                    U u;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5751
                    if ((u = transformer.apply(p.key, p.val)) != null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5752
                        r = (r == null) ? u : reducer.apply(r, u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5753
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5754
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5755
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5756
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5757
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5758
                    MapReduceMappingsTask<K,V,U>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5759
                        t = (MapReduceMappingsTask<K,V,U>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5760
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5761
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5762
                        U tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5763
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5764
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5765
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5766
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5767
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5768
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5769
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5770
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5771
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5772
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5773
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5774
    static final class MapReduceKeysToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5775
        extends BulkTask<K,V,Double> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5776
        final ToDoubleFunction<? super K> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5777
        final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5778
        final double basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5779
        double result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5780
        MapReduceKeysToDoubleTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5781
        MapReduceKeysToDoubleTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5782
            (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
  5783
             MapReduceKeysToDoubleTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5784
             ToDoubleFunction<? super K> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5785
             double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5786
             DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5787
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5788
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5789
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5790
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5791
        public final Double getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5792
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5793
            final ToDoubleFunction<? super K> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5794
            final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5795
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5796
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5797
                double r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5798
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5799
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5800
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5801
                    (rights = new MapReduceKeysToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5802
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5803
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5804
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5805
                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
  5806
                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5807
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5808
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5809
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5810
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5811
                    MapReduceKeysToDoubleTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5812
                        t = (MapReduceKeysToDoubleTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5813
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5814
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5815
                        t.result = reducer.applyAsDouble(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5816
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5817
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5818
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5819
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5820
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5821
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5822
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5823
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5824
    static final class MapReduceValuesToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5825
        extends BulkTask<K,V,Double> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5826
        final ToDoubleFunction<? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5827
        final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5828
        final double basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5829
        double result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5830
        MapReduceValuesToDoubleTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5831
        MapReduceValuesToDoubleTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5832
            (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
  5833
             MapReduceValuesToDoubleTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5834
             ToDoubleFunction<? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5835
             double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5836
             DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5837
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5838
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5839
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5840
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5841
        public final Double getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5842
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5843
            final ToDoubleFunction<? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5844
            final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5845
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5846
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5847
                double r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5848
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5849
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5850
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5851
                    (rights = new MapReduceValuesToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5852
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5853
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5854
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5855
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5856
                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.val));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5857
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5858
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5859
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5860
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5861
                    MapReduceValuesToDoubleTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5862
                        t = (MapReduceValuesToDoubleTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5863
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5864
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5865
                        t.result = reducer.applyAsDouble(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5866
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5867
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5868
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5869
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5870
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5871
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5872
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5873
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5874
    static final class MapReduceEntriesToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5875
        extends BulkTask<K,V,Double> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5876
        final ToDoubleFunction<Map.Entry<K,V>> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5877
        final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5878
        final double basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5879
        double result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5880
        MapReduceEntriesToDoubleTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5881
        MapReduceEntriesToDoubleTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5882
            (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
  5883
             MapReduceEntriesToDoubleTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5884
             ToDoubleFunction<Map.Entry<K,V>> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5885
             double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5886
             DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5887
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5888
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5889
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5890
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5891
        public final Double getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5892
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5893
            final ToDoubleFunction<Map.Entry<K,V>> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5894
            final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5895
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5896
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5897
                double r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5898
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5899
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5900
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5901
                    (rights = new MapReduceEntriesToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5902
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5903
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5904
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5905
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5906
                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5907
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5908
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5909
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5910
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5911
                    MapReduceEntriesToDoubleTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5912
                        t = (MapReduceEntriesToDoubleTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5913
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5914
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5915
                        t.result = reducer.applyAsDouble(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5916
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5917
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5918
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5919
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5920
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5921
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5922
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5923
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5924
    static final class MapReduceMappingsToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5925
        extends BulkTask<K,V,Double> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5926
        final ToDoubleBiFunction<? super K, ? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5927
        final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5928
        final double basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5929
        double result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5930
        MapReduceMappingsToDoubleTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5931
        MapReduceMappingsToDoubleTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5932
            (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
  5933
             MapReduceMappingsToDoubleTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5934
             ToDoubleBiFunction<? super K, ? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5935
             double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5936
             DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5937
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5938
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5939
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5940
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5941
        public final Double getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5942
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5943
            final ToDoubleBiFunction<? super K, ? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5944
            final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5945
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5946
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5947
                double r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5948
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5949
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5950
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5951
                    (rights = new MapReduceMappingsToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5952
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5953
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5954
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5955
                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
  5956
                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key, p.val));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5957
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5958
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5959
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5960
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5961
                    MapReduceMappingsToDoubleTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5962
                        t = (MapReduceMappingsToDoubleTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5963
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5964
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5965
                        t.result = reducer.applyAsDouble(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5966
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5967
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5968
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5969
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5970
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5971
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5972
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5973
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5974
    static final class MapReduceKeysToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5975
        extends BulkTask<K,V,Long> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5976
        final ToLongFunction<? super K> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5977
        final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5978
        final long basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5979
        long result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5980
        MapReduceKeysToLongTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5981
        MapReduceKeysToLongTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5982
            (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
  5983
             MapReduceKeysToLongTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5984
             ToLongFunction<? super K> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5985
             long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5986
             LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5987
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5988
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5989
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5990
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5991
        public final Long getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5992
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5993
            final ToLongFunction<? super K> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5994
            final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5995
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5996
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5997
                long r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5998
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5999
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6000
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6001
                    (rights = new MapReduceKeysToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6002
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6003
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6004
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6005
                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
  6006
                    r = reducer.applyAsLong(r, transformer.applyAsLong(p.key));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6007
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6008
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6009
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6010
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6011
                    MapReduceKeysToLongTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6012
                        t = (MapReduceKeysToLongTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6013
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6014
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6015
                        t.result = reducer.applyAsLong(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6016
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6017
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6018
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6019
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6020
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6021
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6022
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6023
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6024
    static final class MapReduceValuesToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6025
        extends BulkTask<K,V,Long> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6026
        final ToLongFunction<? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6027
        final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6028
        final long basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6029
        long result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6030
        MapReduceValuesToLongTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6031
        MapReduceValuesToLongTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6032
            (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
  6033
             MapReduceValuesToLongTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6034
             ToLongFunction<? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6035
             long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6036
             LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6037
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6038
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6039
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6040
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6041
        public final Long getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6042
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6043
            final ToLongFunction<? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6044
            final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6045
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6046
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6047
                long r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6048
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6049
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6050
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6051
                    (rights = new MapReduceValuesToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6052
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6053
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6054
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6055
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6056
                    r = reducer.applyAsLong(r, transformer.applyAsLong(p.val));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6057
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6058
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6059
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6060
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6061
                    MapReduceValuesToLongTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6062
                        t = (MapReduceValuesToLongTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6063
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6064
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6065
                        t.result = reducer.applyAsLong(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6066
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6067
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6068
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6069
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6070
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6071
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6072
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6073
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6074
    static final class MapReduceEntriesToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6075
        extends BulkTask<K,V,Long> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6076
        final ToLongFunction<Map.Entry<K,V>> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6077
        final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6078
        final long basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6079
        long result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6080
        MapReduceEntriesToLongTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6081
        MapReduceEntriesToLongTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6082
            (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
  6083
             MapReduceEntriesToLongTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6084
             ToLongFunction<Map.Entry<K,V>> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6085
             long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6086
             LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6087
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6088
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6089
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6090
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6091
        public final Long getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6092
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6093
            final ToLongFunction<Map.Entry<K,V>> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6094
            final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6095
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6096
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6097
                long r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6098
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6099
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6100
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6101
                    (rights = new MapReduceEntriesToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6102
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6103
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6104
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6105
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6106
                    r = reducer.applyAsLong(r, transformer.applyAsLong(p));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6107
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6108
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6109
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6110
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6111
                    MapReduceEntriesToLongTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6112
                        t = (MapReduceEntriesToLongTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6113
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6114
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6115
                        t.result = reducer.applyAsLong(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6116
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6117
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6118
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6119
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6120
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6121
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6122
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6123
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6124
    static final class MapReduceMappingsToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6125
        extends BulkTask<K,V,Long> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6126
        final ToLongBiFunction<? super K, ? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6127
        final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6128
        final long basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6129
        long result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6130
        MapReduceMappingsToLongTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6131
        MapReduceMappingsToLongTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6132
            (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
  6133
             MapReduceMappingsToLongTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6134
             ToLongBiFunction<? super K, ? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6135
             long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6136
             LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6137
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6138
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6139
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6140
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6141
        public final Long getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6142
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6143
            final ToLongBiFunction<? super K, ? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6144
            final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6145
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6146
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6147
                long r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6148
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6149
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6150
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6151
                    (rights = new MapReduceMappingsToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6152
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6153
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6154
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6155
                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
  6156
                    r = reducer.applyAsLong(r, transformer.applyAsLong(p.key, p.val));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6157
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6158
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6159
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6160
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6161
                    MapReduceMappingsToLongTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6162
                        t = (MapReduceMappingsToLongTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6163
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6164
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6165
                        t.result = reducer.applyAsLong(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6166
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6167
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6168
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6169
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6170
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6171
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6172
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6173
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6174
    static final class MapReduceKeysToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6175
        extends BulkTask<K,V,Integer> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6176
        final ToIntFunction<? super K> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6177
        final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6178
        final int basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6179
        int result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6180
        MapReduceKeysToIntTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6181
        MapReduceKeysToIntTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6182
            (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
  6183
             MapReduceKeysToIntTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6184
             ToIntFunction<? super K> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6185
             int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6186
             IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6187
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6188
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6189
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6190
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6191
        public final Integer getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6192
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6193
            final ToIntFunction<? super K> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6194
            final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6195
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6196
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6197
                int r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6198
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6199
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6200
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6201
                    (rights = new MapReduceKeysToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6202
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6203
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6204
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6205
                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
  6206
                    r = reducer.applyAsInt(r, transformer.applyAsInt(p.key));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6207
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6208
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6209
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6210
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6211
                    MapReduceKeysToIntTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6212
                        t = (MapReduceKeysToIntTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6213
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6214
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6215
                        t.result = reducer.applyAsInt(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6216
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6217
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6218
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6219
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6220
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6221
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6222
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6223
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6224
    static final class MapReduceValuesToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6225
        extends BulkTask<K,V,Integer> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6226
        final ToIntFunction<? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6227
        final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6228
        final int basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6229
        int result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6230
        MapReduceValuesToIntTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6231
        MapReduceValuesToIntTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6232
            (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
  6233
             MapReduceValuesToIntTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6234
             ToIntFunction<? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6235
             int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6236
             IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6237
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6238
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6239
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6240
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6241
        public final Integer getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6242
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6243
            final ToIntFunction<? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6244
            final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6245
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6246
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6247
                int r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6248
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6249
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6250
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6251
                    (rights = new MapReduceValuesToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6252
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6253
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6254
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6255
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6256
                    r = reducer.applyAsInt(r, transformer.applyAsInt(p.val));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6257
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6258
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6259
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6260
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6261
                    MapReduceValuesToIntTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6262
                        t = (MapReduceValuesToIntTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6263
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6264
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6265
                        t.result = reducer.applyAsInt(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6266
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6267
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6268
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6269
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6270
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6271
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6272
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6273
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6274
    static final class MapReduceEntriesToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6275
        extends BulkTask<K,V,Integer> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6276
        final ToIntFunction<Map.Entry<K,V>> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6277
        final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6278
        final int basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6279
        int result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6280
        MapReduceEntriesToIntTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6281
        MapReduceEntriesToIntTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6282
            (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
  6283
             MapReduceEntriesToIntTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6284
             ToIntFunction<Map.Entry<K,V>> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6285
             int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6286
             IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6287
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6288
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6289
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6290
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6291
        public final Integer getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6292
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6293
            final ToIntFunction<Map.Entry<K,V>> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6294
            final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6295
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6296
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6297
                int r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6298
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6299
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6300
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6301
                    (rights = new MapReduceEntriesToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6302
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6303
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6304
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6305
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6306
                    r = reducer.applyAsInt(r, transformer.applyAsInt(p));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6307
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6308
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6309
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6310
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6311
                    MapReduceEntriesToIntTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6312
                        t = (MapReduceEntriesToIntTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6313
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6314
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6315
                        t.result = reducer.applyAsInt(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6316
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6317
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6318
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6319
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6320
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6321
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6322
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6323
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6324
    static final class MapReduceMappingsToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6325
        extends BulkTask<K,V,Integer> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6326
        final ToIntBiFunction<? super K, ? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6327
        final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6328
        final int basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6329
        int result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6330
        MapReduceMappingsToIntTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6331
        MapReduceMappingsToIntTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6332
            (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
  6333
             MapReduceMappingsToIntTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6334
             ToIntBiFunction<? super K, ? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6335
             int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6336
             IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6337
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6338
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6339
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6340
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6341
        public final Integer getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6342
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6343
            final ToIntBiFunction<? super K, ? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6344
            final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6345
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6346
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6347
                int r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6348
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6349
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6350
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6351
                    (rights = new MapReduceMappingsToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6352
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6353
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6354
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6355
                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
  6356
                    r = reducer.applyAsInt(r, transformer.applyAsInt(p.key, p.val));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6357
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6358
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6359
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6360
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6361
                    MapReduceMappingsToIntTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6362
                        t = (MapReduceMappingsToIntTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6363
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6364
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6365
                        t.result = reducer.applyAsInt(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6366
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6367
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6368
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6369
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  6370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  6371
    }
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6372
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6373
    // Unsafe mechanics
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 38551
diff changeset
  6374
    private static final Unsafe U = Unsafe.getUnsafe();
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6375
    private static final long SIZECTL;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6376
    private static final long TRANSFERINDEX;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6377
    private static final long BASECOUNT;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6378
    private static final long CELLSBUSY;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6379
    private static final long CELLVALUE;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  6380
    private static final int ABASE;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6381
    private static final int ASHIFT;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6382
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6383
    static {
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6384
        try {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6385
            SIZECTL = U.objectFieldOffset
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  6386
                (ConcurrentHashMap.class.getDeclaredField("sizeCtl"));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6387
            TRANSFERINDEX = U.objectFieldOffset
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  6388
                (ConcurrentHashMap.class.getDeclaredField("transferIndex"));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6389
            BASECOUNT = U.objectFieldOffset
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  6390
                (ConcurrentHashMap.class.getDeclaredField("baseCount"));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6391
            CELLSBUSY = U.objectFieldOffset
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  6392
                (ConcurrentHashMap.class.getDeclaredField("cellsBusy"));
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  6393
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6394
            CELLVALUE = U.objectFieldOffset
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  6395
                (CounterCell.class.getDeclaredField("value"));
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  6396
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  6397
            ABASE = U.arrayBaseOffset(Node[].class);
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  6398
            int scale = U.arrayIndexScale(Node[].class);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6399
            if ((scale & (scale - 1)) != 0)
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  6400
                throw new Error("array index scale not a power of two");
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6401
            ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  6402
        } catch (ReflectiveOperationException e) {
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6403
            throw new Error(e);
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6404
        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  6405
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  6406
        // 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
  6407
        // LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  6408
        Class<?> ensureLoaded = LockSupport.class;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6409
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  6410
}