8197531: Miscellaneous changes imported from jsr166 CVS 2018-04

Reviewed-by: martin, psandoz
This commit is contained in:
Doug Lea 2018-04-10 11:37:18 -07:00
parent a4f3a1d9eb
commit cdd36df363
27 changed files with 194 additions and 34 deletions

View File

@ -208,7 +208,7 @@ public class ArrayDeque<E> extends AbstractCollection<E>
*/
public ArrayDeque(Collection<? extends E> c) {
this(c.size());
addAll(c);
copyElements(c);
}
/**
@ -322,10 +322,14 @@ public class ArrayDeque<E> extends AbstractCollection<E>
final int s, needed;
if ((needed = (s = size()) + c.size() + 1 - elements.length) > 0)
grow(needed);
c.forEach(this::addLast);
copyElements(c);
return size() > s;
}
private void copyElements(Collection<? extends E> c) {
c.forEach(this::addLast);
}
/**
* Inserts the specified element at the front of this deque.
*

View File

@ -2883,7 +2883,7 @@ public class CompletableFuture<T> implements Future<T>, CompletionStage<T> {
STACK = l.findVarHandle(CompletableFuture.class, "stack", Completion.class);
NEXT = l.findVarHandle(Completion.class, "next", Completion.class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
// Reduce the risk of rare disastrous classloading in first call to

View File

@ -6383,7 +6383,7 @@ public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
ABASE = U.arrayBaseOffset(Node[].class);
int scale = U.arrayIndexScale(Node[].class);
if ((scale & (scale - 1)) != 0)
throw new Error("array index scale not a power of two");
throw new ExceptionInInitializerError("array index scale not a power of two");
ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
// Reduce the risk of rare disastrous classloading in first call to

View File

@ -1671,7 +1671,7 @@ public class ConcurrentLinkedDeque<E>
NEXT = l.findVarHandle(Node.class, "next", Node.class);
ITEM = l.findVarHandle(Node.class, "item", Object.class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
}
}

View File

@ -1069,7 +1069,7 @@ public class ConcurrentLinkedQueue<E> extends AbstractQueue<E>
ITEM = l.findVarHandle(Node.class, "item", Object.class);
NEXT = l.findVarHandle(Node.class, "next", Node.class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
}
}

View File

@ -3412,7 +3412,7 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
VAL = l.findVarHandle(Node.class, "val", Object.class);
RIGHT = l.findVarHandle(Index.class, "right", Index.class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
}
}

View File

@ -775,7 +775,7 @@ public abstract class CountedCompleter<T> extends ForkJoinTask<T> {
PENDING = l.findVarHandle(CountedCompleter.class, "pending", int.class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
}
}

View File

@ -641,7 +641,7 @@ public class Exchanger<V> {
MATCH = l.findVarHandle(Node.class, "match", Object.class);
AA = MethodHandles.arrayElementVarHandle(Node[].class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
}

View File

@ -1540,7 +1540,7 @@ public abstract class ForkJoinTask<V> implements Future<V>, Serializable {
MethodHandles.Lookup l = MethodHandles.lookup();
STATUS = l.findVarHandle(ForkJoinTask.class, "status", int.class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
}

View File

@ -526,7 +526,7 @@ public class FutureTask<V> implements RunnableFuture<V> {
RUNNER = l.findVarHandle(FutureTask.class, "runner", Thread.class);
WAITERS = l.findVarHandle(FutureTask.class, "waiters", WaitNode.class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
// Reduce the risk of rare disastrous classloading in first call to

View File

@ -1739,7 +1739,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
NEXT = l.findVarHandle(Node.class, "next", Node.class);
WAITER = l.findVarHandle(Node.class, "waiter", Thread.class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
// Reduce the risk of rare disastrous classloading in first call to

View File

@ -1137,7 +1137,7 @@ public class Phaser {
MethodHandles.Lookup l = MethodHandles.lookup();
STATE = l.findVarHandle(Phaser.class, "state", long.class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
// Reduce the risk of rare disastrous classloading in first call to

View File

@ -1014,7 +1014,7 @@ public class PriorityBlockingQueue<E> extends AbstractQueue<E>
"allocationSpinLock",
int.class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
}
}

View File

@ -1096,7 +1096,7 @@ public class SubmissionPublisher<T> implements Publisher<T>,
if (cap > 0) {
boolean added;
if (n >= cap && cap < maxCapacity) // resize
added = growAndoffer(item, a, t);
added = growAndOffer(item, a, t);
else if (n >= cap || unowned) // need volatile CAS
added = QA.compareAndSet(a, i, null, item);
else { // can use release mode
@ -1115,7 +1115,7 @@ public class SubmissionPublisher<T> implements Publisher<T>,
* Tries to expand buffer and add item, returning true on
* success. Currently fails only if out of memory.
*/
final boolean growAndoffer(T item, Object[] a, int t) {
final boolean growAndOffer(T item, Object[] a, int t) {
int cap = 0, newCap = 0;
Object[] newArray = null;
if (a != null && (cap = a.length) > 0 && (newCap = cap << 1) > 0) {
@ -1466,7 +1466,7 @@ public class SubmissionPublisher<T> implements Publisher<T>,
long.class);
QA = MethodHandles.arrayElementVarHandle(Object[].class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
// Reduce the risk of rare disastrous classloading in first call to

View File

@ -293,7 +293,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
SMATCH = l.findVarHandle(SNode.class, "match", SNode.class);
SNEXT = l.findVarHandle(SNode.class, "next", SNode.class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
}
}
@ -516,7 +516,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
MethodHandles.Lookup l = MethodHandles.lookup();
SHEAD = l.findVarHandle(TransferStack.class, "head", SNode.class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
}
}
@ -583,7 +583,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
QITEM = l.findVarHandle(QNode.class, "item", Object.class);
QNEXT = l.findVarHandle(QNode.class, "next", QNode.class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
}
}
@ -830,7 +830,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
QCLEANME = l.findVarHandle(TransferQueue.class, "cleanMe",
QNode.class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
}
}

