1024N/A/*
1024N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1024N/A *
1024N/A * This code is free software; you can redistribute it and/or modify it
1024N/A * under the terms of the GNU General Public License version 2 only, as
1024N/A * published by the Free Software Foundation.
1024N/A *
1024N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1024N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1024N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1024N/A * version 2 for more details (a copy is included in the LICENSE file that
1024N/A * accompanied this code).
1024N/A *
1024N/A * You should have received a copy of the GNU General Public License version
1024N/A * 2 along with this work; if not, write to the Free Software Foundation,
1024N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1024N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1024N/A */
1024N/A
1024N/A/*
1024N/A * This file is available under and governed by the GNU General Public
1024N/A * License version 2 only, as published by the Free Software Foundation.
1024N/A * However, the following notice accompanied the original version of this
1024N/A * file:
1024N/A *
1024N/A * Written by Doug Lea with assistance from members of JCP JSR-166
1024N/A * Expert Group and released to the public domain, as explained at
3984N/A * http://creativecommons.org/publicdomain/zero/1.0/
1024N/A */
1024N/A
1024N/A/*
1024N/A * @test
1024N/A * @bug 6801020 6803402
1024N/A * @summary Try to tickle race conditions in
1024N/A * AbstractQueuedSynchronizer "shared" code
1024N/A */
1024N/A
1024N/Aimport java.util.concurrent.Semaphore;
1024N/A
1024N/Apublic class RacingReleases {
1024N/A
1024N/A /** Increase this for better chance of tickling races */
1024N/A static final int iterations = 1000;
1024N/A
1024N/A public static void test(final boolean fair,
1024N/A final boolean interruptibly)
1024N/A throws Throwable {
1024N/A for (int i = 0; i < iterations; i++) {
1024N/A final Semaphore sem = new Semaphore(0, fair);
1024N/A final Throwable[] badness = new Throwable[1];
1024N/A Runnable blocker = interruptibly ?
1024N/A new Runnable() {
1024N/A public void run() {
1024N/A try {
1024N/A sem.acquire();
1024N/A } catch (Throwable t) {
1024N/A badness[0] = t;
1024N/A throw new Error(t);
1024N/A }}}
1024N/A :
1024N/A new Runnable() {
1024N/A public void run() {
1024N/A try {
1024N/A sem.acquireUninterruptibly();
1024N/A } catch (Throwable t) {
1024N/A badness[0] = t;
1024N/A throw new Error(t);
1024N/A }}};
1024N/A
1024N/A Thread b1 = new Thread(blocker);
1024N/A Thread b2 = new Thread(blocker);
1024N/A Runnable signaller = new Runnable() {
1024N/A public void run() {
1024N/A try {
1024N/A sem.release();
1024N/A } catch (Throwable t) {
1024N/A badness[0] = t;
1024N/A throw new Error(t);
1024N/A }}};
1024N/A Thread s1 = new Thread(signaller);
1024N/A Thread s2 = new Thread(signaller);
1024N/A Thread[] threads = { b1, b2, s1, s2 };
1024N/A java.util.Collections.shuffle(java.util.Arrays.asList(threads));
1024N/A for (Thread thread : threads)
1024N/A thread.start();
1024N/A for (Thread thread : threads) {
1024N/A thread.join(60 * 1000);
1024N/A if (thread.isAlive())
1024N/A throw new Error
1024N/A (String.format
1024N/A ("Semaphore stuck: permits %d, thread waiting %s%n",
1024N/A sem.availablePermits(),
1024N/A sem.hasQueuedThreads() ? "true" : "false"));
1024N/A }
1024N/A if (badness[0] != null)
1024N/A throw new Error(badness[0]);
1024N/A if (sem.availablePermits() != 0)
1024N/A throw new Error(String.valueOf(sem.availablePermits()));
1024N/A if (sem.hasQueuedThreads())
1024N/A throw new Error(String.valueOf(sem.hasQueuedThreads()));
1024N/A if (sem.getQueueLength() != 0)
1024N/A throw new Error(String.valueOf(sem.getQueueLength()));
1024N/A if (sem.isFair() != fair)
1024N/A throw new Error(String.valueOf(sem.isFair()));
1024N/A }
1024N/A }
1024N/A
1024N/A public static void main(String[] args) throws Throwable {
1024N/A for (boolean fair : new boolean[] { true, false })
1024N/A for (boolean interruptibly : new boolean[] { true, false })
1024N/A test(fair, interruptibly);
1024N/A }
1024N/A}