1/* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 */ 22 23/* 24 * This file is available under and governed by the GNU General Public 25 * License version 2 only, as published by the Free Software Foundation. 26 * However, the following notice accompanied the original version of this 27 * file: 28 * 29 * Written by Doug Lea with assistance from members of JCP JSR-166 30 * Expert Group and released to the public domain, as explained at 31 * http://creativecommons.org/publicdomain/zero/1.0/ 32 */ 33 34/* 35 * @test 36 * @bug 6801020 6803402 37 * @summary Try to tickle race conditions in 38 * AbstractQueuedSynchronizer "shared" code 39 */ 40 41import java.util.concurrent.Semaphore; 42 43public class RacingReleases { 44 45 /** Increase this for better chance of tickling races */ 46 static final int iterations = 1000; 47 48 public static void test(final boolean fair, 49 final boolean interruptibly) 50 throws Throwable { 51 for (int i = 0; i < iterations; i++) { 52 final Semaphore sem = new Semaphore(0, fair); 53 final Throwable[] badness = new Throwable[1]; 54 Runnable blocker = interruptibly ? 55 new Runnable() { 56 public void run() { 57 try { 58 sem.acquire(); 59 } catch (Throwable t) { 60 badness[0] = t; 61 throw new Error(t); 62 }}} 63 : 64 new Runnable() { 65 public void run() { 66 try { 67 sem.acquireUninterruptibly(); 68 } catch (Throwable t) { 69 badness[0] = t; 70 throw new Error(t); 71 }}}; 72 73 Thread b1 = new Thread(blocker); 74 Thread b2 = new Thread(blocker); 75 Runnable signaller = new Runnable() { 76 public void run() { 77 try { 78 sem.release(); 79 } catch (Throwable t) { 80 badness[0] = t; 81 throw new Error(t); 82 }}}; 83 Thread s1 = new Thread(signaller); 84 Thread s2 = new Thread(signaller); 85 Thread[] threads = { b1, b2, s1, s2 }; 86 java.util.Collections.shuffle(java.util.Arrays.asList(threads)); 87 for (Thread thread : threads) 88 thread.start(); 89 for (Thread thread : threads) { 90 thread.join(60 * 1000); 91 if (thread.isAlive()) 92 throw new Error 93 (String.format 94 ("Semaphore stuck: permits %d, thread waiting %s%n", 95 sem.availablePermits(), 96 sem.hasQueuedThreads() ? "true" : "false")); 97 } 98 if (badness[0] != null) 99 throw new Error(badness[0]); 100 if (sem.availablePermits() != 0) 101 throw new Error(String.valueOf(sem.availablePermits())); 102 if (sem.hasQueuedThreads()) 103 throw new Error(String.valueOf(sem.hasQueuedThreads())); 104 if (sem.getQueueLength() != 0) 105 throw new Error(String.valueOf(sem.getQueueLength())); 106 if (sem.isFair() != fair) 107 throw new Error(String.valueOf(sem.isFair())); 108 } 109 } 110 111 public static void main(String[] args) throws Throwable { 112 for (boolean fair : new boolean[] { true, false }) 113 for (boolean interruptibly : new boolean[] { true, false }) 114 test(fair, interruptibly); 115 } 116} 117