undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
|
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
* Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 6571733 6460501
|
||||
* @summary Check that regaining a read lock succeeds after a write
|
||||
* lock attempt times out
|
||||
*/
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
|
||||
public class Bug6571733 {
|
||||
|
||||
void test(String[] args) throws Throwable {
|
||||
test(true);
|
||||
test(false);
|
||||
}
|
||||
|
||||
void test(boolean fairness) throws Throwable {
|
||||
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fairness);
|
||||
|
||||
// obtain read lock
|
||||
lock.readLock().lock();
|
||||
|
||||
Thread thread = new Thread() { public void run() {
|
||||
try {
|
||||
check(! lock.writeLock().tryLock(0, TimeUnit.DAYS));
|
||||
|
||||
lock.readLock().lock();
|
||||
lock.readLock().unlock();
|
||||
} catch (Throwable t) { unexpected(t); }}};
|
||||
|
||||
thread.start();
|
||||
thread.join();
|
||||
}
|
||||
|
||||
//--------------------- Infrastructure ---------------------------
|
||||
volatile int passed = 0, failed = 0;
|
||||
void pass() {passed++;}
|
||||
void fail() {failed++; Thread.dumpStack();}
|
||||
void fail(String msg) {System.err.println(msg); fail();}
|
||||
void unexpected(Throwable t) {failed++; t.printStackTrace();}
|
||||
void check(boolean cond) {if (cond) pass(); else fail();}
|
||||
public static void main(String[] args) throws Throwable {
|
||||
new Bug6571733().instanceMain(args);}
|
||||
void instanceMain(String[] args) throws Throwable {
|
||||
try {test(args);} catch (Throwable t) {unexpected(t);}
|
||||
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
|
||||
if (failed > 0) throw new AssertionError("Some tests failed");}
|
||||
}
|
||||
|
|
@ -0,0 +1,208 @@
|
|||
/*
|
||||
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 6207928 6328220 6378321 6625723
|
||||
* @summary Recursive lock invariant sanity checks
|
||||
* @library /test/lib
|
||||
* @author Martin Buchholz
|
||||
*/
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.CyclicBarrier;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
import jdk.test.lib.Utils;
|
||||
|
||||
// I am the Cownt, and I lahve to cownt.
|
||||
public class Count {
|
||||
static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
|
||||
final Random rnd = new Random();
|
||||
|
||||
void lock(Lock lock) {
|
||||
try {
|
||||
switch (rnd.nextInt(4)) {
|
||||
case 0: lock.lock(); break;
|
||||
case 1: lock.lockInterruptibly(); break;
|
||||
case 2: check(lock.tryLock()); break;
|
||||
case 3: check(lock.tryLock(45, TimeUnit.MINUTES)); break;
|
||||
}
|
||||
} catch (Throwable t) { unexpected(t); }
|
||||
}
|
||||
|
||||
void test(String[] args) throws Throwable {
|
||||
for (boolean fair : new boolean[] { true, false })
|
||||
for (boolean serialClone : new boolean[] { true, false }) {
|
||||
testReentrantLocks(fair, serialClone);
|
||||
testConcurrentReadLocks(fair, serialClone);
|
||||
}
|
||||
}
|
||||
|
||||
void testConcurrentReadLocks(final boolean fair,
|
||||
final boolean serialClone) throws Throwable {
|
||||
final int nThreads = 10;
|
||||
final CyclicBarrier barrier = new CyclicBarrier(nThreads);
|
||||
final ExecutorService es = Executors.newFixedThreadPool(nThreads);
|
||||
final ReentrantReadWriteLock rwl = serialClone ?
|
||||
serialClone(new ReentrantReadWriteLock(fair)) :
|
||||
new ReentrantReadWriteLock(fair);
|
||||
for (int i = 0; i < nThreads; i++) {
|
||||
es.submit(new Runnable() { public void run() {
|
||||
try {
|
||||
int n = 5;
|
||||
for (int i = 0; i < n; i++) {
|
||||
barrier.await();
|
||||
equal(rwl.getReadHoldCount(), i);
|
||||
equal(rwl.getWriteHoldCount(), 0);
|
||||
check(! rwl.isWriteLocked());
|
||||
equal(rwl.getReadLockCount(), nThreads * i);
|
||||
barrier.await();
|
||||
lock(rwl.readLock());
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
rwl.readLock().unlock();
|
||||
barrier.await();
|
||||
equal(rwl.getReadHoldCount(), n-i-1);
|
||||
equal(rwl.getReadLockCount(), nThreads*(n-i-1));
|
||||
equal(rwl.getWriteHoldCount(), 0);
|
||||
check(! rwl.isWriteLocked());
|
||||
barrier.await();
|
||||
}
|
||||
THROWS(IllegalMonitorStateException.class,
|
||||
new F(){void f(){rwl.readLock().unlock();}},
|
||||
new F(){void f(){rwl.writeLock().unlock();}});
|
||||
barrier.await();
|
||||
} catch (Throwable t) { unexpected(t); }}});}
|
||||
es.shutdown();
|
||||
check(es.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
|
||||
}
|
||||
|
||||
void testReentrantLocks(final boolean fair,
|
||||
final boolean serialClone) throws Throwable {
|
||||
final ReentrantLock rl = serialClone ?
|
||||
serialClone(new ReentrantLock(fair)) :
|
||||
new ReentrantLock(fair);
|
||||
final ReentrantReadWriteLock rwl = serialClone ?
|
||||
serialClone(new ReentrantReadWriteLock(fair)) :
|
||||
new ReentrantReadWriteLock(fair);
|
||||
final int depth = 10;
|
||||
equal(rl.isFair(), fair);
|
||||
equal(rwl.isFair(), fair);
|
||||
check(! rl.isLocked());
|
||||
check(! rwl.isWriteLocked());
|
||||
check(! rl.isHeldByCurrentThread());
|
||||
check(! rwl.isWriteLockedByCurrentThread());
|
||||
check(! rwl.writeLock().isHeldByCurrentThread());
|
||||
|
||||
for (int i = 0; i < depth; i++) {
|
||||
equal(rl.getHoldCount(), i);
|
||||
equal(rwl.getReadLockCount(), i);
|
||||
equal(rwl.getReadHoldCount(), i);
|
||||
equal(rwl.getWriteHoldCount(), i);
|
||||
equal(rwl.writeLock().getHoldCount(), i);
|
||||
equal(rl.isLocked(), i > 0);
|
||||
equal(rwl.isWriteLocked(), i > 0);
|
||||
lock(rl);
|
||||
lock(rwl.writeLock());
|
||||
lock(rwl.readLock());
|
||||
}
|
||||
|
||||
for (int i = depth; i > 0; i--) {
|
||||
check(! rl.hasQueuedThreads());
|
||||
check(! rwl.hasQueuedThreads());
|
||||
check(! rl.hasQueuedThread(Thread.currentThread()));
|
||||
check(! rwl.hasQueuedThread(Thread.currentThread()));
|
||||
check(rl.isLocked());
|
||||
check(rwl.isWriteLocked());
|
||||
check(rl.isHeldByCurrentThread());
|
||||
check(rwl.isWriteLockedByCurrentThread());
|
||||
check(rwl.writeLock().isHeldByCurrentThread());
|
||||
equal(rl.getQueueLength(), 0);
|
||||
equal(rwl.getQueueLength(), 0);
|
||||
equal(rwl.getReadLockCount(), i);
|
||||
equal(rl.getHoldCount(), i);
|
||||
equal(rwl.getReadHoldCount(), i);
|
||||
equal(rwl.getWriteHoldCount(), i);
|
||||
equal(rwl.writeLock().getHoldCount(), i);
|
||||
rwl.readLock().unlock();
|
||||
rwl.writeLock().unlock();
|
||||
rl.unlock();
|
||||
}
|
||||
THROWS(IllegalMonitorStateException.class,
|
||||
new F(){void f(){rl.unlock();}},
|
||||
new F(){void f(){rwl.readLock().unlock();}},
|
||||
new F(){void f(){rwl.writeLock().unlock();}});
|
||||
}
|
||||
|
||||
//--------------------- Infrastructure ---------------------------
|
||||
volatile int passed = 0, failed = 0;
|
||||
void pass() {passed++;}
|
||||
void fail() {failed++; Thread.dumpStack();}
|
||||
void fail(String msg) {System.err.println(msg); fail();}
|
||||
void unexpected(Throwable t) {failed++; t.printStackTrace();}
|
||||
void check(boolean cond) {if (cond) pass(); else fail();}
|
||||
void equal(Object x, Object y) {
|
||||
if (x == null ? y == null : x.equals(y)) pass();
|
||||
else fail(x + " not equal to " + y);}
|
||||
public static void main(String[] args) throws Throwable {
|
||||
new Count().instanceMain(args);}
|
||||
void instanceMain(String[] args) throws Throwable {
|
||||
try {test(args);} catch (Throwable t) {unexpected(t);}
|
||||
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
|
||||
if (failed > 0) throw new AssertionError("Some tests failed");}
|
||||
abstract class F {abstract void f() throws Throwable;}
|
||||
void THROWS(Class<? extends Throwable> k, F... fs) {
|
||||
for (F f : fs)
|
||||
try {f.f(); fail("Expected " + k.getName() + " not thrown");}
|
||||
catch (Throwable t) {
|
||||
if (k.isAssignableFrom(t.getClass())) pass();
|
||||
else unexpected(t);}}
|
||||
|
||||
static byte[] serializedForm(Object obj) {
|
||||
try {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
new ObjectOutputStream(baos).writeObject(obj);
|
||||
return baos.toByteArray();
|
||||
} catch (IOException e) { throw new RuntimeException(e); }}
|
||||
static Object readObject(byte[] bytes)
|
||||
throws IOException, ClassNotFoundException {
|
||||
InputStream is = new ByteArrayInputStream(bytes);
|
||||
return new ObjectInputStream(is).readObject();}
|
||||
@SuppressWarnings("unchecked")
|
||||
static <T> T serialClone(T obj) {
|
||||
try { return (T) readObject(serializedForm(obj)); }
|
||||
catch (Exception e) { throw new RuntimeException(e); }}
|
||||
}
|
||||
|
|
@ -0,0 +1,103 @@
|
|||
/*
|
||||
* 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 Doug Lea 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/
|
||||
*/
|
||||
|
||||
/**
|
||||
* Misc utilities in JSR166 performance tests
|
||||
*/
|
||||
class LoopHelpers {
|
||||
|
||||
// Some mindless computation to do between synchronizations...
|
||||
|
||||
/**
|
||||
* generates 32 bit pseudo-random numbers.
|
||||
* Adapted from http://www.snippets.org
|
||||
*/
|
||||
public static int compute1(int x) {
|
||||
int lo = 16807 * (x & 0xFFFF);
|
||||
int hi = 16807 * (x >>> 16);
|
||||
lo += (hi & 0x7FFF) << 16;
|
||||
if ((lo & 0x80000000) != 0) {
|
||||
lo &= 0x7fffffff;
|
||||
++lo;
|
||||
}
|
||||
lo += hi >>> 15;
|
||||
if (lo == 0 || (lo & 0x80000000) != 0) {
|
||||
lo &= 0x7fffffff;
|
||||
++lo;
|
||||
}
|
||||
return lo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes a linear congruential random number a random number
|
||||
* of times.
|
||||
*/
|
||||
public static int compute2(int x) {
|
||||
int loops = (x >>> 4) & 7;
|
||||
while (loops-- > 0) {
|
||||
x = (x * 2147483647) % 16807;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
public static class BarrierTimer implements Runnable {
|
||||
public volatile long startTime;
|
||||
public volatile long endTime;
|
||||
public void run() {
|
||||
long t = System.nanoTime();
|
||||
if (startTime == 0)
|
||||
startTime = t;
|
||||
else
|
||||
endTime = t;
|
||||
}
|
||||
public void clear() {
|
||||
startTime = 0;
|
||||
endTime = 0;
|
||||
}
|
||||
public long getTime() {
|
||||
return endTime - startTime;
|
||||
}
|
||||
}
|
||||
|
||||
public static String rightJustify(long n) {
|
||||
// There's probably a better way to do this...
|
||||
String field = " ";
|
||||
String num = Long.toString(n);
|
||||
if (num.length() >= field.length())
|
||||
return num;
|
||||
StringBuilder b = new StringBuilder(field);
|
||||
b.replace(b.length()-num.length(), b.length(), num);
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,201 @@
|
|||
/*
|
||||
* 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 Doug Lea 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
|
||||
* @bug 4486658
|
||||
* @summary Exercise multithreaded maps, by default ConcurrentHashMap.
|
||||
* Multithreaded hash table test. Each thread does a random walk
|
||||
* though elements of "key" array. On each iteration, it checks if
|
||||
* table includes key. If absent, with probability pinsert it
|
||||
* inserts it, and if present, with probability premove it removes
|
||||
* it. (pinsert and premove are expressed as percentages to simplify
|
||||
* parsing from command line.)
|
||||
* @library /test/lib
|
||||
*/
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.SplittableRandom;
|
||||
import java.util.concurrent.CyclicBarrier;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import jdk.test.lib.Utils;
|
||||
|
||||
public class MapLoops {
|
||||
static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
|
||||
static final int NKEYS = 100000;
|
||||
static int pinsert = 60;
|
||||
static int premove = 2;
|
||||
static int maxThreads = 5;
|
||||
static int nops = 10000; // 1000000
|
||||
static int removesPerMaxRandom;
|
||||
static int insertsPerMaxRandom;
|
||||
|
||||
static final ExecutorService pool = Executors.newCachedThreadPool();
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
Class mapClass = null;
|
||||
if (args.length > 0) {
|
||||
try {
|
||||
mapClass = Class.forName(args[0]);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException("Class " + args[0] + " not found.");
|
||||
}
|
||||
}
|
||||
else
|
||||
mapClass = RWMap.class;
|
||||
|
||||
if (args.length > 1)
|
||||
maxThreads = Integer.parseInt(args[1]);
|
||||
|
||||
if (args.length > 2)
|
||||
nops = Integer.parseInt(args[2]);
|
||||
|
||||
if (args.length > 3)
|
||||
pinsert = Integer.parseInt(args[3]);
|
||||
|
||||
if (args.length > 4)
|
||||
premove = Integer.parseInt(args[4]);
|
||||
|
||||
// normalize probabilities wrt random number generator
|
||||
removesPerMaxRandom = (int)((double)premove/100.0 * 0x7FFFFFFFL);
|
||||
insertsPerMaxRandom = (int)((double)pinsert/100.0 * 0x7FFFFFFFL);
|
||||
|
||||
System.out.println("Using " + mapClass.getName());
|
||||
|
||||
SplittableRandom rnd = new SplittableRandom();
|
||||
Integer[] key = new Integer[NKEYS];
|
||||
for (int i = 0; i < key.length; ++i)
|
||||
key[i] = new Integer(rnd.nextInt());
|
||||
|
||||
// warmup
|
||||
System.out.println("Warmup...");
|
||||
for (int k = 0; k < 2; ++k) {
|
||||
Map<Integer, Integer> map = (Map<Integer, Integer>)
|
||||
mapClass.getDeclaredConstructor().newInstance();
|
||||
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
|
||||
CyclicBarrier barrier = new CyclicBarrier(1, timer);
|
||||
new Runner(map, key, barrier, rnd.split()).run();
|
||||
map.clear();
|
||||
}
|
||||
|
||||
for (int i = 1; i <= maxThreads; i += (i+1) >>> 1) {
|
||||
System.out.print("Threads: " + i + "\t:");
|
||||
Map<Integer, Integer> map = (Map<Integer, Integer>)
|
||||
mapClass.getDeclaredConstructor().newInstance();
|
||||
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
|
||||
CyclicBarrier barrier = new CyclicBarrier(i+1, timer);
|
||||
for (int k = 0; k < i; ++k)
|
||||
pool.execute(new Runner(map, key, barrier, rnd.split()));
|
||||
barrier.await();
|
||||
barrier.await();
|
||||
long time = timer.getTime();
|
||||
long tpo = time / (i * (long)nops);
|
||||
System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op");
|
||||
double secs = (double)time / 1000000000.0;
|
||||
System.out.println("\t " + secs + "s run time");
|
||||
map.clear();
|
||||
}
|
||||
pool.shutdown();
|
||||
if (! pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS))
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
static class Runner implements Runnable {
|
||||
final Map<Integer,Integer> map;
|
||||
final Integer[] key;
|
||||
final CyclicBarrier barrier;
|
||||
final SplittableRandom rnd;
|
||||
int position;
|
||||
int total;
|
||||
|
||||
Runner(Map<Integer,Integer> map,
|
||||
Integer[] key,
|
||||
CyclicBarrier barrier,
|
||||
SplittableRandom rnd) {
|
||||
this.map = map;
|
||||
this.key = key;
|
||||
this.barrier = barrier;
|
||||
this.rnd = rnd;
|
||||
position = key.length / 2;
|
||||
}
|
||||
|
||||
int step() {
|
||||
// random-walk around key positions, bunching accesses
|
||||
int r = rnd.nextInt(Integer.MAX_VALUE);
|
||||
position += (r & 7) - 3;
|
||||
while (position >= key.length) position -= key.length;
|
||||
while (position < 0) position += key.length;
|
||||
|
||||
Integer k = key[position];
|
||||
Integer x = map.get(k);
|
||||
|
||||
if (x != null) {
|
||||
if (x.intValue() != k.intValue())
|
||||
throw new Error("bad mapping: " + x + " to " + k);
|
||||
|
||||
if (r < removesPerMaxRandom) {
|
||||
// get away from this position
|
||||
position = r % key.length;
|
||||
map.remove(k);
|
||||
return 2;
|
||||
}
|
||||
else
|
||||
total += LoopHelpers.compute2(LoopHelpers.compute1(x.intValue()));
|
||||
}
|
||||
else {
|
||||
if (r < insertsPerMaxRandom) {
|
||||
map.put(k, k);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
barrier.await();
|
||||
int ops = nops;
|
||||
while (ops > 0)
|
||||
ops -= step();
|
||||
barrier.await();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,143 @@
|
|||
/*
|
||||
* 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 Doug Lea 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/
|
||||
*/
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
|
||||
/**
|
||||
* This is an incomplete implementation of a wrapper class
|
||||
* that places read-write locks around unsynchronized Maps.
|
||||
* Exists as a sample input for MapLoops test.
|
||||
*/
|
||||
public class RWMap implements Map {
|
||||
private final Map m;
|
||||
private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
|
||||
|
||||
public RWMap(Map m) {
|
||||
if (m == null)
|
||||
throw new NullPointerException();
|
||||
this.m = m;
|
||||
}
|
||||
|
||||
public RWMap() {
|
||||
this(new TreeMap()); // use TreeMap by default
|
||||
}
|
||||
|
||||
public int size() {
|
||||
rwl.readLock().lock();
|
||||
try { return m.size(); }
|
||||
finally { rwl.readLock().unlock(); }
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
rwl.readLock().lock();
|
||||
try { return m.isEmpty(); }
|
||||
finally { rwl.readLock().unlock(); }
|
||||
}
|
||||
|
||||
public Object get(Object key) {
|
||||
rwl.readLock().lock();
|
||||
try { return m.get(key); }
|
||||
finally { rwl.readLock().unlock(); }
|
||||
}
|
||||
|
||||
public boolean containsKey(Object key) {
|
||||
rwl.readLock().lock();
|
||||
try { return m.containsKey(key); }
|
||||
finally { rwl.readLock().unlock(); }
|
||||
}
|
||||
|
||||
public boolean containsValue(Object value) {
|
||||
rwl.readLock().lock();
|
||||
try { return m.containsValue(value); }
|
||||
finally { rwl.readLock().unlock(); }
|
||||
}
|
||||
|
||||
public Set keySet() { // Not implemented
|
||||
return null;
|
||||
}
|
||||
|
||||
public Set entrySet() { // Not implemented
|
||||
return null;
|
||||
}
|
||||
|
||||
public Collection values() { // Not implemented
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
rwl.readLock().lock();
|
||||
try { return m.equals(o); }
|
||||
finally { rwl.readLock().unlock(); }
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
rwl.readLock().lock();
|
||||
try { return m.hashCode(); }
|
||||
finally { rwl.readLock().unlock(); }
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
rwl.readLock().lock();
|
||||
try { return m.toString(); }
|
||||
finally { rwl.readLock().unlock(); }
|
||||
}
|
||||
|
||||
public Object put(Object key, Object value) {
|
||||
rwl.writeLock().lock();
|
||||
try { return m.put(key, value); }
|
||||
finally { rwl.writeLock().unlock(); }
|
||||
}
|
||||
|
||||
public Object remove(Object key) {
|
||||
rwl.writeLock().lock();
|
||||
try { return m.remove(key); }
|
||||
finally { rwl.writeLock().unlock(); }
|
||||
}
|
||||
|
||||
public void putAll(Map map) {
|
||||
rwl.writeLock().lock();
|
||||
try { m.putAll(map); }
|
||||
finally { rwl.writeLock().unlock(); }
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
rwl.writeLock().lock();
|
||||
try { m.clear(); }
|
||||
finally { rwl.writeLock().unlock(); }
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue