test/jdk/java/util/concurrent/tck/StampedLockTest.java
author dl
Sat, 02 Dec 2017 10:13:18 -0800
changeset 48048 f55cdd83e303
parent 47343 75ee0b48ea63
child 54686 09f09b4e7808
permissions -rw-r--r--
8191069: Miscellaneous changes imported from jsr166 CVS 2017-12 Reviewed-by: martin, psandoz
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;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    45
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
    46
import java.util.concurrent.Future;
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
    47
import java.util.concurrent.TimeUnit;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    48
import java.util.concurrent.locks.Lock;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    49
import java.util.concurrent.locks.StampedLock;
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
    50
import java.util.function.BiConsumer;
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
    51
import java.util.function.Function;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    52
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    53
import junit.framework.Test;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    54
import junit.framework.TestSuite;
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    55
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    56
public class StampedLockTest extends JSR166TestCase {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    57
    public static void main(String[] args) {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    58
        main(suite(), args);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    59
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    60
    public static Test suite() {
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    61
        return new TestSuite(StampedLockTest.class);
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    62
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    63
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    64
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    65
     * Releases write lock, checking isWriteLocked before and after
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    66
     */
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    67
    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
    68
        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
    69
        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
    70
        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
    71
        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
    72
        assertFalse(lock.validate(stamp));
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    73
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    74
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    75
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    76
     * Releases read lock, checking isReadLocked before and after
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
    77
     */
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    78
    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
    79
        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
    80
        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
    81
        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
    82
        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
    83
        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
    84
    }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    85
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    86
    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
    87
        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
    88
        return v;
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 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
    92
        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
    93
        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
    94
        return stamp;
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
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
    97
    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
    98
        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
    99
        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
   100
        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
   101
        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
   102
    }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   103
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   104
    List<Action> lockLockers(Lock lock) {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   105
        List<Action> lockers = new ArrayList<>();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   106
        lockers.add(() -> lock.lock());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   107
        lockers.add(() -> lock.lockInterruptibly());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   108
        lockers.add(() -> lock.tryLock());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   109
        lockers.add(() -> lock.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
   110
        lockers.add(() -> lock.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
   111
        lockers.add(() -> lock.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
   112
        return lockers;
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   113
    }
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
   114
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   115
    List<Function<StampedLock, Long>> readLockers() {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   116
        List<Function<StampedLock, Long>> readLockers = new ArrayList<>();
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 40817
diff changeset
   117
        readLockers.add(sl -> sl.readLock());
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 40817
diff changeset
   118
        readLockers.add(sl -> sl.tryReadLock());
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 40817
diff changeset
   119
        readLockers.add(sl -> readLockInterruptiblyUninterrupted(sl));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 40817
diff changeset
   120
        readLockers.add(sl -> tryReadLockUninterrupted(sl, Long.MIN_VALUE, DAYS));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 40817
diff changeset
   121
        readLockers.add(sl -> tryReadLockUninterrupted(sl, 0L, DAYS));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 40817
diff changeset
   122
        readLockers.add(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
   123
        return readLockers;
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   124
    }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   125
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   126
    List<BiConsumer<StampedLock, Long>> readUnlockers() {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   127
        List<BiConsumer<StampedLock, Long>> readUnlockers = new ArrayList<>();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   128
        readUnlockers.add((sl, stamp) -> 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
   129
        readUnlockers.add((sl, stamp) -> assertTrue(sl.tryUnlockRead()));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   130
        readUnlockers.add((sl, stamp) -> sl.asReadLock().unlock());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   131
        readUnlockers.add((sl, stamp) -> sl.unlock(stamp));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   132
        readUnlockers.add((sl, stamp) -> assertValid(sl, sl.tryConvertToOptimisticRead(stamp)));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   133
        return readUnlockers;
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   134
    }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   135
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   136
    List<Function<StampedLock, Long>> writeLockers() {
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   137
        List<Function<StampedLock, Long>> writeLockers = new ArrayList<>();
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 40817
diff changeset
   138
        writeLockers.add(sl -> sl.writeLock());
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 40817
diff changeset
   139
        writeLockers.add(sl -> sl.tryWriteLock());
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 40817
diff changeset
   140
        writeLockers.add(sl -> writeLockInterruptiblyUninterrupted(sl));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 40817
diff changeset
   141
        writeLockers.add(sl -> tryWriteLockUninterrupted(sl, Long.MIN_VALUE, DAYS));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 40817
diff changeset
   142
        writeLockers.add(sl -> tryWriteLockUninterrupted(sl, 0L, DAYS));
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 40817
diff changeset
   143
        writeLockers.add(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
   144
        return writeLockers;
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   145
    }
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
    List<BiConsumer<StampedLock, Long>> writeUnlockers() {
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 = new ArrayList<>();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   149
        writeUnlockers.add((sl, stamp) -> 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
   150
        writeUnlockers.add((sl, stamp) -> assertTrue(sl.tryUnlockWrite()));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   151
        writeUnlockers.add((sl, stamp) -> sl.asWriteLock().unlock());
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   152
        writeUnlockers.add((sl, stamp) -> sl.unlock(stamp));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   153
        writeUnlockers.add((sl, stamp) -> assertValid(sl, sl.tryConvertToOptimisticRead(stamp)));
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
   154
        return writeUnlockers;
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() {
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1012
        Runnable[] actions = {
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1013
            () -> {
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1014
                StampedLock sl = 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
  1015
                long stamp = assertValid(sl, sl.tryOptimisticRead());
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1016
                sl.unlockRead(stamp);
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1017
            },
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1018
            () -> {
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1019
                StampedLock sl = new StampedLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1020
                long stamp = sl.tryOptimisticRead();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1021
                sl.unlock(stamp);
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1022
            },
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1023
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1024
            () -> {
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1025
                StampedLock sl = new StampedLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1026
                long stamp = sl.tryOptimisticRead();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1027
                sl.writeLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1028
                sl.unlock(stamp);
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1029
            },
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1030
            () -> {
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1031
                StampedLock sl = new StampedLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1032
                sl.readLock();
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1033
                long stamp = assertValid(sl, sl.tryOptimisticRead());
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1034
                sl.unlockRead(stamp);
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1035
            },
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1036
            () -> {
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1037
                StampedLock sl = new StampedLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1038
                sl.readLock();
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1039
                long stamp = assertValid(sl, sl.tryOptimisticRead());
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1040
                sl.unlock(stamp);
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1041
            },
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1042
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1043
            () -> {
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1044
                StampedLock sl = new StampedLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1045
                long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1046
                assertValid(sl, stamp);
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1047
                sl.writeLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1048
                sl.unlockWrite(stamp);
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1049
            },
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1050
            () -> {
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1051
                StampedLock sl = new StampedLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1052
                long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1053
                sl.writeLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1054
                sl.unlock(stamp);
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1055
            },
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1056
            () -> {
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1057
                StampedLock sl = new StampedLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1058
                long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1059
                sl.readLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1060
                sl.unlockRead(stamp);
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1061
            },
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1062
            () -> {
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1063
                StampedLock sl = new StampedLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1064
                long stamp = sl.tryConvertToOptimisticRead(sl.writeLock());
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1065
                sl.readLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1066
                sl.unlock(stamp);
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1067
            },
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1068
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1069
            () -> {
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1070
                StampedLock sl = new StampedLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1071
                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1072
                assertValid(sl, stamp);
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1073
                sl.writeLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1074
                sl.unlockWrite(stamp);
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1075
            },
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1076
            () -> {
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1077
                StampedLock sl = new StampedLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1078
                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1079
                sl.writeLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1080
                sl.unlock(stamp);
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1081
            },
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1082
            () -> {
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1083
                StampedLock sl = new StampedLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1084
                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1085
                sl.readLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1086
                sl.unlockRead(stamp);
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1087
            },
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1088
            () -> {
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1089
                StampedLock sl = new StampedLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1090
                sl.readLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1091
                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1092
                assertValid(sl, stamp);
39722
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1093
                sl.readLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1094
                sl.unlockRead(stamp);
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1095
            },
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1096
            () -> {
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1097
                StampedLock sl = new StampedLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1098
                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1099
                sl.readLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1100
                sl.unlock(stamp);
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1101
            },
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1102
            () -> {
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1103
                StampedLock sl = new StampedLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1104
                sl.readLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1105
                long stamp = sl.tryConvertToOptimisticRead(sl.readLock());
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1106
                sl.readLock();
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1107
                sl.unlock(stamp);
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1108
            },
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1109
        };
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1110
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1111
        assertThrows(IllegalMonitorStateException.class, actions);
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1112
    }
a0b0fa5763b1 8159924: Various improvements to StampedLock code
dl
parents: 35394
diff changeset
  1113
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1114
    static long writeLockInterruptiblyUninterrupted(StampedLock sl) {
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1115
        try { return sl.writeLockInterruptibly(); }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1116
        catch (InterruptedException ex) { throw new AssertionError(ex); }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1117
    }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1118
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1119
    static long tryWriteLockUninterrupted(StampedLock sl, long time, TimeUnit unit) {
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1120
        try { return sl.tryWriteLock(time, unit); }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1121
        catch (InterruptedException ex) { throw new AssertionError(ex); }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1122
    }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1123
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1124
    static long readLockInterruptiblyUninterrupted(StampedLock sl) {
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1125
        try { return sl.readLockInterruptibly(); }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1126
        catch (InterruptedException ex) { throw new AssertionError(ex); }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1127
    }
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
    static long tryReadLockUninterrupted(StampedLock sl, long time, TimeUnit unit) {
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1130
        try { return sl.tryReadLock(time, unit); }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1131
        catch (InterruptedException ex) { throw new AssertionError(ex); }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1132
    }
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
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1135
     * Invalid stamps result in IllegalMonitorStateException
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1136
     */
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1137
    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
  1138
        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
  1139
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1140
        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
  1141
                     () -> 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
  1142
                     () -> 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
  1143
                     () -> sl.unlock(0L));
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1144
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1145
        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
  1146
        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
  1147
        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
  1148
        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
  1149
        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
  1150
        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
  1151
        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
  1152
        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
  1153
            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
  1154
                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
  1155
                             () -> 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
  1156
                             () -> 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
  1157
                             () -> 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
  1158
        };
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1159
        assertNoLongerValidStampsThrow.run();
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1160
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1161
        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
  1162
        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
  1163
            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
  1164
            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
  1165
            assertNoLongerValidStampsThrow.run();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1166
            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
  1167
                         () -> 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
  1168
                         () -> 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
  1169
                         () -> 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
  1170
            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
  1171
            assertUnlocked(sl);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1172
            assertNoLongerValidStampsThrow.run();
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1173
        }
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1174
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1175
        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
  1176
        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) {
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1177
            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
  1178
            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
  1179
            assertNoLongerValidStampsThrow.run();
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1180
            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
  1181
                         () -> 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
  1182
                         () -> sl.unlockWrite(0L));
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1183
            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
  1184
            assertUnlocked(sl);
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1185
            assertNoLongerValidStampsThrow.run();
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1186
        }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1187
    }
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1188
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1189
    /**
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1190
     * Read locks can be very deeply nested
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1191
     */
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1192
    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
  1193
        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
  1194
        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
  1195
        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
  1196
        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
  1197
        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
  1198
        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
  1199
            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
  1200
                = 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
  1201
            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
  1202
            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
  1203
            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
  1204
            stamps[i] = stamp;
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1205
        }
40278
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1206
        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
  1207
            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
  1208
                = 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
  1209
            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
  1210
            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
  1211
            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
  1212
        }
8801563939d0 8163210: java/util/concurrent/tck/JSR166TestCase.java testWriteAfterReadLock(StampedLockTest): timed out waiting for thread to terminate
dl
parents: 39780
diff changeset
  1213
        assertUnlocked(lock);
39780
18618975fbb6 8161608: StampedLock should use storeStoreFence when acquiring write lock
dl
parents: 39722
diff changeset
  1214
    }
