jdk/src/share/classes/java/util/concurrent/ConcurrentHashMap.java
author smarks
Mon, 16 Jun 2014 17:52:21 -0700
changeset 24972 d3304c4e66b5
parent 21981 48b31d370bc9
permissions -rw-r--r--
8044730: small errors in ConcurrentHashMap and LongAdder docs Reviewed-by: martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 7518
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util.concurrent;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
    37
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
    38
import java.io.ObjectStreamField;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.Serializable;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    40
import java.lang.reflect.ParameterizedType;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    41
import java.lang.reflect.Type;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    42
import java.util.AbstractMap;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    43
import java.util.Arrays;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    44
import java.util.Collection;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    45
import java.util.Comparator;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    46
import java.util.Enumeration;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    47
import java.util.HashMap;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    48
import java.util.Hashtable;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    49
import java.util.Iterator;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    50
import java.util.Map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    51
import java.util.NoSuchElementException;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    52
import java.util.Set;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    53
import java.util.Spliterator;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    54
import java.util.concurrent.ConcurrentMap;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    55
import java.util.concurrent.ForkJoinPool;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    56
import java.util.concurrent.atomic.AtomicReference;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
    57
import java.util.concurrent.locks.LockSupport;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    58
import java.util.concurrent.locks.ReentrantLock;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    59
import java.util.function.BiConsumer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    60
import java.util.function.BiFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    61
import java.util.function.BinaryOperator;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    62
import java.util.function.Consumer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    63
import java.util.function.DoubleBinaryOperator;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    64
import java.util.function.Function;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    65
import java.util.function.IntBinaryOperator;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    66
import java.util.function.LongBinaryOperator;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    67
import java.util.function.ToDoubleBiFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    68
import java.util.function.ToDoubleFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    69
import java.util.function.ToIntBiFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    70
import java.util.function.ToIntFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    71
import java.util.function.ToLongBiFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    72
import java.util.function.ToLongFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    73
import java.util.stream.Stream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * A hash table supporting full concurrency of retrievals and
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    77
 * high expected concurrency for updates. This class obeys the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * same functional specification as {@link java.util.Hashtable}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * includes versions of methods corresponding to each method of
