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 * @test 25 * @run main/othervm/timeout=60 ReadersUnlockAfterWriteUnlock 26 * @bug 8023234 27 * @summary StampedLock serializes readers on writer unlock 28 * @author Dmitry Chyuko 29 * @author Aleksey Shipilev 30 */ 31 32 import java.util.concurrent.CyclicBarrier; 33 import java.util.concurrent.locks.StampedLock; 34 35 public class ReadersUnlockAfterWriteUnlock { 36 static final int RNUM = 2; 37 static final StampedLock sl = new StampedLock(); 38 static volatile boolean isDone; 39 40 static CyclicBarrier iterationStart = new CyclicBarrier(RNUM + 1); 41 static CyclicBarrier readersHaveLocks = new CyclicBarrier(RNUM); 42 static CyclicBarrier writerHasLock = new CyclicBarrier(RNUM + 1); 43 44 static class Reader extends Thread { 45 final String name; 46 Reader(String name) { 47 super(); 48 this.name = name; 49 } 50 public void run() { 51 while (!isDone && !isInterrupted()) { 52 try { 53 iterationStart.await(); 54 writerHasLock.await(); 55 long rs = sl.readLock(); 56 57 // single reader blocks here indefinitely if readers 58 // are serialized 59 readersHaveLocks.await(); 60 61 sl.unlockRead(rs); 62 } catch (Exception e) { 63 throw new IllegalStateException(e); 64 } 65 } 66 } 67 } 68 69 public static void main(String[] args) throws InterruptedException { 70 for (int r = 0 ; r < RNUM; ++r) { 71 new Reader("r" + r).start(); 72 } 73 int i; 74 for (i = 0; i < 1024; ++i) { 75 try { 76 iterationStart.await(); 77 long ws = sl.writeLock(); 78 writerHasLock.await(); 79 Thread.sleep(10); 80 sl.unlockWrite(ws); 81 } catch (Exception e) { 82 throw new IllegalStateException(e); 83 } 84 } 85 isDone = true; 86 } 87 88 }