View File

@ -56,7 +56,7 @@ public class AtomicBoolean implements java.io.Serializable {
MethodHandles.Lookup l = MethodHandles.lookup();
VALUE = l.findVarHandle(AtomicBoolean.class, "value", int.class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
}

View File

@ -199,7 +199,7 @@ public class AtomicMarkableReference<V> {
PAIR = l.findVarHandle(AtomicMarkableReference.class, "pair",
Pair.class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
}

View File

@ -56,7 +56,7 @@ public class AtomicReference<V> implements java.io.Serializable {
MethodHandles.Lookup l = MethodHandles.lookup();
VALUE = l.findVarHandle(AtomicReference.class, "value", Object.class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
}

View File

@ -199,7 +199,7 @@ public class AtomicStampedReference<V> {
PAIR = l.findVarHandle(AtomicStampedReference.class, "pair",
Pair.class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
}

View File

@ -144,7 +144,7 @@ abstract class Striped64 extends Number {
MethodHandles.Lookup l = MethodHandles.lookup();
VALUE = l.findVarHandle(Cell.class, "value", long.class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
}
}
@ -396,13 +396,13 @@ abstract class Striped64 extends Number {
try {
return MethodHandles.privateLookupIn(Thread.class, MethodHandles.lookup());
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
}});
THREAD_PROBE = l.findVarHandle(Thread.class,
"threadLocalRandomProbe", int.class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
}

View File

@ -1830,7 +1830,7 @@ public abstract class AbstractQueuedLongSynchronizer
HEAD = l.findVarHandle(AbstractQueuedLongSynchronizer.class, "head", Node.class);
TAIL = l.findVarHandle(AbstractQueuedLongSynchronizer.class, "tail", Node.class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
// Reduce the risk of rare disastrous classloading in first call to

View File

@ -555,7 +555,7 @@ public abstract class AbstractQueuedSynchronizer
THREAD = l.findVarHandle(Node.class, "thread", Thread.class);
WAITSTATUS = l.findVarHandle(Node.class, "waitStatus", int.class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
}
}
@ -2308,7 +2308,7 @@ public abstract class AbstractQueuedSynchronizer
HEAD = l.findVarHandle(AbstractQueuedSynchronizer.class, "head", Node.class);
TAIL = l.findVarHandle(AbstractQueuedSynchronizer.class, "tail", Node.class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
// Reduce the risk of rare disastrous classloading in first call to

View File

@ -1614,7 +1614,7 @@ public class StampedLock implements java.io.Serializable {
WNEXT = l.findVarHandle(WNode.class, "next", WNode.class);
WCOWAIT = l.findVarHandle(WNode.class, "cowait", WNode.class);
} catch (ReflectiveOperationException e) {
throw new Error(e);
throw new ExceptionInInitializerError(e);
}
}
}

View File

@ -0,0 +1,155 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* This file is available under and governed by the GNU General Public
* License version 2 only, as published by the Free Software Foundation.
* However, the following notice accompanied the original version of this
* file:
*
* Written by Martin Buchholz with assistance from members of JCP
* JSR-166 Expert Group and released to the public domain, as
* explained at http://creativecommons.org/publicdomain/zero/1.0/
*/
/*
* @test
* @modules java.base/java.util.concurrent:open
* @run testng WhiteBox
* @summary White box tests of implementation details
*/
import static org.testng.Assert.*;
import org.testng.annotations.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;
@Test
public class WhiteBox {
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
final VarHandle TABLE, NEXTTABLE, SIZECTL;
WhiteBox() throws ReflectiveOperationException {
Class<?> mClass = ConcurrentHashMap.class;
String nodeClassName = mClass.getName() + "$Node";
Class<?> nodeClass = Class.forName(nodeClassName);
Class<?> nodeArrayClass = Class.forName("[L" + nodeClassName + ";");
MethodHandles.Lookup lookup
= MethodHandles.privateLookupIn(mClass, MethodHandles.lookup());
TABLE = lookup.findVarHandle(mClass, "table", nodeArrayClass);
NEXTTABLE = lookup.findVarHandle(mClass, "nextTable", nodeArrayClass);
SIZECTL = lookup.findVarHandle(mClass, "sizeCtl", int.class);
}
Object[] table(ConcurrentHashMap m) { return (Object[]) TABLE.getVolatile(m); }
Object[] nextTable(ConcurrentHashMap m) { return (Object[]) NEXTTABLE.getVolatile(m); }
int sizeCtl(ConcurrentHashMap m) { return (int) SIZECTL.getVolatile(m); }
@Test
public void defaultConstructor() {
ConcurrentHashMap m = new ConcurrentHashMap();
assertNull(table(m));
assertEquals(sizeCtl(m), 0);
assertResizeNotInProgress(m);
}
@Test
public void shouldNotResizeWhenInitialCapacityProvided() {
int initialCapacity = rnd.nextInt(1, 100);
Object[] initialTable = null;
ConcurrentHashMap m = new ConcurrentHashMap(initialCapacity);
// table is lazily initialized
assertNull(table(m));
int expectedInitialTableLength = sizeCtl(m);
assertInvariants(m);
for (int i = 0; i < initialCapacity; i++) {
m.put(i * 100 + rnd.nextInt(100), i);
if (i == 0)
initialTable = table(m);
else
assertSame(table(m), initialTable);
assertInvariants(m);
}
assertEquals(initialTable.length, expectedInitialTableLength);
}
byte[] serialBytes(Object o) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(o);
oos.flush();
oos.close();
return bos.toByteArray();
} catch (Exception fail) {
throw new AssertionError(fail);
}
}
@SuppressWarnings("unchecked")
<T> T serialClone(T o) {
try {
ObjectInputStream ois = new ObjectInputStream
(new ByteArrayInputStream(serialBytes(o)));
T clone = (T) ois.readObject();
assertNotSame(o, clone);
assertSame(o.getClass(), clone.getClass());
return clone;
} catch (Exception fail) {
throw new AssertionError(fail);
}
}
@Test
public void testSerialization() {
assertInvariants(serialClone(new ConcurrentHashMap()));
ConcurrentHashMap m = new ConcurrentHashMap(rnd.nextInt(100));
m.put(1, 1);
ConcurrentHashMap clone = serialClone(m);
assertInvariants(clone);
assertNotSame(table(m), table(clone));
assertEquals(m, clone);
assertResizeNotInProgress(m);
assertResizeNotInProgress(clone);
}
/** Checks conditions which should always be true. */
void assertInvariants(ConcurrentHashMap m) {
if (!m.isEmpty())
assertNotNull(table(m));
}
void assertResizeNotInProgress(ConcurrentHashMap m) {
assertTrue(sizeCtl(m) >= 0);
assertNull(nextTable(m));
}
}

View File

@ -338,6 +338,7 @@ public class WhiteBox {
}
}
@Test
public void testSerialization() {
ConcurrentLinkedQueue q = serialClone(new ConcurrentLinkedQueue());
assertInvariants(q);

View File

@ -101,8 +101,7 @@ public class PrivilegedCallables {
final Policy policy = new Policy();
Policy.setPolicy(policy);
policy.setPermissions(new RuntimePermission("getClassLoader"),
new RuntimePermission("setContextClassLoader"),
new RuntimePermission("stopThread"));
new RuntimePermission("setContextClassLoader"));
System.setSecurityManager(new SecurityManager());
testPrivileged();

View File

@ -361,6 +361,7 @@ public class WhiteBox {
}
}
@Test
public void testSerialization() {
LinkedTransferQueue q = serialClone(new LinkedTransferQueue());
assertInvariants(q);