Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,8 @@ private void cleanQueue() {
if (q.status < 0) { // cancelled
if ((s == null ? casTail(q, p) : s.casPrev(q, p)) &&
q.prev == p) {
p.casNext(q, s); // OK if fails
if (s != null)
p.casNext(q, s); // OK if fails
if (p.prev == null)
signalNext(p);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,8 @@ private void cleanQueue() {
if (q.status < 0) { // cancelled
if ((s == null ? casTail(q, p) : s.casPrev(q, p)) &&
q.prev == p) {
p.casNext(q, s); // OK if fails
if (s != null)
p.casNext(q, s); // OK if fails
if (p.prev == null)
signalNext(p);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,8 @@ private void cleanQueue() {
if (q.status < 0) { // cancelled
if ((s == null ? casTail(q, p) : s.casPrev(q, p)) &&
q.prev == p) {
p.casNext(q, s); // OK if fails
if (s != null)
p.casNext(q, s); // OK if fails
if (p.prev == null)
signalNext(p);
}
Expand Down
39 changes: 39 additions & 0 deletions test/jdk/java/util/concurrent/tck/SemaphoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@
* Pat Fisher, Mike Judd.
*/

import static java.util.concurrent.TimeUnit.MICROSECONDS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

import java.util.Collection;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicBoolean;

import junit.framework.Test;
import junit.framework.TestSuite;
Expand Down Expand Up @@ -672,4 +676,39 @@ public void testToString(boolean fair) {
assertTrue(s.toString().contains("Permits = -2"));
}

/**
* Test scenario for JDK-8386085
* When investigating failures of this test, it is important to know that AbstractQueuedSynchronizer,
* AbstractQueuedLongSynchronizer, and StampedLock all share similar code which was fixed for JDK-8386085
*/
public void testShortTimeoutAcquisition() throws InterruptedException {
final int width = Runtime.getRuntime().availableProcessors();
try (var pool = Executors.newFixedThreadPool(width)) {
// Setup
final AtomicBoolean done = new AtomicBoolean(false);
final CountDownLatch waitingToRun = new CountDownLatch(width);
final Semaphore s = new Semaphore(0);
final Callable<Void> c = () -> {
waitingToRun.countDown();
do {
s.tryAcquire(1, MICROSECONDS); // acquisition storm
} while (!done.get());
return null;
};

// Task creation
for(int i = 0; i < width; ++i)
pool.submit(c);

waitingToRun.await(); // Wait for all tasks to start
Thread.sleep(3000); // Wait a while for acquisitions
s.release(width); // Hand out permits
Thread.sleep(1000); // Wait a while for permit acquisitions

final int permitsAvailable = s.availablePermits();
done.set(true); // Ensure that tasks can exit
assertTrue(permitsAvailable < width); // Some permits should've been taken
}
}

}
37 changes: 36 additions & 1 deletion test/jdk/java/util/concurrent/tck/StampedLockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
*/

import static java.util.concurrent.TimeUnit.DAYS;
import static java.util.concurrent.TimeUnit.MICROSECONDS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

import static java.util.concurrent.locks.StampedLock.isLockStamp;
import static java.util.concurrent.locks.StampedLock.isOptimisticReadStamp;
import static java.util.concurrent.locks.StampedLock.isReadLockStamp;
Expand All @@ -45,6 +45,7 @@
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -1527,4 +1528,38 @@ public void testConcurrentAccess() throws Exception {
checkTimedGet(future, null);
}

/**
* test scenario for JDK-8386085
*/
public void testShortTimeoutAcquisition() throws InterruptedException {
final int width = Runtime.getRuntime().availableProcessors();
final StampedLock s = new StampedLock();
try (var pool = Executors.newFixedThreadPool(width)) {
// Setup
final AtomicBoolean done = new AtomicBoolean(false);
final CountDownLatch waitingToRun = new CountDownLatch(width);
final long stamp = s.writeLock();
assertTrue(s.validate(stamp));
final Callable<Void> c = () -> {
waitingToRun.countDown();
do {
long lock = s.tryWriteLock(1, MICROSECONDS); // acquisition storm
if (s.validate(lock))
s.unlockWrite(lock);
} while (!done.get());
return null;
};

// Task creation
for(int i = 0; i < width; ++i)
pool.submit(c);

waitingToRun.await(); // Wait for all tasks to start
Thread.sleep(3000); // Wait a while for acquisitions
s.unlockWrite(stamp); // Hand out permits
Thread.sleep(1000); // Wait a while for permit acquisitions
done.set(true); // Ensure that tasks can exit
}
assertTrue(s.validate(s.writeLockInterruptibly())); // Should succeed
}
}