hotspot/test/compiler/7009231/Test7009231.java
changeset 7713 1e06d2419258
equal deleted inserted replaced
7712:cee30a0f4315 7713:1e06d2419258
       
     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 Test7009231
       
    31  *
       
    32  */
       
    33 
       
    34 import java.util.Random;
       
    35 import java.util.concurrent.atomic.AtomicLong;
       
    36 
       
    37 
       
    38 public class Test7009231 {
       
    39     public static void main(String[] args) throws InterruptedException {
       
    40         doTest(8);
       
    41     }
       
    42 
       
    43     private static void doTest(int nThreads) throws InterruptedException {
       
    44         Thread[]         aThreads = new Thread[nThreads];
       
    45         final AtomicLong atl      = new AtomicLong();
       
    46 
       
    47         for (int i = 0; i < nThreads; i++) {
       
    48           aThreads[i] = new RunnerThread(atl, 1L << (8 * i));
       
    49         }
       
    50 
       
    51         for (int i = 0; i < nThreads; i++) {
       
    52           aThreads[i].start();
       
    53         }
       
    54 
       
    55         for (int i = 0; i < nThreads; i++) {
       
    56           aThreads[i].join();
       
    57         }
       
    58     }
       
    59 
       
    60     public static class RunnerThread extends Thread {
       
    61         public RunnerThread(AtomicLong atomic, long lMask) {
       
    62             m_lMask  = lMask;
       
    63             m_atomic = atomic;
       
    64         }
       
    65 
       
    66         public void run() {
       
    67             AtomicLong atomic = m_atomic;
       
    68             long       lMask  = m_lMask;
       
    69             for (int i = 0; i < 100000; i++) {
       
    70                 setBit(atomic, lMask);
       
    71                 clearBit(atomic, lMask);
       
    72             }
       
    73         }
       
    74 
       
    75         protected void setBit(AtomicLong atomic, long lMask) {
       
    76             long lWord;
       
    77             do {
       
    78                 lWord = atomic.get();
       
    79             } while (!atomic.compareAndSet(lWord, lWord | lMask));
       
    80 
       
    81             if ((atomic.get() & lMask) == 0L) {
       
    82                 throw new InternalError();
       
    83             }
       
    84         }
       
    85 
       
    86         protected void clearBit(AtomicLong atomic, long lMask) {
       
    87             long lWord;
       
    88             do {
       
    89                 lWord = atomic.get();
       
    90             } while (!atomic.compareAndSet(lWord, lWord & ~lMask));
       
    91 
       
    92             if ((atomic.get() & lMask) != 0L) {
       
    93                 throw new InternalError();
       
    94             }
       
    95         }
       
    96 
       
    97         private long m_lMask;
       
    98         private AtomicLong m_atomic;
       
    99     }
       
   100 }