test/jdk/java/util/concurrent/tck/StampedLockTest.java
author dl
Fri, 01 Nov 2019 09:07:59 -0700
changeset 58893 ec954ef6caf1
parent 54686 09f09b4e7808
permissions -rw-r--r--
8231026: Miscellaneous changes imported from jsr166 CVS 2019-11 Reviewed-by: martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     1
/*
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     3
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     6
 * published by the Free Software Foundation.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     7
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     8
 * This code is distributed in the hope that it will be useful, but WITHOUT
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
     9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    11
 * version 2 for more details (a copy is included in the LICENSE file that
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    12
 * accompanied this code).
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    13
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License version
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    15
 * 2 along with this work; if not, write to the Free Software Foundation,
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    17
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    18
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    19
 * or visit www.oracle.com if you need additional information or have any
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    20
 * questions.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    21
 */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    22
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    23
/*
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    24
 * This file is available under and governed by the GNU General Public
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    25
 * License version 2 only, as published by the Free Software Foundation.
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    26
 * However, the following notice accompanied the original version of this
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    27
 * file:
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    28
 *
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    29
 * Written by Doug Lea and Martin Buchholz
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    30
 * with assistance from members of JCP JSR-166 Expert Group and
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    31
 * released to the public domain, as explained at
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    32
 * http://creativecommons.org/publicdomain/zero/1.0/
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    33
 */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    34
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
    35
import static java.util.concurrent.TimeUnit.DAYS;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    36
import static java.util.concurrent.TimeUnit.MILLISECONDS;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    37
47343
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
    38
import static java.util.concurrent.locks.StampedLock.isLockStamp;
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
    39
import static java.util.concurrent.locks.StampedLock.isOptimisticReadStamp;
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
    40
import static java.util.concurrent.locks.StampedLock.isReadLockStamp;
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
    41
import static java.util.concurrent.locks.StampedLock.isWriteLockStamp;
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
    42
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
    43
import java.util.ArrayList;
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
    44
import java.util.List;
58893
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
    45
import java.util.concurrent.Callable;
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
    46
import java.util.concurrent.CompletableFuture;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    47
import java.util.concurrent.CountDownLatch;
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    48
import java.util.concurrent.Future;
58893
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
    49
import java.util.concurrent.ThreadLocalRandom;
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
    50
import java.util.concurrent.TimeUnit;
58893
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
    51
import java.util.concurrent.atomic.AtomicBoolean;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    52
import java.util.concurrent.locks.Lock;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    53
import java.util.concurrent.locks.StampedLock;
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
    54
import java.util.function.BiConsumer;
58893
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
    55
import java.util.function.Consumer;
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
    56
