Test7009231.java revision 11707:ad7af1afda7a
1/* 2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25/** 26 * @test 27 * @bug 7009231 28 * @summary C1: Incorrect CAS code for longs on SPARC 32bit 29 * 30 * @run main/othervm -Xbatch compiler.codegen.Test7009231 31 */ 32 33package compiler.codegen; 34 35import java.util.concurrent.atomic.AtomicLong; 36 37public class Test7009231 { 38 public static void main(String[] args) throws InterruptedException { 39 doTest(8); 40 } 41 42 private static void doTest(int nThreads) throws InterruptedException { 43 Thread[] aThreads = new Thread[nThreads]; 44 final AtomicLong atl = new AtomicLong(); 45 46 for (int i = 0; i < nThreads; i++) { 47 aThreads[i] = new RunnerThread(atl, 1L << (8 * i)); 48 } 49 50 for (int i = 0; i < nThreads; i++) { 51 aThreads[i].start(); 52 } 53 54 for (int i = 0; i < nThreads; i++) { 55 aThreads[i].join(); 56 } 57 } 58 59 public static class RunnerThread extends Thread { 60 public RunnerThread(AtomicLong atomic, long lMask) { 61 m_lMask = lMask; 62 m_atomic = atomic; 63 } 64 65 public void run() { 66 AtomicLong atomic = m_atomic; 67 long lMask = m_lMask; 68 for (int i = 0; i < 100000; i++) { 69 setBit(atomic, lMask); 70 clearBit(atomic, lMask); 71 } 72 } 73 74 protected void setBit(AtomicLong atomic, long lMask) { 75 long lWord; 76 do { 77 lWord = atomic.get(); 78 } while (!atomic.compareAndSet(lWord, lWord | lMask)); 79 80 if ((atomic.get() & lMask) == 0L) { 81 throw new InternalError(); 82 } 83 } 84 85 protected void clearBit(AtomicLong atomic, long lMask) { 86 long lWord; 87 do { 88 lWord = atomic.get(); 89 } while (!atomic.compareAndSet(lWord, lWord & ~lMask)); 90 91 if ((atomic.get() & lMask) != 0L) { 92 throw new InternalError(); 93 } 94 } 95 96 private long m_lMask; 97 private AtomicLong m_atomic; 98 } 99} 100