test/jdk/java/util/concurrent/locks/Lock/FlakyMutex.java
author iignatyev
Fri, 14 Sep 2018 14:02:57 -0700
changeset 51754 594919232b8f
parent 47730 c7b5b1ce8145
child 58134 51cd29502ea9
permissions -rw-r--r--
8210732: remove jdk.testlibrary.Utils Reviewed-by: alanb, jcbeyler
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 18160
diff changeset
     2
 * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6503247 6574123
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test resilience to tryAcquire methods that throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    31
import java.util.concurrent.ThreadLocalRandom;
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 23010
diff changeset
    32
import java.util.concurrent.CyclicBarrier;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 23010
diff changeset
    33
import java.util.concurrent.ExecutorService;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 23010
diff changeset
    34
import java.util.concurrent.Executors;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 23010
diff changeset
    35
import java.util.concurrent.TimeUnit;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 23010
diff changeset
    36
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 23010
diff changeset
    37
import java.util.concurrent.locks.Condition;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 23010
diff changeset
    38
import java.util.concurrent.locks.Lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This uses a variant of the standard Mutex demo, except with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * tryAcquire method that randomly throws various Throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
18160
da854405dc59 8016311: Update j.u.c. tests to avoid using Thread.stop(Throwable)
alanb
parents: 14342
diff changeset
    45
@SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class FlakyMutex implements Lock {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static class MyError extends Error {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static class MyException extends Exception {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static class MyRuntimeException extends RuntimeException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static void checkThrowable(Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        check((t instanceof MyError) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
              (t instanceof MyException) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
              (t instanceof MyRuntimeException));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        final int nThreads = 3;
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    59
        final int iterations = 10_000;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    60
        final CyclicBarrier startingGate = new CyclicBarrier(nThreads);
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    61
        final FlakyMutex mutex = new FlakyMutex();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        final ExecutorService es = Executors.newFixedThreadPool(nThreads);
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    63
        final Runnable task = () -> {
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    64
            try {
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    65
                startingGate.await();
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    66
                for (int i = 0; i < iterations; i++) {
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    67
                    for (;;) {
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    68
                        try { mutex.lock(); break; }
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    69
                        catch (Throwable t) { checkThrowable(t); }
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    70
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    72
                    try { check(! mutex.tryLock()); }
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    73
                    catch (Throwable t) { checkThrowable(t); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    75
                    try { check(! mutex.tryLock(1, TimeUnit.MICROSECONDS)); }
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    76
                    catch (Throwable t) { checkThrowable(t); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    78
                    mutex.unlock();
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    79
                }
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    80
            } catch (Throwable t) { unexpected(t); }
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    81
        };
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    82
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    83
        for (int i = 0; i < nThreads; i++)
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    84
            es.submit(task);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        es.shutdown();
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    86
        // Let test harness handle timeout
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
    87
        check(es.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static class FlakySync extends AbstractQueuedLongSynchronizer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        private static final long serialVersionUID = -1L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        public boolean isHeldExclusively() { return getState() == 1; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        public boolean tryAcquire(long acquires) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            // Sneak in some tests for queue state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            if (hasQueuedPredecessors())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                check(getFirstQueuedThread() != Thread.currentThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            if (getFirstQueuedThread() == Thread.currentThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                check(hasQueuedThreads());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                check(!hasQueuedPredecessors());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                // Might be true, but only transiently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                do {} while (hasQueuedPredecessors() != hasQueuedThreads());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
   107
            switch (ThreadLocalRandom.current().nextInt(10)) {
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
   108
            case 0: throw new MyError();
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
   109
            case 1: throw new MyRuntimeException();
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
   110
            case 2: FlakyMutex.<RuntimeException>uncheckedThrow(new MyException());
47730
c7b5b1ce8145 8189764: Miscellaneous changes imported from jsr166 CVS 2017-11
dl
parents: 47216
diff changeset
   111
                // fall through ... NOT!
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
   112
            default: return compareAndSetState(0, 1);
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 36233
diff changeset
   113
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        public boolean tryRelease(long releases) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            setState(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        Condition newCondition() { return new ConditionObject(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private final FlakySync sync = new FlakySync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public void lock() { sync.acquire(1); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public boolean tryLock() { return sync.tryAcquire(1); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public void lockInterruptibly() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        sync.acquireInterruptibly(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return sync.tryAcquireNanos(1, unit.toNanos(timeout));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public void unlock() { sync.release(1); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public Condition newCondition()   { return sync.newCondition(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public boolean isLocked()         { return sync.isHeldExclusively(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public boolean hasQueuedThreads() { return sync.hasQueuedThreads(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (failed > 0) throw new AssertionError("Some tests failed");}
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 38551
diff changeset
   152
    @SuppressWarnings("unchecked")
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 38551
diff changeset
   153
    static <T extends Throwable> void uncheckedThrow(Throwable t) throws T {
18160
da854405dc59 8016311: Update j.u.c. tests to avoid using Thread.stop(Throwable)
alanb
parents: 14342
diff changeset
   154
        throw (T)t; // rely on vacuous cast
da854405dc59 8016311: Update j.u.c. tests to avoid using Thread.stop(Throwable)
alanb
parents: 14342
diff changeset
   155
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
}