47307
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1215
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1216
    /**
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1217
     * Stamped locks are not reentrant.
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1218
     */
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1219
    public void testNonReentrant() throws InterruptedException {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1220
        final StampedLock lock = new StampedLock();
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1221
        long stamp;
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1222
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1223
        stamp = lock.writeLock();
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1224
        assertValid(lock, stamp);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1225
        assertEquals(0L, lock.tryWriteLock(0L, DAYS));
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1226
        assertEquals(0L, lock.tryReadLock(0L, DAYS));
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1227
        assertValid(lock, stamp);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1228
        lock.unlockWrite(stamp);
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
        stamp = lock.tryWriteLock(1L, DAYS);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1231
        assertEquals(0L, lock.tryWriteLock(0L, DAYS));
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1232
        assertValid(lock, stamp);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1233
        lock.unlockWrite(stamp);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1234
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1235
        stamp = lock.readLock();
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
        assertValid(lock, stamp);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1238
        lock.unlockRead(stamp);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1239
    }
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
    /**
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1242
     * """StampedLocks have no notion of ownership. Locks acquired in
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1243
     * one thread can be released or converted in another."""
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1244
     */
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1245
    public void testNoOwnership() throws Throwable {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1246
        ArrayList<Future<?>> futures = new ArrayList<>();
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1247
        for (Function<StampedLock, Long> writeLocker : writeLockers())
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1248
        for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1249
            StampedLock lock = new StampedLock();
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1250
            long stamp = writeLocker.apply(lock);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1251
            futures.add(cachedThreadPool.submit(new CheckedRunnable() {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1252
                public void realRun() {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1253
                    writeUnlocker.accept(lock, stamp);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1254
                    assertUnlocked(lock);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1255
                    assertFalse(lock.validate(stamp));
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1256
                }}));
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1257
        }
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1258
        for (Future<?> future : futures)
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1259
            assertNull(future.get());
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1260
    }
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1261
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1262
    /** Tries out sample usage code from StampedLock javadoc. */
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1263
    public void testSampleUsage() throws Throwable {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1264
        class Point {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1265
            private double x, y;
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1266
            private final StampedLock sl = new StampedLock();
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
            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
  1269
                long stamp = sl.writeLock();
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1270
                try {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1271
                    x += deltaX;
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1272
                    y += deltaY;
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1273
                } finally {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1274
                    sl.unlockWrite(stamp);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1275
                }
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1276
            }
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1277
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1278
            double distanceFromOrigin() { // A read-only method
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1279
                double currentX, currentY;
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1280
                long stamp = sl.tryOptimisticRead();
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1281
                do {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1282
                    if (stamp == 0L)
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1283
                        stamp = sl.readLock();
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1284
                    try {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1285
                        // possibly racy reads
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1286
                        currentX = x;
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1287
                        currentY = y;
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1288
                    } finally {
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1289
                        stamp = sl.tryConvertToOptimisticRead(stamp);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1290
                    }
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1291
                } while (stamp == 0);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1292
                return Math.hypot(currentX, currentY);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1293
            }
47343
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1294
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1295
            double distanceFromOrigin2() {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1296
                long stamp = sl.tryOptimisticRead();
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1297
                try {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1298
                    retryHoldingLock:
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1299
                    for (;; stamp = sl.readLock()) {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1300
                        if (stamp == 0L)
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1301
                            continue retryHoldingLock;
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1302
                        // possibly racy reads
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1303
                        double currentX = x;
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1304
                        double currentY = y;
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1305
                        if (!sl.validate(stamp))
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1306
                            continue retryHoldingLock;
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1307
                        return Math.hypot(currentX, currentY);
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1308
                    }
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1309
                } finally {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1310
                    if (StampedLock.isReadLockStamp(stamp))
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1311
                        sl.unlockRead(stamp);
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1312
                }
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1313
            }
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1314
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1315
            void moveIfAtOrigin(double newX, double newY) {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1316
                long stamp = sl.readLock();
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1317
                try {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1318
                    while (x == 0.0 && y == 0.0) {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1319
                        long ws = sl.tryConvertToWriteLock(stamp);
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1320
                        if (ws != 0L) {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1321
                            stamp = ws;
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1322
                            x = newX;
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1323
                            y = newY;
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1324
                            return;
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
                        else {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1327
                            sl.unlockRead(stamp);
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1328
                            stamp = sl.writeLock();
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1329
                        }
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1330
                    }
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1331
                } finally {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1332
                    sl.unlock(stamp);
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1333
                }
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1334
            }
47307
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1335
        }
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1336
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1337
        Point p = new Point();
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1338
        p.move(3.0, 4.0);
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1339
        assertEquals(5.0, p.distanceFromOrigin());
47343
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1340
        p.moveIfAtOrigin(5.0, 12.0);
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1341
        assertEquals(5.0, p.distanceFromOrigin2());
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1342
    }
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1343
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
     * Stamp inspection methods work as expected, and do not inspect
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1346
     * the state of the lock itself.
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1347
     */
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1348
    public void testStampStateInspectionMethods() {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1349
        StampedLock lock = new StampedLock();
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1350
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1351
        assertFalse(isWriteLockStamp(0L));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1352
        assertFalse(isReadLockStamp(0L));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1353
        assertFalse(isLockStamp(0L));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1354
        assertFalse(isOptimisticReadStamp(0L));
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
        {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1357
            long stamp = lock.writeLock();
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1358
            for (int i = 0; i < 2; i++) {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1359
                assertTrue(isWriteLockStamp(stamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1360
                assertFalse(isReadLockStamp(stamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1361
                assertTrue(isLockStamp(stamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1362
                assertFalse(isOptimisticReadStamp(stamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1363
                if (i == 0)
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1364
                    lock.unlockWrite(stamp);
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1365
            }
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
        {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1369
            long stamp = lock.readLock();
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1370
            for (int i = 0; i < 2; i++) {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1371
                assertFalse(isWriteLockStamp(stamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1372
                assertTrue(isReadLockStamp(stamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1373
                assertTrue(isLockStamp(stamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1374
                assertFalse(isOptimisticReadStamp(stamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1375
                if (i == 0)
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1376
                    lock.unlockRead(stamp);
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
        {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1381
            long optimisticStamp = lock.tryOptimisticRead();
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1382
            long readStamp = lock.tryConvertToReadLock(optimisticStamp);
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1383
            long writeStamp = lock.tryConvertToWriteLock(readStamp);
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1384
            for (int i = 0; i < 2; i++) {
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1385
                assertFalse(isWriteLockStamp(optimisticStamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1386
                assertFalse(isReadLockStamp(optimisticStamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1387
                assertFalse(isLockStamp(optimisticStamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1388
                assertTrue(isOptimisticReadStamp(optimisticStamp));
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
                assertFalse(isWriteLockStamp(readStamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1391
                assertTrue(isReadLockStamp(readStamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1392
                assertTrue(isLockStamp(readStamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1393
                assertFalse(isOptimisticReadStamp(readStamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1394
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1395
                assertTrue(isWriteLockStamp(writeStamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1396
                assertFalse(isReadLockStamp(writeStamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1397
                assertTrue(isLockStamp(writeStamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1398
                assertFalse(isOptimisticReadStamp(writeStamp));
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1399
                if (i == 0)
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1400
                    lock.unlockWrite(writeStamp);
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1401
            }
75ee0b48ea63 8187941: Add StampedLock stamp inspection methods
dl
parents: 47307
diff changeset
  1402
        }
47307
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1403
    }
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
  1404
35394
282c3cb6a0c1 8146467: Integrate JSR 166 jck tests into JDK repo
dl
parents:
diff changeset
  1405
}