17717
fe0b28a1a3bd 8015439: Minor/sync/cleanup of ConcurrentHashMap
chegar
parents: 16883
diff changeset
    80
 * {@code Hashtable}. However, even though all operations are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * thread-safe, retrieval operations do <em>not</em> entail locking,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * and there is <em>not</em> any support for locking the entire table
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * in a way that prevents all access.  This class is fully
17717
fe0b28a1a3bd 8015439: Minor/sync/cleanup of ConcurrentHashMap
chegar
parents: 16883
diff changeset
    84
 * interoperable with {@code Hashtable} in programs that rely on its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * thread safety but not on its synchronization details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    87
 * <p>Retrieval operations (including {@code get}) generally do not
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    88
 * block, so may overlap with update operations (including {@code put}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    89
 * and {@code remove}). Retrievals reflect the results of the most
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    90
 * recently <em>completed</em> update operations holding upon their
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    91
 * onset. (More formally, an update operation for a given key bears a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    92
 * <em>happens-before</em> relation with any (non-null) retrieval for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    93
 * that key reporting the updated value.)  For aggregate operations
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    94
 * such as {@code putAll} and {@code clear}, concurrent retrievals may
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
    95
 * 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
    96
 * 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
    97
 * 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
    98
 * 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
    99
 * java.util.ConcurrentModificationException ConcurrentModificationException}.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
   100
 * 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
   101
 * 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
   102
 * {@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
   103
 * 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
   104
 * Otherwise the results of these methods reflect transient states
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   105
 * that may be adequate for monitoring or estimation purposes, but not
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   106
 * for program control.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   108
 * <p>The table is dynamically expanded when there are too many
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   109
 * collisions (i.e., keys that have distinct hash codes but fall into
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   110
 * the same slot modulo the table size), with the expected average
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   111
 * effect of maintaining roughly two bins per mapping (corresponding
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   112
 * to a 0.75 load factor threshold for resizing). There may be much
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   113
 * variance around this average as mappings are added and removed, but
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   114
 * overall, this maintains a commonly accepted time/space tradeoff for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   115
 * hash tables.  However, resizing this or any other kind of hash
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   116
 * table may be a relatively slow operation. When possible, it is a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   117
 * good idea to provide a size estimate as an optional {@code
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   118
 * initialCapacity} constructor argument. An additional optional
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   119
 * {@code loadFactor} constructor argument provides a further means of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   120
 * customizing initial table capacity by specifying the table density
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   121
 * to be used in calculating the amount of space to allocate for the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   122
 * given number of elements.  Also, for compatibility with previous
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   123
 * versions of this class, constructors may optionally specify an
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   124
 * expected {@code concurrencyLevel} as an additional hint for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   125
 * internal sizing.  Note that using many keys with exactly the same
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   126
 * {@code hashCode()} is a sure way to slow down performance of any
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   127
 * hash table. To ameliorate impact, when keys are {@link Comparable},
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   128
 * this class may use comparison order among keys to help break ties.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   129
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   130
 * <p>A {@link Set} projection of a ConcurrentHashMap may be created
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   131
 * (using {@link #newKeySet()} or {@link #newKeySet(int)}), or viewed
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   132
 * (using {@link #keySet(Object)} when only keys are of interest, and the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   133
 * mapped values are (perhaps transiently) not used or all take the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   134
 * same mapping value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   135
 *
24972
d3304c4e66b5 8044730: small errors in ConcurrentHashMap and LongAdder docs
smarks
parents: 21981
diff changeset
   136
 * <p>A ConcurrentHashMap can be used as a scalable frequency map (a
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   137
 * form of histogram or multiset) by using {@link
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   138
 * java.util.concurrent.atomic.LongAdder} values and initializing via
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   139
 * {@link #computeIfAbsent computeIfAbsent}. For example, to add a count
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   140
 * 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
   141
 * {@code freqs.computeIfAbsent(key, k -> new LongAdder()).increment();}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * <p>This class and its views and iterators implement all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * <em>optional</em> methods of the {@link Map} and {@link Iterator}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *
17717
fe0b28a1a3bd 8015439: Minor/sync/cleanup of ConcurrentHashMap
chegar
parents: 16883
diff changeset
   147
 * <p>Like {@link Hashtable} but unlike {@link HashMap}, this class
fe0b28a1a3bd 8015439: Minor/sync/cleanup of ConcurrentHashMap
chegar
parents: 16883
diff changeset
   148
 * does <em>not</em> allow {@code null} to be used as a key or value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 *
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   150
 * <p>ConcurrentHashMaps support a set of sequential and parallel bulk
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   151
 * operations that, unlike most {@link Stream} methods, are designed
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   152
 * to be safely, and often sensibly, applied even with maps that are
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   153
 * being concurrently updated by other threads; for example, when
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   154
 * computing a snapshot summary of the values in a shared registry.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   155
 * There are three kinds of operation, each with four forms, accepting
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   156
 * functions with Keys, Values, Entries, and (Key, Value) arguments
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   157
 * and/or return values. Because the elements of a ConcurrentHashMap
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   158
 * are not ordered in any particular way, and may be processed in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   159
 * different orders in different parallel executions, the correctness
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   160
 * of supplied functions should not depend on any ordering, or on any
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   161
 * other objects or values that may transiently change while
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   162
 * computation is in progress; and except for forEach actions, should
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   163
 * ideally be side-effect-free. Bulk operations on {@link java.util.Map.Entry}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   164
 * objects do not support method {@code setValue}.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   165
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   166
 * <ul>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   167
 * <li> forEach: Perform a given action on each element.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   168
 * A variant form applies a given transformation on each element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   169
 * before performing the action.</li>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   170
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   171
 * <li> search: Return the first available non-null result of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   172
 * applying a given function on each element; skipping further
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   173
 * search when a result is found.</li>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   174
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   175
 * <li> reduce: Accumulate each element.  The supplied reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   176
 * function cannot rely on ordering (more formally, it should be
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   177
 * both associative and commutative).  There are five variants:
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   178
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   179
 * <ul>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   180
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   181
 * <li> Plain reductions. (There is not a form of this method for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   182
 * (key, value) function arguments since there is no corresponding
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   183
 * return type.)</li>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   184
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   185
 * <li> Mapped reductions that accumulate the results of a given
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   186
 * function applied to each element.</li>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   187
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   188
 * <li> Reductions to scalar doubles, longs, and ints, using a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   189
 * given basis value.</li>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   190
 *
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
 * </li>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   193
 * </ul>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   194
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   195
 * <p>These bulk operations accept a {@code parallelismThreshold}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   196
 * argument. Methods proceed sequentially if the current map size is
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   197
 * estimated to be less than the given threshold. Using a value of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   198
 * {@code Long.MAX_VALUE} suppresses all parallelism.  Using a value
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   199
 * of {@code 1} results in maximal parallelism by partitioning into
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   200
 * enough subtasks to fully utilize the {@link
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   201
 * ForkJoinPool#commonPool()} that is used for all parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   202
 * computations. Normally, you would initially choose one of these
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   203
 * extreme values, and then measure performance of using in-between
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   204
 * values that trade off overhead versus throughput.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   205
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   206
 * <p>The concurrency properties of bulk operations follow
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   207
 * from those of ConcurrentHashMap: Any non-null result returned
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   208
 * from {@code get(key)} and related access methods bears a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   209
 * happens-before relation with the associated insertion or
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   210
 * update.  The result of any bulk operation reflects the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   211
 * composition of these per-element relations (but is not
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   212
 * necessarily atomic with respect to the map as a whole unless it
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   213
 * is somehow known to be quiescent).  Conversely, because keys
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   214
 * and values in the map are never null, null serves as a reliable
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   215
 * atomic indicator of the current lack of any result.  To
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   216
 * maintain this property, null serves as an implicit basis for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   217
 * all non-scalar reduction operations. For the double, long, and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   218
 * int versions, the basis should be one that, when combined with
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   219
 * any other value, returns that other value (more formally, it
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   220
 * should be the identity element for the reduction). Most common
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   221
 * reductions have these properties; for example, computing a sum
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   222
 * with basis 0 or a minimum with basis MAX_VALUE.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   223
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   224
 * <p>Search and transformation functions provided as arguments
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   225
 * should similarly return null to indicate the lack of any result
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   226
 * (in which case it is not used). In the case of mapped
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   227
 * reductions, this also enables transformations to serve as
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   228
 * filters, returning null (or, in the case of primitive
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   229
 * specializations, the identity basis) if the element should not
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   230
 * be combined. You can create compound transformations and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   231
 * filterings by composing them yourself under this "null means
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   232
 * there is nothing there now" rule before using them in search or
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   233
 * reduce operations.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   234
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   235
 * <p>Methods accepting and/or returning Entry arguments maintain
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   236
 * key-value associations. They may be useful for example when
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   237
 * finding the key for the greatest value. Note that "plain" Entry
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   238
 * arguments can be supplied using {@code new
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   239
 * AbstractMap.SimpleEntry(k,v)}.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   240
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   241
 * <p>Bulk operations may complete abruptly, throwing an
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   242
 * exception encountered in the application of a supplied
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   243
 * function. Bear in mind when handling such exceptions that other
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   244
 * concurrently executing functions could also have thrown
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   245
 * exceptions, or would have done so if the first exception had
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   246
 * not occurred.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   247
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   248
 * <p>Speedups for parallel compared to sequential forms are common
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   249
 * but not guaranteed.  Parallel operations involving brief functions
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   250
 * on small maps may execute more slowly than sequential forms if the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   251
 * underlying work to parallelize the computation is more expensive
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   252
 * than the computation itself.  Similarly, parallelization may not
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   253
 * lead to much actual parallelism if all processors are busy
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   254
 * performing unrelated tasks.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   255
 *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   256
 * <p>All arguments to all task methods must be non-null.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   257
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 * @param <K> the type of keys maintained by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 * @param <V> the type of mapped values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
 */
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   267
public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   268
    implements ConcurrentMap<K,V>, Serializable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private static final long serialVersionUID = 7249069246763182397L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /*
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   272
     * Overview:
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   273
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   274
     * The primary design goal of this hash table is to maintain
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   275
     * concurrent readability (typically method get(), but also
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   276
     * iterators and related methods) while minimizing update
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   277
     * contention. Secondary goals are to keep space consumption about
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   278
     * the same or better than java.util.HashMap, and to support high
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   279
     * initial insertion rates on an empty table by many threads.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   280
     *
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   281
     * 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
   282
     * 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
   283
     * 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
   284
     * fields. However, various subclasses exist: TreeNodes are
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   285
     * 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
   286
     * 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
   287
     * of bins during resizing. ReservationNodes are used as
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   288
     * placeholders while establishing values in computeIfAbsent and
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   289
     * related methods.  The types TreeBin, ForwardingNode, and
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   290
     * 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
   291
     * hashes, and are readily distinguishable during search etc
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   292
     * 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
   293
     * 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
   294
     * 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
   295
     * insignificant.)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   296
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   297
     * The table is lazily initialized to a power-of-two size upon the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   298
     * first insertion.  Each bin in the table normally contains a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   299
     * list of Nodes (most often, the list has only zero or one Node).
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   300
     * Table accesses require volatile/atomic reads, writes, and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   301
     * CASes.  Because there is no other way to arrange this without
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   302
     * adding further indirections, we use intrinsics
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   303
     * (sun.misc.Unsafe) operations.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   304
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   305
     * We use the top (sign) bit of Node hash fields for control
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   306
     * 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
   307
     * constraints.  Nodes with negative hash fields are specially
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   308
     * handled or ignored in map methods.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   309
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   310
     * Insertion (via put or its variants) of the first node in an
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   311
     * empty bin is performed by just CASing it to the bin.  This is
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   312
     * by far the most common case for put operations under most
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   313
     * key/hash distributions.  Other update operations (insert,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   314
     * delete, and replace) require locks.  We do not want to waste
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   315
     * the space required to associate a distinct lock object with
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   316
     * 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
   317
     * a lock. Locking support for these locks relies on builtin
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   318
     * "synchronized" monitors.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   319
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   320
     * 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
   321
     * suffice though: When a node is locked, any update must first
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   322
     * validate that it is still the first node after locking it, and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   323
     * retry if not. Because new nodes are always appended to lists,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   324
     * once a node is first in a bin, it remains first until deleted
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   325
     * or the bin becomes invalidated (upon resizing).
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   326
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   327
     * The main disadvantage of per-bin locks is that other update
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   328
     * operations on other nodes in a bin list protected by the same
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   329
     * lock can stall, for example when user equals() or mapping
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   330
     * functions take a long time.  However, statistically, under
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   331
     * random hash codes, this is not a common problem.  Ideally, the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   332
     * frequency of nodes in bins follows a Poisson distribution
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   333
     * (http://en.wikipedia.org/wiki/Poisson_distribution) with a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   334
     * parameter of about 0.5 on average, given the resizing threshold
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   335
     * of 0.75, although with a large variance because of resizing
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   336
     * granularity. Ignoring variance, the expected occurrences of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   337
     * 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
   338
     * first values are:
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   339
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   340
     * 0:    0.60653066
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   341
     * 1:    0.30326533
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   342
     * 2:    0.07581633
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   343
     * 3:    0.01263606
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   344
     * 4:    0.00157952
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   345
     * 5:    0.00015795
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   346
     * 6:    0.00001316
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   347
     * 7:    0.00000094
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   348
     * 8:    0.00000006
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   349
     * more: less than 1 in ten million
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   350
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   351
     * Lock contention probability for two threads accessing distinct
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   352
     * elements is roughly 1 / (8 * #elements) under random hashes.
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
   353
     *
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   354
     * Actual hash code distributions encountered in practice
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   355
     * sometimes deviate significantly from uniform randomness.  This
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   356
     * includes the case when N > (1<<30), so some keys MUST collide.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   357
     * 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
   358
     * 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
   359
     * 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
   360
     * 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
   361
     * 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
   362
     * 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
   363
     * 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
   364
     * slow as in a regular list, but given that N cannot exceed
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   365
     * (1<<64) (before running out of addresses) this bounds search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   366
     * steps, lock hold times, etc, to reasonable constants (roughly
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   367
     * 100 nodes inspected per operation worst case) so long as keys
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   368
     * are Comparable (which is very common -- String, Long, etc).
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   369
     * TreeBin nodes (TreeNodes) also maintain the same "next"
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   370
     * traversal pointers as regular nodes, so can be traversed in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   371
     * iterators in the same way.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   372
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   373
     * The table is resized when occupancy exceeds a percentage
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   374
     * threshold (nominally, 0.75, but see below).  Any thread
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   375
     * 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
   376
     * initiating thread allocates and sets up the replacement array.
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   377
     * However, rather than stalling, these other threads may proceed
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   378
     * 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
   379
     * worst case effects of overfilling while resizes are in
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   380
     * 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
   381
     * 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
   382
     * 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
   383
     * 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
   384
     * 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
   385
     * 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
   386
     * 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
   387
     * 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
   388
     * 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
   389
     * 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
   390
     * 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
   391
     * 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
   392
     * 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
   393
     * 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
   394
     * 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
   395
     * 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
   396
     * 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
   397
     * the new table.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   398
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   399
     * Each bin transfer requires its bin lock, which can stall
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   400
     * waiting for locks while resizing. However, because other
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   401
     * threads can join in and help resize rather than contend for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   402
     * locks, average aggregate waits become shorter as resizing
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   403
     * progresses.  The transfer operation must also ensure that all
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   404
     * 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
   405
     * 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
   406
     * 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
   407
     * a forwarding node, traversals (see class Traverser) arrange to
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   408
     * 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
   409
     * 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
   410
     * 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
   411
     * 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
   412
     * later processing the current table. The need for these
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   413
     * save/restore mechanics is relatively rare, but when one
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   414
     * forwarding node is encountered, typically many more will be.
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   415
     * 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
   416
     * many new TableStack nodes. (Thanks to Peter Levart for
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
   417
     * suggesting use of a stack here.)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   418
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   419
     * The traversal scheme also applies to partial traversals of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   420
     * ranges of bins (via an alternate Traverser constructor)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   421
     * to support partitioned aggregate operations.  Also, read-only
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   422
     * operations give up if ever forwarded to a null table, which
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   423
     * provides support for shutdown-style clearing, which is also not
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   424
     * currently implemented.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   425
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   426
     * Lazy table initialization minimizes footprint until first use,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   427
     * and also avoids resizings when the first operation is from a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   428
     * putAll, constructor with map argument, or deserialization.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   429
     * These cases attempt to override the initial capacity settings,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   430
     * but harmlessly fail to take effect in cases of races.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   431
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   432
     * The element count is maintained using a specialization of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   433
     * LongAdder. We need to incorporate a specialization rather than
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   434
     * just use a LongAdder in order to access implicit
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   435
     * 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
   436
     * CounterCells.  The counter mechanics avoid contention on
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   437
     * updates but can encounter cache thrashing if read too
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   438
     * frequently during concurrent access. To avoid reading so often,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   439
     * resizing under contention is attempted only upon adding to a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   440
     * bin already holding two or more nodes. Under uniform hash
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   441
     * distributions, the probability of this occurring at threshold
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   442
     * 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
   443
     * threshold (and after resizing, many fewer do so).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   444
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   445
     * 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
   446
     * 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
   447
     * existing collections such as TreeMaps). TreeBins contain
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   448
     * Comparable elements, but may contain others, as well as
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   449
     * elements that are Comparable but not necessarily Comparable for
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   450
     * the same T, so we cannot invoke compareTo among them. To handle
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   451
     * this, the tree is ordered primarily by hash value, then by
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   452
     * Comparable.compareTo order if applicable.  On lookup at a node,
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   453
     * if elements are not comparable or compare as 0 then both left
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   454
     * 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
   455
     * hash values. (This corresponds to the full list search that
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   456
     * would be necessary if all elements were non-Comparable and had
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   457
     * tied hashes.) On insertion, to keep a total ordering (or as
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   458
     * close as is required here) across rebalancings, we compare
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   459
     * classes and identityHashCodes as tie-breakers. The red-black
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   460
     * 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
   461
     * (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
   462
     * 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
   463
     * Algorithms" (CLR).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   464
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   465
     * TreeBins also require an additional locking mechanism.  While
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   466
     * 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
   467
     * 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
   468
     * 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
   469
     * 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
   470
     * main bin-synchronization strategy: Structural adjustments
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   471
     * 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
   472
     * (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
   473
     * 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
   474
     * 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
   475
     * 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
   476
     * 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
   477
     * 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
   478
     * 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
   479
     * maximize aggregate expected throughput.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   480
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   481
     * Maintaining API and serialization compatibility with previous
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   482
     * versions of this class introduces several oddities. Mainly: We
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   483
     * leave untouched but unused constructor arguments refering to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   484
     * concurrencyLevel. We accept a loadFactor constructor argument,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   485
     * but apply it only to initial table capacity (which is the only
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   486
     * time that we can guarantee to honor it.) We also declare an
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   487
     * unused "Segment" class that is instantiated in minimal form
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   488
     * only when serializing.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   489
     *
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   490
     * Also, solely for compatibility with previous versions of this
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   491
     * class, it extends AbstractMap, even though all of its methods
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   492
     * are overridden, so it is just useless baggage.
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
   493
     *
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   494
     * 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
   495
     * 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
   496
     * declarations and utilities, then fields, then main public
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   497
     * 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
   498
     * internal ones), then sizing methods, trees, traversers, and
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   499
     * bulk operations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    /* ---------------- Constants -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   505
     * The largest possible table capacity.  This value must be
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   506
     * exactly 1<<30 to stay within Java array allocation and indexing
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   507
     * bounds for power of two table sizes, and is further required
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   508
     * because the top two bits of 32bit hash fields are used for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   509
     * control purposes.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   511
    private static final int MAXIMUM_CAPACITY = 1 << 30;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   512
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   513
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   514
     * The default initial table capacity.  Must be a power of 2
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   515
     * (i.e., at least 1) and at most MAXIMUM_CAPACITY.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   516
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   517
    private static final int DEFAULT_CAPACITY = 16;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   520
     * The largest possible (non-power of two) array size.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   521
     * Needed by toArray and related methods.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   523
    static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   526
     * The default concurrency level for this table. Unused but
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   527
     * defined for compatibility with previous versions of this class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   529
    private static final int DEFAULT_CONCURRENCY_LEVEL = 16;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   530
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   531
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   532
     * The load factor for this table. Overrides of this value in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   533
     * constructors affect only the initial table capacity.  The
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   534
     * actual floating point value isn't normally used -- it is
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   535
     * simpler to use expressions such as {@code n - (n >>> 2)} for
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   536
     * the associated resizing threshold.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   537
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   538
    private static final float LOAD_FACTOR = 0.75f;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   541
     * 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
   542
     * 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
   543
     * 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
   544
     * 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
   545
     * 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
   546
     * shrinkage.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   548
    static final int TREEIFY_THRESHOLD = 8;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   549
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   550
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   551
     * 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
   552
     * 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
   553
     * 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
   554
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   555
    static final int UNTREEIFY_THRESHOLD = 6;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   556
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   557
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   558
     * 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
   559
     * (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
   560
     * 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
   561
     * conflicts between resizing and treeification thresholds.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   562
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   563
    static final int MIN_TREEIFY_CAPACITY = 64;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   566
     * Minimum number of rebinnings per transfer step. Ranges are
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   567
     * subdivided to allow multiple resizer threads.  This value
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   568
     * serves as a lower bound to avoid resizers encountering
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   569
     * excessive memory contention.  The value should be at least
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   570
     * DEFAULT_CAPACITY.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   571
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   572
    private static final int MIN_TRANSFER_STRIDE = 16;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   573
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   574
    /**
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   575
     * 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
   576
     * 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
   577
     */
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   578
    private static int RESIZE_STAMP_BITS = 16;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   579
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   580
    /**
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   581
     * 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
   582
     * 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
   583
     */
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   584
    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
   585
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
     * 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
   588
     */
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   589
    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
   590
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   591
    /*
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   592
     * Encodings for Node hash fields. See above for explanation.
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
   593
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   594
    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
   595
    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
   596
    static final int RESERVED  = -3; // hash for transient reservations
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   597
    static final int HASH_BITS = 0x7fffffff; // usable bits of normal node hash
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   598
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   599
    /** Number of CPUS, to place bounds on some sizings */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   600
    static final int NCPU = Runtime.getRuntime().availableProcessors();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   601
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   602
    /** For serialization compatibility. */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   603
    private static final ObjectStreamField[] serialPersistentFields = {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   604
        new ObjectStreamField("segments", Segment[].class),
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   605
        new ObjectStreamField("segmentMask", Integer.TYPE),
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   606
        new ObjectStreamField("segmentShift", Integer.TYPE)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   607
    };
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
   608
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   609
    /* ---------------- Nodes -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   610
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
   611
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   612
     * 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
   613
     * 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
   614
     * 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
   615
     * 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
   616
     * 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
   617
     * exported).  Otherwise, keys and vals are never null.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   618
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   619
    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
   620
        final int hash;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   621
        final K key;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   622
        volatile V val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   623
        volatile Node<K,V> next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   624
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   625
        Node(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
   626
            this.hash = hash;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   627
            this.key = key;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   628
            this.val = val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   629
            this.next = next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   630
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   631
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   632
        public final K getKey()       { return key; }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   633
        public final V getValue()     { return val; }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   634
        public final int hashCode()   { return key.hashCode() ^ val.hashCode(); }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   635
        public final String toString(){ return key + "=" + val; }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   636
        public final V setValue(V value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   637
            throw new UnsupportedOperationException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   638
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   639
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   640
        public final boolean equals(Object o) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   641
            Object k, v, u; Map.Entry<?,?> e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   642
            return ((o instanceof Map.Entry) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   643
                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   644
                    (v = e.getValue()) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   645
                    (k == key || k.equals(key)) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   646
                    (v == (u = val) || v.equals(u)));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   647
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   648
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   649
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   650
         * Virtualized support for map.get(); overridden in subclasses.
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
        Node<K,V> find(int h, Object k) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   653
            Node<K,V> e = this;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   654
            if (k != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   655
                do {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   656
                    K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   657
                    if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   658
                        ((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
   659
                        return e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   660
                } while ((e = e.next) != null);
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
            return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   663
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   664
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   665
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   666
    /* ---------------- Static utilities -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   667
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   668
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   669
     * 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
   670
     * 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
   671
     * 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
   672
     * 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
   673
     * 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
   674
     * 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
   675
     * 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
   676
     * 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
   677
     * 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
   678
     * 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
   679
     * 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
   680
     * 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
   681
     * 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
   682
     * never be used in index calculations because of table bounds.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   684
    static final int spread(int h) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   685
        return (h ^ (h >>> 16)) & HASH_BITS;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   686
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   687
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   688
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   689
     * 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
   690
     * See Hackers Delight, sec 3.2
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   691
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   692
    private static final int tableSizeFor(int c) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   693
        int n = c - 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   694
        n |= n >>> 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   695
        n |= n >>> 2;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   696
        n |= n >>> 4;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   697
        n |= n >>> 8;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   698
        n |= n >>> 16;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   699
        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
   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
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   703
     * 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
   704
     * Comparable<C>", else null.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   705
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   706
    static Class<?> comparableClassFor(Object x) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   707
        if (x instanceof Comparable) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   708
            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
   709
            if ((c = x.getClass()) == String.class) // bypass checks
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   710
                return c;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   711
            if ((ts = c.getGenericInterfaces()) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   712
                for (int i = 0; i < ts.length; ++i) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   713
                    if (((t = ts[i]) instanceof ParameterizedType) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   714
                        ((p = (ParameterizedType)t).getRawType() ==
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   715
                         Comparable.class) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   716
                        (as = p.getActualTypeArguments()) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   717
                        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
   718
                        return c;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   719
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   720
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   721
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   722
        return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   723
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   724
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   725
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   726
     * 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
   727
     * class), else 0.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   728
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   729
    @SuppressWarnings({"rawtypes","unchecked"}) // for cast to Comparable
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   730
    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
   731
        return (x == null || x.getClass() != kc ? 0 :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   732
                ((Comparable)k).compareTo(x));
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
    /* ---------------- Table element access -------------- */
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
     * Volatile access methods are used for table elements as well as
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   739
     * 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
   740
     * 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
   741
     * 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
   742
     * 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
   743
     * 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
   744
     * 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
   745
     * 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
   746
     * 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
   747
     * Note that calls to setTabAt always occur within locked regions,
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
   748
     * and so in principle require only release ordering, not
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   749
     * full volatile semantics, but are currently coded as volatile
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   750
     * writes to be conservative.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   751
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   752
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   753
    @SuppressWarnings("unchecked")
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   754
    static final <K,V> Node<K,V> tabAt(Node<K,V>[] tab, int i) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   755
        return (Node<K,V>)U.getObjectVolatile(tab, ((long)i << ASHIFT) + ABASE);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   756
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   757
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   758
    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
   759
                                        Node<K,V> c, Node<K,V> v) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   760
        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
   761
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   762
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   763
    static final <K,V> void setTabAt(Node<K,V>[] tab, int i, Node<K,V> v) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   764
        U.putObjectVolatile(tab, ((long)i << ASHIFT) + ABASE, v);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   765
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    /* ---------------- Fields -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   770
     * The array of bins. Lazily initialized upon first insertion.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   771
     * Size is always a power of two. Accessed directly by iterators.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   772
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   773
    transient volatile Node<K,V>[] table;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   774
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   775
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   776
     * The next table to use; non-null only while resizing.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   777
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   778
    private transient volatile Node<K,V>[] nextTable;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   779
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   780
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   781
     * Base counter value, used mainly when there is no contention,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   782
     * but also as a fallback during table initialization
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   783
     * races. Updated via CAS.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   784
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   785
    private transient volatile long baseCount;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   786
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   787
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   788
     * Table initialization and resizing control.  When negative, the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   789
     * table is being initialized or resized: -1 for initialization,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   790
     * else -(1 + the number of active resizing threads).  Otherwise,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   791
     * when table is null, holds the initial table size to use upon
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   792
     * creation, or 0 for default. After initialization, holds the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   793
     * next element count value upon which to resize the table.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   794
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   795
    private transient volatile int sizeCtl;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   796
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   797
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   798
     * The next table index (plus one) to split while resizing.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   799
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   800
    private transient volatile int transferIndex;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   801
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   802
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   803
     * Spinlock (locked via CAS) used when resizing and/or creating CounterCells.
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 11279
diff changeset
   804
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   805
    private transient volatile int cellsBusy;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   806
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   807
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   808
     * Table of counter cells. When non-null, size is a power of 2.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   809
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   810
    private transient volatile CounterCell[] counterCells;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   811
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   812
    // views
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   813
    private transient KeySetView<K,V> keySet;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   814
    private transient ValuesView<K,V> values;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   815
    private transient EntrySetView<K,V> entrySet;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   816
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   817
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   818
    /* ---------------- Public operations -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   819
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   820
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   821
     * 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
   822
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   823
    public ConcurrentHashMap() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   824
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   825
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   826
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   827
     * 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
   828
     * 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
   829
     * to dynamically resize.
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
     * @param initialCapacity The implementation performs internal
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   832
     * sizing to accommodate this many elements.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   833
     * @throws IllegalArgumentException if the initial capacity of
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   834
     * elements is negative
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   835
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   836
    public ConcurrentHashMap(int initialCapacity) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   837
        if (initialCapacity < 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   838
            throw new IllegalArgumentException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   839
        int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   840
                   MAXIMUM_CAPACITY :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   841
                   tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   842
        this.sizeCtl = cap;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   843
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   844
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   845
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   846
     * Creates a new map with the same mappings as the given map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   847
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   848
     * @param m the map
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   849
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   850
    public ConcurrentHashMap(Map<? extends K, ? extends V> m) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   851
        this.sizeCtl = DEFAULT_CAPACITY;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   852
        putAll(m);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   853
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   854
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   855
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   856
     * Creates a new, empty map with an initial table size based on
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   857
     * the given number of elements ({@code initialCapacity}) and
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   858
     * initial table density ({@code loadFactor}).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   859
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   860
     * @param initialCapacity the initial capacity. The implementation
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   861
     * performs internal sizing to accommodate this many elements,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   862
     * given the specified load factor.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   863
     * @param loadFactor the load factor (table density) for
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   864
     * establishing the initial table size
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   865
     * @throws IllegalArgumentException if the initial capacity of
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   866
     * elements is negative or the load factor is nonpositive
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   867
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   868
     * @since 1.6
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   869
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   870
    public ConcurrentHashMap(int initialCapacity, float loadFactor) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   871
        this(initialCapacity, loadFactor, 1);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   872
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   873
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 11279
diff changeset
   874
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   875
     * Creates a new, empty map with an initial table size based on
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   876
     * the given number of elements ({@code initialCapacity}), table
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   877
     * density ({@code loadFactor}), and number of concurrently
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   878
     * updating threads ({@code concurrencyLevel}).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   879
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   880
     * @param initialCapacity the initial capacity. The implementation
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   881
     * performs internal sizing to accommodate this many elements,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   882
     * given the specified load factor.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   883
     * @param loadFactor the load factor (table density) for
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   884
     * establishing the initial table size
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   885
     * @param concurrencyLevel the estimated number of concurrently
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   886
     * updating threads. The implementation may use this value as
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   887
     * a sizing hint.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   888
     * @throws IllegalArgumentException if the initial capacity is
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   889
     * negative or the load factor or concurrencyLevel are
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   890
     * nonpositive
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   891
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   892
    public ConcurrentHashMap(int initialCapacity,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   893
                             float loadFactor, int concurrencyLevel) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   894
        if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   895
            throw new IllegalArgumentException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   896
        if (initialCapacity < concurrencyLevel)   // Use at least as many bins
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   897
            initialCapacity = concurrencyLevel;   // as estimated threads
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   898
        long size = (long)(1.0 + (long)initialCapacity / loadFactor);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   899
        int cap = (size >= (long)MAXIMUM_CAPACITY) ?
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   900
            MAXIMUM_CAPACITY : tableSizeFor((int)size);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   901
        this.sizeCtl = cap;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   902
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   903
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   904
    // Original (since JDK1.2) Map methods
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   905
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   906
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   907
     * {@inheritDoc}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   908
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   909
    public int size() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   910
        long n = sumCount();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   911
        return ((n < 0L) ? 0 :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   912
                (n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   913
                (int)n);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   914
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   915
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   916
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   917
     * {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   919
    public boolean isEmpty() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   920
        return sumCount() <= 0L; // ignore transient negative values
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   921
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   922
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   923
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   924
     * Returns the value to which the specified key is mapped,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   925
     * or {@code null} if this map contains no mapping for the key.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   926
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   927
     * <p>More formally, if this map contains a mapping from a key
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   928
     * {@code k} to a value {@code v} such that {@code key.equals(k)},
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   929
     * then this method returns {@code v}; otherwise it returns
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   930
     * {@code null}.  (There can be at most one such mapping.)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   931
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   932
     * @throws NullPointerException if the specified key is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   933
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   934
    public V get(Object key) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   935
        Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   936
        int h = spread(key.hashCode());
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   937
        if ((tab = table) != null && (n = tab.length) > 0 &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   938
            (e = tabAt(tab, (n - 1) & h)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   939
            if ((eh = e.hash) == h) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   940
                if ((ek = e.key) == key || (ek != null && key.equals(ek)))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   941
                    return e.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   942
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   943
            else if (eh < 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   944
                return (p = e.find(h, key)) != null ? p.val : null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   945
            while ((e = e.next) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   946
                if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   947
                    ((ek = e.key) == key || (ek != null && key.equals(ek))))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   948
                    return e.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   949
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   950
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   951
        return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   952
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   953
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   954
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   955
     * Tests if the specified object is a key in this table.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   956
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   957
     * @param  key possible key
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   958
     * @return {@code true} if and only if the specified object
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   959
     *         is a key in this table, as determined by the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   960
     *         {@code equals} method; {@code false} otherwise
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   961
     * @throws NullPointerException if the specified key is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   962
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   963
    public boolean containsKey(Object key) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   964
        return get(key) != null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   965
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   966
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   967
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   968
     * Returns {@code true} if this map maps one or more keys to the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   969
     * specified value. Note: This method may require a full traversal
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   970
     * of the map, and is much slower than method {@code containsKey}.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   971
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   972
     * @param value value whose presence in this map is to be tested
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   973
     * @return {@code true} if this map maps one or more keys to the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   974
     *         specified value
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   975
     * @throws NullPointerException if the specified value is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   976
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   977
    public boolean containsValue(Object value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   978
        if (value == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   979
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   980
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   981
        if ((t = table) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   982
            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   983
            for (Node<K,V> p; (p = it.advance()) != null; ) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   984
                V v;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   985
                if ((v = p.val) == value || (v != null && value.equals(v)))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   986
                    return true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   987
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   988
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   989
        return false;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
   990
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   993
     * Maps the specified key to the specified value in this table.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   994
     * Neither the key nor the value can be null.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   995
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   996
     * <p>The value can be retrieved by calling the {@code get} method
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   997
     * with a key that is equal to the original key.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   998
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
   999
     * @param key key with which the specified value is to be associated
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1000
     * @param value value to be associated with the specified key
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1001
     * @return the previous value associated with {@code key}, or
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1002
     *         {@code null} if there was no mapping for {@code key}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1003
     * @throws NullPointerException if the specified key or value is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1005
    public V put(K key, V value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1006
        return putVal(key, value, false);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1007
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1008
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1009
    /** Implementation for put and putIfAbsent */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1010
    final V putVal(K key, V value, boolean onlyIfAbsent) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1011
        if (key == null || value == null) throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1012
        int hash = spread(key.hashCode());
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1013
        int binCount = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1014
        for (Node<K,V>[] tab = table;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1015
            Node<K,V> f; int n, i, fh;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1016
            if (tab == null || (n = tab.length) == 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1017
                tab = initTable();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1018
            else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1019
                if (casTabAt(tab, i, null,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1020
                             new Node<K,V>(hash, key, value, null)))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1021
                    break;                   // no lock when adding to empty bin
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1022
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1023
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1024
                tab = helpTransfer(tab, f);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1025
            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1026
                V oldVal = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1027
                synchronized (f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1028
                    if (tabAt(tab, i) == f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1029
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1030
                            binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1031
                            for (Node<K,V> e = f;; ++binCount) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1032
                                K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1033
                                if (e.hash == hash &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1034
                                    ((ek = e.key) == key ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1035
                                     (ek != null && key.equals(ek)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1036
                                    oldVal = e.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1037
                                    if (!onlyIfAbsent)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1038
                                        e.val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1039
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1040
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1041
                                Node<K,V> pred = e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1042
                                if ((e = e.next) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1043
                                    pred.next = new Node<K,V>(hash, key,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1044
                                                              value, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1045
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1046
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1047
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1048
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1049
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1050
                            Node<K,V> p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1051
                            binCount = 2;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1052
                            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
  1053
                                                           value)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1054
                                oldVal = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1055
                                if (!onlyIfAbsent)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1056
                                    p.val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1057
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1058
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1059
                    }
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
                if (binCount != 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1062
                    if (binCount >= TREEIFY_THRESHOLD)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1063
                        treeifyBin(tab, i);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1064
                    if (oldVal != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1065
                        return oldVal;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1066
                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1067
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1068
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1069
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1070
        addCount(1L, binCount);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1071
        return null;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1072
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1073
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1074
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1075
     * 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
  1076
     * 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
  1077
     * keys currently in the specified map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1078
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1079
     * @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
  1080
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1081
    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
  1082
        tryPresize(m.size());
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1083
        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
  1084
            putVal(e.getKey(), e.getValue(), false);
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
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1087
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1088
     * 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
  1089
     * 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
  1090
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1091
     * @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
  1092
     * @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
  1093
     *         {@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
  1094
     * @throws NullPointerException if the specified key is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1095
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1096
    public V remove(Object key) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1097
        return replaceNode(key, null, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1098
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1099
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1100
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1101
     * Implementation for the four public remove/replace methods:
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1102
     * 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
  1103
     * non-null.  If resulting value is null, delete.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1104
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1105
    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
  1106
        int hash = spread(key.hashCode());
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1107
        for (Node<K,V>[] tab = table;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1108
            Node<K,V> f; int n, i, fh;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1109
            if (tab == null || (n = tab.length) == 0 ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1110
                (f = tabAt(tab, i = (n - 1) & hash)) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1111
                break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1112
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1113
                tab = helpTransfer(tab, f);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1114
            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1115
                V oldVal = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1116
                boolean validated = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1117
                synchronized (f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1118
                    if (tabAt(tab, i) == f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1119
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1120
                            validated = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1121
                            for (Node<K,V> e = f, pred = null;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1122
                                K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1123
                                if (e.hash == hash &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1124
                                    ((ek = e.key) == key ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1125
                                     (ek != null && key.equals(ek)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1126
                                    V ev = e.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1127
                                    if (cv == null || cv == ev ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1128
                                        (ev != null && cv.equals(ev))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1129
                                        oldVal = ev;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1130
                                        if (value != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1131
                                            e.val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1132
                                        else if (pred != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1133
                                            pred.next = e.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1134
                                        else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1135
                                            setTabAt(tab, i, e.next);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1136
                                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1137
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1138
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1139
                                pred = e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1140
                                if ((e = e.next) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1141
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1142
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1143
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1144
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1145
                            validated = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1146
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1147
                            TreeNode<K,V> r, p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1148
                            if ((r = t.root) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1149
                                (p = r.findTreeNode(hash, key, null)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1150
                                V pv = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1151
                                if (cv == null || cv == pv ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1152
                                    (pv != null && cv.equals(pv))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1153
                                    oldVal = pv;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1154
                                    if (value != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1155
                                        p.val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1156
                                    else if (t.removeTreeNode(p))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1157
                                        setTabAt(tab, i, untreeify(t.first));
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
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1161
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1162
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1163
                if (validated) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1164
                    if (oldVal != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1165
                        if (value == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1166
                            addCount(-1L, -1);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1167
                        return oldVal;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1168
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1169
                    break;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1170
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1171
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1172
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1173
        return null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1174
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1177
     * Removes all of the mappings from this map.
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  1178
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1179
    public void clear() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1180
        long delta = 0L; // negative number of deletions
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1181
        int i = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1182
        Node<K,V>[] tab = table;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1183
        while (tab != null && i < tab.length) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1184
            int fh;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1185
            Node<K,V> f = tabAt(tab, i);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1186
            if (f == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1187
                ++i;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1188
            else if ((fh = f.hash) == MOVED) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1189
                tab = helpTransfer(tab, f);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1190
                i = 0; // restart
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1191
            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1192
            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1193
                synchronized (f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1194
                    if (tabAt(tab, i) == f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1195
                        Node<K,V> p = (fh >= 0 ? f :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1196
                                       (f instanceof TreeBin) ?
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1197
                                       ((TreeBin<K,V>)f).first : null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1198
                        while (p != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1199
                            --delta;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1200
                            p = p.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1201
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1202
                        setTabAt(tab, i++, null);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1203
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1204
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1205
            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1206
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1207
        if (delta != 0L)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1208
            addCount(delta, -1);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1209
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1210
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1211
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1212
     * 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
  1213
     * 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
  1214
     * 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
  1215
     * 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
  1216
     * via the {@code Iterator.remove}, {@code Set.remove},
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1217
     * {@code removeAll}, {@code retainAll}, and {@code clear}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1218
     * 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
  1219
     * {@code addAll} operations.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1220
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1221
     * <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
  1222
     * <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
  1223
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1224
     * <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
  1225
     * {@link Spliterator#DISTINCT}, and {@link Spliterator#NONNULL}.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1226
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1227
     * @return the set view
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
    public KeySetView<K,V> keySet() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1230
        KeySetView<K,V> ks;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1231
        return (ks = keySet) != null ? ks : (keySet = new KeySetView<K,V>(this, null));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1232
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1233
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1234
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1235
     * 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
  1236
     * 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
  1237
     * 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
  1238
     * supports element removal, which removes the corresponding
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1239
     * 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
  1240
     * {@code Collection.remove}, {@code removeAll},
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1241
     * {@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
  1242
     * support the {@code add} or {@code addAll} operations.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1243
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1244
     * <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
  1245
     * <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
  1246
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1247
     * <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
  1248
     * and {@link Spliterator#NONNULL}.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1249
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1250
     * @return the collection view
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
    public Collection<V> values() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1253
        ValuesView<K,V> vs;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1254
        return (vs = values) != null ? vs : (values = new ValuesView<K,V>(this));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1255
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1256
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1257
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1258
     * 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
  1259
     * 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
  1260
     * 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
  1261
     * 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
  1262
     * via the {@code Iterator.remove}, {@code Set.remove},
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1263
     * {@code removeAll}, {@code retainAll}, and {@code clear}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1264
     * operations.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1265
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1266
     * <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
  1267
     * <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
  1268
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  1269
     * <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
  1270
     * {@link Spliterator#DISTINCT}, and {@link Spliterator#NONNULL}.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1271
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1272
     * @return the set view
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1273
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1274
    public Set<Map.Entry<K,V>> entrySet() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1275
        EntrySetView<K,V> es;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1276
        return (es = entrySet) != null ? es : (entrySet = new EntrySetView<K,V>(this));
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
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1279
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1280
     * 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
  1281
     * 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
  1282
     * {@code key.hashCode() ^ value.hashCode()}.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1283
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1284
     * @return the hash code value for this map
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1285
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1286
    public int hashCode() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1287
        int h = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1288
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1289
        if ((t = table) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1290
            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
  1291
            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
  1292
                h += p.key.hashCode() ^ p.val.hashCode();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1293
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1294
        return h;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1295
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1296
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1297
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1298
     * 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
  1299
     * 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
  1300
     * particular order) enclosed in braces ("{@code {}}").  Adjacent
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1301
     * mappings are separated by the characters {@code ", "} (comma
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1302
     * 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
  1303
     * 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
  1304
     * associated value.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1305
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1306
     * @return a string representation of this map
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1307
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1308
    public String toString() {
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
        int f = (t = table) == null ? 0 : t.length;
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, f, 0, f);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1312
        StringBuilder sb = new StringBuilder();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1313
        sb.append('{');
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1314
        Node<K,V> p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1315
        if ((p = it.advance()) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1316
            for (;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1317
                K k = p.key;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1318
                V v = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1319
                sb.append(k == this ? "(this Map)" : k);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1320
                sb.append('=');
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1321
                sb.append(v == this ? "(this Map)" : v);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1322
                if ((p = it.advance()) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1323
                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1324
                sb.append(',').append(' ');
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1325
            }
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 sb.append('}').toString();
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
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1330
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1331
     * 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
  1332
     * 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
  1333
     * 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
  1334
     * 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
  1335
     * of this method.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1336
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1337
     * @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
  1338
     * @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
  1339
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1340
    public boolean equals(Object o) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1341
        if (o != this) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1342
            if (!(o instanceof Map))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1343
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1344
            Map<?,?> m = (Map<?,?>) o;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1345
            Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1346
            int f = (t = table) == null ? 0 : t.length;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1347
            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
  1348
            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
  1349
                V val = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1350
                Object v = m.get(p.key);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1351
                if (v == null || (v != val && !v.equals(val)))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1352
                    return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1353
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1354
            for (Map.Entry<?,?> e : m.entrySet()) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1355
                Object mk, mv, v;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1356
                if ((mk = e.getKey()) == null ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1357
                    (mv = e.getValue()) == null ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1358
                    (v = get(mk)) == null ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1359
                    (mv != v && !mv.equals(v)))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1360
                    return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1361
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1362
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1363
        return true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1364
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1365
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1366
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1367
     * Stripped-down version of helper class used in previous version,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1368
     * declared for the sake of serialization compatibility
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1369
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1370
    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
  1371
        private static final long serialVersionUID = 2249069246763182397L;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1372
        final float loadFactor;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1373
        Segment(float lf) { this.loadFactor = lf; }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1374
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1375
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1376
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1377
     * 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
  1378
     * stream (i.e., serializes it).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1379
     * @param s the stream
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  1380
     * @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
  1381
     * @serialData
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1382
     * the key (Object) and value (Object)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1383
     * for each key-value mapping, followed by a null pair.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1384
     * 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
  1385
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1386
    private void writeObject(java.io.ObjectOutputStream s)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1387
        throws java.io.IOException {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1388
        // For serialization compatibility
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1389
        // 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
  1390
        int sshift = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1391
        int ssize = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1392
        while (ssize < DEFAULT_CONCURRENCY_LEVEL) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1393
            ++sshift;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1394
            ssize <<= 1;
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
        int segmentShift = 32 - sshift;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1397
        int segmentMask = ssize - 1;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1398
        @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1399
        Segment<K,V>[] segments = (Segment<K,V>[])
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1400
            new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1401
        for (int i = 0; i < segments.length; ++i)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1402
            segments[i] = new Segment<K,V>(LOAD_FACTOR);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1403
        s.putFields().put("segments", segments);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1404
        s.putFields().put("segmentShift", segmentShift);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1405
        s.putFields().put("segmentMask", segmentMask);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1406
        s.writeFields();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1407
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1408
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1409
        if ((t = table) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1410
            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
  1411
            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
  1412
                s.writeObject(p.key);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1413
                s.writeObject(p.val);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1414
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1415
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1416
        s.writeObject(null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1417
        s.writeObject(null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1418
        segments = null; // throw away
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1419
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1420
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1421
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1422
     * 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
  1423
     * @param s the stream
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  1424
     * @throws ClassNotFoundException if the class of a serialized object
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  1425
     *         could not be found
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  1426
     * @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
  1427
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1428
    private void readObject(java.io.ObjectInputStream s)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1429
        throws java.io.IOException, ClassNotFoundException {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1430
        /*
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1431
         * 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
  1432
         * 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
  1433
         * 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
  1434
         * overpopulated bins while doing so, which requires
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1435
         * specialized versions of putVal mechanics.
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1436
         */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1437
        sizeCtl = -1; // force exclusion for table construction
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1438
        s.defaultReadObject();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1439
        long size = 0L;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1440
        Node<K,V> p = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1441
        for (;;) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1442
            @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1443
            K k = (K) s.readObject();
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1444
            @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  1445
            V v = (V) s.readObject();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1446
            if (k != null && v != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1447
                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
  1448
                ++size;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1449
            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1450
            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1451
                break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1452
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1453
        if (size == 0L)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1454
            sizeCtl = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1455
        else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1456
            int n;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1457
            if (size >= (long)(MAXIMUM_CAPACITY >>> 1))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1458
                n = MAXIMUM_CAPACITY;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1459
            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1460
                int sz = (int)size;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1461
                n = tableSizeFor(sz + (sz >>> 1) + 1);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1462
            }
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
            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
  1465
            int mask = n - 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1466
            long added = 0L;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1467
            while (p != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1468
                boolean insertAtFront;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1469
                Node<K,V> next = p.next, first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1470
                int h = p.hash, j = h & mask;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1471
                if ((first = tabAt(tab, j)) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1472
                    insertAtFront = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1473
                else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1474
                    K k = p.key;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1475
                    if (first.hash < 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1476
                        TreeBin<K,V> t = (TreeBin<K,V>)first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1477
                        if (t.putTreeVal(h, k, p.val) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1478
                            ++added;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1479
                        insertAtFront = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1480
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1481
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1482
                        int binCount = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1483
                        insertAtFront = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1484
                        Node<K,V> q; K qk;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1485
                        for (q = first; q != null; q = q.next) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1486
                            if (q.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1487
                                ((qk = q.key) == k ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1488
                                 (qk != null && k.equals(qk)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1489
                                insertAtFront = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1490
                                break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1491
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1492
                            ++binCount;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1493
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1494
                        if (insertAtFront && binCount >= TREEIFY_THRESHOLD) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1495
                            insertAtFront = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1496
                            ++added;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1497
                            p.next = first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1498
                            TreeNode<K,V> hd = null, tl = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1499
                            for (q = p; q != null; q = q.next) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1500
                                TreeNode<K,V> t = new TreeNode<K,V>
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1501
                                    (q.hash, q.key, q.val, null, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1502
                                if ((t.prev = tl) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1503
                                    hd = t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1504
                                else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1505
                                    tl.next = t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1506
                                tl = t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1507
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1508
                            setTabAt(tab, j, new TreeBin<K,V>(hd));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1509
                        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1510
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1511
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1512
                if (insertAtFront) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1513
                    ++added;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1514
                    p.next = first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1515
                    setTabAt(tab, j, p);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1516
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1517
                p = next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1518
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1519
            table = tab;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1520
            sizeCtl = n - (n >>> 2);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1521
            baseCount = added;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1522
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1523
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1524
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1525
    // ConcurrentMap methods
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1526
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1527
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1528
     * {@inheritDoc}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1529
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1530
     * @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
  1531
     *         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
  1532
     * @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
  1533
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1534
    public V putIfAbsent(K key, V value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1535
        return putVal(key, value, true);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1536
    }
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
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1539
     * {@inheritDoc}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1540
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1541
     * @throws NullPointerException if the specified key is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1542
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1543
    public boolean remove(Object key, Object value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1544
        if (key == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1545
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1546
        return value != null && replaceNode(key, null, value) != null;
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
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1550
     * {@inheritDoc}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1551
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1552
     * @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
  1553
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1554
    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
  1555
        if (key == null || oldValue == null || newValue == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1556
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1557
        return replaceNode(key, newValue, oldValue) != null;
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
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1561
     * {@inheritDoc}
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1562
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1563
     * @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
  1564
     *         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
  1565
     * @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
  1566
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1567
    public V replace(K key, V value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1568
        if (key == null || value == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1569
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1570
        return replaceNode(key, value, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1571
    }
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
    // Overrides of JDK8+ Map extension method defaults
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
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1576
     * 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
  1577
     * 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
  1578
     * key.
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
     * @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
  1581
     * @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
  1582
     * no mapping for the given key
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1583
     * @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
  1584
     * @throws NullPointerException if the specified key is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1585
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1586
    public V getOrDefault(Object key, V defaultValue) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1587
        V v;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1588
        return (v = get(key)) == null ? defaultValue : v;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1589
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1590
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1591
    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
  1592
        if (action == null) throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1593
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1594
        if ((t = table) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1595
            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
  1596
            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
  1597
                action.accept(p.key, p.val);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1598
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1599
        }
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
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1602
    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
  1603
        if (function == null) throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1604
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1605
        if ((t = table) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1606
            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
  1607
            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
  1608
                V oldValue = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1609
                for (K key = p.key;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1610
                    V newValue = function.apply(key, oldValue);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1611
                    if (newValue == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1612
                        throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1613
                    if (replaceNode(key, newValue, oldValue) != null ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1614
                        (oldValue = get(key)) == null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1615
                        break;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1616
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1617
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1618
        }
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
     * 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
  1623
     * 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
  1624
     * 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
  1625
     * 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
  1626
     * 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
  1627
     * 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
  1628
     * 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
  1629
     * 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
  1630
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1631
     * @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
  1632
     * @param mappingFunction the function to compute a value
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1633
     * @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
  1634
     *         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
  1635
     * @throws NullPointerException if the specified key or mappingFunction
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1636
     *         is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1637
     * @throws IllegalStateException if the computation detectably
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1638
     *         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
  1639
     *         otherwise never complete
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1640
     * @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
  1641
     *         in which case the mapping is left unestablished
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1642
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1643
    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
  1644
        if (key == null || mappingFunction == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1645
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1646
        int h = spread(key.hashCode());
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1647
        V val = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1648
        int binCount = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1649
        for (Node<K,V>[] tab = table;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1650
            Node<K,V> f; int n, i, fh;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1651
            if (tab == null || (n = tab.length) == 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1652
                tab = initTable();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1653
            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
  1654
                Node<K,V> r = new ReservationNode<K,V>();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1655
                synchronized (r) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1656
                    if (casTabAt(tab, i, null, r)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1657
                        binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1658
                        Node<K,V> node = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1659
                        try {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1660
                            if ((val = mappingFunction.apply(key)) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1661
                                node = new Node<K,V>(h, key, val, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1662
                        } finally {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1663
                            setTabAt(tab, i, node);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1664
                        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1665
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1666
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1667
                if (binCount != 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1668
                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1669
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1670
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1671
                tab = helpTransfer(tab, f);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1672
            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1673
                boolean added = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1674
                synchronized (f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1675
                    if (tabAt(tab, i) == f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1676
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1677
                            binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1678
                            for (Node<K,V> e = f;; ++binCount) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1679
                                K ek; V ev;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1680
                                if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1681
                                    ((ek = e.key) == key ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1682
                                     (ek != null && key.equals(ek)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1683
                                    val = e.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1684
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1685
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1686
                                Node<K,V> pred = e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1687
                                if ((e = e.next) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1688
                                    if ((val = mappingFunction.apply(key)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1689
                                        added = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1690
                                        pred.next = new Node<K,V>(h, key, val, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1691
                                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1692
                                    break;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1693
                                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1694
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1695
                        }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1696
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1697
                            binCount = 2;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1698
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1699
                            TreeNode<K,V> r, p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1700
                            if ((r = t.root) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1701
                                (p = r.findTreeNode(h, key, null)) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1702
                                val = p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1703
                            else if ((val = mappingFunction.apply(key)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1704
                                added = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1705
                                t.putTreeVal(h, key, val);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1706
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1707
                        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1708
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1709
                }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1710
                if (binCount != 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1711
                    if (binCount >= TREEIFY_THRESHOLD)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1712
                        treeifyBin(tab, i);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1713
                    if (!added)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1714
                        return val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1715
                    break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1716
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1717
            }
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  1718
        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1719
        if (val != null)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1720
            addCount(1L, binCount);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1721
        return val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1722
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1723
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1724
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1725
     * 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
  1726
     * 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
  1727
     * value.  The entire method invocation is performed atomically.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1728
     * 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
  1729
     * 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
  1730
     * 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
  1731
     * update any other mappings of this map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1732
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1733
     * @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
  1734
     * @param remappingFunction the function to compute a value
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1735
     * @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
  1736
     * @throws NullPointerException if the specified key or remappingFunction
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1737
     *         is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1738
     * @throws IllegalStateException if the computation detectably
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1739
     *         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
  1740
     *         otherwise never complete
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1741
     * @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
  1742
     *         in which case the mapping is unchanged
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1743
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1744
    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
  1745
        if (key == null || remappingFunction == null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1746
            throw new NullPointerException();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1747
        int h = spread(key.hashCode());
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1748
        V val = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1749
        int delta = 0;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1750
        int binCount = 0;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1751
        for (Node<K,V>[] tab = table;;) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1752
            Node<K,V> f; int n, i, fh;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1753
            if (tab == null || (n = tab.length) == 0)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1754
                tab = initTable();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1755
            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
  1756
                break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1757
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1758
                tab = helpTransfer(tab, f);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1759
            else {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1760
                synchronized (f) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1761
                    if (tabAt(tab, i) == f) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1762
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1763
                            binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1764
                            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
  1765
                                K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1766
                                if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1767
                                    ((ek = e.key) == key ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1768
                                     (ek != null && key.equals(ek)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1769
                                    val = remappingFunction.apply(key, e.val);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1770
                                    if (val != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1771
                                        e.val = val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1772
                                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1773
                                        delta = -1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1774
                                        Node<K,V> en = e.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1775
                                        if (pred != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1776
                                            pred.next = en;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1777
                                        else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1778
                                            setTabAt(tab, i, en);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1779
                                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1780
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1781
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1782
                                pred = e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1783
                                if ((e = e.next) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1784
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1785
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1786
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1787
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1788
                            binCount = 2;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1789
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1790
                            TreeNode<K,V> r, p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1791
                            if ((r = t.root) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1792
                                (p = r.findTreeNode(h, key, null)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1793
                                val = remappingFunction.apply(key, p.val);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1794
                                if (val != null)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1795
                                    p.val = val;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1796
                                else {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1797
                                    delta = -1;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1798
                                    if (t.removeTreeNode(p))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1799
                                        setTabAt(tab, i, untreeify(t.first));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1800
                                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1801
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1802
                        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1803
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1804
                }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1805
                if (binCount != 0)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1806
                    break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1807
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1808
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1809
        if (delta != 0)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1810
            addCount((long)delta, binCount);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1811
        return val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1812
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1813
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1814
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1815
     * 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
  1816
     * 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
  1817
     * mapping). The entire method invocation is performed atomically.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1818
     * 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
  1819
     * 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
  1820
     * 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
  1821
     * update any other mappings of this Map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1822
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1823
     * @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
  1824
     * @param remappingFunction the function to compute a value
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1825
     * @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
  1826
     * @throws NullPointerException if the specified key or remappingFunction
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1827
     *         is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1828
     * @throws IllegalStateException if the computation detectably
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1829
     *         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
  1830
     *         otherwise never complete
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1831
     * @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
  1832
     *         in which case the mapping is unchanged
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1833
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1834
    public V compute(K key,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1835
                     BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1836
        if (key == null || remappingFunction == null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1837
            throw new NullPointerException();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1838
        int h = spread(key.hashCode());
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1839
        V val = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1840
        int delta = 0;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1841
        int binCount = 0;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1842
        for (Node<K,V>[] tab = table;;) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1843
            Node<K,V> f; int n, i, fh;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1844
            if (tab == null || (n = tab.length) == 0)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1845
                tab = initTable();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1846
            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
  1847
                Node<K,V> r = new ReservationNode<K,V>();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1848
                synchronized (r) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1849
                    if (casTabAt(tab, i, null, r)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1850
                        binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1851
                        Node<K,V> node = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1852
                        try {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1853
                            if ((val = remappingFunction.apply(key, null)) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1854
                                delta = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1855
                                node = new Node<K,V>(h, key, val, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1856
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1857
                        } finally {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1858
                            setTabAt(tab, i, node);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1859
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1860
                    }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1861
                }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1862
                if (binCount != 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1863
                    break;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1864
            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1865
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1866
                tab = helpTransfer(tab, f);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1867
            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1868
                synchronized (f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1869
                    if (tabAt(tab, i) == f) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1870
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1871
                            binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1872
                            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
  1873
                                K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1874
                                if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1875
                                    ((ek = e.key) == key ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1876
                                     (ek != null && key.equals(ek)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1877
                                    val = remappingFunction.apply(key, e.val);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1878
                                    if (val != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1879
                                        e.val = val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1880
                                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1881
                                        delta = -1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1882
                                        Node<K,V> en = e.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1883
                                        if (pred != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1884
                                            pred.next = en;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1885
                                        else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1886
                                            setTabAt(tab, i, en);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1887
                                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1888
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1889
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1890
                                pred = e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1891
                                if ((e = e.next) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1892
                                    val = remappingFunction.apply(key, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1893
                                    if (val != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1894
                                        delta = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1895
                                        pred.next =
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1896
                                            new Node<K,V>(h, key, val, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1897
                                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1898
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1899
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1900
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1901
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1902
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1903
                            binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1904
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1905
                            TreeNode<K,V> r, p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1906
                            if ((r = t.root) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1907
                                p = r.findTreeNode(h, key, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1908
                            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1909
                                p = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1910
                            V pv = (p == null) ? null : p.val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1911
                            val = remappingFunction.apply(key, pv);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1912
                            if (val != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1913
                                if (p != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1914
                                    p.val = val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1915
                                else {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1916
                                    delta = 1;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1917
                                    t.putTreeVal(h, key, val);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1918
                                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1919
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1920
                            else if (p != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1921
                                delta = -1;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1922
                                if (t.removeTreeNode(p))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1923
                                    setTabAt(tab, i, untreeify(t.first));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1924
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1925
                        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1926
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1927
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1928
                if (binCount != 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1929
                    if (binCount >= TREEIFY_THRESHOLD)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1930
                        treeifyBin(tab, i);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1931
                    break;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1932
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1933
            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1934
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1935
        if (delta != 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1936
            addCount((long)delta, binCount);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1937
        return val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1938
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1939
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1940
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1941
     * 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
  1942
     * (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
  1943
     * 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
  1944
     * 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
  1945
     * method invocation is performed atomically.  Some attempted
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1946
     * 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
  1947
     * 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
  1948
     * 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
  1949
     * mappings of this Map.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1950
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1951
     * @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
  1952
     * @param value the value to use if absent
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1953
     * @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
  1954
     * @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
  1955
     * @throws NullPointerException if the specified key or the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1956
     *         remappingFunction is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1957
     * @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
  1958
     *         in which case the mapping is unchanged
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
    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
  1961
        if (key == null || value == null || remappingFunction == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1962
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1963
        int h = spread(key.hashCode());
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1964
        V val = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1965
        int delta = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1966
        int binCount = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1967
        for (Node<K,V>[] tab = table;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1968
            Node<K,V> f; int n, i, fh;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1969
            if (tab == null || (n = tab.length) == 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1970
                tab = initTable();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1971
            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
  1972
                if (casTabAt(tab, i, null, new Node<K,V>(h, key, value, null))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1973
                    delta = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1974
                    val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1975
                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1976
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1977
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1978
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1979
                tab = helpTransfer(tab, f);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1980
            else {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1981
                synchronized (f) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  1982
                    if (tabAt(tab, i) == f) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1983
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1984
                            binCount = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1985
                            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
  1986
                                K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1987
                                if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1988
                                    ((ek = e.key) == key ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1989
                                     (ek != null && key.equals(ek)))) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1990
                                    val = remappingFunction.apply(e.val, value);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1991
                                    if (val != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1992
                                        e.val = val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1993
                                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1994
                                        delta = -1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1995
                                        Node<K,V> en = e.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1996
                                        if (pred != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1997
                                            pred.next = en;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1998
                                        else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  1999
                                            setTabAt(tab, i, en);
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
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2002
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2003
                                pred = e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2004
                                if ((e = e.next) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2005
                                    delta = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2006
                                    val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2007
                                    pred.next =
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2008
                                        new Node<K,V>(h, key, val, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2009
                                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2010
                                }
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
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2014
                            binCount = 2;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2015
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2016
                            TreeNode<K,V> r = t.root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2017
                            TreeNode<K,V> p = (r == null) ? null :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2018
                                r.findTreeNode(h, key, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2019
                            val = (p == null) ? value :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2020
                                remappingFunction.apply(p.val, value);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2021
                            if (val != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2022
                                if (p != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2023
                                    p.val = val;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2024
                                else {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2025
                                    delta = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2026
                                    t.putTreeVal(h, key, val);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2027
                                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2028
                            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2029
                            else if (p != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2030
                                delta = -1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2031
                                if (t.removeTreeNode(p))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2032
                                    setTabAt(tab, i, untreeify(t.first));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2033
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2034
                        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2035
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2036
                }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2037
                if (binCount != 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2038
                    if (binCount >= TREEIFY_THRESHOLD)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2039
                        treeifyBin(tab, i);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2040
                    break;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2041
                }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2042
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2043
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2044
        if (delta != 0)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2045
            addCount((long)delta, binCount);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2046
        return val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2047
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2048
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2049
    // Hashtable legacy methods
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
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2052
     * Legacy method testing if some key maps into the specified value
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2053
     * in this table.  This method is identical in functionality to
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2054
     * {@link #containsValue(Object)}, and exists solely to ensure
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2055
     * full compatibility with class {@link java.util.Hashtable},
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2056
     * which supported this method prior to introduction of the
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2057
     * Java Collections framework.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2058
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2059
     * @param  value a value to search for
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2060
     * @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
  2061
     *         {@code value} argument in this table as
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2062
     *         determined by the {@code equals} method;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2063
     *         {@code false} otherwise
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2064
     * @throws NullPointerException if the specified value is null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2065
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2066
    public boolean contains(Object value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2067
        return containsValue(value);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2068
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2069
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2070
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2071
     * 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
  2072
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2073
     * @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
  2074
     * @see #keySet()
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
    public Enumeration<K> keys() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2077
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2078
        int f = (t = table) == null ? 0 : t.length;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2079
        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
  2080
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2081
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
     * 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
  2084
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2085
     * @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
  2086
     * @see #values()
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2087
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2088
    public Enumeration<V> elements() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2089
        Node<K,V>[] t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2090
        int f = (t = table) == null ? 0 : t.length;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2091
        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
  2092
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2093
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2094
    // ConcurrentHashMap-only methods
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2095
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2096
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2097
     * 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
  2098
     * instead of {@link #size} because a ConcurrentHashMap may
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2099
     * 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
  2100
     * 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
  2101
     * there are concurrent insertions or removals.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2102
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2103
     * @return the number of mappings
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2104
     * @since 1.8
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2105
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2106
    public long mappingCount() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2107
        long n = sumCount();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2108
        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
  2109
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2110
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2111
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2112
     * 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
  2113
     * from the given type to {@code Boolean.TRUE}.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2114
     *
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2115
     * @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
  2116
     * @return the new set
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2117
     * @since 1.8
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2118
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2119
    public static <K> KeySetView<K,Boolean> newKeySet() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2120
        return new KeySetView<K,Boolean>
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2121
            (new ConcurrentHashMap<K,Boolean>(), Boolean.TRUE);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2122
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2123
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2124
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2125
     * 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
  2126
     * from the given type to {@code Boolean.TRUE}.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2127
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2128
     * @param initialCapacity The implementation performs internal
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2129
     * sizing to accommodate this many elements.
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2130
     * @param <K> the element type of the returned set
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2131
     * @return the new set
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2132
     * @throws IllegalArgumentException if the initial capacity of
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2133
     * elements is negative
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2134
     * @since 1.8
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2135
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2136
    public static <K> KeySetView<K,Boolean> newKeySet(int initialCapacity) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2137
        return new KeySetView<K,Boolean>
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2138
            (new ConcurrentHashMap<K,Boolean>(initialCapacity), Boolean.TRUE);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2139
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2140
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2141
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2142
     * 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
  2143
     * 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
  2144
     * Collection#add} and {@link Collection#addAll(Collection)}).
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2145
     * 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
  2146
     * 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
  2147
     *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2148
     * @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
  2149
     * @return the set view
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2150
     * @throws NullPointerException if the mappedValue is null
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2151
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2152
    public KeySetView<K,V> keySet(V mappedValue) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2153
        if (mappedValue == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2154
            throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2155
        return new KeySetView<K,V>(this, mappedValue);
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
    /* ---------------- Special Nodes -------------- */
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
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2161
     * 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
  2162
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2163
    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
  2164
        final Node<K,V>[] nextTable;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2165
        ForwardingNode(Node<K,V>[] tab) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2166
            super(MOVED, null, null, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2167
            this.nextTable = tab;
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
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2170
        Node<K,V> find(int h, Object k) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2171
            // 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
  2172
            outer: for (Node<K,V>[] tab = nextTable;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2173
                Node<K,V> e; int n;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2174
                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
  2175
                    (e = tabAt(tab, (n - 1) & h)) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2176
                    return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2177
                for (;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2178
                    int eh; K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2179
                    if ((eh = e.hash) == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2180
                        ((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
  2181
                        return e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2182
                    if (eh < 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2183
                        if (e instanceof ForwardingNode) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2184
                            tab = ((ForwardingNode<K,V>)e).nextTable;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2185
                            continue outer;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2186
                        }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2187
                        else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2188
                            return e.find(h, k);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2189
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2190
                    if ((e = e.next) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2191
                        return null;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2192
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2193
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2194
        }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2195
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2196
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2197
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2198
     * A place-holder node used in computeIfAbsent and compute
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2199
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2200
    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
  2201
        ReservationNode() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2202
            super(RESERVED, null, null, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2203
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2204
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2205
        Node<K,V> find(int h, Object k) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2206
            return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2207
        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2208
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2209
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2210
    /* ---------------- Table Initialization and Resizing -------------- */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2211
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2212
    /**
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2213
     * 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
  2214
     * 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
  2215
     */
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2216
    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
  2217
        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
  2218
    }
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2219
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2220
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2221
     * Initializes table, using the size recorded in sizeCtl.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2222
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2223
    private final Node<K,V>[] initTable() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2224
        Node<K,V>[] tab; int sc;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2225
        while ((tab = table) == null || tab.length == 0) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2226
            if ((sc = sizeCtl) < 0)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2227
                Thread.yield(); // lost initialization race; just spin
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2228
            else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2229
                try {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2230
                    if ((tab = table) == null || tab.length == 0) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2231
                        int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2232
                        @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2233
                        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
  2234
                        table = tab = nt;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2235
                        sc = n - (n >>> 2);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2236
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2237
                } finally {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2238
                    sizeCtl = sc;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2239
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2240
                break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2241
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2242
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2243
        return tab;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2244
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2245
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2246
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2247
     * Adds to count, and if table is too small and not already
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2248
     * resizing, initiates transfer. If already resizing, helps
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2249
     * perform transfer if work is available.  Rechecks occupancy
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2250
     * after a transfer to see if another resize is already needed
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2251
     * because resizings are lagging additions.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2252
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2253
     * @param x the count to add
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2254
     * @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
  2255
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2256
    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
  2257
        CounterCell[] as; long b, s;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2258
        if ((as = counterCells) != null ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2259
            !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
  2260
            CounterCell a; long v; int m;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2261
            boolean uncontended = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2262
            if (as == null || (m = as.length - 1) < 0 ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2263
                (a = as[ThreadLocalRandom.getProbe() & m]) == null ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2264
                !(uncontended =
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2265
                  U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2266
                fullAddCount(x, uncontended);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2267
                return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2268
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2269
            if (check <= 1)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2270
                return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2271
            s = sumCount();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2272
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2273
        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
  2274
            Node<K,V>[] tab, nt; int n, sc;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2275
            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
  2276
                   (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
  2277
                int rs = resizeStamp(n);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2278
                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
  2279
                    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
  2280
                        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
  2281
                        transferIndex <= 0)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2282
                        break;
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2283
                    if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1))
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2284
                        transfer(tab, nt);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2285
                }
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2286
                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
  2287
                                             (rs << RESIZE_STAMP_SHIFT) + 2))
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2288
                    transfer(tab, null);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2289
                s = sumCount();
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2290
            }
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2291
        }
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2292
    }
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2293
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2294
    /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2295
     * Helps transfer if a resize is in progress.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2296
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2297
    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
  2298
        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
  2299
        if (tab != null && (f instanceof ForwardingNode) &&
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2300
            (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
  2301
            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
  2302
            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
  2303
                   (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
  2304
                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
  2305
                    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
  2306
                    break;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2307
                if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1)) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2308
                    transfer(tab, nextTab);
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2309
                    break;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2310
                }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2311
            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2312
            return nextTab;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2313
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2314
        return table;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2315
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2316
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2317
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2318
     * Tries to presize table to accommodate the given number of elements.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2319
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2320
     * @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
  2321
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2322
    private final void tryPresize(int size) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2323
        int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2324
            tableSizeFor(size + (size >>> 1) + 1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2325
        int sc;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2326
        while ((sc = sizeCtl) >= 0) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2327
            Node<K,V>[] tab = table; int n;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2328
            if (tab == null || (n = tab.length) == 0) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2329
                n = (sc > c) ? sc : c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2330
                if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2331
                    try {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2332
                        if (table == tab) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2333
                            @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2334
                            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
  2335
                            table = nt;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2336
                            sc = n - (n >>> 2);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2337
                        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2338
                    } finally {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2339
                        sizeCtl = sc;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2340
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2341
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2342
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2343
            else if (c <= sc || n >= MAXIMUM_CAPACITY)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2344
                break;
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2345
            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
  2346
                int rs = resizeStamp(n);
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2347
                if (sc < 0) {
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2348
                    Node<K,V>[] nt;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2349
                    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
  2350
                        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
  2351
                        transferIndex <= 0)
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2352
                        break;
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2353
                    if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1))
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2354
                        transfer(tab, nt);
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2355
                }
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2356
                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
  2357
                                             (rs << RESIZE_STAMP_SHIFT) + 2))
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2358
                    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
  2359
            }
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 11279
diff changeset
  2360
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2364
     * Moves and/or copies the nodes in each bin to new table. See
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2365
     * above for explanation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2367
    private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2368
        int n = tab.length, stride;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2369
        if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2370
            stride = MIN_TRANSFER_STRIDE; // subdivide range
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2371
        if (nextTab == null) {            // initiating
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2372
            try {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2373
                @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2374
                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
  2375
                nextTab = nt;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2376
            } catch (Throwable ex) {      // try to cope with OOME
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2377
                sizeCtl = Integer.MAX_VALUE;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2378
                return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2379
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2380
            nextTable = nextTab;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2381
            transferIndex = n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2383
        int nextn = nextTab.length;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2384
        ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2385
        boolean advance = true;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2386
        boolean finishing = false; // to ensure sweep before committing nextTab
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2387
        for (int i = 0, bound = 0;;) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2388
            Node<K,V> f; int fh;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2389
            while (advance) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2390
                int nextIndex, nextBound;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2391
                if (--i >= bound || finishing)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2392
                    advance = false;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2393
                else if ((nextIndex = transferIndex) <= 0) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2394
                    i = -1;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2395
                    advance = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2396
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2397
                else if (U.compareAndSwapInt
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2398
                         (this, TRANSFERINDEX, nextIndex,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2399
                          nextBound = (nextIndex > stride ?
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2400
                                       nextIndex - stride : 0))) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2401
                    bound = nextBound;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2402
                    i = nextIndex - 1;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2403
                    advance = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2404
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2405
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2406
            if (i < 0 || i >= n || i + n >= nextn) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2407
                int sc;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2408
                if (finishing) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2409
                    nextTable = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2410
                    table = nextTab;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2411
                    sizeCtl = (n << 1) - (n >>> 1);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2412
                    return;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2413
                }
21981
48b31d370bc9 8028564: Concurrent calls to CHM.put can fail to add the key/value to the map
psandoz
parents: 19862
diff changeset
  2414
                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
  2415
                    if ((sc - 2) != resizeStamp(n) << RESIZE_STAMP_SHIFT)
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2416
                        return;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2417
                    finishing = advance = true;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2418
                    i = n; // recheck before commit
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2419
                }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2420
            }
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2421
            else if ((f = tabAt(tab, i)) == null)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  2422
                advance = casTabAt(tab, i, null, fwd);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2423
            else if ((fh = f.hash) == MOVED)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2424
                advance = true; // already processed
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2425
            else {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2426
                synchronized (f) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2427
                    if (tabAt(tab, i) == f) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2428
                        Node<K,V> ln, hn;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2429
                        if (fh >= 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2430
                            int runBit = fh & n;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2431
                            Node<K,V> lastRun = f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2432
                            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
  2433
                                int b = p.hash & n;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2434
                                if (b != runBit) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2435
                                    runBit = b;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2436
                                    lastRun = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2437
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2438
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2439
                            if (runBit == 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2440
                                ln = lastRun;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2441
                                hn = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
                            }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2443
                            else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2444
                                hn = lastRun;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2445
                                ln = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2446
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2447
                            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
  2448
                                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
  2449
                                if ((ph & n) == 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2450
                                    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
  2451
                                else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2452
                                    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
  2453
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2454
                            setTabAt(nextTab, i, ln);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2455
                            setTabAt(nextTab, i + n, hn);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2456
                            setTabAt(tab, i, fwd);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2457
                            advance = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
                        }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2459
                        else if (f instanceof TreeBin) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2460
                            TreeBin<K,V> t = (TreeBin<K,V>)f;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2461
                            TreeNode<K,V> lo = null, loTail = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2462
                            TreeNode<K,V> hi = null, hiTail = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2463
                            int lc = 0, hc = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2464
                            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
  2465
                                int h = e.hash;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2466
                                TreeNode<K,V> p = new TreeNode<K,V>
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2467
                                    (h, e.key, e.val, null, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2468
                                if ((h & n) == 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2469
                                    if ((p.prev = loTail) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2470
                                        lo = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2471
                                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2472
                                        loTail.next = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2473
                                    loTail = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2474
                                    ++lc;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2475
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2476
                                else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2477
                                    if ((p.prev = hiTail) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2478
                                        hi = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2479
                                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2480
                                        hiTail.next = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2481
                                    hiTail = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2482
                                    ++hc;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2483
                                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2484
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2485
                            ln = (lc <= UNTREEIFY_THRESHOLD) ? untreeify(lo) :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2486
                                (hc != 0) ? new TreeBin<K,V>(lo) : t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2487
                            hn = (hc <= UNTREEIFY_THRESHOLD) ? untreeify(hi) :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2488
                                (lc != 0) ? new TreeBin<K,V>(hi) : t;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2489
                            setTabAt(nextTab, i, ln);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2490
                            setTabAt(nextTab, i + n, hn);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2491
                            setTabAt(tab, i, fwd);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2492
                            advance = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
            }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2497
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2498
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2499
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2500
    /* ---------------- Counter support -------------- */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2501
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2502
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2503
     * 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
  2504
     * and Striped64.  See their internal docs for explanation.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2505
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2506
    @sun.misc.Contended static final class CounterCell {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2507
        volatile long value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2508
        CounterCell(long x) { value = x; }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2509
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2510
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2511
    final long sumCount() {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2512
        CounterCell[] as = counterCells; CounterCell a;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2513
        long sum = baseCount;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2514
        if (as != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2515
            for (int i = 0; i < as.length; ++i) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2516
                if ((a = as[i]) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2517
                    sum += a.value;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2518
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2519
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2520
        return sum;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2521
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2522
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2523
    // See LongAdder version for explanation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2524
    private final void fullAddCount(long x, boolean wasUncontended) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2525
        int h;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2526
        if ((h = ThreadLocalRandom.getProbe()) == 0) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2527
            ThreadLocalRandom.localInit();      // force initialization
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2528
            h = ThreadLocalRandom.getProbe();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2529
            wasUncontended = true;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2530
        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2531
        boolean collide = false;                // True if last slot nonempty
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2532
        for (;;) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2533
            CounterCell[] as; CounterCell a; int n; long v;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2534
            if ((as = counterCells) != null && (n = as.length) > 0) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2535
                if ((a = as[(n - 1) & h]) == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2536
                    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
  2537
                        CounterCell r = new CounterCell(x); // Optimistic create
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2538
                        if (cellsBusy == 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2539
                            U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2540
                            boolean created = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2541
                            try {               // Recheck under lock
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2542
                                CounterCell[] rs; int m, j;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2543
                                if ((rs = counterCells) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2544
                                    (m = rs.length) > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2545
                                    rs[j = (m - 1) & h] == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2546
                                    rs[j] = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2547
                                    created = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2548
                                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2549
                            } finally {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2550
                                cellsBusy = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2551
                            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2552
                            if (created)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2553
                                break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2554
                            continue;           // Slot is now non-empty
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2555
                        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2556
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2557
                    collide = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2558
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2559
                else if (!wasUncontended)       // CAS already known to fail
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2560
                    wasUncontended = true;      // Continue after rehash
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2561
                else if (U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2562
                    break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2563
                else if (counterCells != as || n >= NCPU)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2564
                    collide = false;            // At max size or stale
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2565
                else if (!collide)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2566
                    collide = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2567
                else if (cellsBusy == 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2568
                         U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2569
                    try {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2570
                        if (counterCells == as) {// Expand table unless stale
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2571
                            CounterCell[] rs = new CounterCell[n << 1];
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2572
                            for (int i = 0; i < n; ++i)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2573
                                rs[i] = as[i];
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2574
                            counterCells = rs;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2575
                        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2576
                    } finally {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2577
                        cellsBusy = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2578
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2579
                    collide = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2580
                    continue;                   // Retry with expanded table
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2581
                }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2582
                h = ThreadLocalRandom.advanceProbe(h);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2583
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2584
            else if (cellsBusy == 0 && counterCells == as &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2585
                     U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2586
                boolean init = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2587
                try {                           // Initialize table
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2588
                    if (counterCells == as) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2589
                        CounterCell[] rs = new CounterCell[2];
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2590
                        rs[h & 1] = new CounterCell(x);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2591
                        counterCells = rs;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2592
                        init = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2593
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2594
                } finally {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2595
                    cellsBusy = 0;
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
                if (init)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2598
                    break;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  2599
            }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2600
            else if (U.compareAndSwapLong(this, BASECOUNT, v = baseCount, v + x))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2601
                break;                          // Fall back on using base
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2602
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2603
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  2604
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2605
    /* ---------------- Conversion from/to TreeBins -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2606
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2607
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2608
     * 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
  2609
     * too small, in which case resizes instead.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2610
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2611
    private final void treeifyBin(Node<K,V>[] tab, int index) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2612
        Node<K,V> b; int n, sc;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2613
        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
  2614
            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
  2615
                tryPresize(n << 1);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2616
            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
  2617
                synchronized (b) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2618
                    if (tabAt(tab, index) == b) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2619
                        TreeNode<K,V> hd = null, tl = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2620
                        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
  2621
                            TreeNode<K,V> p =
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2622
                                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
  2623
                                                  null, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2624
                            if ((p.prev = tl) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2625
                                hd = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2626
                            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2627
                                tl.next = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2628
                            tl = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2629
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2630
                        setTabAt(tab, index, new TreeBin<K,V>(hd));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2631
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2632
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2633
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2634
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2635
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2636
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2637
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2638
     * Returns a list on non-TreeNodes replacing those in given list.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2639
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2640
    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
  2641
        Node<K,V> hd = null, tl = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2642
        for (Node<K,V> q = b; q != null; q = q.next) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2643
            Node<K,V> p = new Node<K,V>(q.hash, q.key, q.val, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2644
            if (tl == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2645
                hd = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2646
            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2647
                tl.next = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2648
            tl = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2649
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2650
        return hd;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2651
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2652
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2653
    /* ---------------- TreeNodes -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2654
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2655
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2656
     * Nodes for use in TreeBins
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2657
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2658
    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
  2659
        TreeNode<K,V> parent;  // red-black tree links
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2660
        TreeNode<K,V> left;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2661
        TreeNode<K,V> right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2662
        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
  2663
        boolean red;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2664
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2665
        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
  2666
                 TreeNode<K,V> parent) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2667
            super(hash, key, val, next);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2668
            this.parent = parent;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2669
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2670
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2671
        Node<K,V> find(int h, Object k) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2672
            return findTreeNode(h, k, null);
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
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2675
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2676
         * 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
  2677
         * starting at given root.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2678
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2679
        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
  2680
            if (k != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2681
                TreeNode<K,V> p = this;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2682
                do  {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2683
                    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
  2684
                    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
  2685
                    if ((ph = p.hash) > h)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2686
                        p = pl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2687
                    else if (ph < h)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2688
                        p = pr;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2689
                    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
  2690
                        return p;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2691
                    else if (pl == null)
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2692
                        p = pr;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2693
                    else if (pr == null)
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2694
                        p = pl;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2695
                    else if ((kc != null ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2696
                              (kc = comparableClassFor(k)) != null) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2697
                             (dir = compareComparables(kc, k, pk)) != 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2698
                        p = (dir < 0) ? pl : pr;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2699
                    else if ((q = pr.findTreeNode(h, k, kc)) != null)
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2700
                        return q;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2701
                    else
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2702
                        p = pl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2703
                } while (p != null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2704
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2705
            return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2706
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2707
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2708
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2709
    /* ---------------- TreeBins -------------- */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2710
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2711
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2712
     * 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
  2713
     * 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
  2714
     * 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
  2715
     * 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
  2716
     * not) to complete before tree restructuring operations.
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
    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
  2719
        TreeNode<K,V> root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2720
        volatile TreeNode<K,V> first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2721
        volatile Thread waiter;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2722
        volatile int lockState;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2723
        // values for lockState
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2724
        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
  2725
        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
  2726
        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
  2727
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2728
        /**
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2729
         * Tie-breaking utility for ordering insertions when equal
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2730
         * hashCodes and non-comparable. We don't require a total
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2731
         * order, just a consistent insertion rule to maintain
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2732
         * equivalence across rebalancings. Tie-breaking further than
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2733
         * necessary simplifies testing a bit.
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2734
         */
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2735
        static int tieBreakOrder(Object a, Object b) {
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2736
            int d;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2737
            if (a == null || b == null ||
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2738
                (d = a.getClass().getName().
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2739
                 compareTo(b.getClass().getName())) == 0)
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2740
                d = (System.identityHashCode(a) <= System.identityHashCode(b) ?
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2741
                     -1 : 1);
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2742
            return d;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2743
        }
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2744
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2745
        /**
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2746
         * 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
  2747
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2748
        TreeBin(TreeNode<K,V> b) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2749
            super(TREEBIN, null, null, null);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2750
            this.first = b;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2751
            TreeNode<K,V> r = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2752
            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
  2753
                next = (TreeNode<K,V>)x.next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2754
                x.left = x.right = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2755
                if (r == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2756
                    x.parent = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2757
                    x.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2758
                    r = x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2759
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2760
                else {
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2761
                    K k = x.key;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2762
                    int h = x.hash;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2763
                    Class<?> kc = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2764
                    for (TreeNode<K,V> p = r;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2765
                        int dir, ph;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2766
                        K pk = p.key;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2767
                        if ((ph = p.hash) > h)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2768
                            dir = -1;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2769
                        else if (ph < h)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2770
                            dir = 1;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2771
                        else if ((kc == null &&
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2772
                                  (kc = comparableClassFor(k)) == null) ||
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2773
                                 (dir = compareComparables(kc, k, pk)) == 0)
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2774
                            dir = tieBreakOrder(k, pk);
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2775
                            TreeNode<K,V> xp = p;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2776
                        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
  2777
                            x.parent = xp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2778
                            if (dir <= 0)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2779
                                xp.left = x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2780
                            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2781
                                xp.right = x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2782
                            r = balanceInsertion(r, x);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2783
                            break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2784
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2785
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2786
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2787
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2788
            this.root = r;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2789
            assert checkInvariants(root);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2790
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2791
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2792
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2793
         * Acquires write lock for tree restructuring.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2794
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2795
        private final void lockRoot() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2796
            if (!U.compareAndSwapInt(this, LOCKSTATE, 0, WRITER))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2797
                contendedLock(); // offload to separate method
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2798
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2799
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2800
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2801
         * Releases write lock for tree restructuring.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2802
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2803
        private final void unlockRoot() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2804
            lockState = 0;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2805
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2806
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2807
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2808
         * Possibly blocks awaiting root lock.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2809
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2810
        private final void contendedLock() {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2811
            boolean waiting = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2812
            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
  2813
                if (((s = lockState) & ~WAITER) == 0) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2814
                    if (U.compareAndSwapInt(this, LOCKSTATE, s, WRITER)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2815
                        if (waiting)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2816
                            waiter = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2817
                        return;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2818
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2819
                }
19413
56082c988815 8023104: ConcurrentHashMap typo
dl
parents: 19380
diff changeset
  2820
                else if ((s & WAITER) == 0) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2821
                    if (U.compareAndSwapInt(this, LOCKSTATE, s, s | WAITER)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2822
                        waiting = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2823
                        waiter = Thread.currentThread();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2824
                    }
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 if (waiting)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2827
                    LockSupport.park(this);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2828
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2829
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2830
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2831
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2832
         * 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
  2833
         * using tree comparisons from root, but continues linear
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2834
         * search when lock not available.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2835
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2836
        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
  2837
            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
  2838
                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
  2839
                    int s; K ek;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2840
                    if (((s = lockState) & (WAITER|WRITER)) != 0) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2841
                        if (e.hash == h &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2842
                            ((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
  2843
                            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
  2844
                        e = e.next;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2845
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2846
                    else if (U.compareAndSwapInt(this, LOCKSTATE, s,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2847
                                                 s + READER)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2848
                        TreeNode<K,V> r, p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2849
                        try {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2850
                            p = ((r = root) == null ? null :
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2851
                                 r.findTreeNode(h, k, null));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2852
                        } finally {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2853
                            Thread w;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2854
                            if (U.getAndAddInt(this, LOCKSTATE, -READER) ==
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2855
                                (READER|WAITER) && (w = waiter) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2856
                                LockSupport.unpark(w);
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
                        return p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2859
                    }
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
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2862
            return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2863
        }
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
         * Finds or adds a node.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2867
         * @return null if added
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
        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
  2870
            Class<?> kc = null;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2871
            boolean searched = false;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2872
            for (TreeNode<K,V> p = root;;) {
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2873
                int dir, ph; K pk;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2874
                if (p == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2875
                    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
  2876
                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2877
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2878
                else if ((ph = p.hash) > h)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2879
                    dir = -1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2880
                else if (ph < h)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2881
                    dir = 1;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2882
                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
  2883
                    return p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2884
                else if ((kc == null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2885
                          (kc = comparableClassFor(k)) == null) ||
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2886
                         (dir = compareComparables(kc, k, pk)) == 0) {
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2887
                    if (!searched) {
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2888
                        TreeNode<K,V> q, ch;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2889
                        searched = true;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2890
                        if (((ch = p.left) != null &&
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2891
                             (q = ch.findTreeNode(h, k, kc)) != null) ||
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2892
                            ((ch = p.right) != null &&
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2893
                             (q = ch.findTreeNode(h, k, kc)) != null))
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2894
                            return q;
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2895
                    }
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2896
                    dir = tieBreakOrder(k, pk);
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2897
                }
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2898
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2899
                TreeNode<K,V> xp = p;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2900
                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
  2901
                    TreeNode<K,V> x, f = first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2902
                    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
  2903
                    if (f != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2904
                        f.prev = x;
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  2905
                    if (dir <= 0)
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2906
                        xp.left = x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2907
                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2908
                        xp.right = x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2909
                    if (!xp.red)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2910
                        x.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2911
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2912
                        lockRoot();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2913
                        try {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2914
                            root = balanceInsertion(root, x);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2915
                        } finally {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2916
                            unlockRoot();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2917
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2918
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2919
                    break;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2920
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2921
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2922
            assert checkInvariants(root);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2923
            return null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2924
        }
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
         * 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
  2928
         * 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
  2929
         * 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
  2930
         * 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
  2931
         * 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
  2932
         * swap the tree linkages.
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2933
         *
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2934
         * @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
  2935
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2936
        final boolean removeTreeNode(TreeNode<K,V> p) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2937
            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
  2938
            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
  2939
            TreeNode<K,V> r, rl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2940
            if (pred == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2941
                first = next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2942
            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2943
                pred.next = next;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2944
            if (next != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2945
                next.prev = pred;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2946
            if (first == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2947
                root = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2948
                return true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2949
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2950
            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
  2951
                (rl = r.left) == null || rl.left == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2952
                return true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2953
            lockRoot();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2954
            try {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2955
                TreeNode<K,V> replacement;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2956
                TreeNode<K,V> pl = p.left;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2957
                TreeNode<K,V> pr = p.right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2958
                if (pl != null && pr != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2959
                    TreeNode<K,V> s = pr, sl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2960
                    while ((sl = s.left) != null) // find successor
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2961
                        s = sl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2962
                    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
  2963
                    TreeNode<K,V> sr = s.right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2964
                    TreeNode<K,V> pp = p.parent;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2965
                    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
  2966
                        p.parent = s;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2967
                        s.right = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2968
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2969
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2970
                        TreeNode<K,V> sp = s.parent;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2971
                        if ((p.parent = sp) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2972
                            if (s == sp.left)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2973
                                sp.left = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2974
                            else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2975
                                sp.right = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2976
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2977
                        if ((s.right = pr) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2978
                            pr.parent = s;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2979
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2980
                    p.left = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2981
                    if ((p.right = sr) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2982
                        sr.parent = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2983
                    if ((s.left = pl) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2984
                        pl.parent = s;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2985
                    if ((s.parent = pp) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2986
                        r = s;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2987
                    else if (p == pp.left)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2988
                        pp.left = s;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2989
                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2990
                        pp.right = s;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2991
                    if (sr != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2992
                        replacement = sr;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2993
                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2994
                        replacement = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2995
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2996
                else if (pl != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2997
                    replacement = pl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2998
                else if (pr != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  2999
                    replacement = pr;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3000
                else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3001
                    replacement = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3002
                if (replacement != p) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3003
                    TreeNode<K,V> pp = replacement.parent = p.parent;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3004
                    if (pp == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3005
                        r = replacement;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3006
                    else if (p == pp.left)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3007
                        pp.left = replacement;
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
                        pp.right = replacement;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3010
                    p.left = p.right = p.parent = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3011
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3012
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3013
                root = (p.red) ? r : balanceDeletion(r, replacement);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3014
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3015
                if (p == replacement) {  // detach pointers
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3016
                    TreeNode<K,V> pp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3017
                    if ((pp = p.parent) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3018
                        if (p == pp.left)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3019
                            pp.left = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3020
                        else if (p == pp.right)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3021
                            pp.right = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3022
                        p.parent = null;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3023
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3024
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3025
            } finally {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3026
                unlockRoot();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3027
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3028
            assert checkInvariants(root);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3029
            return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3030
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3031
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3032
        /* ------------------------------------------------------------ */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3033
        // Red-black tree methods, all adapted from CLR
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
        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
  3036
                                              TreeNode<K,V> p) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3037
            TreeNode<K,V> r, pp, rl;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3038
            if (p != null && (r = p.right) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3039
                if ((rl = p.right = r.left) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3040
                    rl.parent = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3041
                if ((pp = r.parent = p.parent) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3042
                    (root = r).red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3043
                else if (pp.left == p)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3044
                    pp.left = r;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3045
                else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3046
                    pp.right = r;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3047
                r.left = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3048
                p.parent = r;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3049
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3050
            return root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3051
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3052
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3053
        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
  3054
                                               TreeNode<K,V> p) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3055
            TreeNode<K,V> l, pp, lr;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3056
            if (p != null && (l = p.left) != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3057
                if ((lr = p.left = l.right) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3058
                    lr.parent = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3059
                if ((pp = l.parent = p.parent) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3060
                    (root = l).red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3061
                else if (pp.right == p)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3062
                    pp.right = l;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3063
                else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3064
                    pp.left = l;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3065
                l.right = p;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3066
                p.parent = l;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3067
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3068
            return root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3069
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3070
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3071
        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
  3072
                                                    TreeNode<K,V> x) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3073
            x.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3074
            for (TreeNode<K,V> xp, xpp, xppl, xppr;;) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3075
                if ((xp = x.parent) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3076
                    x.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3077
                    return x;
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
                else if (!xp.red || (xpp = xp.parent) == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3080
                    return root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3081
                if (xp == (xppl = xpp.left)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3082
                    if ((xppr = xpp.right) != null && xppr.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3083
                        xppr.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3084
                        xp.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3085
                        xpp.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3086
                        x = xpp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3087
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3088
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3089
                        if (x == xp.right) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3090
                            root = rotateLeft(root, x = xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3091
                            xpp = (xp = x.parent) == null ? null : xp.parent;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3092
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3093
                        if (xp != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3094
                            xp.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3095
                            if (xpp != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3096
                                xpp.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3097
                                root = rotateRight(root, xpp);
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
                        }
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
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3102
                else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3103
                    if (xppl != null && xppl.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3104
                        xppl.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3105
                        xp.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3106
                        xpp.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3107
                        x = xpp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3108
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3109
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3110
                        if (x == xp.left) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3111
                            root = rotateRight(root, x = xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3112
                            xpp = (xp = x.parent) == null ? null : xp.parent;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3113
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3114
                        if (xp != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3115
                            xp.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3116
                            if (xpp != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3117
                                xpp.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3118
                                root = rotateLeft(root, xpp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3119
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3120
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3121
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3122
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3123
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3124
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3125
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3126
        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
  3127
                                                   TreeNode<K,V> x) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3128
            for (TreeNode<K,V> xp, xpl, xpr;;)  {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3129
                if (x == null || x == root)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3130
                    return root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3131
                else if ((xp = x.parent) == null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3132
                    x.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3133
                    return x;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3134
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3135
                else if (x.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3136
                    x.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3137
                    return root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3138
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3139
                else if ((xpl = xp.left) == x) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3140
                    if ((xpr = xp.right) != null && xpr.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3141
                        xpr.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3142
                        xp.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3143
                        root = rotateLeft(root, xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3144
                        xpr = (xp = x.parent) == null ? null : xp.right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3145
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3146
                    if (xpr == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3147
                        x = xp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3148
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3149
                        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
  3150
                        if ((sr == null || !sr.red) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3151
                            (sl == null || !sl.red)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3152
                            xpr.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3153
                            x = xp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3154
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3155
                        else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3156
                            if (sr == null || !sr.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3157
                                if (sl != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3158
                                    sl.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3159
                                xpr.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3160
                                root = rotateRight(root, xpr);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3161
                                xpr = (xp = x.parent) == null ?
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3162
                                    null : xp.right;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3163
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3164
                            if (xpr != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3165
                                xpr.red = (xp == null) ? false : xp.red;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3166
                                if ((sr = xpr.right) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3167
                                    sr.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3168
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3169
                            if (xp != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3170
                                xp.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3171
                                root = rotateLeft(root, xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3172
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3173
                            x = root;
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
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3176
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3177
                else { // symmetric
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3178
                    if (xpl != null && xpl.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3179
                        xpl.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3180
                        xp.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3181
                        root = rotateRight(root, xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3182
                        xpl = (xp = x.parent) == null ? null : xp.left;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3183
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3184
                    if (xpl == null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3185
                        x = xp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3186
                    else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3187
                        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
  3188
                        if ((sl == null || !sl.red) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3189
                            (sr == null || !sr.red)) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3190
                            xpl.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3191
                            x = xp;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3192
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3193
                        else {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3194
                            if (sl == null || !sl.red) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3195
                                if (sr != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3196
                                    sr.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3197
                                xpl.red = true;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3198
                                root = rotateLeft(root, xpl);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3199
                                xpl = (xp = x.parent) == null ?
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3200
                                    null : xp.left;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3201
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3202
                            if (xpl != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3203
                                xpl.red = (xp == null) ? false : xp.red;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3204
                                if ((sl = xpl.left) != null)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3205
                                    sl.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3206
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3207
                            if (xp != null) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3208
                                xp.red = false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3209
                                root = rotateRight(root, xp);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3210
                            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3211
                            x = root;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3212
                        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3213
                    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3214
                }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3215
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3216
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3217
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3218
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3219
         * Recursive invariant check
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
        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
  3222
            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
  3223
                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
  3224
            if (tb != null && tb.next != t)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3225
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3226
            if (tn != null && tn.prev != t)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3227
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3228
            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
  3229
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3230
            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
  3231
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3232
            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
  3233
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3234
            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
  3235
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3236
            if (tl != null && !checkInvariants(tl))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3237
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3238
            if (tr != null && !checkInvariants(tr))
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3239
                return false;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3240
            return true;
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
        private static final sun.misc.Unsafe U;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3244
        private static final long LOCKSTATE;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3245
        static {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3246
            try {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3247
                U = sun.misc.Unsafe.getUnsafe();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3248
                Class<?> k = TreeBin.class;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3249
                LOCKSTATE = U.objectFieldOffset
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3250
                    (k.getDeclaredField("lockState"));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3251
            } catch (Exception e) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3252
                throw new Error(e);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3253
            }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3254
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3255
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3256
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3257
    /* ----------------Table Traversal -------------- */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3258
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3259
    /**
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3260
     * 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
  3261
     * 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
  3262
     * proceeding with current table.
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3263
     */
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3264
    static final class TableStack<K,V> {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3265
        int length;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3266
        int index;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3267
        Node<K,V>[] tab;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3268
        TableStack<K,V> next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3269
    }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3270
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3271
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3272
     * Encapsulates traversal for methods such as containsValue; also
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3273
     * serves as a base class for other iterators and spliterators.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3274
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3275
     * Method advance visits once each still-valid node that was
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3276
     * reachable upon iterator construction. It might miss some that
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3277
     * 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
  3278
     * consistency guarantees. Maintaining this property in the face
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3279
     * of possible ongoing resizes requires a fair amount of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3280
     * bookkeeping state that is difficult to optimize away amidst
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3281
     * volatile accesses.  Even so, traversal maintains reasonable
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3282
     * throughput.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3283
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3284
     * Normally, iteration proceeds bin-by-bin traversing lists.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3285
     * However, if the table has been resized, then all future steps
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3286
     * must traverse both the bin at the current index as well as at
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3287
     * (index + baseSize); and so on for further resizings. To
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3288
     * paranoically cope with potential sharing by users of iterators
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3289
     * across threads, iteration terminates if a bounds checks fails
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3290
     * for a table read.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3291
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3292
    static class Traverser<K,V> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3293
        Node<K,V>[] tab;        // current table; updated if resized
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3294
        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
  3295
        TableStack<K,V> stack, spare; // to save/restore on ForwardingNodes
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3296
        int index;              // index of bin to use next
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3297
        int baseIndex;          // current index of initial table
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3298
        int baseLimit;          // index bound for initial table
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3299
        final int baseSize;     // initial table size
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3300
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3301
        Traverser(Node<K,V>[] tab, int size, int index, int limit) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3302
            this.tab = tab;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3303
            this.baseSize = size;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3304
            this.baseIndex = this.index = index;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3305
            this.baseLimit = limit;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3306
            this.next = null;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3307
        }
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3308
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3309
        /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3310
         * Advances if possible, returning next valid node, or null if none.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
         */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3312
        final Node<K,V> advance() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3313
            Node<K,V> e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3314
            if ((e = next) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3315
                e = e.next;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3316
            for (;;) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3317
                Node<K,V>[] t; int i, n;  // must use locals in checks
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3318
                if (e != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3319
                    return next = e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3320
                if (baseIndex >= baseLimit || (t = tab) == null ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3321
                    (n = t.length) <= (i = index) || i < 0)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3322
                    return next = null;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3323
                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
  3324
                    if (e instanceof ForwardingNode) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3325
                        tab = ((ForwardingNode<K,V>)e).nextTable;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3326
                        e = null;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3327
                        pushState(t, i, n);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3328
                        continue;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3329
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3330
                    else if (e instanceof TreeBin)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3331
                        e = ((TreeBin<K,V>)e).first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3332
                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3333
                        e = null;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3334
                }
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3335
                if (stack != null)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3336
                    recoverState(n);
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3337
                else if ((index = i + baseSize) >= n)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3338
                    index = ++baseIndex; // visit upper slots if present
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
        }
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3341
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3342
        /**
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3343
         * Saves traversal state upon encountering a forwarding node.
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3344
         */
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3345
        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
  3346
            TableStack<K,V> s = spare;  // reuse if possible
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3347
            if (s != null)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3348
                spare = s.next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3349
            else
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3350
                s = new TableStack<K,V>();
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3351
            s.tab = t;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3352
            s.length = n;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3353
            s.index = i;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3354
            s.next = stack;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3355
            stack = s;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3356
        }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3357
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3358
        /**
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3359
         * Possibly pops traversal state.
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3360
         *
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3361
         * @param n length of current table
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3362
         */
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3363
        private void recoverState(int n) {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3364
            TableStack<K,V> s; int len;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3365
            while ((s = stack) != null && (index += (len = s.length)) >= n) {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3366
                n = len;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3367
                index = s.index;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3368
                tab = s.tab;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3369
                s.tab = null;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3370
                TableStack<K,V> next = s.next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3371
                s.next = spare; // save for reuse
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3372
                stack = next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3373
                spare = s;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3374
            }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3375
            if (s == null && (index += baseSize) >= n)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3376
                index = ++baseIndex;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  3377
        }
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3378
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3380
    /**
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3381
     * 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
  3382
     * Traverser to support iterator.remove.
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3383
     */
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3384
    static class BaseIterator<K,V> extends Traverser<K,V> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3385
        final ConcurrentHashMap<K,V> map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3386
        Node<K,V> lastReturned;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3387
        BaseIterator(Node<K,V>[] tab, int size, int index, int limit,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3388
                    ConcurrentHashMap<K,V> map) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3389
            super(tab, size, index, limit);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3390
            this.map = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3391
            advance();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3392
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3393
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3394
        public final boolean hasNext() { return next != null; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3395
        public final boolean hasMoreElements() { return next != null; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3396
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3397
        public final void remove() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3398
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3399
            if ((p = lastReturned) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3400
                throw new IllegalStateException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3401
            lastReturned = null;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3402
            map.replaceNode(p.key, null, null);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3403
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3404
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3405
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3406
    static final class KeyIterator<K,V> extends BaseIterator<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3407
        implements Iterator<K>, Enumeration<K> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3408
        KeyIterator(Node<K,V>[] tab, int index, int size, int limit,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3409
                    ConcurrentHashMap<K,V> map) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3410
            super(tab, index, size, limit, map);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3411
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3412
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3413
        public final K next() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3414
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3415
            if ((p = next) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3416
                throw new NoSuchElementException();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3417
            K k = p.key;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3418
            lastReturned = p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3419
            advance();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3420
            return k;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
        }
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3422
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3423
        public final K nextElement() { return next(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3424
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3425
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3426
    static final class ValueIterator<K,V> extends BaseIterator<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3427
        implements Iterator<V>, Enumeration<V> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3428
        ValueIterator(Node<K,V>[] tab, int index, int size, int limit,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3429
                      ConcurrentHashMap<K,V> map) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3430
            super(tab, index, size, limit, map);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3431
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3432
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3433
        public final V next() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3434
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3435
            if ((p = next) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3436
                throw new NoSuchElementException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3437
            V v = p.val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3438
            lastReturned = p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3439
            advance();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3440
            return v;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3441
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3442
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3443
        public final V nextElement() { return next(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3444
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3445
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3446
    static final class EntryIterator<K,V> extends BaseIterator<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3447
        implements Iterator<Map.Entry<K,V>> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3448
        EntryIterator(Node<K,V>[] tab, int index, int size, int limit,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3449
                      ConcurrentHashMap<K,V> map) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3450
            super(tab, index, size, limit, map);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3451
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3452
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3453
        public final Map.Entry<K,V> next() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3454
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3455
            if ((p = next) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3456
                throw new NoSuchElementException();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3457
            K k = p.key;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3458
            V v = p.val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3459
            lastReturned = p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3460
            advance();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3461
            return new MapEntry<K,V>(k, v, map);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3462
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3465
    /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3466
     * Exported Entry for EntryIterator
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3467
     */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3468
    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
  3469
        final K key; // non-null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3470
        V val;       // non-null
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3471
        final ConcurrentHashMap<K,V> map;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3472
        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
  3473
            this.key = key;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3474
            this.val = val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3475
            this.map = map;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3476
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3477
        public K getKey()        { return key; }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3478
        public V getValue()      { return val; }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3479
        public int hashCode()    { return key.hashCode() ^ val.hashCode(); }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3480
        public String toString() { return key + "=" + val; }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3481
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3482
        public boolean equals(Object o) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3483
            Object k, v; Map.Entry<?,?> e;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3484
            return ((o instanceof Map.Entry) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3485
                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3486
                    (v = e.getValue()) != null &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3487
                    (k == key || k.equals(key)) &&
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3488
                    (v == val || v.equals(val)));
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3489
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3490
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3491
        /**
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3492
         * 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
  3493
         * 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
  3494
         * necessarily track asynchronous changes, the most recent
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3495
         * "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
  3496
         * 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
  3497
         * 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
  3498
         */
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3499
        public V setValue(V value) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3500
            if (value == null) throw new NullPointerException();
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3501
            V v = val;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3502
            val = value;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3503
            map.put(key, value);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3504
            return v;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3505
        }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3506
    }
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3507
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3508
    static final class KeySpliterator<K,V> extends Traverser<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3509
        implements Spliterator<K> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3510
        long est;               // size estimate
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3511
        KeySpliterator(Node<K,V>[] tab, int size, int index, int limit,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3512
                       long est) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3513
            super(tab, size, index, limit);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3514
            this.est = est;
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 Spliterator<K> trySplit() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3518
            int i, f, h;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3519
            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3520
                new KeySpliterator<K,V>(tab, baseSize, baseLimit = h,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3521
                                        f, est >>>= 1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3522
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3523
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3524
        public void forEachRemaining(Consumer<? super K> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3525
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3526
            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
  3527
                action.accept(p.key);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3528
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3529
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3530
        public boolean tryAdvance(Consumer<? super K> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3531
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3532
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3533
            if ((p = advance()) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3534
                return false;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3535
            action.accept(p.key);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3536
            return true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3537
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3538
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3539
        public long estimateSize() { return est; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3540
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3541
        public int characteristics() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3542
            return Spliterator.DISTINCT | Spliterator.CONCURRENT |
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3543
                Spliterator.NONNULL;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3544
        }
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3545
    }
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3546
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3547
    static final class ValueSpliterator<K,V> extends Traverser<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3548
        implements Spliterator<V> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3549
        long est;               // size estimate
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3550
        ValueSpliterator(Node<K,V>[] tab, int size, int index, int limit,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3551
                         long est) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3552
            super(tab, size, index, limit);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3553
            this.est = est;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3554
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3555
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3556
        public Spliterator<V> trySplit() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3557
            int i, f, h;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3558
            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3559
                new ValueSpliterator<K,V>(tab, baseSize, baseLimit = h,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3560
                                          f, est >>>= 1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3561
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3562
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3563
        public void forEachRemaining(Consumer<? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3564
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3565
            for (Node<K,V> p; (p = advance()) != null;)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3566
                action.accept(p.val);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3567
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3568
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3569
        public boolean tryAdvance(Consumer<? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3570
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3571
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3572
            if ((p = advance()) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3573
                return false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3574
            action.accept(p.val);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3575
            return true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3576
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3577
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3578
        public long estimateSize() { return est; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3579
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3580
        public int characteristics() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3581
            return Spliterator.CONCURRENT | Spliterator.NONNULL;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3582
        }
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  3583
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3585
    static final class EntrySpliterator<K,V> extends Traverser<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3586
        implements Spliterator<Map.Entry<K,V>> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3587
        final ConcurrentHashMap<K,V> map; // To export MapEntry
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3588
        long est;               // size estimate
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3589
        EntrySpliterator(Node<K,V>[] tab, int size, int index, int limit,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3590
                         long est, ConcurrentHashMap<K,V> map) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3591
            super(tab, size, index, limit);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3592
            this.map = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3593
            this.est = est;
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 Spliterator<Map.Entry<K,V>> trySplit() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3597
            int i, f, h;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3598
            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3599
                new EntrySpliterator<K,V>(tab, baseSize, baseLimit = h,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3600
                                          f, est >>>= 1, map);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3601
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3602
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3603
        public void forEachRemaining(Consumer<? super Map.Entry<K,V>> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3604
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3605
            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
  3606
                action.accept(new MapEntry<K,V>(p.key, p.val, map));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3607
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3608
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3609
        public boolean tryAdvance(Consumer<? super Map.Entry<K,V>> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3610
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3611
            Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3612
            if ((p = advance()) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3613
                return false;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3614
            action.accept(new MapEntry<K,V>(p.key, p.val, map));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3615
            return true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3616
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3617
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3618
        public long estimateSize() { return est; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3619
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3620
        public int characteristics() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3621
            return Spliterator.DISTINCT | Spliterator.CONCURRENT |
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3622
                Spliterator.NONNULL;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3623
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3624
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3625
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3626
    // Parallel bulk operations
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
     * Computes initial batch value for bulk tasks. The returned value
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3630
     * is approximately exp2 of the number of times (minus one) to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3631
     * split task by two before executing leaf action. This value is
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3632
     * faster to compute and more convenient to use as a guide to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3633
     * splitting than is the depth, since it is used while dividing by
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3634
     * two anyway.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3635
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3636
    final int batchFor(long b) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3637
        long n;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3638
        if (b == Long.MAX_VALUE || (n = sumCount()) <= 1L || n < b)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3639
            return 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3640
        int sp = ForkJoinPool.getCommonPoolParallelism() << 2; // slack of 4
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3641
        return (b <= 0L || (n /= b) >= sp) ? sp : (int)n;
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
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3645
     * Performs the given action for each (key, value).
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3646
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3647
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3648
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3649
     * @param action the action
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3650
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3651
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3652
    public void forEach(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3653
                        BiConsumer<? super K,? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3654
        if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3655
        new ForEachMappingTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3656
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3657
             action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3658
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3659
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3660
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3661
     * Performs the given action for each non-null transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3662
     * of each (key, value).
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3663
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3664
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3665
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3666
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3667
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3668
     * which case the action is not applied)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3669
     * @param action the action
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  3670
     * @param <U> the return type of the transformer
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3671
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3672
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3673
    public <U> void forEach(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3674
                            BiFunction<? super K, ? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3675
                            Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3676
        if (transformer == null || action == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3677
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3678
        new ForEachTransformedMappingTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3679
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3680
             transformer, action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3681
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3682
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3683
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3684
     * Returns a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3685
     * function on each (key, value), or null if none.  Upon
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3686
     * success, further element processing is suppressed and the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3687
     * results of any other parallel invocations of the search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3688
     * function are ignored.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3689
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3690
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3691
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3692
     * @param searchFunction a function returning a non-null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3693
     * result on success, else null
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  3694
     * @param <U> the return type of the search function
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3695
     * @return a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3696
     * 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
  3697
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3698
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3699
    public <U> U search(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3700
                        BiFunction<? super K, ? super V, ? extends U> searchFunction) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3701
        if (searchFunction == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3702
        return new SearchMappingsTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3703
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3704
             searchFunction, new AtomicReference<U>()).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3705
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3706
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3707
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3708
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3709
     * of all (key, value) pairs using the given reducer to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3710
     * combine values, or null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3711
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3712
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3713
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3714
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3715
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3716
     * which case it is not combined)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3717
     * @param reducer a commutative associative combining function
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  3718
     * @param <U> the return type of the transformer
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3719
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3720
     * of all (key, value) pairs
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3721
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3722
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3723
    public <U> U reduce(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3724
                        BiFunction<? super K, ? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3725
                        BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3726
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3727
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3728
        return new MapReduceMappingsTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3729
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3730
             null, transformer, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3731
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3732
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3733
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3734
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3735
     * of all (key, value) pairs using the given reducer to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3736
     * combine values, and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3737
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3738
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3739
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3740
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3741
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3742
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3743
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3744
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3745
     * of all (key, value) pairs
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3746
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3747
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3748
    public double reduceToDouble(long parallelismThreshold,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3749
                                 ToDoubleBiFunction<? super K, ? super V> transformer,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3750
                                 double basis,
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  3751
                                 DoubleBinaryOperator reducer) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3752
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3753
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3754
        return new MapReduceMappingsToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3755
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3756
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3757
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3758
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3759
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3760
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3761
     * of all (key, value) pairs using the given reducer to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3762
     * combine values, and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3763
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3764
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3765
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3766
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3767
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3768
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3769
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3770
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3771
     * of all (key, value) pairs
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3772
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3773
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3774
    public long reduceToLong(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3775
                             ToLongBiFunction<? super K, ? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3776
                             long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3777
                             LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3778
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3779
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3780
        return new MapReduceMappingsToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3781
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3782
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3783
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3784
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3785
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3786
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3787
     * of all (key, value) pairs using the given reducer to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3788
     * combine values, and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3789
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3790
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3791
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3792
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3793
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3794
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3795
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3796
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3797
     * of all (key, value) pairs
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3798
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3799
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3800
    public int reduceToInt(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3801
                           ToIntBiFunction<? super K, ? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3802
                           int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3803
                           IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3804
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3805
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3806
        return new MapReduceMappingsToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3807
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3808
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3809
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3810
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3811
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3812
     * Performs the given action for each key.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3813
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3814
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3815
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3816
     * @param action the action
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3817
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3818
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3819
    public void forEachKey(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3820
                           Consumer<? super K> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3821
        if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3822
        new ForEachKeyTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3823
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3824
             action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3825
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3826
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3827
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3828
     * Performs the given action for each non-null transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3829
     * of each key.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3830
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3831
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3832
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3833
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3834
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3835
     * which case the action is not applied)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3836
     * @param action the action
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  3837
     * @param <U> the return type of the transformer
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 <U> void forEachKey(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3841
                               Function<? super K, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3842
                               Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3843
        if (transformer == null || action == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3844
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3845
        new ForEachTransformedKeyTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3846
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3847
             transformer, action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3848
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3849
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3850
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3851
     * Returns a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3852
     * function on each key, or null if none. Upon success,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3853
     * further element processing is suppressed and the results of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3854
     * any other parallel invocations of the search function are
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3855
     * ignored.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3856
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3857
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3858
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3859
     * @param searchFunction a function returning a non-null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3860
     * result on success, else null
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  3861
     * @param <U> the return type of the search function
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3862
     * @return a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3863
     * function on each key, or null if none
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 <U> U searchKeys(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3867
                            Function<? super K, ? extends U> searchFunction) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3868
        if (searchFunction == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3869
        return new SearchKeysTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3870
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3871
             searchFunction, new AtomicReference<U>()).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3872
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3873
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3874
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3875
     * Returns the result of accumulating all keys using the given
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3876
     * reducer to combine values, or null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3877
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3878
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3879
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3880
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3881
     * @return the result of accumulating all keys using the given
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3882
     * reducer to combine values, or null if none
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 K reduceKeys(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3886
                        BiFunction<? super K, ? super K, ? extends K> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3887
        if (reducer == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3888
        return new ReduceKeysTask<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
             null, reducer).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
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3895
     * of all keys using the given reducer to combine values, or
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3896
     * null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3897
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3898
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3899
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3900
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3901
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3902
     * which case it is not combined)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3903
     * @param reducer a commutative associative combining function
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  3904
     * @param <U> the return type of the transformer
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3905
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3906
     * of all keys
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3907
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3908
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3909
    public <U> U reduceKeys(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3910
                            Function<? super K, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3911
         BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3912
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3913
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3914
        return new MapReduceKeysTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3915
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3916
             null, transformer, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3917
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3918
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3919
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3920
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3921
     * of all keys using the given reducer to combine values, and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3922
     * the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3923
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3924
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3925
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3926
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3927
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3928
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3929
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3930
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3931
     * of all keys
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3932
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3933
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3934
    public double reduceKeysToDouble(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3935
                                     ToDoubleFunction<? super K> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3936
                                     double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3937
                                     DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3938
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3939
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3940
        return new MapReduceKeysToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3941
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3942
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3943
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3944
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3945
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3946
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3947
     * of all keys using the given reducer to combine values, and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3948
     * the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3949
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3950
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3951
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3952
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3953
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3954
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3955
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3956
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3957
     * of all keys
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3958
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3959
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3960
    public long reduceKeysToLong(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3961
                                 ToLongFunction<? super K> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3962
                                 long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3963
                                 LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3964
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3965
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3966
        return new MapReduceKeysToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3967
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3968
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3969
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3970
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3971
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3972
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3973
     * of all keys using the given reducer to combine values, and
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3974
     * the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3975
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3976
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3977
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3978
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3979
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3980
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3981
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3982
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3983
     * of all keys
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  3984
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3985
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3986
    public int reduceKeysToInt(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3987
                               ToIntFunction<? super K> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3988
                               int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3989
                               IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3990
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3991
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3992
        return new MapReduceKeysToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3993
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3994
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3995
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3996
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3997
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3998
     * Performs the given action for each value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  3999
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4000
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4001
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4002
     * @param action the action
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4003
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4004
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4005
    public void forEachValue(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4006
                             Consumer<? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4007
        if (action == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4008
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4009
        new ForEachValueTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4010
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4011
             action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4012
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4013
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4014
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4015
     * Performs the given action for each non-null transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4016
     * of each value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4017
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4018
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4019
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4020
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4021
     * for an element, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4022
     * which case the action is not applied)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4023
     * @param action the action
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  4024
     * @param <U> the return type of the transformer
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4025
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4026
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4027
    public <U> void forEachValue(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4028
                                 Function<? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4029
                                 Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4030
        if (transformer == null || action == 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
        new ForEachTransformedValueTask<K,V,U>
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
             transformer, action).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 a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4039
     * function on each value, or null if none.  Upon success,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4040
     * further element processing is suppressed and the results of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4041
     * any other parallel invocations of the search function are
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4042
     * ignored.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4043
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4044
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4045
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4046
     * @param searchFunction a function returning a non-null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4047
     * result on success, else null
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  4048
     * @param <U> the return type of the search function
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4049
     * @return a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4050
     * function on each value, or null if none
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4051
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4052
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4053
    public <U> U searchValues(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4054
                              Function<? super V, ? extends U> searchFunction) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4055
        if (searchFunction == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4056
        return new SearchValuesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4057
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4058
             searchFunction, new AtomicReference<U>()).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4059
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4060
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4061
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4062
     * Returns the result of accumulating all values using the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4063
     * given reducer to combine values, or null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4064
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4065
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4066
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4067
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4068
     * @return the result of accumulating all values
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 V reduceValues(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4072
                          BiFunction<? super V, ? super V, ? extends V> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4073
        if (reducer == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4074
        return new ReduceValuesTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4075
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4076
             null, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4077
    }
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
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4081
     * of all values using the given reducer to combine values, or
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4082
     * null if none.
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 it is not combined)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4089
     * @param reducer a commutative associative combining function
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  4090
     * @param <U> the return type of the transformer
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4091
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4092
     * of all values
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4093
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4094
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4095
    public <U> U reduceValues(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4096
                              Function<? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4097
                              BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4098
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4099
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4100
        return new MapReduceValuesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4101
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4102
             null, transformer, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4103
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4104
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4105
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4106
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4107
     * of all values using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4108
     * and the given basis as an identity value.
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 transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4113
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4114
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4115
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4116
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4117
     * of all values
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4118
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4119
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4120
    public double reduceValuesToDouble(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4121
                                       ToDoubleFunction<? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4122
                                       double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4123
                                       DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4124
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4125
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4126
        return new MapReduceValuesToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4127
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4128
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4129
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4130
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4131
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4132
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4133
     * of all values using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4134
     * and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4135
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4136
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4137
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4138
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4139
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4140
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4141
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4142
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4143
     * of all values
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4144
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4145
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4146
    public long reduceValuesToLong(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4147
                                   ToLongFunction<? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4148
                                   long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4149
                                   LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4150
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4151
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4152
        return new MapReduceValuesToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4153
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4154
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4155
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4156
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4157
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4158
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4159
     * of all values using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4160
     * and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4161
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4162
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4163
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4164
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4165
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4166
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4167
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4168
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4169
     * of all values
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4170
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4171
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4172
    public int reduceValuesToInt(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4173
                                 ToIntFunction<? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4174
                                 int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4175
                                 IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4176
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4177
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4178
        return new MapReduceValuesToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4179
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4180
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4181
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4182
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4183
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4184
     * Performs the given action for each entry.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4185
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4186
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4187
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4188
     * @param action the action
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4189
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4190
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4191
    public void forEachEntry(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4192
                             Consumer<? super Map.Entry<K,V>> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4193
        if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4194
        new ForEachEntryTask<K,V>(null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4195
                                  action).invoke();
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
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4199
     * Performs the given action for each non-null transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4200
     * of each entry.
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, or null if there is no transformation (in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4206
     * which case the action is not applied)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4207
     * @param action the action
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  4208
     * @param <U> the return type of the transformer
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4209
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4210
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4211
    public <U> void forEachEntry(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4212
                                 Function<Map.Entry<K,V>, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4213
                                 Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4214
        if (transformer == null || action == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4215
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4216
        new ForEachTransformedEntryTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4217
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4218
             transformer, action).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4219
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4220
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4221
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4222
     * Returns a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4223
     * function on each entry, or null if none.  Upon success,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4224
     * further element processing is suppressed and the results of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4225
     * any other parallel invocations of the search function are
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4226
     * ignored.
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 searchFunction a function returning a non-null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4231
     * result on success, else null
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  4232
     * @param <U> the return type of the search function
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4233
     * @return a non-null result from applying the given search
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4234
     * function on each entry, or null if none
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4235
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4236
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4237
    public <U> U searchEntries(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4238
                               Function<Map.Entry<K,V>, ? extends U> searchFunction) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4239
        if (searchFunction == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4240
        return new SearchEntriesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4241
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4242
             searchFunction, new AtomicReference<U>()).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4243
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4244
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4245
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4246
     * Returns the result of accumulating all entries using the
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4247
     * given reducer to combine values, or null if none.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4248
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4249
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4250
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4251
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4252
     * @return the result of accumulating all entries
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4253
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4254
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4255
    public Map.Entry<K,V> reduceEntries(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4256
                                        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
  4257
        if (reducer == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4258
        return new ReduceEntriesTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4259
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4260
             null, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4261
    }
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
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4265
     * of all entries using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4266
     * or null if none.
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 it is not combined)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4273
     * @param reducer a commutative associative combining function
19036
7e9050510b51 8020976: Ensure consistent insertion for ConcurrentHashMap
dl
parents: 18802
diff changeset
  4274
     * @param <U> the return type of the transformer
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4275
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4276
     * of all entries
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4277
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4278
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4279
    public <U> U reduceEntries(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4280
                               Function<Map.Entry<K,V>, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4281
                               BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4282
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4283
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4284
        return new MapReduceEntriesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4285
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4286
             null, transformer, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4287
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4288
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4289
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4290
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4291
     * of all entries using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4292
     * and the given basis as an identity value.
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 transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4297
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4298
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4299
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4300
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4301
     * of all entries
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4302
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4303
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4304
    public double reduceEntriesToDouble(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4305
                                        ToDoubleFunction<Map.Entry<K,V>> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4306
                                        double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4307
                                        DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4308
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4309
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4310
        return new MapReduceEntriesToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4311
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4312
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4313
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4314
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4315
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4316
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4317
     * of all entries using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4318
     * and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4319
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4320
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4321
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4322
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4323
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4324
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4325
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4326
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4327
     * of all entries
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4328
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4329
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4330
    public long reduceEntriesToLong(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4331
                                    ToLongFunction<Map.Entry<K,V>> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4332
                                    long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4333
                                    LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4334
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4335
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4336
        return new MapReduceEntriesToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4337
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4338
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4339
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4340
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4341
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4342
     * Returns the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4343
     * of all entries using the given reducer to combine values,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4344
     * and the given basis as an identity value.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4345
     *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4346
     * @param parallelismThreshold the (estimated) number of elements
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4347
     * needed for this operation to be executed in parallel
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4348
     * @param transformer a function returning the transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4349
     * for an element
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4350
     * @param basis the identity (initial default value) for the reduction
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4351
     * @param reducer a commutative associative combining function
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4352
     * @return the result of accumulating the given transformation
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4353
     * of all entries
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4354
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4355
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4356
    public int reduceEntriesToInt(long parallelismThreshold,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4357
                                  ToIntFunction<Map.Entry<K,V>> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4358
                                  int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4359
                                  IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4360
        if (transformer == null || reducer == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4361
            throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4362
        return new MapReduceEntriesToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4363
            (null, batchFor(parallelismThreshold), 0, 0, table,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4364
             null, transformer, basis, reducer).invoke();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4365
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4366
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4367
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4368
    /* ----------------Views -------------- */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4369
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4370
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4371
     * Base class for views.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4372
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4373
    abstract static class CollectionView<K,V,E>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4374
        implements Collection<E>, java.io.Serializable {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4375
        private static final long serialVersionUID = 7249069246763182397L;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4376
        final ConcurrentHashMap<K,V> map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4377
        CollectionView(ConcurrentHashMap<K,V> map)  { this.map = map; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4378
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4379
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4380
         * Returns the map backing this view.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4381
         *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4382
         * @return the map backing this view
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4383
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4384
        public ConcurrentHashMap<K,V> getMap() { return map; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4385
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4386
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4387
         * Removes all of the elements from this view, by removing all
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4388
         * the mappings from the map backing this view.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4389
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4390
        public final void clear()      { map.clear(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4391
        public final int size()        { return map.size(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4392
        public final boolean isEmpty() { return map.isEmpty(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4393
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4394
        // implementations below rely on concrete classes supplying these
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4395
        // abstract methods
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4396
        /**
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  4397
         * 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
  4398
         *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  4399
         * <p>The returned iterator is
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  4400
         * <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
  4401
         *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19413
diff changeset
  4402
         * @return an iterator over the elements in this collection
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4403
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4404
        public abstract Iterator<E> iterator();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4405
        public abstract boolean contains(Object o);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4406
        public abstract boolean remove(Object o);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4407
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4408
        private static final String oomeMsg = "Required array size too large";
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4409
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4410
        public final Object[] toArray() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4411
            long sz = map.mappingCount();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4412
            if (sz > MAX_ARRAY_SIZE)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4413
                throw new OutOfMemoryError(oomeMsg);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4414
            int n = (int)sz;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4415
            Object[] r = new Object[n];
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4416
            int i = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4417
            for (E e : this) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4418
                if (i == n) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4419
                    if (n >= MAX_ARRAY_SIZE)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4420
                        throw new OutOfMemoryError(oomeMsg);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4421
                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4422
                        n = MAX_ARRAY_SIZE;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4423
                    else
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4424
                        n += (n >>> 1) + 1;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4425
                    r = Arrays.copyOf(r, n);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4426
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4427
                r[i++] = e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4428
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4429
            return (i == n) ? r : Arrays.copyOf(r, i);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4430
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4431
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4432
        @SuppressWarnings("unchecked")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4433
        public final <T> T[] toArray(T[] a) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4434
            long sz = map.mappingCount();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4435
            if (sz > MAX_ARRAY_SIZE)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4436
                throw new OutOfMemoryError(oomeMsg);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4437
            int m = (int)sz;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4438
            T[] r = (a.length >= m) ? a :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4439
                (T[])java.lang.reflect.Array
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4440
                .newInstance(a.getClass().getComponentType(), m);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4441
            int n = r.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4442
            int i = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4443
            for (E e : this) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4444
                if (i == n) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4445
                    if (n >= MAX_ARRAY_SIZE)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4446
                        throw new OutOfMemoryError(oomeMsg);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4447
                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4448
                        n = MAX_ARRAY_SIZE;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4449
                    else
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4450
                        n += (n >>> 1) + 1;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4451
                    r = Arrays.copyOf(r, n);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4452
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4453
                r[i++] = (T)e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4454
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4455
            if (a == r && i < n) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4456
                r[i] = null; // null-terminate
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4457
                return r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4458
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4459
            return (i == n) ? r : Arrays.copyOf(r, i);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4460
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4461
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4462
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4463
         * Returns a string representation of this collection.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4464
         * The string representation consists of the string representations
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4465
         * of the collection's elements in the order they are returned by
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4466
         * its iterator, enclosed in square brackets ({@code "[]"}).
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4467
         * Adjacent elements are separated by the characters {@code ", "}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4468
         * (comma and space).  Elements are converted to strings as by
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4469
         * {@link String#valueOf(Object)}.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4470
         *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4471
         * @return a string representation of this collection
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4472
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4473
        public final String toString() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4474
            StringBuilder sb = new StringBuilder();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4475
            sb.append('[');
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4476
            Iterator<E> it = iterator();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4477
            if (it.hasNext()) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4478
                for (;;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4479
                    Object e = it.next();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4480
                    sb.append(e == this ? "(this Collection)" : e);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4481
                    if (!it.hasNext())
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4482
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4483
                    sb.append(',').append(' ');
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4484
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4485
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4486
            return sb.append(']').toString();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4487
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4488
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4489
        public final boolean containsAll(Collection<?> c) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4490
            if (c != this) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4491
                for (Object e : c) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4492
                    if (e == null || !contains(e))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4493
                        return false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4494
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4495
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4496
            return true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4497
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4498
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4499
        public final 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
  4500
            if (c == null) throw new NullPointerException();
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4501
            boolean modified = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4502
            for (Iterator<E> it = iterator(); it.hasNext();) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4503
                if (c.contains(it.next())) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4504
                    it.remove();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4505
                    modified = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4506
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4507
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4508
            return modified;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4509
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4510
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4511
        public final 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
  4512
            if (c == null) throw new NullPointerException();
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4513
            boolean modified = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4514
            for (Iterator<E> it = iterator(); it.hasNext();) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4515
                if (!c.contains(it.next())) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4516
                    it.remove();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4517
                    modified = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4518
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4519
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4520
            return modified;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4521
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4522
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4523
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4524
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4525
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4526
     * A view of a ConcurrentHashMap as a {@link Set} of keys, in
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4527
     * which additions may optionally be enabled by mapping to a
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4528
     * common value.  This class cannot be directly instantiated.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4529
     * See {@link #keySet() keySet()},
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4530
     * {@link #keySet(Object) keySet(V)},
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4531
     * {@link #newKeySet() newKeySet()},
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4532
     * {@link #newKeySet(int) newKeySet(int)}.
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4533
     *
17953
9a56976d1ab2 8015963: Add at since tags to new ConcurrentHashMap methods
chegar
parents: 17945
diff changeset
  4534
     * @since 1.8
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4535
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4536
    public static class KeySetView<K,V> extends CollectionView<K,V,K>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4537
        implements Set<K>, java.io.Serializable {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4538
        private static final long serialVersionUID = 7249069246763182397L;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4539
        private final V value;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4540
        KeySetView(ConcurrentHashMap<K,V> map, V value) {  // non-public
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4541
            super(map);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4542
            this.value = value;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4543
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4544
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4545
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4546
         * Returns the default mapped value for additions,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4547
         * or {@code null} if additions are not supported.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4548
         *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4549
         * @return the default mapped value for additions, or {@code null}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4550
         * if not supported
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4551
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4552
        public V getMappedValue() { return value; }
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
         * {@inheritDoc}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4556
         * @throws NullPointerException if the specified key is null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4557
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4558
        public boolean contains(Object o) { return map.containsKey(o); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4559
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4560
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4561
         * Removes the key from this map view, by removing the key (and its
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4562
         * corresponding value) from the backing map.  This method does
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4563
         * nothing if the key is not in the map.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4564
         *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4565
         * @param  o the key to be removed from the backing map
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4566
         * @return {@code true} if the backing map contained the specified key
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4567
         * @throws NullPointerException if the specified key is null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4568
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4569
        public boolean remove(Object o) { return map.remove(o) != null; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4570
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4571
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4572
         * @return an iterator over the keys of the backing map
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4573
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4574
        public Iterator<K> iterator() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4575
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4576
            ConcurrentHashMap<K,V> m = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4577
            int f = (t = m.table) == null ? 0 : t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4578
            return new KeyIterator<K,V>(t, f, 0, f, m);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4579
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4580
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4581
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4582
         * Adds the specified key to this set view by mapping the key to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4583
         * the default mapped value in the backing map, if defined.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4584
         *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4585
         * @param e key to be added
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4586
         * @return {@code true} if this set changed as a result of the call
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4587
         * @throws NullPointerException if the specified key is null
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4588
         * @throws UnsupportedOperationException if no default mapped value
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4589
         * for additions was provided
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4590
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4591
        public boolean add(K e) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4592
            V v;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4593
            if ((v = value) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4594
                throw new UnsupportedOperationException();
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4595
            return map.putVal(e, v, true) == null;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4596
        }
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
         * Adds all of the elements in the specified collection to this set,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4600
         * as if by calling {@link #add} on each one.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4601
         *
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4602
         * @param c the elements to be inserted into this set
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4603
         * @return {@code true} if this set changed as a result of the call
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4604
         * @throws NullPointerException if the collection or any of its
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4605
         * elements are {@code null}
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4606
         * @throws UnsupportedOperationException if no default mapped value
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4607
         * for additions was provided
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4608
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4609
        public boolean addAll(Collection<? extends K> c) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4610
            boolean added = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4611
            V v;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4612
            if ((v = value) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4613
                throw new UnsupportedOperationException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4614
            for (K e : c) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4615
                if (map.putVal(e, v, true) == null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4616
                    added = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4617
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4618
            return added;
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
        public int hashCode() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4622
            int h = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4623
            for (K e : this)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4624
                h += e.hashCode();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4625
            return h;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4626
        }
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 boolean equals(Object o) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4629
            Set<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4630
            return ((o instanceof Set) &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4631
                    ((c = (Set<?>)o) == this ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4632
                     (containsAll(c) && c.containsAll(this))));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4633
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4634
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4635
        public Spliterator<K> spliterator() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4636
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4637
            ConcurrentHashMap<K,V> m = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4638
            long n = m.sumCount();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4639
            int f = (t = m.table) == null ? 0 : t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4640
            return new KeySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4641
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4642
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4643
        public void forEach(Consumer<? super K> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4644
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4645
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4646
            if ((t = map.table) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4647
                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
  4648
                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
  4649
                    action.accept(p.key);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4650
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4651
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4652
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4653
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4654
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4655
     * A view of a ConcurrentHashMap as a {@link Collection} of
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4656
     * values, in which additions are disabled. This class cannot be
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4657
     * directly instantiated. See {@link #values()}.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4658
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4659
    static final class ValuesView<K,V> extends CollectionView<K,V,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4660
        implements Collection<V>, java.io.Serializable {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4661
        private static final long serialVersionUID = 2249069246763182397L;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4662
        ValuesView(ConcurrentHashMap<K,V> map) { super(map); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4663
        public final boolean contains(Object o) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4664
            return map.containsValue(o);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4665
        }
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 final boolean remove(Object o) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4668
            if (o != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4669
                for (Iterator<V> it = iterator(); it.hasNext();) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4670
                    if (o.equals(it.next())) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4671
                        it.remove();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4672
                        return true;
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
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4676
            return false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4677
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4678
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4679
        public final Iterator<V> iterator() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4680
            ConcurrentHashMap<K,V> m = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4681
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4682
            int f = (t = m.table) == null ? 0 : t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4683
            return new ValueIterator<K,V>(t, f, 0, f, m);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4684
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4685
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4686
        public final boolean add(V e) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4687
            throw new UnsupportedOperationException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4688
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4689
        public final boolean addAll(Collection<? extends V> c) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4690
            throw new UnsupportedOperationException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4691
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4692
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4693
        public Spliterator<V> spliterator() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4694
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4695
            ConcurrentHashMap<K,V> m = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4696
            long n = m.sumCount();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4697
            int f = (t = m.table) == null ? 0 : t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4698
            return new ValueSpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4699
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4700
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4701
        public void forEach(Consumer<? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4702
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4703
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4704
            if ((t = map.table) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4705
                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
  4706
                for (Node<K,V> p; (p = it.advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4707
                    action.accept(p.val);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4708
            }
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
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4712
    /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4713
     * A view of a ConcurrentHashMap as a {@link Set} of (key, value)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4714
     * entries.  This class cannot be directly instantiated. See
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4715
     * {@link #entrySet()}.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4716
     */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4717
    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
  4718
        implements Set<Map.Entry<K,V>>, java.io.Serializable {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4719
        private static final long serialVersionUID = 2249069246763182397L;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4720
        EntrySetView(ConcurrentHashMap<K,V> map) { super(map); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4721
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4722
        public boolean contains(Object o) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4723
            Object k, v, r; Map.Entry<?,?> e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4724
            return ((o instanceof Map.Entry) &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4725
                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4726
                    (r = map.get(k)) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4727
                    (v = e.getValue()) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4728
                    (v == r || v.equals(r)));
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
        public boolean remove(Object o) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4732
            Object k, v; Map.Entry<?,?> e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4733
            return ((o instanceof Map.Entry) &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4734
                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4735
                    (v = e.getValue()) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4736
                    map.remove(k, v));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4737
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4738
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4739
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4740
         * @return an iterator over the entries of the backing map
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4741
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4742
        public Iterator<Map.Entry<K,V>> iterator() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4743
            ConcurrentHashMap<K,V> m = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4744
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4745
            int f = (t = m.table) == null ? 0 : t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4746
            return new EntryIterator<K,V>(t, f, 0, f, m);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4747
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4748
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4749
        public boolean add(Entry<K,V> e) {
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4750
            return map.putVal(e.getKey(), e.getValue(), false) == null;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4751
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4752
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4753
        public boolean addAll(Collection<? extends Entry<K,V>> c) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4754
            boolean added = false;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4755
            for (Entry<K,V> e : c) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4756
                if (add(e))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4757
                    added = true;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4758
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4759
            return added;
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 int hashCode() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4763
            int h = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4764
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4765
            if ((t = map.table) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4766
                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
  4767
                for (Node<K,V> p; (p = it.advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4768
                    h += p.hashCode();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4769
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4770
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4771
            return h;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4772
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4773
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4774
        public final boolean equals(Object o) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4775
            Set<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4776
            return ((o instanceof Set) &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4777
                    ((c = (Set<?>)o) == this ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4778
                     (containsAll(c) && c.containsAll(this))));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4779
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4780
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4781
        public Spliterator<Map.Entry<K,V>> spliterator() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4782
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4783
            ConcurrentHashMap<K,V> m = map;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4784
            long n = m.sumCount();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4785
            int f = (t = m.table) == null ? 0 : t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4786
            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
  4787
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4788
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4789
        public void forEach(Consumer<? super Map.Entry<K,V>> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4790
            if (action == null) throw new NullPointerException();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4791
            Node<K,V>[] t;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4792
            if ((t = map.table) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4793
                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
  4794
                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
  4795
                    action.accept(new MapEntry<K,V>(p.key, p.val, map));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4796
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4797
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4798
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4799
    }
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
     * Base class for bulk tasks. Repeats some fields and code from
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4805
     * class Traverser, because we need to subclass CountedCompleter.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4806
     */
19380
22eefc44fad6 8022724: lint warnings in j.u.concurrent packages
dl
parents: 19036
diff changeset
  4807
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4808
    abstract static class BulkTask<K,V,R> extends CountedCompleter<R> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4809
        Node<K,V>[] tab;        // same as Traverser
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4810
        Node<K,V> next;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4811
        TableStack<K,V> stack, spare;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4812
        int index;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4813
        int baseIndex;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4814
        int baseLimit;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4815
        final int baseSize;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4816
        int batch;              // split control
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4817
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4818
        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
  4819
            super(par);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4820
            this.batch = b;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4821
            this.index = this.baseIndex = i;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4822
            if ((this.tab = t) == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4823
                this.baseSize = this.baseLimit = 0;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4824
            else if (par == null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4825
                this.baseSize = this.baseLimit = t.length;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4826
            else {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4827
                this.baseLimit = f;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4828
                this.baseSize = par.baseSize;
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
        /**
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4833
         * Same as Traverser version
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4834
         */
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4835
        final Node<K,V> advance() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4836
            Node<K,V> e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4837
            if ((e = next) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4838
                e = e.next;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4839
            for (;;) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4840
                Node<K,V>[] t; int i, n;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4841
                if (e != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4842
                    return next = e;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4843
                if (baseIndex >= baseLimit || (t = tab) == null ||
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4844
                    (n = t.length) <= (i = index) || i < 0)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4845
                    return next = null;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4846
                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
  4847
                    if (e instanceof ForwardingNode) {
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4848
                        tab = ((ForwardingNode<K,V>)e).nextTable;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4849
                        e = null;
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4850
                        pushState(t, i, n);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4851
                        continue;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4852
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4853
                    else if (e instanceof TreeBin)
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4854
                        e = ((TreeBin<K,V>)e).first;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4855
                    else
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4856
                        e = null;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4857
                }
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4858
                if (stack != null)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4859
                    recoverState(n);
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4860
                else if ((index = i + baseSize) >= n)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4861
                    index = ++baseIndex;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  4862
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4863
        }
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4864
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4865
        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
  4866
            TableStack<K,V> s = spare;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4867
            if (s != null)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4868
                spare = s.next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4869
            else
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4870
                s = new TableStack<K,V>();
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4871
            s.tab = t;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4872
            s.length = n;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4873
            s.index = i;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4874
            s.next = stack;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4875
            stack = s;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4876
        }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4877
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4878
        private void recoverState(int n) {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4879
            TableStack<K,V> s; int len;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4880
            while ((s = stack) != null && (index += (len = s.length)) >= n) {
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4881
                n = len;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4882
                index = s.index;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4883
                tab = s.tab;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4884
                s.tab = null;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4885
                TableStack<K,V> next = s.next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4886
                s.next = spare; // save for reuse
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4887
                stack = next;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4888
                spare = s;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4889
            }
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4890
            if (s == null && (index += baseSize) >= n)
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4891
                index = ++baseIndex;
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  4892
        }
17945
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
     * Task classes. Coded in a regular but ugly format/style to
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4897
     * simplify checks that each variant differs in the right way from
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4898
     * others. The null screenings exist because compilers cannot tell
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4899
     * that we've already null-checked task arguments, so we force
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4900
     * simplest hoisted bypass to help avoid convoluted traps.
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4901
     */
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4902
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4903
    static final class ForEachKeyTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4904
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4905
        final Consumer<? super K> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4906
        ForEachKeyTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4907
            (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
  4908
             Consumer<? super K> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4909
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4910
            this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4911
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4912
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4913
            final Consumer<? super K> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4914
            if ((action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4915
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4916
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4917
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4918
                    new ForEachKeyTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4919
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4920
                         action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4921
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4922
                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
  4923
                    action.accept(p.key);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4924
                propagateCompletion();
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
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4929
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4930
    static final class ForEachValueTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4931
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4932
        final Consumer<? super V> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4933
        ForEachValueTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4934
            (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
  4935
             Consumer<? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4936
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4937
            this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4938
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4939
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4940
            final Consumer<? super V> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4941
            if ((action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4942
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4943
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4944
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4945
                    new ForEachValueTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4946
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4947
                         action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4948
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4949
                for (Node<K,V> p; (p = advance()) != null;)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4950
                    action.accept(p.val);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4951
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4952
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4953
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4954
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4955
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4956
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4957
    static final class ForEachEntryTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4958
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4959
        final Consumer<? super Entry<K,V>> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4960
        ForEachEntryTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4961
            (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
  4962
             Consumer<? super Entry<K,V>> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4963
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4964
            this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4965
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4966
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4967
            final Consumer<? super Entry<K,V>> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4968
            if ((action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4969
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4970
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4971
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4972
                    new ForEachEntryTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4973
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4974
                         action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4975
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4976
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4977
                    action.accept(p);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4978
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4979
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4980
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4981
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4982
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  4983
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4984
    static final class ForEachMappingTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4985
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4986
        final BiConsumer<? super K, ? super V> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4987
        ForEachMappingTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4988
            (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
  4989
             BiConsumer<? super K,? super V> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4990
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4991
            this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4992
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4993
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4994
            final BiConsumer<? super K, ? super V> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4995
            if ((action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4996
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4997
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4998
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  4999
                    new ForEachMappingTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5000
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5001
                         action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5002
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5003
                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
  5004
                    action.accept(p.key, p.val);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5005
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5006
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5007
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5008
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5009
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5010
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5011
    static final class ForEachTransformedKeyTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5012
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5013
        final Function<? super K, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5014
        final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5015
        ForEachTransformedKeyTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5016
            (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
  5017
             Function<? super K, ? extends U> transformer, Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5018
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5019
            this.transformer = transformer; this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5020
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5021
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5022
            final Function<? super K, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5023
            final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5024
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5025
                (action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5026
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5027
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5028
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5029
                    new ForEachTransformedKeyTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5030
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5031
                         transformer, action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5032
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5033
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5034
                    U u;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5035
                    if ((u = transformer.apply(p.key)) != null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5036
                        action.accept(u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5037
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5038
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5039
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5040
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5041
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5042
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5043
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5044
    static final class ForEachTransformedValueTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5045
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5046
        final Function<? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5047
        final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5048
        ForEachTransformedValueTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5049
            (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
  5050
             Function<? super V, ? extends U> transformer, Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5051
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5052
            this.transformer = transformer; this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5053
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5054
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5055
            final Function<? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5056
            final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5057
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5058
                (action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5059
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5060
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5061
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5062
                    new ForEachTransformedValueTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5063
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5064
                         transformer, action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5065
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5066
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5067
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5068
                    if ((u = transformer.apply(p.val)) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5069
                        action.accept(u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5070
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5071
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5072
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5073
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5074
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5075
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5076
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5077
    static final class ForEachTransformedEntryTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5078
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5079
        final Function<Map.Entry<K,V>, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5080
        final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5081
        ForEachTransformedEntryTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5082
            (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
  5083
             Function<Map.Entry<K,V>, ? extends U> transformer, Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5084
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5085
            this.transformer = transformer; this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5086
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5087
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5088
            final Function<Map.Entry<K,V>, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5089
            final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5090
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5091
                (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 ForEachTransformedEntryTask<K,V,U>
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
                         transformer, 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; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5100
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5101
                    if ((u = transformer.apply(p)) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5102
                        action.accept(u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5103
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5104
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5105
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5106
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5107
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5108
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5109
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5110
    static final class ForEachTransformedMappingTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5111
        extends BulkTask<K,V,Void> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5112
        final BiFunction<? super K, ? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5113
        final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5114
        ForEachTransformedMappingTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5115
            (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
  5116
             BiFunction<? super K, ? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5117
             Consumer<? super U> action) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5118
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5119
            this.transformer = transformer; this.action = action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5120
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5121
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5122
            final BiFunction<? super K, ? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5123
            final Consumer<? super U> action;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5124
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5125
                (action = this.action) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5126
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5127
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5128
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5129
                    new ForEachTransformedMappingTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5130
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5131
                         transformer, action).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5132
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5133
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5134
                    U u;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5135
                    if ((u = transformer.apply(p.key, p.val)) != null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5136
                        action.accept(u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5137
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5138
                propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5139
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5140
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5141
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5142
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5143
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5144
    static final class SearchKeysTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5145
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5146
        final Function<? super K, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5147
        final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5148
        SearchKeysTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5149
            (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
  5150
             Function<? super K, ? extends U> searchFunction,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5151
             AtomicReference<U> result) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5152
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5153
            this.searchFunction = searchFunction; this.result = result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5154
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5155
        public final U getRawResult() { return result.get(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5156
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5157
            final Function<? super K, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5158
            final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5159
            if ((searchFunction = this.searchFunction) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5160
                (result = this.result) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5161
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5162
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5163
                    if (result.get() != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5164
                        return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5165
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5166
                    new SearchKeysTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5167
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5168
                         searchFunction, result).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5169
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5170
                while (result.get() == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5171
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5172
                    Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5173
                    if ((p = advance()) == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5174
                        propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5175
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5176
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5177
                    if ((u = searchFunction.apply(p.key)) != null) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5178
                        if (result.compareAndSet(null, u))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5179
                            quietlyCompleteRoot();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5180
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5181
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5182
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5183
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5184
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5185
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5186
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5187
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5188
    static final class SearchValuesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5189
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5190
        final Function<? super V, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5191
        final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5192
        SearchValuesTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5193
            (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
  5194
             Function<? super V, ? extends U> searchFunction,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5195
             AtomicReference<U> result) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5196
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5197
            this.searchFunction = searchFunction; this.result = result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5198
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5199
        public final U getRawResult() { return result.get(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5200
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5201
            final Function<? super V, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5202
            final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5203
            if ((searchFunction = this.searchFunction) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5204
                (result = this.result) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5205
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5206
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5207
                    if (result.get() != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5208
                        return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5209
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5210
                    new SearchValuesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5211
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5212
                         searchFunction, result).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5213
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5214
                while (result.get() == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5215
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5216
                    Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5217
                    if ((p = advance()) == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5218
                        propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5219
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5220
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5221
                    if ((u = searchFunction.apply(p.val)) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5222
                        if (result.compareAndSet(null, u))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5223
                            quietlyCompleteRoot();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5224
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5225
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5226
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5227
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5228
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5229
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5230
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5231
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5232
    static final class SearchEntriesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5233
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5234
        final Function<Entry<K,V>, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5235
        final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5236
        SearchEntriesTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5237
            (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
  5238
             Function<Entry<K,V>, ? extends U> searchFunction,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5239
             AtomicReference<U> result) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5240
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5241
            this.searchFunction = searchFunction; this.result = result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5242
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5243
        public final U getRawResult() { return result.get(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5244
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5245
            final Function<Entry<K,V>, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5246
            final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5247
            if ((searchFunction = this.searchFunction) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5248
                (result = this.result) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5249
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5250
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5251
                    if (result.get() != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5252
                        return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5253
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5254
                    new SearchEntriesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5255
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5256
                         searchFunction, result).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5257
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5258
                while (result.get() == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5259
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5260
                    Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5261
                    if ((p = advance()) == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5262
                        propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5263
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5264
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5265
                    if ((u = searchFunction.apply(p)) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5266
                        if (result.compareAndSet(null, u))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5267
                            quietlyCompleteRoot();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5268
                        return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5269
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5270
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5271
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5272
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5273
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5274
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5275
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5276
    static final class SearchMappingsTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5277
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5278
        final BiFunction<? super K, ? super V, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5279
        final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5280
        SearchMappingsTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5281
            (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
  5282
             BiFunction<? super K, ? super V, ? extends U> searchFunction,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5283
             AtomicReference<U> result) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5284
            super(p, b, i, f, t);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5285
            this.searchFunction = searchFunction; this.result = result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5286
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5287
        public final U getRawResult() { return result.get(); }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5288
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5289
            final BiFunction<? super K, ? super V, ? extends U> searchFunction;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5290
            final AtomicReference<U> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5291
            if ((searchFunction = this.searchFunction) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5292
                (result = this.result) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5293
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5294
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5295
                    if (result.get() != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5296
                        return;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5297
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5298
                    new SearchMappingsTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5299
                        (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5300
                         searchFunction, result).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5301
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5302
                while (result.get() == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5303
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5304
                    Node<K,V> p;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5305
                    if ((p = advance()) == null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5306
                        propagateCompletion();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5307
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5308
                    }
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5309
                    if ((u = searchFunction.apply(p.key, p.val)) != null) {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5310
                        if (result.compareAndSet(null, u))
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5311
                            quietlyCompleteRoot();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5312
                        break;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5313
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5314
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5315
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5316
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5317
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5318
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5319
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5320
    static final class ReduceKeysTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5321
        extends BulkTask<K,V,K> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5322
        final BiFunction<? super K, ? super K, ? extends K> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5323
        K result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5324
        ReduceKeysTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5325
        ReduceKeysTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5326
            (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
  5327
             ReduceKeysTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5328
             BiFunction<? super K, ? super K, ? extends K> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5329
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5330
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5331
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5332
        public final K getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5333
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5334
            final BiFunction<? super K, ? super K, ? extends K> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5335
            if ((reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5336
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5337
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5338
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5339
                    (rights = new ReduceKeysTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5340
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5341
                      rights, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5342
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5343
                K r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5344
                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
  5345
                    K u = p.key;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5346
                    r = (r == null) ? u : u == null ? r : reducer.apply(r, u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5347
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5348
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5349
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5350
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5351
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5352
                    ReduceKeysTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5353
                        t = (ReduceKeysTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5354
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5355
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5356
                        K tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5357
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5358
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5359
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5360
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5361
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5362
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5363
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5364
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5365
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5366
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5367
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5368
    static final class ReduceValuesTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5369
        extends BulkTask<K,V,V> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5370
        final BiFunction<? super V, ? super V, ? extends V> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5371
        V result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5372
        ReduceValuesTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5373
        ReduceValuesTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5374
            (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
  5375
             ReduceValuesTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5376
             BiFunction<? super V, ? super V, ? extends V> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5377
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5378
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5379
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5380
        public final V getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5381
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5382
            final BiFunction<? super V, ? super V, ? extends V> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5383
            if ((reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5384
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5385
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5386
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5387
                    (rights = new ReduceValuesTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5388
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5389
                      rights, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5390
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5391
                V r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5392
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5393
                    V v = p.val;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5394
                    r = (r == null) ? v : reducer.apply(r, v);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5395
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5396
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5397
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5398
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5399
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5400
                    ReduceValuesTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5401
                        t = (ReduceValuesTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5402
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5403
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5404
                        V tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5405
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5406
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5407
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5408
                        s = t.rights = s.nextRight;
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 ReduceEntriesTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5417
        extends BulkTask<K,V,Map.Entry<K,V>> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5418
        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
  5419
        Map.Entry<K,V> result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5420
        ReduceEntriesTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5421
        ReduceEntriesTask
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
             ReduceEntriesTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5424
             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
  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 Map.Entry<K,V> 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<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
  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 ReduceEntriesTask<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
                Map.Entry<K,V> r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5440
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5441
                    r = (r == null) ? p : reducer.apply(r, p);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5442
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5443
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5444
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5445
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5446
                    ReduceEntriesTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5447
                        t = (ReduceEntriesTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5448
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5449
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5450
                        Map.Entry<K,V> tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5451
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5452
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5453
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5454
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5455
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5456
                }
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
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5461
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5462
    static final class MapReduceKeysTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5463
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5464
        final Function<? super K, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5465
        final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5466
        U result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5467
        MapReduceKeysTask<K,V,U> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5468
        MapReduceKeysTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5469
            (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
  5470
             MapReduceKeysTask<K,V,U> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5471
             Function<? super K, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5472
             BiFunction<? super U, ? super U, ? extends U> 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.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5475
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5476
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5477
        public final U getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5478
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5479
            final Function<? super K, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5480
            final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5481
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5482
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5483
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5484
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5485
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5486
                    (rights = new MapReduceKeysTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5487
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5488
                      rights, transformer, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5489
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5490
                U r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5491
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5492
                    U u;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5493
                    if ((u = transformer.apply(p.key)) != null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5494
                        r = (r == null) ? u : reducer.apply(r, u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5495
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5496
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5497
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5498
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5499
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5500
                    MapReduceKeysTask<K,V,U>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5501
                        t = (MapReduceKeysTask<K,V,U>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5502
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5503
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5504
                        U tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5505
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5506
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5507
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5508
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5509
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5510
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5511
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5512
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5513
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5514
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5515
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5516
    static final class MapReduceValuesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5517
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5518
        final Function<? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5519
        final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5520
        U result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5521
        MapReduceValuesTask<K,V,U> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5522
        MapReduceValuesTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5523
            (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
  5524
             MapReduceValuesTask<K,V,U> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5525
             Function<? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5526
             BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5527
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5528
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5529
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5530
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5531
        public final U getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5532
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5533
            final Function<? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5534
            final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5535
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5536
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5537
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5538
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5539
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5540
                    (rights = new MapReduceValuesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5541
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5542
                      rights, transformer, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5543
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5544
                U r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5545
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5546
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5547
                    if ((u = transformer.apply(p.val)) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5548
                        r = (r == null) ? u : reducer.apply(r, u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5549
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5550
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5551
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5552
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5553
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5554
                    MapReduceValuesTask<K,V,U>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5555
                        t = (MapReduceValuesTask<K,V,U>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5556
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5557
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5558
                        U tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5559
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5560
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5561
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5562
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5563
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5564
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5565
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5566
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5567
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5568
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5569
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5570
    static final class MapReduceEntriesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5571
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5572
        final Function<Map.Entry<K,V>, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5573
        final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5574
        U result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5575
        MapReduceEntriesTask<K,V,U> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5576
        MapReduceEntriesTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5577
            (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
  5578
             MapReduceEntriesTask<K,V,U> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5579
             Function<Map.Entry<K,V>, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5580
             BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5581
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5582
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5583
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5584
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5585
        public final U getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5586
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5587
            final Function<Map.Entry<K,V>, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5588
            final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5589
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5590
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5591
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5592
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5593
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5594
                    (rights = new MapReduceEntriesTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5595
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5596
                      rights, transformer, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5597
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5598
                U r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5599
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5600
                    U u;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5601
                    if ((u = transformer.apply(p)) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5602
                        r = (r == null) ? u : reducer.apply(r, u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5603
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5604
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5605
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5606
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5607
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5608
                    MapReduceEntriesTask<K,V,U>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5609
                        t = (MapReduceEntriesTask<K,V,U>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5610
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5611
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5612
                        U tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5613
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5614
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5615
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5616
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5617
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5618
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5619
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5620
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5621
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5622
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5623
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5624
    static final class MapReduceMappingsTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5625
        extends BulkTask<K,V,U> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5626
        final BiFunction<? super K, ? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5627
        final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5628
        U result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5629
        MapReduceMappingsTask<K,V,U> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5630
        MapReduceMappingsTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5631
            (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
  5632
             MapReduceMappingsTask<K,V,U> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5633
             BiFunction<? super K, ? super V, ? extends U> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5634
             BiFunction<? super U, ? super U, ? extends U> reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5635
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5636
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5637
            this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5638
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5639
        public final U getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5640
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5641
            final BiFunction<? super K, ? super V, ? extends U> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5642
            final BiFunction<? super U, ? super U, ? extends U> reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5643
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5644
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5645
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5646
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5647
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5648
                    (rights = new MapReduceMappingsTask<K,V,U>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5649
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5650
                      rights, transformer, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5651
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5652
                U r = null;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5653
                for (Node<K,V> p; (p = advance()) != null; ) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5654
                    U u;
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5655
                    if ((u = transformer.apply(p.key, p.val)) != null)
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5656
                        r = (r == null) ? u : reducer.apply(r, u);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5657
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5658
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5659
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5660
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5661
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5662
                    MapReduceMappingsTask<K,V,U>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5663
                        t = (MapReduceMappingsTask<K,V,U>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5664
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5665
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5666
                        U tr, sr;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5667
                        if ((sr = s.result) != null)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5668
                            t.result = (((tr = t.result) == null) ? sr :
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5669
                                        reducer.apply(tr, sr));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5670
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5671
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5672
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5673
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5674
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5675
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5676
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5677
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5678
    static final class MapReduceKeysToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5679
        extends BulkTask<K,V,Double> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5680
        final ToDoubleFunction<? super K> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5681
        final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5682
        final double basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5683
        double result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5684
        MapReduceKeysToDoubleTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5685
        MapReduceKeysToDoubleTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5686
            (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
  5687
             MapReduceKeysToDoubleTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5688
             ToDoubleFunction<? super K> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5689
             double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5690
             DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5691
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5692
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5693
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5694
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5695
        public final Double getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5696
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5697
            final ToDoubleFunction<? super K> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5698
            final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5699
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5700
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5701
                double r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5702
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5703
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5704
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5705
                    (rights = new MapReduceKeysToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5706
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5707
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5708
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5709
                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
  5710
                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5711
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5712
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5713
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5714
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5715
                    MapReduceKeysToDoubleTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5716
                        t = (MapReduceKeysToDoubleTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5717
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5718
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5719
                        t.result = reducer.applyAsDouble(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5720
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5721
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5722
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5723
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5724
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5725
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5726
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5727
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5728
    static final class MapReduceValuesToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5729
        extends BulkTask<K,V,Double> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5730
        final ToDoubleFunction<? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5731
        final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5732
        final double basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5733
        double result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5734
        MapReduceValuesToDoubleTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5735
        MapReduceValuesToDoubleTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5736
            (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
  5737
             MapReduceValuesToDoubleTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5738
             ToDoubleFunction<? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5739
             double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5740
             DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5741
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5742
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5743
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5744
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5745
        public final Double getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5746
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5747
            final ToDoubleFunction<? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5748
            final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5749
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5750
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5751
                double r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5752
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5753
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5754
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5755
                    (rights = new MapReduceValuesToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5756
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5757
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5758
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5759
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5760
                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.val));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5761
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5762
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5763
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5764
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5765
                    MapReduceValuesToDoubleTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5766
                        t = (MapReduceValuesToDoubleTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5767
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5768
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5769
                        t.result = reducer.applyAsDouble(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5770
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5771
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5772
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5773
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5774
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5775
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5776
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5777
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5778
    static final class MapReduceEntriesToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5779
        extends BulkTask<K,V,Double> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5780
        final ToDoubleFunction<Map.Entry<K,V>> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5781
        final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5782
        final double basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5783
        double result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5784
        MapReduceEntriesToDoubleTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5785
        MapReduceEntriesToDoubleTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5786
            (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
  5787
             MapReduceEntriesToDoubleTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5788
             ToDoubleFunction<Map.Entry<K,V>> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5789
             double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5790
             DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5791
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5792
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5793
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5794
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5795
        public final Double getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5796
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5797
            final ToDoubleFunction<Map.Entry<K,V>> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5798
            final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5799
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5800
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5801
                double r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5802
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5803
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5804
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5805
                    (rights = new MapReduceEntriesToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5806
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5807
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5808
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5809
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5810
                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5811
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5812
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5813
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5814
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5815
                    MapReduceEntriesToDoubleTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5816
                        t = (MapReduceEntriesToDoubleTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5817
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5818
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5819
                        t.result = reducer.applyAsDouble(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5820
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5821
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5822
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5823
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5824
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5825
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5826
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5827
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5828
    static final class MapReduceMappingsToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5829
        extends BulkTask<K,V,Double> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5830
        final ToDoubleBiFunction<? super K, ? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5831
        final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5832
        final double basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5833
        double result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5834
        MapReduceMappingsToDoubleTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5835
        MapReduceMappingsToDoubleTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5836
            (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
  5837
             MapReduceMappingsToDoubleTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5838
             ToDoubleBiFunction<? super K, ? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5839
             double basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5840
             DoubleBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5841
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5842
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5843
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5844
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5845
        public final Double getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5846
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5847
            final ToDoubleBiFunction<? super K, ? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5848
            final DoubleBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5849
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5850
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5851
                double r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5852
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5853
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5854
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5855
                    (rights = new MapReduceMappingsToDoubleTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5856
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5857
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5858
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5859
                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
  5860
                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key, p.val));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5861
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5862
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5863
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5864
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5865
                    MapReduceMappingsToDoubleTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5866
                        t = (MapReduceMappingsToDoubleTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5867
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5868
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5869
                        t.result = reducer.applyAsDouble(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5870
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5871
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5872
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5873
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5874
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5875
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5876
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5877
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5878
    static final class MapReduceKeysToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5879
        extends BulkTask<K,V,Long> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5880
        final ToLongFunction<? super K> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5881
        final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5882
        final long basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5883
        long result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5884
        MapReduceKeysToLongTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5885
        MapReduceKeysToLongTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5886
            (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
  5887
             MapReduceKeysToLongTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5888
             ToLongFunction<? super K> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5889
             long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5890
             LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5891
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5892
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5893
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5894
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5895
        public final Long getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5896
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5897
            final ToLongFunction<? super K> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5898
            final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5899
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5900
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5901
                long r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5902
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5903
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5904
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5905
                    (rights = new MapReduceKeysToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5906
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5907
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5908
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5909
                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
  5910
                    r = reducer.applyAsLong(r, transformer.applyAsLong(p.key));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5911
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5912
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5913
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5914
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5915
                    MapReduceKeysToLongTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5916
                        t = (MapReduceKeysToLongTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5917
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5918
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5919
                        t.result = reducer.applyAsLong(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5920
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5921
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5922
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5923
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5924
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5925
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5926
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5927
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5928
    static final class MapReduceValuesToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5929
        extends BulkTask<K,V,Long> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5930
        final ToLongFunction<? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5931
        final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5932
        final long basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5933
        long result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5934
        MapReduceValuesToLongTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5935
        MapReduceValuesToLongTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5936
            (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
  5937
             MapReduceValuesToLongTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5938
             ToLongFunction<? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5939
             long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5940
             LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5941
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5942
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5943
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5944
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5945
        public final Long getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5946
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5947
            final ToLongFunction<? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5948
            final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5949
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5950
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5951
                long r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5952
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5953
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5954
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5955
                    (rights = new MapReduceValuesToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5956
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5957
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5958
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5959
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5960
                    r = reducer.applyAsLong(r, transformer.applyAsLong(p.val));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5961
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5962
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5963
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5964
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  5965
                    MapReduceValuesToLongTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5966
                        t = (MapReduceValuesToLongTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5967
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5968
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5969
                        t.result = reducer.applyAsLong(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5970
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5971
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5972
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5973
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5974
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5975
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5976
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  5977
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5978
    static final class MapReduceEntriesToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5979
        extends BulkTask<K,V,Long> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5980
        final ToLongFunction<Map.Entry<K,V>> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5981
        final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5982
        final long basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5983
        long result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5984
        MapReduceEntriesToLongTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5985
        MapReduceEntriesToLongTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5986
            (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
  5987
             MapReduceEntriesToLongTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5988
             ToLongFunction<Map.Entry<K,V>> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5989
             long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5990
             LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5991
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5992
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5993
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5994
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5995
        public final Long getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5996
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5997
            final ToLongFunction<Map.Entry<K,V>> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5998
            final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  5999
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6000
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6001
                long r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6002
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6003
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6004
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6005
                    (rights = new MapReduceEntriesToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6006
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6007
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6008
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6009
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6010
                    r = reducer.applyAsLong(r, transformer.applyAsLong(p));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6011
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6012
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6013
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6014
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6015
                    MapReduceEntriesToLongTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6016
                        t = (MapReduceEntriesToLongTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6017
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6018
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6019
                        t.result = reducer.applyAsLong(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6020
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6021
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6022
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6023
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6024
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6025
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6026
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6027
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6028
    static final class MapReduceMappingsToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6029
        extends BulkTask<K,V,Long> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6030
        final ToLongBiFunction<? super K, ? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6031
        final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6032
        final long basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6033
        long result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6034
        MapReduceMappingsToLongTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6035
        MapReduceMappingsToLongTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6036
            (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
  6037
             MapReduceMappingsToLongTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6038
             ToLongBiFunction<? super K, ? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6039
             long basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6040
             LongBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6041
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6042
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6043
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6044
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6045
        public final Long getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6046
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6047
            final ToLongBiFunction<? super K, ? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6048
            final LongBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6049
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6050
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6051
                long r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6052
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6053
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6054
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6055
                    (rights = new MapReduceMappingsToLongTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6056
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6057
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6058
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6059
                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
  6060
                    r = reducer.applyAsLong(r, transformer.applyAsLong(p.key, p.val));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6061
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6062
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6063
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6064
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6065
                    MapReduceMappingsToLongTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6066
                        t = (MapReduceMappingsToLongTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6067
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6068
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6069
                        t.result = reducer.applyAsLong(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6070
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6071
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6072
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6073
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6074
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6075
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6076
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6077
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6078
    static final class MapReduceKeysToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6079
        extends BulkTask<K,V,Integer> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6080
        final ToIntFunction<? super K> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6081
        final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6082
        final int basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6083
        int result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6084
        MapReduceKeysToIntTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6085
        MapReduceKeysToIntTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6086
            (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
  6087
             MapReduceKeysToIntTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6088
             ToIntFunction<? super K> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6089
             int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6090
             IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6091
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6092
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6093
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6094
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6095
        public final Integer getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6096
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6097
            final ToIntFunction<? super K> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6098
            final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6099
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6100
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6101
                int r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6102
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6103
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6104
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6105
                    (rights = new MapReduceKeysToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6106
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6107
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6108
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6109
                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
  6110
                    r = reducer.applyAsInt(r, transformer.applyAsInt(p.key));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6111
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6112
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6113
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6114
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6115
                    MapReduceKeysToIntTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6116
                        t = (MapReduceKeysToIntTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6117
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6118
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6119
                        t.result = reducer.applyAsInt(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6120
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6121
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6122
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6123
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6124
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6125
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6126
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6127
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6128
    static final class MapReduceValuesToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6129
        extends BulkTask<K,V,Integer> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6130
        final ToIntFunction<? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6131
        final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6132
        final int basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6133
        int result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6134
        MapReduceValuesToIntTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6135
        MapReduceValuesToIntTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6136
            (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
  6137
             MapReduceValuesToIntTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6138
             ToIntFunction<? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6139
             int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6140
             IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6141
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6142
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6143
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6144
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6145
        public final Integer getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6146
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6147
            final ToIntFunction<? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6148
            final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6149
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6150
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6151
                int r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6152
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6153
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6154
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6155
                    (rights = new MapReduceValuesToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6156
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6157
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6158
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6159
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6160
                    r = reducer.applyAsInt(r, transformer.applyAsInt(p.val));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6161
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6162
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6163
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6164
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6165
                    MapReduceValuesToIntTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6166
                        t = (MapReduceValuesToIntTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6167
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6168
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6169
                        t.result = reducer.applyAsInt(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6170
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6171
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6172
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6173
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6174
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6175
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6176
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6177
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6178
    static final class MapReduceEntriesToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6179
        extends BulkTask<K,V,Integer> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6180
        final ToIntFunction<Map.Entry<K,V>> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6181
        final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6182
        final int basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6183
        int result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6184
        MapReduceEntriesToIntTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6185
        MapReduceEntriesToIntTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6186
            (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
  6187
             MapReduceEntriesToIntTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6188
             ToIntFunction<Map.Entry<K,V>> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6189
             int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6190
             IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6191
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6192
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6193
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6194
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6195
        public final Integer getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6196
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6197
            final ToIntFunction<Map.Entry<K,V>> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6198
            final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6199
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6200
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6201
                int r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6202
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6203
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6204
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6205
                    (rights = new MapReduceEntriesToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6206
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6207
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6208
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6209
                for (Node<K,V> p; (p = advance()) != null; )
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6210
                    r = reducer.applyAsInt(r, transformer.applyAsInt(p));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6211
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6212
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6213
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6214
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6215
                    MapReduceEntriesToIntTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6216
                        t = (MapReduceEntriesToIntTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6217
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6218
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6219
                        t.result = reducer.applyAsInt(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6220
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6221
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6222
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6223
            }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6224
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6225
    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6226
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6227
    @SuppressWarnings("serial")
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6228
    static final class MapReduceMappingsToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6229
        extends BulkTask<K,V,Integer> {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6230
        final ToIntBiFunction<? super K, ? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6231
        final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6232
        final int basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6233
        int result;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6234
        MapReduceMappingsToIntTask<K,V> rights, nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6235
        MapReduceMappingsToIntTask
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6236
            (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
  6237
             MapReduceMappingsToIntTask<K,V> nextRight,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6238
             ToIntBiFunction<? super K, ? super V> transformer,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6239
             int basis,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6240
             IntBinaryOperator reducer) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6241
            super(p, b, i, f, t); this.nextRight = nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6242
            this.transformer = transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6243
            this.basis = basis; this.reducer = reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6244
        }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6245
        public final Integer getRawResult() { return result; }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6246
        public final void compute() {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6247
            final ToIntBiFunction<? super K, ? super V> transformer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6248
            final IntBinaryOperator reducer;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6249
            if ((transformer = this.transformer) != null &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6250
                (reducer = this.reducer) != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6251
                int r = this.basis;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6252
                for (int i = baseIndex, f, h; batch > 0 &&
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6253
                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6254
                    addToPendingCount(1);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6255
                    (rights = new MapReduceMappingsToIntTask<K,V>
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6256
                     (this, batch >>>= 1, baseLimit = h, f, tab,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6257
                      rights, transformer, r, reducer)).fork();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6258
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6259
                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
  6260
                    r = reducer.applyAsInt(r, transformer.applyAsInt(p.key, p.val));
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6261
                result = r;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6262
                CountedCompleter<?> c;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6263
                for (c = firstComplete(); c != null; c = c.nextComplete()) {
19862
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6264
                    @SuppressWarnings("unchecked")
186f3a70a4f7 8010293: java/util/concurrent/ConcurrentHashMap/toArray.java fails intermittently
psandoz
parents: 19855
diff changeset
  6265
                    MapReduceMappingsToIntTask<K,V>
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6266
                        t = (MapReduceMappingsToIntTask<K,V>)c,
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6267
                        s = t.rights;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6268
                    while (s != null) {
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6269
                        t.result = reducer.applyAsInt(t.result, s.result);
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6270
                        s = t.rights = s.nextRight;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6271
                    }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6272
                }
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6273
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  6274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  6275
    }
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6276
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6277
    // Unsafe mechanics
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6278
    private static final sun.misc.Unsafe U;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6279
    private static final long SIZECTL;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6280
    private static final long TRANSFERINDEX;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6281
    private static final long BASECOUNT;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6282
    private static final long CELLSBUSY;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6283
    private static final long CELLVALUE;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6284
    private static final long ABASE;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6285
    private static final int ASHIFT;
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6286
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6287
    static {
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6288
        try {
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6289
            U = sun.misc.Unsafe.getUnsafe();
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6290
            Class<?> k = ConcurrentHashMap.class;
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6291
            SIZECTL = U.objectFieldOffset
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6292
                (k.getDeclaredField("sizeCtl"));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6293
            TRANSFERINDEX = U.objectFieldOffset
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6294
                (k.getDeclaredField("transferIndex"));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6295
            BASECOUNT = U.objectFieldOffset
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6296
                (k.getDeclaredField("baseCount"));
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6297
            CELLSBUSY = U.objectFieldOffset
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6298
                (k.getDeclaredField("cellsBusy"));
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6299
            Class<?> ck = CounterCell.class;
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6300
            CELLVALUE = U.objectFieldOffset
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6301
                (ck.getDeclaredField("value"));
18802
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6302
            Class<?> ak = Node[].class;
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6303
            ABASE = U.arrayBaseOffset(ak);
68564211d16b 8019484: Sync j.u.c.ConcurrentHashMap from 166 to tl
psandoz
parents: 17953
diff changeset
  6304
            int scale = U.arrayIndexScale(ak);
17945
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6305
            if ((scale & (scale - 1)) != 0)
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6306
                throw new Error("data type scale not a power of two");
97634ef9dd1c 8005704: Update ConcurrentHashMap to v8
dl
parents: 17717
diff changeset
  6307
            ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
9279
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6308
        } catch (Exception e) {
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6309
            throw new Error(e);
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6310
        }
5f5d493d30a0 7036559: ConcurrentHashMap footprint and contention improvements
dl
parents: 9242
diff changeset
  6311
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  6312
}