import java.util.function.Function;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    57
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    58
import junit.framework.Test;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    59
import junit.framework.TestSuite;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    60
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    61
public class StampedLockTest extends JSR166TestCase {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    62
    public static void main(String[] args) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    63
        main(suite(), args);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    64
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    65
    public static Test suite() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    66
        return new TestSuite(StampedLockTest.class);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    67
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    68
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    69
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    70
     * Releases write lock, checking isWriteLocked before and after
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    71
     */
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    72
    void releaseWriteLock(StampedLock lock, long stamp) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    73
        assertTrue(lock.isWriteLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    74
        assertValid(lock, stamp);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    75
        lock.unlockWrite(stamp);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    76
        assertFalse(lock.isWriteLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    77
        assertFalse(lock.validate(stamp));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    78
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    79
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    80
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    81
     * Releases read lock, checking isReadLocked before and after
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    82
     */
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    83
    void releaseReadLock(StampedLock lock, long stamp) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    84
        assertTrue(lock.isReadLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    85
        assertValid(lock, stamp);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    86
        lock.unlockRead(stamp);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    87
        assertFalse(lock.isReadLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    88
        assertTrue(lock.validate(stamp));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    89
    }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    90
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    91
    long assertNonZero(long v) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    92
        assertTrue(v != 0L);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    93
        return v;
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    94
    }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    95
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    96
    long assertValid(StampedLock lock, long stamp) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    97
        assertTrue(stamp != 0L);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    98
        assertTrue(lock.validate(stamp));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    99
        return stamp;
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   100
    }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   101
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   102
    void assertUnlocked(StampedLock lock) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   103
        assertFalse(lock.isReadLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   104
        assertFalse(lock.isWriteLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   105
        assertEquals(0, lock.getReadLockCount());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   106
        assertValid(lock, lock.tryOptimisticRead());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   107
    }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   108
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   109
    List<Action> lockLockers(Lock lock) {
58893
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   110
        return List.of(
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   111
            () -> lock.lock(),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   112
            () -> lock.lockInterruptibly(),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   113
            () -> lock.tryLock(),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   114
            () -> lock.tryLock(Long.MIN_VALUE, DAYS),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   115
            () -> lock.tryLock(0L, DAYS),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   116
            () -> lock.tryLock(Long.MAX_VALUE, DAYS));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   117
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   118
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   119
    List<Function<StampedLock, Long>> readLockers() {
58893
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   120
        return List.of(
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   121
            sl -> sl.readLock(),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   122
            sl -> sl.tryReadLock(),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   123
            sl -> readLockInterruptiblyUninterrupted(sl),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   124
            sl -> tryReadLockUninterrupted(sl, Long.MIN_VALUE, DAYS),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   125
            sl -> tryReadLockUninterrupted(sl, 0L, DAYS),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   126
            sl -> sl.tryConvertToReadLock(sl.tryOptimisticRead()));
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   127
    }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   128
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   129
    List<BiConsumer<StampedLock, Long>> readUnlockers() {
58893
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   130
        return List.of(
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   131
            (sl, stamp) -> sl.unlockRead(stamp),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   132
            (sl, stamp) -> assertTrue(sl.tryUnlockRead()),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   133
            (sl, stamp) -> sl.asReadLock().unlock(),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   134
            (sl, stamp) -> sl.unlock(stamp),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   135
            (sl, stamp) -> assertValid(sl, sl.tryConvertToOptimisticRead(stamp)));
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   136
    }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   137
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   138
    List<Function<StampedLock, Long>> writeLockers() {
58893
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   139
        return List.of(
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   140
            sl -> sl.writeLock(),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   141
            sl -> sl.tryWriteLock(),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   142
            sl -> writeLockInterruptiblyUninterrupted(sl),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   143
            sl -> tryWriteLockUninterrupted(sl, Long.MIN_VALUE, DAYS),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   144
            sl -> tryWriteLockUninterrupted(sl, 0L, DAYS),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   145
            sl -> sl.tryConvertToWriteLock(sl.tryOptimisticRead()));
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   146
    }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   147
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   148
    List<BiConsumer<StampedLock, Long>> writeUnlockers() {
58893
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   149
        return List.of(
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   150
            (sl, stamp) -> sl.unlockWrite(stamp),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   151
            (sl, stamp) -> assertTrue(sl.tryUnlockWrite()),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   152
            (sl, stamp) -> sl.asWriteLock().unlock(),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   153
            (sl, stamp) -> sl.unlock(stamp),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
   154
            (sl, stamp) -> assertValid(sl, sl.tryConvertToOptimisticRead(stamp)));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   155
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   156
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   157
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   158
     * Constructed StampedLock is in unlocked state
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   159
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   160
    public void testConstructor() {
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   161
        assertUnlocked(new StampedLock());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   162
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   163
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   164
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   165
     * write-locking, then unlocking, an unlocked lock succeed
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   166
     */
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   167
    public void testWriteLock_lockUnlock() {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   168
        StampedLock lock = new StampedLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   169
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   170
        for (Function<StampedLock, Long> writeLocker : writeLockers())
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   171
        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   172
            assertFalse(lock.isWriteLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   173
            assertFalse(lock.isReadLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   174
            assertEquals(0, lock.getReadLockCount());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   175
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   176
            long s = writeLocker.apply(lock);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   177
            assertValid(lock, s);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   178
            assertTrue(lock.isWriteLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   179
            assertFalse(lock.isReadLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   180
            assertEquals(0, lock.getReadLockCount());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   181
            writeUnlocker.accept(lock, s);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   182
            assertUnlocked(lock);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   183
        }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   184
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   185
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   186
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   187
     * read-locking, then unlocking, an unlocked lock succeed
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   188
     */
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   189
    public void testReadLock_lockUnlock() {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   190
        StampedLock lock = new StampedLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   191
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   192
        for (Function<StampedLock, Long> readLocker : readLockers())
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   193
        for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers()) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   194
            long s = 42;
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   195
            for (int i = 0; i < 2; i++) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   196
                s = assertValid(lock, readLocker.apply(lock));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   197
                assertFalse(lock.isWriteLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   198
                assertTrue(lock.isReadLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   199
                assertEquals(i + 1, lock.getReadLockCount());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   200
            }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   201
            for (int i = 0; i < 2; i++) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   202
                assertFalse(lock.isWriteLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   203
                assertTrue(lock.isReadLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   204
                assertEquals(2 - i, lock.getReadLockCount());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   205
                readUnlocker.accept(lock, s);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   206
            }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   207
            assertUnlocked(lock);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   208
        }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   209
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   210
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   211
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   212
     * tryUnlockWrite fails if not write locked
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   213
     */
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   214
    public void testTryUnlockWrite_failure() {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   215
        StampedLock lock = new StampedLock();
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   216
        assertFalse(lock.tryUnlockWrite());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   217
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   218
        for (Function<StampedLock, Long> readLocker : readLockers())
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   219
        for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers()) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   220
            long s = assertValid(lock, readLocker.apply(lock));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   221
            assertFalse(lock.tryUnlockWrite());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   222
            assertTrue(lock.isReadLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   223
            readUnlocker.accept(lock, s);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   224
            assertUnlocked(lock);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   225
        }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   226
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   227
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   228
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   229
     * tryUnlockRead fails if not read locked
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   230
     */
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   231
    public void testTryUnlockRead_failure() {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   232
        StampedLock lock = new StampedLock();
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   233
        assertFalse(lock.tryUnlockRead());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   234
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   235
        for (Function<StampedLock, Long> writeLocker : writeLockers())
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   236
        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   237
            long s = writeLocker.apply(lock);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   238
            assertFalse(lock.tryUnlockRead());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   239
            assertTrue(lock.isWriteLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   240
            writeUnlocker.accept(lock, s);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   241
            assertUnlocked(lock);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   242
        }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   243
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   244
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   245
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   246
     * validate(0L) fails
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   247
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   248
    public void testValidate0() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   249
        StampedLock lock = new StampedLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   250
        assertFalse(lock.validate(0L));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   251
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   252
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   253
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   254
     * A stamp obtained from a successful lock operation validates while the lock is held
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   255
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   256
    public void testValidate() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   257
        StampedLock lock = new StampedLock();
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   258
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   259
        for (Function<StampedLock, Long> readLocker : readLockers())
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   260
        for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers()) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   261
            long s = assertNonZero(readLocker.apply(lock));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   262
            assertTrue(lock.validate(s));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   263
            readUnlocker.accept(lock, s);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   264
        }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   265
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   266
        for (Function<StampedLock, Long> writeLocker : writeLockers())
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   267
        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   268
            long s = assertNonZero(writeLocker.apply(lock));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   269
            assertTrue(lock.validate(s));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   270
            writeUnlocker.accept(lock, s);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   271
        }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   272
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   273
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   274
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   275
     * A stamp obtained from an unsuccessful lock operation does not validate
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   276
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   277
    public void testValidate2() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   278
        StampedLock lock = new StampedLock();
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   279
        long s = assertNonZero(lock.writeLock());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   280
        assertTrue(lock.validate(s));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   281
        assertFalse(lock.validate(lock.tryWriteLock()));
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44589
diff changeset
   282
        assertFalse(lock.validate(lock.tryWriteLock(randomExpiredTimeout(),
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44589
diff changeset
   283
                                                    randomTimeUnit())));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   284
        assertFalse(lock.validate(lock.tryReadLock()));
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44589
diff changeset
   285
        assertFalse(lock.validate(lock.tryWriteLock(randomExpiredTimeout(),
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44589
diff changeset
   286
                                                    randomTimeUnit())));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   287
        assertFalse(lock.validate(lock.tryOptimisticRead()));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   288
        lock.unlockWrite(s);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   289
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   290
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   291
    void assertThrowInterruptedExceptionWhenPreInterrupted(Action[] actions) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   292
        for (Action action : actions) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   293
            Thread.currentThread().interrupt();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   294
            try {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   295
                action.run();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   296
                shouldThrow();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   297
            }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   298
            catch (InterruptedException success) {}
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   299
            catch (Throwable fail) { threadUnexpectedException(fail); }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   300
            assertFalse(Thread.interrupted());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   301
        }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   302
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   303
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   304
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   305
     * interruptible operations throw InterruptedException when pre-interrupted
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   306
     */
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   307
    public void testInterruptibleOperationsThrowInterruptedExceptionWhenPreInterrupted() {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   308
        final StampedLock lock = new StampedLock();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   309
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   310
        Action[] interruptibleLockActions = {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   311
            () -> lock.writeLockInterruptibly(),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   312
            () -> lock.tryWriteLock(Long.MIN_VALUE, DAYS),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   313
            () -> lock.tryWriteLock(Long.MAX_VALUE, DAYS),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   314
            () -> lock.readLockInterruptibly(),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   315
            () -> lock.tryReadLock(Long.MIN_VALUE, DAYS),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   316
            () -> lock.tryReadLock(Long.MAX_VALUE, DAYS),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   317
            () -> lock.asWriteLock().lockInterruptibly(),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   318
            () -> lock.asWriteLock().tryLock(0L, DAYS),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   319
            () -> lock.asWriteLock().tryLock(Long.MAX_VALUE, DAYS),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   320
            () -> lock.asReadLock().lockInterruptibly(),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   321
            () -> lock.asReadLock().tryLock(0L, DAYS),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   322
            () -> lock.asReadLock().tryLock(Long.MAX_VALUE, DAYS),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   323
        };
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   324
        shuffle(interruptibleLockActions);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   325
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   326
        assertThrowInterruptedExceptionWhenPreInterrupted(interruptibleLockActions);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   327
        {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   328
            long s = lock.writeLock();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   329
            assertThrowInterruptedExceptionWhenPreInterrupted(interruptibleLockActions);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   330
            lock.unlockWrite(s);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   331
        }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   332
        {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   333
            long s = lock.readLock();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   334
            assertThrowInterruptedExceptionWhenPreInterrupted(interruptibleLockActions);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   335
            lock.unlockRead(s);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   336
        }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   337
    }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   338
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   339
    void assertThrowInterruptedExceptionWhenInterrupted(Action[] actions) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   340
        int n = actions.length;
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   341
        Future<?>[] futures = new Future<?>[n];
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   342
        CountDownLatch threadsStarted = new CountDownLatch(n);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   343
        CountDownLatch done = new CountDownLatch(n);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   344
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   345
        for (int i = 0; i < n; i++) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   346
            Action action = actions[i];
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   347
            futures[i] = cachedThreadPool.submit(new CheckedRunnable() {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   348
                public void realRun() throws Throwable {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   349
                    threadsStarted.countDown();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   350
                    try {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   351
                        action.run();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   352
                        shouldThrow();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   353
                    }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   354
                    catch (InterruptedException success) {}
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   355
                    catch (Throwable fail) { threadUnexpectedException(fail); }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   356
                    assertFalse(Thread.interrupted());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   357
                    done.countDown();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   358
                }});
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   359
        }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   360
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   361
        await(threadsStarted);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   362
        assertEquals(n, done.getCount());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   363
        for (Future<?> future : futures) // Interrupt all the tasks
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   364
            future.cancel(true);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   365
        await(done);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   366
    }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   367
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   368
    /**
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   369
     * interruptible operations throw InterruptedException when write locked and interrupted
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   370
     */
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   371
    public void testInterruptibleOperationsThrowInterruptedExceptionWriteLockedInterrupted() {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   372
        final StampedLock lock = new StampedLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   373
        long s = lock.writeLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   374
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   375
        Action[] interruptibleLockBlockingActions = {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   376
            () -> lock.writeLockInterruptibly(),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   377
            () -> lock.tryWriteLock(Long.MAX_VALUE, DAYS),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   378
            () -> lock.readLockInterruptibly(),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   379
            () -> lock.tryReadLock(Long.MAX_VALUE, DAYS),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   380
            () -> lock.asWriteLock().lockInterruptibly(),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   381
            () -> lock.asWriteLock().tryLock(Long.MAX_VALUE, DAYS),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   382
            () -> lock.asReadLock().lockInterruptibly(),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   383
            () -> lock.asReadLock().tryLock(Long.MAX_VALUE, DAYS),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   384
        };
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   385
        shuffle(interruptibleLockBlockingActions);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   386
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   387
        assertThrowInterruptedExceptionWhenInterrupted(interruptibleLockBlockingActions);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   388
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   389
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   390
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   391
     * interruptible operations throw InterruptedException when read locked and interrupted
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   392
     */
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   393
    public void testInterruptibleOperationsThrowInterruptedExceptionReadLockedInterrupted() {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   394
        final StampedLock lock = new StampedLock();
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   395
        long s = lock.readLock();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   396
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   397
        Action[] interruptibleLockBlockingActions = {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   398
            () -> lock.writeLockInterruptibly(),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   399
            () -> lock.tryWriteLock(Long.MAX_VALUE, DAYS),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   400
            () -> lock.asWriteLock().lockInterruptibly(),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   401
            () -> lock.asWriteLock().tryLock(Long.MAX_VALUE, DAYS),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   402
        };
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   403
        shuffle(interruptibleLockBlockingActions);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   404
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   405
        assertThrowInterruptedExceptionWhenInterrupted(interruptibleLockBlockingActions);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   406
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   407
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   408
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   409
     * Non-interruptible operations ignore and preserve interrupt status
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   410
     */
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   411
    public void testNonInterruptibleOperationsIgnoreInterrupts() {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   412
        final StampedLock lock = new StampedLock();
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   413
        Thread.currentThread().interrupt();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   414
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   415
        for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers()) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   416
            long s = assertValid(lock, lock.readLock());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   417
            readUnlocker.accept(lock, s);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   418
            s = assertValid(lock, lock.tryReadLock());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   419
            readUnlocker.accept(lock, s);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   420
        }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   421
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   422
        lock.asReadLock().lock();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   423
        lock.asReadLock().unlock();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   424
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   425
        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   426
            long s = assertValid(lock, lock.writeLock());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   427
            writeUnlocker.accept(lock, s);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   428
            s = assertValid(lock, lock.tryWriteLock());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   429
            writeUnlocker.accept(lock, s);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   430
        }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   431
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   432
        lock.asWriteLock().lock();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   433
        lock.asWriteLock().unlock();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   434
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   435
        assertTrue(Thread.interrupted());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   436
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   437
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   438
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   439
     * tryWriteLock on an unlocked lock succeeds
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   440
     */
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   441
    public void testTryWriteLock() {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   442
        final StampedLock lock = new StampedLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   443
        long s = lock.tryWriteLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   444
        assertTrue(s != 0L);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   445
        assertTrue(lock.isWriteLocked());
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   446
        assertEquals(0L, lock.tryWriteLock());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   447
        releaseWriteLock(lock, s);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   448
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   449
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   450
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   451
     * tryWriteLock fails if locked
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   452
     */
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   453
    public void testTryWriteLockWhenLocked() {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   454
        final StampedLock lock = new StampedLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   455
        long s = lock.writeLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   456
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   457
            public void realRun() {
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   458
                assertEquals(0L, lock.tryWriteLock());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   459
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   460
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   461
        assertEquals(0L, lock.tryWriteLock());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   462
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   463
        releaseWriteLock(lock, s);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   464
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   465
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   466
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   467
     * tryReadLock fails if write-locked
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   468
     */
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   469
    public void testTryReadLockWhenLocked() {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   470
        final StampedLock lock = new StampedLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   471
        long s = lock.writeLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   472
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   473
            public void realRun() {
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   474
                assertEquals(0L, lock.tryReadLock());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   475
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   476
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   477
        assertEquals(0L, lock.tryReadLock());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   478
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   479
        releaseWriteLock(lock, s);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   480
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   481
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   482
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   483
     * Multiple threads can hold a read lock when not write-locked
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   484
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   485
    public void testMultipleReadLocks() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   486
        final StampedLock lock = new StampedLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   487
        final long s = lock.readLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   488
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   489
            public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   490
                long s2 = lock.tryReadLock();
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   491
                assertValid(lock, s2);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   492
                lock.unlockRead(s2);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   493
                long s3 = lock.tryReadLock(LONG_DELAY_MS, MILLISECONDS);
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   494
                assertValid(lock, s3);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   495
                lock.unlockRead(s3);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   496
                long s4 = lock.readLock();
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   497
                assertValid(lock, s4);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   498
                lock.unlockRead(s4);
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   499
                lock.asReadLock().lock();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   500
                lock.asReadLock().unlock();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   501
                lock.asReadLock().lockInterruptibly();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   502
                lock.asReadLock().unlock();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   503
                lock.asReadLock().tryLock(Long.MIN_VALUE, DAYS);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   504
                lock.asReadLock().unlock();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   505
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   506
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   507
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   508
        lock.unlockRead(s);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   509
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   510
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   511
    /**
44039
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 42322
diff changeset
   512
     * writeLock() succeeds only after a reading thread unlocks
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   513
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   514
    public void testWriteAfterReadLock() throws InterruptedException {
44039
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 42322
diff changeset
   515
        final CountDownLatch aboutToLock = new CountDownLatch(1);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   516
        final StampedLock lock = new StampedLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   517
        long rs = lock.readLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   518
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   519
            public void realRun() {
44039
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 42322
diff changeset
   520
                aboutToLock.countDown();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   521
                long s = lock.writeLock();
44039
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 42322
diff changeset
   522
                assertTrue(lock.isWriteLocked());
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 42322
diff changeset
   523
                assertFalse(lock.isReadLocked());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   524
                lock.unlockWrite(s);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   525
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   526
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 44039
diff changeset
   527
        await(aboutToLock);
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44589
diff changeset
   528
        assertThreadBlocks(t, Thread.State.WAITING);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   529
        assertFalse(lock.isWriteLocked());
44039
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 42322
diff changeset
   530
        assertTrue(lock.isReadLocked());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   531
        lock.unlockRead(rs);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   532
        awaitTermination(t);
44039
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 42322
diff changeset
   533
        assertUnlocked(lock);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   534
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   535
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   536
    /**
44039
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 42322
diff changeset
   537
     * writeLock() succeeds only after reading threads unlock
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   538
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   539
    public void testWriteAfterMultipleReadLocks() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   540
        final StampedLock lock = new StampedLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   541
        long s = lock.readLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   542
        Thread t1 = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   543
            public void realRun() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   544
                long rs = lock.readLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   545
                lock.unlockRead(rs);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   546
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   547
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   548
        awaitTermination(t1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   549
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   550
        Thread t2 = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   551
            public void realRun() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   552
                long ws = lock.writeLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   553
                lock.unlockWrite(ws);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   554
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   555
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   556
        assertTrue(lock.isReadLocked());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   557
        assertFalse(lock.isWriteLocked());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   558
        lock.unlockRead(s);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   559
        awaitTermination(t2);
44039
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 42322
diff changeset
   560
        assertUnlocked(lock);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   561
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   562
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   563
    /**
44039
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 42322
diff changeset
   564
     * readLock() succeed only after a writing thread unlocks
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   565
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   566
    public void testReadAfterWriteLock() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   567
        final StampedLock lock = new StampedLock();
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   568
        final CountDownLatch threadsStarted = new CountDownLatch(2);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   569
        final long s = lock.writeLock();
44039
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 42322
diff changeset
   570
        final Runnable acquireReleaseReadLock = new CheckedRunnable() {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   571
            public void realRun() {
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   572
                threadsStarted.countDown();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   573
                long rs = lock.readLock();
44039
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 42322
diff changeset
   574
                assertTrue(lock.isReadLocked());
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 42322
diff changeset
   575
                assertFalse(lock.isWriteLocked());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   576
                lock.unlockRead(rs);
44039
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 42322
diff changeset
   577
            }};
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 42322
diff changeset
   578
        Thread t1 = newStartedThread(acquireReleaseReadLock);
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 42322
diff changeset
   579
        Thread t2 = newStartedThread(acquireReleaseReadLock);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   580
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   581
        await(threadsStarted);
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44589
diff changeset
   582
        assertThreadBlocks(t1, Thread.State.WAITING);
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44589
diff changeset
   583
        assertThreadBlocks(t2, Thread.State.WAITING);
44039
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 42322
diff changeset
   584
        assertTrue(lock.isWriteLocked());
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 42322
diff changeset
   585
        assertFalse(lock.isReadLocked());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   586
        releaseWriteLock(lock, s);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   587
        awaitTermination(t1);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   588
        awaitTermination(t2);
44039
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 42322
diff changeset
   589
        assertUnlocked(lock);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   590
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   591
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   592
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   593
     * tryReadLock succeeds if read locked but not write locked
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   594
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   595
    public void testTryLockWhenReadLocked() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   596
        final StampedLock lock = new StampedLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   597
        long s = lock.readLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   598
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   599
            public void realRun() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   600
                long rs = lock.tryReadLock();
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   601
                assertValid(lock, rs);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   602
                lock.unlockRead(rs);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   603
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   604
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   605
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   606
        lock.unlockRead(s);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   607
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   608
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   609
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   610
     * tryWriteLock fails when read locked
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   611
     */
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   612
    public void testTryWriteLockWhenReadLocked() {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   613
        final StampedLock lock = new StampedLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   614
        long s = lock.readLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   615
        Thread t = newStartedThread(new CheckedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   616
            public void realRun() {
48048
f55cdd83e303 8191069: Miscellaneous changes imported from jsr166 CVS 2017-12
dl
parents: 47343
diff changeset
   617
                assertEquals(0L, lock.tryWriteLock());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   618
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   619
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   620
        awaitTermination(t);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   621
        lock.unlockRead(s);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   622
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   623
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   624
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   625
     * timed lock operations time out if lock not available
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   626
     */
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   627
    public void testTimedLock_Timeout() throws Exception {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   628
        ArrayList<Future<?>> futures = new ArrayList<>();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   629
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   630
        // Write locked
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   631
        final StampedLock lock = new StampedLock();
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   632
        long stamp = lock.writeLock();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   633
        assertEquals(0L, lock.tryReadLock(0L, DAYS));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   634
        assertEquals(0L, lock.tryReadLock(Long.MIN_VALUE, DAYS));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   635
        assertFalse(lock.asReadLock().tryLock(0L, DAYS));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   636
        assertFalse(lock.asReadLock().tryLock(Long.MIN_VALUE, DAYS));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   637
        assertEquals(0L, lock.tryWriteLock(0L, DAYS));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   638
        assertEquals(0L, lock.tryWriteLock(Long.MIN_VALUE, DAYS));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   639
        assertFalse(lock.asWriteLock().tryLock(0L, DAYS));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   640
        assertFalse(lock.asWriteLock().tryLock(Long.MIN_VALUE, DAYS));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   641
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   642
        futures.add(cachedThreadPool.submit(new CheckedRunnable() {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   643
            public void realRun() throws InterruptedException {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   644
                long startTime = System.nanoTime();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   645
                assertEquals(0L, lock.tryWriteLock(timeoutMillis(), MILLISECONDS));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   646
                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   647
            }}));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   648
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   649
        futures.add(cachedThreadPool.submit(new CheckedRunnable() {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   650
            public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   651
                long startTime = System.nanoTime();
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   652
                assertEquals(0L, lock.tryReadLock(timeoutMillis(), MILLISECONDS));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   653
                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   654
            }}));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   655
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   656
        // Read locked
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   657
        final StampedLock lock2 = new StampedLock();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   658
        long stamp2 = lock2.readLock();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   659
        assertEquals(0L, lock2.tryWriteLock(0L, DAYS));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   660
        assertEquals(0L, lock2.tryWriteLock(Long.MIN_VALUE, DAYS));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   661
        assertFalse(lock2.asWriteLock().tryLock(0L, DAYS));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   662
        assertFalse(lock2.asWriteLock().tryLock(Long.MIN_VALUE, DAYS));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   663
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   664
        futures.add(cachedThreadPool.submit(new CheckedRunnable() {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   665
            public void realRun() throws InterruptedException {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   666
                long startTime = System.nanoTime();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   667
                assertEquals(0L, lock2.tryWriteLock(timeoutMillis(), MILLISECONDS));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   668
                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   669
            }}));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   670
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   671
        for (Future<?> future : futures)
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   672
            assertNull(future.get());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   673
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   674
        releaseWriteLock(lock, stamp);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   675
        releaseReadLock(lock2, stamp2);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   676
    }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   677
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   678
    /**
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   679
     * writeLockInterruptibly succeeds if unlocked
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   680
     */
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   681
    public void testWriteLockInterruptibly() throws InterruptedException {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   682
        final StampedLock lock = new StampedLock();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   683
        long s = lock.writeLockInterruptibly();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   684
        assertTrue(lock.isWriteLocked());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   685
        releaseWriteLock(lock, s);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   686
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   687
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   688
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   689
     * readLockInterruptibly succeeds if lock free
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   690
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   691
    public void testReadLockInterruptibly() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   692
        final StampedLock lock = new StampedLock();
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   693
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   694
        long s = assertValid(lock, lock.readLockInterruptibly());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   695
        assertTrue(lock.isReadLocked());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   696
        lock.unlockRead(s);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   697
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   698
        lock.asReadLock().lockInterruptibly();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   699
        assertTrue(lock.isReadLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   700
        lock.asReadLock().unlock();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   701
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   702
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   703
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   704
     * A serialized lock deserializes as unlocked
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   705
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   706
    public void testSerialization() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   707
        StampedLock lock = new StampedLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   708
        lock.writeLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   709
        StampedLock clone = serialClone(lock);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   710
        assertTrue(lock.isWriteLocked());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   711
        assertFalse(clone.isWriteLocked());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   712
        long s = clone.writeLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   713
        assertTrue(clone.isWriteLocked());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   714
        clone.unlockWrite(s);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   715
        assertFalse(clone.isWriteLocked());
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   716
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   717
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   718
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   719
     * toString indicates current lock state
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   720
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   721
    public void testToString() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   722
        StampedLock lock = new StampedLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   723
        assertTrue(lock.toString().contains("Unlocked"));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   724
        long s = lock.writeLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   725
        assertTrue(lock.toString().contains("Write-locked"));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   726
        lock.unlockWrite(s);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   727
        s = lock.readLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   728
        assertTrue(lock.toString().contains("Read-locks"));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   729
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   730
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   731
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   732
     * tryOptimisticRead succeeds and validates if unlocked, fails if
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   733
     * exclusively locked
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   734
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   735
    public void testValidateOptimistic() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   736
        StampedLock lock = new StampedLock();
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   737
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   738
        assertValid(lock, lock.tryOptimisticRead());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   739
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   740
        for (Function<StampedLock, Long> writeLocker : writeLockers()) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   741
            long s = assertValid(lock, writeLocker.apply(lock));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   742
            assertEquals(0L, lock.tryOptimisticRead());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   743
            releaseWriteLock(lock, s);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   744
        }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   745
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   746
        for (Function<StampedLock, Long> readLocker : readLockers()) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   747
            long s = assertValid(lock, readLocker.apply(lock));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   748
            long p = assertValid(lock, lock.tryOptimisticRead());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   749
            releaseReadLock(lock, s);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   750
            assertTrue(lock.validate(p));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   751
        }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   752
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   753
        assertValid(lock, lock.tryOptimisticRead());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   754
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   755
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   756
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   757
     * tryOptimisticRead stamp does not validate if a write lock intervenes
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   758
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   759
    public void testValidateOptimisticWriteLocked() {
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   760
        final StampedLock lock = new StampedLock();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   761
        final long p = assertValid(lock, lock.tryOptimisticRead());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   762
        final long s = assertValid(lock, lock.writeLock());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   763
        assertFalse(lock.validate(p));
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   764
        assertEquals(0L, lock.tryOptimisticRead());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   765
        assertTrue(lock.validate(s));
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   766
        lock.unlockWrite(s);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   767
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   768
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   769
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   770
     * tryOptimisticRead stamp does not validate if a write lock
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   771
     * intervenes in another thread
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   772
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   773
    public void testValidateOptimisticWriteLocked2()
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   774
            throws InterruptedException {
44039
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 42322
diff changeset
   775
        final CountDownLatch locked = new CountDownLatch(1);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   776
        final StampedLock lock = new StampedLock();
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   777
        final long p = assertValid(lock, lock.tryOptimisticRead());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   778
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   779
        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   780
            public void realRun() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   781
                lock.writeLockInterruptibly();
44039
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 42322
diff changeset
   782
                locked.countDown();
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   783
                lock.writeLockInterruptibly();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   784
            }});
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   785
44589
64d9270bd24c 8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents: 44039
diff changeset
   786
        await(locked);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   787
        assertFalse(lock.validate(p));
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   788
        assertEquals(0L, lock.tryOptimisticRead());
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44589
diff changeset
   789
        assertThreadBlocks(t, Thread.State.WAITING);
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   790
        t.interrupt();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   791
        awaitTermination(t);
44039
058585425bb7 8173909: Miscellaneous changes imported from jsr166 CVS 2017-03
dl
parents: 42322
diff changeset
   792
        assertTrue(lock.isWriteLocked());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   793
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   794
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   795
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   796
     * tryConvertToOptimisticRead succeeds and validates if successfully locked
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   797
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   798
    public void testTryConvertToOptimisticRead() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   799
        StampedLock lock = new StampedLock();
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   800
        long s, p, q;
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   801
        assertEquals(0L, lock.tryConvertToOptimisticRead(0L));
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   802
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   803
        s = assertValid(lock, lock.tryOptimisticRead());
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   804
        assertEquals(s, lock.tryConvertToOptimisticRead(s));
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   805
        assertTrue(lock.validate(s));
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   806
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   807
        for (Function<StampedLock, Long> writeLocker : writeLockers()) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   808
            s = assertValid(lock, writeLocker.apply(lock));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   809
            p = assertValid(lock, lock.tryConvertToOptimisticRead(s));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   810
            assertFalse(lock.validate(s));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   811
            assertTrue(lock.validate(p));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   812
            assertUnlocked(lock);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   813
        }
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   814
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   815
        for (Function<StampedLock, Long> readLocker : readLockers()) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   816
            s = assertValid(lock, readLocker.apply(lock));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   817
            q = assertValid(lock, lock.tryOptimisticRead());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   818
            assertEquals(q, lock.tryConvertToOptimisticRead(q));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   819
            assertTrue(lock.validate(q));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   820
            assertTrue(lock.isReadLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   821
            p = assertValid(lock, lock.tryConvertToOptimisticRead(s));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   822
            assertTrue(lock.validate(p));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   823
            assertTrue(lock.validate(s));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   824
            assertUnlocked(lock);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   825
            assertEquals(q, lock.tryConvertToOptimisticRead(q));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   826
            assertTrue(lock.validate(q));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   827
        }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   828
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   829
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   830
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   831
     * tryConvertToReadLock succeeds for valid stamps
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   832
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   833
    public void testTryConvertToReadLock() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   834
        StampedLock lock = new StampedLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   835
        long s, p;
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   836
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   837
        assertEquals(0L, lock.tryConvertToReadLock(0L));
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   838
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   839
        s = assertValid(lock, lock.tryOptimisticRead());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   840
        p = assertValid(lock, lock.tryConvertToReadLock(s));
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   841
        assertTrue(lock.isReadLocked());
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   842
        assertEquals(1, lock.getReadLockCount());
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   843
        assertTrue(lock.validate(s));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   844
        lock.unlockRead(p);
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   845
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   846
        s = assertValid(lock, lock.tryOptimisticRead());
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   847
        lock.readLock();
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   848
        p = assertValid(lock, lock.tryConvertToReadLock(s));
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   849
        assertTrue(lock.isReadLocked());
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   850
        assertEquals(2, lock.getReadLockCount());
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   851
        lock.unlockRead(p);
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   852
        lock.unlockRead(p);
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   853
        assertUnlocked(lock);
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   854
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   855
        for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers()) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   856
            for (Function<StampedLock, Long> writeLocker : writeLockers()) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   857
                s = assertValid(lock, writeLocker.apply(lock));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   858
                p = assertValid(lock, lock.tryConvertToReadLock(s));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   859
                assertFalse(lock.validate(s));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   860
                assertTrue(lock.isReadLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   861
                assertEquals(1, lock.getReadLockCount());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   862
                readUnlocker.accept(lock, p);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   863
            }
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   864
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   865
            for (Function<StampedLock, Long> readLocker : readLockers()) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   866
                s = assertValid(lock, readLocker.apply(lock));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   867
                assertEquals(s, lock.tryConvertToReadLock(s));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   868
                assertTrue(lock.validate(s));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   869
                assertTrue(lock.isReadLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   870
                assertEquals(1, lock.getReadLockCount());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   871
                readUnlocker.accept(lock, s);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   872
            }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   873
        }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   874
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   875
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   876
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   877
     * tryConvertToWriteLock succeeds if lock available; fails if multiply read locked
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   878
     */
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   879
    public void testTryConvertToWriteLock() throws InterruptedException {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   880
        StampedLock lock = new StampedLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   881
        long s, p;
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   882
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   883
        assertEquals(0L, lock.tryConvertToWriteLock(0L));
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   884
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   885
        assertTrue((s = lock.tryOptimisticRead()) != 0L);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   886
        assertTrue((p = lock.tryConvertToWriteLock(s)) != 0L);
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   887
        assertTrue(lock.isWriteLocked());
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   888
        lock.unlockWrite(p);
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   889
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   890
        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   891
            for (Function<StampedLock, Long> writeLocker : writeLockers()) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   892
                s = assertValid(lock, writeLocker.apply(lock));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   893
                assertEquals(s, lock.tryConvertToWriteLock(s));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   894
                assertTrue(lock.validate(s));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   895
                assertTrue(lock.isWriteLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   896
                writeUnlocker.accept(lock, s);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   897
            }
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   898
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   899
            for (Function<StampedLock, Long> readLocker : readLockers()) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   900
                s = assertValid(lock, readLocker.apply(lock));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   901
                p = assertValid(lock, lock.tryConvertToWriteLock(s));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   902
                assertFalse(lock.validate(s));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   903
                assertTrue(lock.validate(p));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   904
                assertTrue(lock.isWriteLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   905
                writeUnlocker.accept(lock, p);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   906
            }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   907
        }
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   908
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   909
        // failure if multiply read locked
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   910
        for (Function<StampedLock, Long> readLocker : readLockers()) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   911
            s = assertValid(lock, readLocker.apply(lock));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   912
            p = assertValid(lock, readLocker.apply(lock));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   913
            assertEquals(0L, lock.tryConvertToWriteLock(s));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   914
            assertTrue(lock.validate(s));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   915
            assertTrue(lock.validate(p));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   916
            assertEquals(2, lock.getReadLockCount());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   917
            lock.unlock(p);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   918
            lock.unlock(s);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   919
            assertUnlocked(lock);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   920
        }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   921
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   922
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   923
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   924
     * asWriteLock can be locked and unlocked
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   925
     */
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   926
    public void testAsWriteLock() throws Throwable {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   927
        StampedLock sl = new StampedLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   928
        Lock lock = sl.asWriteLock();
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   929
        for (Action locker : lockLockers(lock)) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   930
            locker.run();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   931
            assertTrue(sl.isWriteLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   932
            assertFalse(sl.isReadLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   933
            assertFalse(lock.tryLock());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   934
            lock.unlock();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   935
            assertUnlocked(sl);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   936
        }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   937
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   938
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   939
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   940
     * asReadLock can be locked and unlocked
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   941
     */
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   942
    public void testAsReadLock() throws Throwable {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   943
        StampedLock sl = new StampedLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   944
        Lock lock = sl.asReadLock();
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   945
        for (Action locker : lockLockers(lock)) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   946
            locker.run();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   947
            assertTrue(sl.isReadLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   948
            assertFalse(sl.isWriteLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   949
            assertEquals(1, sl.getReadLockCount());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   950
            locker.run();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   951
            assertTrue(sl.isReadLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   952
            assertEquals(2, sl.getReadLockCount());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   953
            lock.unlock();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   954
            lock.unlock();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   955
            assertUnlocked(sl);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   956
        }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   957
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   958
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   959
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   960
     * asReadWriteLock.writeLock can be locked and unlocked
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   961
     */
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   962
    public void testAsReadWriteLockWriteLock() throws Throwable {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   963
        StampedLock sl = new StampedLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   964
        Lock lock = sl.asReadWriteLock().writeLock();
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   965
        for (Action locker : lockLockers(lock)) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   966
            locker.run();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   967
            assertTrue(sl.isWriteLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   968
            assertFalse(sl.isReadLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   969
            assertFalse(lock.tryLock());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   970
            lock.unlock();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   971
            assertUnlocked(sl);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   972
        }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   973
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   974
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   975
    /**
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   976
     * asReadWriteLock.readLock can be locked and unlocked
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   977
     */
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   978
    public void testAsReadWriteLockReadLock() throws Throwable {
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   979
        StampedLock sl = new StampedLock();
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   980
        Lock lock = sl.asReadWriteLock().readLock();
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   981
        for (Action locker : lockLockers(lock)) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   982
            locker.run();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   983
            assertTrue(sl.isReadLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   984
            assertFalse(sl.isWriteLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   985
            assertEquals(1, sl.getReadLockCount());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   986
            locker.run();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   987
            assertTrue(sl.isReadLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   988
            assertEquals(2, sl.getReadLockCount());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   989
            lock.unlock();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   990
            lock.unlock();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   991
            assertUnlocked(sl);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   992
        }
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   993
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   994
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   995
    /**
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   996
     * Lock.newCondition throws UnsupportedOperationException
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   997
     */
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   998
    public void testLockViewsDoNotSupportConditions() {
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
   999
        StampedLock sl = new StampedLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1000
        assertThrows(UnsupportedOperationException.class,
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1001
                     () -> sl.asWriteLock().newCondition(),
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1002
                     () -> sl.asReadLock().newCondition(),
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1003
                     () -> sl.asReadWriteLock().writeLock().newCondition(),
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1004
                     () -> sl.asReadWriteLock().readLock().newCondition());
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1005
    }
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1006
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1007
    /**
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1008
     * Passing optimistic read stamps to unlock operations result in
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1009
     * IllegalMonitorStateException
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1010
     */
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1011
    public void testCannotUnlockOptimisticReadStamps() {
54686
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1012
        {
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1013
            StampedLock sl = new StampedLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1014
            long stamp = assertValid(sl, sl.tryOptimisticRead());
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1015
            assertThrows(IllegalMonitorStateException.class,
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1016
                () -> sl.unlockRead(stamp));
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1017
        }
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1018
        {
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1019
            StampedLock sl = new StampedLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1020
            long stamp = sl.tryOptimisticRead();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1021
            assertThrows(IllegalMonitorStateException.class,
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1022
                () -> sl.unlock(stamp));
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1023
        }
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1024
54686
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1025
        {
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1026
            StampedLock sl = new StampedLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1027
            long stamp = sl.tryOptimisticRead();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1028
            sl.writeLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1029
            assertThrows(IllegalMonitorStateException.class,
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1030
                () -> sl.unlock(stamp));
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1031
        }
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1032
        {
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1033
            StampedLock sl = new StampedLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1034
            sl.readLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1035
            long stamp = assertValid(sl, sl.tryOptimisticRead());
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1036
            assertThrows(IllegalMonitorStateException.class,
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1037
                () -> sl.unlockRead(stamp));
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1038
        }
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1039
        {
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1040
            StampedLock sl = new StampedLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1041
            sl.readLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1042
            long stamp = assertValid(sl, sl.tryOptimisticRead());
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1043
            assertThrows(IllegalMonitorStateException.class,
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1044
                () -> sl.unlock(stamp));
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1045
        }
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1046
54686
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1047
        {
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1048
            StampedLock sl = new StampedLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1049
            long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1050
            assertValid(sl, stamp);
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1051
            sl.writeLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1052
            assertThrows(IllegalMonitorStateException.class,
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1053
                () -> sl.unlockWrite(stamp));
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1054
        }
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1055
        {
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1056
            StampedLock sl = new StampedLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1057
            long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1058
            sl.writeLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1059
            assertThrows(IllegalMonitorStateException.class,
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1060
                () -> sl.unlock(stamp));
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1061
        }
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1062
        {
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1063
            StampedLock sl = new StampedLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1064
            long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1065
            sl.readLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1066
            assertThrows(IllegalMonitorStateException.class,
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1067
                () -> sl.unlockRead(stamp));
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1068
        }
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1069
        {
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1070
            StampedLock sl = new StampedLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1071
            long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1072
            sl.readLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1073
            assertThrows(IllegalMonitorStateException.class,
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1074
                () -> sl.unlock(stamp));
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1075
        }
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1076
54686
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1077
        {
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1078
            StampedLock sl = new StampedLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1079
            long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1080
            assertValid(sl, stamp);
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1081
            sl.writeLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1082
            assertThrows(IllegalMonitorStateException.class,
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1083
                () -> sl.unlockWrite(stamp));
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1084
            }
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1085
        {
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1086
            StampedLock sl = new StampedLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1087
            long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1088
            sl.writeLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1089
            assertThrows(IllegalMonitorStateException.class,
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1090
                () -> sl.unlock(stamp));
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1091
        }
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1092
        {
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1093
            StampedLock sl = new StampedLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1094
            long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1095
            sl.readLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1096
            assertThrows(IllegalMonitorStateException.class,
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1097
                () -> sl.unlockRead(stamp));
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1098
        }
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1099
        {
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1100
            StampedLock sl = new StampedLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1101
            sl.readLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1102
            long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1103
            assertValid(sl, stamp);
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1104
            sl.readLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1105
            assertThrows(IllegalMonitorStateException.class,
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1106
                () -> sl.unlockRead(stamp));
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1107
        }
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1108
        {
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1109
            StampedLock sl = new StampedLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1110
            long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1111
            sl.readLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1112
            assertThrows(IllegalMonitorStateException.class,
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1113
                () -> sl.unlock(stamp));
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1114
        }
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1115
        {
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1116
            StampedLock sl = new StampedLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1117
            sl.readLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1118
            long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1119
            sl.readLock();
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1120
            assertThrows(IllegalMonitorStateException.class,
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1121
                () -> sl.unlock(stamp));
09f09b4e7808 8219138: Miscellaneous changes imported from jsr166 CVS 2019-05
dl
parents: 48048
diff changeset
  1122
        }
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1123
    }
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1124
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1125
    static long writeLockInterruptiblyUninterrupted(StampedLock sl) {
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1126
        try { return sl.writeLockInterruptibly(); }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1127
        catch (InterruptedException ex) { throw new AssertionError(ex); }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1128
    }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1129
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1130
    static long tryWriteLockUninterrupted(StampedLock sl, long time, TimeUnit unit) {
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1131
        try { return sl.tryWriteLock(time, unit); }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1132
        catch (InterruptedException ex) { throw new AssertionError(ex); }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1133
    }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1134
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1135
    static long readLockInterruptiblyUninterrupted(StampedLock sl) {
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1136
        try { return sl.readLockInterruptibly(); }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1137
        catch (InterruptedException ex) { throw new AssertionError(ex); }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1138
    }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1139
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1140
    static long tryReadLockUninterrupted(StampedLock sl, long time, TimeUnit unit) {
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1141
        try { return sl.tryReadLock(time, unit); }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1142
        catch (InterruptedException ex) { throw new AssertionError(ex); }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1143
    }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1144
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1145
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1146
     * Invalid stamps result in IllegalMonitorStateException
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1147
     */
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1148
    public void testInvalidStampsThrowIllegalMonitorStateException() {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1149
        final StampedLock sl = new StampedLock();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1150
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1151
        assertThrows(IllegalMonitorStateException.class,
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1152
                     () -> sl.unlockWrite(0L),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1153
                     () -> sl.unlockRead(0L),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1154
                     () -> sl.unlock(0L));
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1155
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1156
        final long optimisticStamp = sl.tryOptimisticRead();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1157
        final long readStamp = sl.readLock();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1158
        sl.unlockRead(readStamp);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1159
        final long writeStamp = sl.writeLock();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1160
        sl.unlockWrite(writeStamp);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1161
        assertTrue(optimisticStamp != 0L && readStamp != 0L && writeStamp != 0L);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1162
        final long[] noLongerValidStamps = { optimisticStamp, readStamp, writeStamp };
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1163
        final Runnable assertNoLongerValidStampsThrow = () -> {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1164
            for (long noLongerValidStamp : noLongerValidStamps)
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1165
                assertThrows(IllegalMonitorStateException.class,
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1166
                             () -> sl.unlockWrite(noLongerValidStamp),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1167
                             () -> sl.unlockRead(noLongerValidStamp),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1168
                             () -> sl.unlock(noLongerValidStamp));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1169
        };
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1170
        assertNoLongerValidStampsThrow.run();
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1171
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1172
        for (Function<StampedLock, Long> readLocker : readLockers())
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1173
        for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers()) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1174
            final long stamp = readLocker.apply(sl);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1175
            assertValid(sl, stamp);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1176
            assertNoLongerValidStampsThrow.run();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1177
            assertThrows(IllegalMonitorStateException.class,
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1178
                         () -> sl.unlockWrite(stamp),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1179
                         () -> sl.unlockRead(sl.tryOptimisticRead()),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1180
                         () -> sl.unlockRead(0L));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1181
            readUnlocker.accept(sl, stamp);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1182
            assertUnlocked(sl);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1183
            assertNoLongerValidStampsThrow.run();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1184
        }
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1185
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1186
        for (Function<StampedLock, Long> writeLocker : writeLockers())
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1187
        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) {
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1188
            final long stamp = writeLocker.apply(sl);
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1189
            assertValid(sl, stamp);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1190
            assertNoLongerValidStampsThrow.run();
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1191
            assertThrows(IllegalMonitorStateException.class,
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1192
                         () -> sl.unlockRead(stamp),
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1193
                         () -> sl.unlockWrite(0L));
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1194
            writeUnlocker.accept(sl, stamp);
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1195
            assertUnlocked(sl);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1196
            assertNoLongerValidStampsThrow.run();
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1197
        }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1198
    }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1199
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1200
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1201
     * Read locks can be very deeply nested
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1202
     */
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1203
    public void testDeeplyNestedReadLocks() {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1204
        final StampedLock lock = new StampedLock();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1205
        final int depth = 300;
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1206
        final long[] stamps = new long[depth];
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1207
        final List<Function<StampedLock, Long>> readLockers = readLockers();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1208
        final List<BiConsumer<StampedLock, Long>> readUnlockers = readUnlockers();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1209
        for (int i = 0; i < depth; i++) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1210
            Function<StampedLock, Long> readLocker
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1211
                = readLockers.get(i % readLockers.size());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1212
            long stamp = readLocker.apply(lock);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1213
            assertEquals(i + 1, lock.getReadLockCount());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1214
            assertTrue(lock.isReadLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1215
            stamps[i] = stamp;
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1216
        }
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1217
        for (int i = 0; i < depth; i++) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1218
            BiConsumer<StampedLock, Long> readUnlocker
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1219
                = readUnlockers.get(i % readUnlockers.size());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1220
            assertEquals(depth - i, lock.getReadLockCount());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1221
            assertTrue(lock.isReadLocked());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1222
            readUnlocker.accept(lock, stamps[depth - 1 - i]);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1223
        }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1224
        assertUnlocked(lock);
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1225
    }
47307
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1226
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1227
    /**
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1228
     * Stamped locks are not reentrant.
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1229
     */
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1230
    public void testNonReentrant() throws InterruptedException {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1231
        final StampedLock lock = new StampedLock();
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1232
        long stamp;
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1233
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1234
        stamp = lock.writeLock();
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1235
        assertValid(lock, stamp);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1236
        assertEquals(0L, lock.tryWriteLock(0L, DAYS));
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1237
        assertEquals(0L, lock.tryReadLock(0L, DAYS));
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1238
        assertValid(lock, stamp);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1239
        lock.unlockWrite(stamp);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1240
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1241
        stamp = lock.tryWriteLock(1L, DAYS);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1242
        assertEquals(0L, lock.tryWriteLock(0L, DAYS));
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1243
        assertValid(lock, stamp);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1244
        lock.unlockWrite(stamp);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1245
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1246
        stamp = lock.readLock();
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1247
        assertEquals(0L, lock.tryWriteLock(0L, DAYS));
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1248
        assertValid(lock, stamp);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1249
        lock.unlockRead(stamp);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1250
    }
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1251
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1252
    /**
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1253
     * """StampedLocks have no notion of ownership. Locks acquired in
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1254
     * one thread can be released or converted in another."""
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1255
     */
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1256
    public void testNoOwnership() throws Throwable {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1257
        ArrayList<Future<?>> futures = new ArrayList<>();
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1258
        for (Function<StampedLock, Long> writeLocker : writeLockers())
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1259
        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1260
            StampedLock lock = new StampedLock();
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1261
            long stamp = writeLocker.apply(lock);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1262
            futures.add(cachedThreadPool.submit(new CheckedRunnable() {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1263
                public void realRun() {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1264
                    writeUnlocker.accept(lock, stamp);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1265
                    assertUnlocked(lock);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1266
                    assertFalse(lock.validate(stamp));
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1267
                }}));
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1268
        }
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1269
        for (Future<?> future : futures)
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1270
            assertNull(future.get());
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1271
    }
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1272
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1273
    /** Tries out sample usage code from StampedLock javadoc. */
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1274
    public void testSampleUsage() throws Throwable {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1275
        class Point {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1276
            private double x, y;
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1277
            private final StampedLock sl = new StampedLock();
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1278
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1279
            void move(double deltaX, double deltaY) { // an exclusively locked method
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1280
                long stamp = sl.writeLock();
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1281
                try {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1282
                    x += deltaX;
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1283
                    y += deltaY;
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1284
                } finally {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1285
                    sl.unlockWrite(stamp);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1286
                }
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1287
            }
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1288
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1289
            double distanceFromOrigin() { // A read-only method
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1290
                double currentX, currentY;
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1291
                long stamp = sl.tryOptimisticRead();
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1292
                do {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1293
                    if (stamp == 0L)
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1294
                        stamp = sl.readLock();
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1295
                    try {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1296
                        // possibly racy reads
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1297
                        currentX = x;
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1298
                        currentY = y;
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1299
                    } finally {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1300
                        stamp = sl.tryConvertToOptimisticRead(stamp);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1301
                    }
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1302
                } while (stamp == 0);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1303
                return Math.hypot(currentX, currentY);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1304
            }
47343
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1305
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1306
            double distanceFromOrigin2() {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1307
                long stamp = sl.tryOptimisticRead();
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1308
                try {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1309
                    retryHoldingLock:
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1310
                    for (;; stamp = sl.readLock()) {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1311
                        if (stamp == 0L)
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1312
                            continue retryHoldingLock;
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1313
                        // possibly racy reads
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1314
                        double currentX = x;
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1315
                        double currentY = y;
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1316
                        if (!sl.validate(stamp))
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1317
                            continue retryHoldingLock;
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1318
                        return Math.hypot(currentX, currentY);
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1319
                    }
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1320
                } finally {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1321
                    if (StampedLock.isReadLockStamp(stamp))
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1322
                        sl.unlockRead(stamp);
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1323
                }
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1324
            }
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1325
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1326
            void moveIfAtOrigin(double newX, double newY) {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1327
                long stamp = sl.readLock();
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1328
                try {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1329
                    while (x == 0.0 && y == 0.0) {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1330
                        long ws = sl.tryConvertToWriteLock(stamp);
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1331
                        if (ws != 0L) {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1332
                            stamp = ws;
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1333
                            x = newX;
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1334
                            y = newY;
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1335
                            return;
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1336
                        }
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1337
                        else {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1338
                            sl.unlockRead(stamp);
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1339
                            stamp = sl.writeLock();
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1340
                        }
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1341
                    }
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1342
                } finally {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1343
                    sl.unlock(stamp);
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1344
                }
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1345
            }
47307
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1346
        }
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1347
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1348
        Point p = new Point();
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1349
        p.move(3.0, 4.0);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1350
        assertEquals(5.0, p.distanceFromOrigin());
47343
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1351
        p.moveIfAtOrigin(5.0, 12.0);
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1352
        assertEquals(5.0, p.distanceFromOrigin2());
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1353
    }
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1354
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1355
    /**
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1356
     * Stamp inspection methods work as expected, and do not inspect
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1357
     * the state of the lock itself.
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1358
     */
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1359
    public void testStampStateInspectionMethods() {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1360
        StampedLock lock = new StampedLock();
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1361
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1362
        assertFalse(isWriteLockStamp(0L));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1363
        assertFalse(isReadLockStamp(0L));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1364
        assertFalse(isLockStamp(0L));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1365
        assertFalse(isOptimisticReadStamp(0L));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1366
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1367
        {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1368
            long stamp = lock.writeLock();
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1369
            for (int i = 0; i < 2; i++) {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1370
                assertTrue(isWriteLockStamp(stamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1371
                assertFalse(isReadLockStamp(stamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1372
                assertTrue(isLockStamp(stamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1373
                assertFalse(isOptimisticReadStamp(stamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1374
                if (i == 0)
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1375
                    lock.unlockWrite(stamp);
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1376
            }
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1377
        }
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1378
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1379
        {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1380
            long stamp = lock.readLock();
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1381
            for (int i = 0; i < 2; i++) {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1382
                assertFalse(isWriteLockStamp(stamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1383
                assertTrue(isReadLockStamp(stamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1384
                assertTrue(isLockStamp(stamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1385
                assertFalse(isOptimisticReadStamp(stamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1386
                if (i == 0)
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1387
                    lock.unlockRead(stamp);
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1388
            }
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1389
        }
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1390
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1391
        {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1392
            long optimisticStamp = lock.tryOptimisticRead();
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1393
            long readStamp = lock.tryConvertToReadLock(optimisticStamp);
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1394
            long writeStamp = lock.tryConvertToWriteLock(readStamp);
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1395
            for (int i = 0; i < 2; i++) {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1396
                assertFalse(isWriteLockStamp(optimisticStamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1397
                assertFalse(isReadLockStamp(optimisticStamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1398
                assertFalse(isLockStamp(optimisticStamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1399
                assertTrue(isOptimisticReadStamp(optimisticStamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1400
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1401
                assertFalse(isWriteLockStamp(readStamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1402
                assertTrue(isReadLockStamp(readStamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1403
                assertTrue(isLockStamp(readStamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1404
                assertFalse(isOptimisticReadStamp(readStamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1405
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1406
                assertTrue(isWriteLockStamp(writeStamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1407
                assertFalse(isReadLockStamp(writeStamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1408
                assertTrue(isLockStamp(writeStamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1409
                assertFalse(isOptimisticReadStamp(writeStamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1410
                if (i == 0)
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1411
                    lock.unlockWrite(writeStamp);
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1412
            }
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1413
        }
47307
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1414
    }
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1415
58893
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1416
    /**
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1417
     * Multiple threads repeatedly contend for the same lock.
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1418
     */
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1419
    public void testConcurrentAccess() throws Exception {
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1420
        final StampedLock sl = new StampedLock();
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1421
        final Lock wl = sl.asWriteLock();
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1422
        final Lock rl = sl.asReadLock();
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1423
        final long testDurationMillis = expensiveTests ? 1000 : 2;
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1424
        final int nTasks = ThreadLocalRandom.current().nextInt(1, 10);
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1425
        final AtomicBoolean done = new AtomicBoolean(false);
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1426
        final List<CompletableFuture> futures = new ArrayList<>();
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1427
        final List<Callable<Long>> stampedWriteLockers = List.of(
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1428
            () -> sl.writeLock(),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1429
            () -> writeLockInterruptiblyUninterrupted(sl),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1430
            () -> tryWriteLockUninterrupted(sl, LONG_DELAY_MS, MILLISECONDS),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1431
            () -> {
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1432
                long stamp;
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1433
                do { stamp = sl.tryConvertToWriteLock(sl.tryOptimisticRead()); }
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1434
                while (stamp == 0L);
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1435
                return stamp;
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1436
            },
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1437
            () -> {
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1438
              long stamp;
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1439
              do { stamp = sl.tryWriteLock(); } while (stamp == 0L);
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1440
              return stamp;
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1441
            },
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1442
            () -> {
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1443
              long stamp;
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1444
              do { stamp = sl.tryWriteLock(0L, DAYS); } while (stamp == 0L);
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1445
              return stamp;
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1446
            });
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1447
        final List<Callable<Long>> stampedReadLockers = List.of(
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1448
            () -> sl.readLock(),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1449
            () -> readLockInterruptiblyUninterrupted(sl),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1450
            () -> tryReadLockUninterrupted(sl, LONG_DELAY_MS, MILLISECONDS),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1451
            () -> {
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1452
                long stamp;
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1453
                do { stamp = sl.tryConvertToReadLock(sl.tryOptimisticRead()); }
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1454
                while (stamp == 0L);
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1455
                return stamp;
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1456
            },
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1457
            () -> {
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1458
              long stamp;
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1459
              do { stamp = sl.tryReadLock(); } while (stamp == 0L);
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1460
              return stamp;
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1461
            },
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1462
            () -> {
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1463
              long stamp;
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1464
              do { stamp = sl.tryReadLock(0L, DAYS); } while (stamp == 0L);
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1465
              return stamp;
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1466
            });
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1467
        final List<Consumer<Long>> stampedWriteUnlockers = List.of(
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1468
            stamp -> sl.unlockWrite(stamp),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1469
            stamp -> sl.unlock(stamp),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1470
            stamp -> assertTrue(sl.tryUnlockWrite()),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1471
            stamp -> wl.unlock(),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1472
            stamp -> sl.tryConvertToOptimisticRead(stamp));
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1473
        final List<Consumer<Long>> stampedReadUnlockers = List.of(
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1474
            stamp -> sl.unlockRead(stamp),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1475
            stamp -> sl.unlock(stamp),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1476
            stamp -> assertTrue(sl.tryUnlockRead()),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1477
            stamp -> rl.unlock(),
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1478
            stamp -> sl.tryConvertToOptimisticRead(stamp));
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1479
        final Action writer = () -> {
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1480
            // repeatedly acquires write lock
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1481
            var locker = chooseRandomly(stampedWriteLockers);
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1482
            var unlocker = chooseRandomly(stampedWriteUnlockers);
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1483
            while (!done.getAcquire()) {
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1484
                long stamp = locker.call();
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1485
                try {
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1486
                    assertTrue(isWriteLockStamp(stamp));
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1487
                    assertTrue(sl.isWriteLocked());
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1488
                    assertFalse(isReadLockStamp(stamp));
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1489
                    assertFalse(sl.isReadLocked());
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1490
                    assertEquals(0, sl.getReadLockCount());
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1491
                    assertTrue(sl.validate(stamp));
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1492
                } finally {
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1493
                    unlocker.accept(stamp);
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1494
                }
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1495
            }
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1496
        };
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1497
        final Action reader = () -> {
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1498
            // repeatedly acquires read lock
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1499
            var locker = chooseRandomly(stampedReadLockers);
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1500
            var unlocker = chooseRandomly(stampedReadUnlockers);
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1501
            while (!done.getAcquire()) {
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1502
                long stamp = locker.call();
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1503
                try {
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1504
                    assertFalse(isWriteLockStamp(stamp));
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1505
                    assertFalse(sl.isWriteLocked());
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1506
                    assertTrue(isReadLockStamp(stamp));
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1507
                    assertTrue(sl.isReadLocked());
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1508
                    assertTrue(sl.getReadLockCount() > 0);
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1509
                    assertTrue(sl.validate(stamp));
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1510
                } finally {
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1511
                    unlocker.accept(stamp);
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1512
                }
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1513
            }
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1514
        };
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1515
        for (int i = nTasks; i--> 0; ) {
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1516
            Action task = chooseRandomly(writer, reader);
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1517
            futures.add(CompletableFuture.runAsync(checkedRunnable(task)));
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1518
        }
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1519
        Thread.sleep(testDurationMillis);
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1520
        done.setRelease(true);
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1521
        for (var future : futures)
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1522
            checkTimedGet(future, null);
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1523
    }
ec954ef6caf1 8231026: Miscellaneous changes imported from jsr166 CVS 2019-11
dl
parents: 54686
diff changeset
  1524
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1525
}