< prev index next >

test/compiler/unsafe/JdkInternalMiscUnsafeAccessTestDouble.java

Print this page
rev 12711 : 8181292: Backport Rename internal Unsafe.compare methods from 10 to 9
Reviewed-by: psandoz, dholmes, thartmann
Contributed-by: ron.pressler@oracle.com, claes.redestad@oracle.com
   1 /*
   2  * Copyright (c) 2015, 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  */


 163 
 164         // Lazy
 165         {
 166             UNSAFE.putDoubleRelease(base, offset, 1.0d);
 167             double x = UNSAFE.getDoubleAcquire(base, offset);
 168             assertEquals(x, 1.0d, "putRelease double value");
 169         }
 170 
 171         // Opaque
 172         {
 173             UNSAFE.putDoubleOpaque(base, offset, 2.0d);
 174             double x = UNSAFE.getDoubleOpaque(base, offset);
 175             assertEquals(x, 2.0d, "putOpaque double value");
 176         }
 177 
 178 
 179         UNSAFE.putDouble(base, offset, 1.0d);
 180 
 181         // Compare
 182         {
 183             boolean r = UNSAFE.compareAndSwapDouble(base, offset, 1.0d, 2.0d);
 184             assertEquals(r, true, "success compareAndSwap double");
 185             double x = UNSAFE.getDouble(base, offset);
 186             assertEquals(x, 2.0d, "success compareAndSwap double value");
 187         }
 188 
 189         {
 190             boolean r = UNSAFE.compareAndSwapDouble(base, offset, 1.0d, 3.0d);
 191             assertEquals(r, false, "failing compareAndSwap double");
 192             double x = UNSAFE.getDouble(base, offset);
 193             assertEquals(x, 2.0d, "failing compareAndSwap double value");
 194         }
 195 
 196         // Advanced compare
 197         {
 198             double r = UNSAFE.compareAndExchangeDoubleVolatile(base, offset, 2.0d, 1.0d);
 199             assertEquals(r, 2.0d, "success compareAndExchangeVolatile double");
 200             double x = UNSAFE.getDouble(base, offset);
 201             assertEquals(x, 1.0d, "success compareAndExchangeVolatile double value");
 202         }
 203 
 204         {
 205             double r = UNSAFE.compareAndExchangeDoubleVolatile(base, offset, 2.0d, 3.0d);
 206             assertEquals(r, 1.0d, "failing compareAndExchangeVolatile double");
 207             double x = UNSAFE.getDouble(base, offset);
 208             assertEquals(x, 1.0d, "failing compareAndExchangeVolatile double value");
 209         }
 210 
 211         {
 212             double r = UNSAFE.compareAndExchangeDoubleAcquire(base, offset, 1.0d, 2.0d);
 213             assertEquals(r, 1.0d, "success compareAndExchangeAcquire double");
 214             double x = UNSAFE.getDouble(base, offset);
 215             assertEquals(x, 2.0d, "success compareAndExchangeAcquire double value");
 216         }
 217 
 218         {
 219             double r = UNSAFE.compareAndExchangeDoubleAcquire(base, offset, 1.0d, 3.0d);
 220             assertEquals(r, 2.0d, "failing compareAndExchangeAcquire double");
 221             double x = UNSAFE.getDouble(base, offset);
 222             assertEquals(x, 2.0d, "failing compareAndExchangeAcquire double value");
 223         }
 224 
 225         {
 226             double r = UNSAFE.compareAndExchangeDoubleRelease(base, offset, 2.0d, 1.0d);
 227             assertEquals(r, 2.0d, "success compareAndExchangeRelease double");
 228             double x = UNSAFE.getDouble(base, offset);
 229             assertEquals(x, 1.0d, "success compareAndExchangeRelease double value");
 230         }
 231 
 232         {
 233             double r = UNSAFE.compareAndExchangeDoubleRelease(base, offset, 2.0d, 3.0d);
 234             assertEquals(r, 1.0d, "failing compareAndExchangeRelease double");
 235             double x = UNSAFE.getDouble(base, offset);
 236             assertEquals(x, 1.0d, "failing compareAndExchangeRelease double value");
 237         }
 238 
 239         {
 240             boolean success = false;
 241             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 242                 success = UNSAFE.weakCompareAndSwapDouble(base, offset, 1.0d, 2.0d);
 243             }
 244             assertEquals(success, true, "weakCompareAndSwap double");
 245             double x = UNSAFE.getDouble(base, offset);
 246             assertEquals(x, 2.0d, "weakCompareAndSwap double value");
 247         }
 248 
 249         {
 250             boolean success = false;
 251             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 252                 success = UNSAFE.weakCompareAndSwapDoubleAcquire(base, offset, 2.0d, 1.0d);
 253             }
 254             assertEquals(success, true, "weakCompareAndSwapAcquire double");
 255             double x = UNSAFE.getDouble(base, offset);
 256             assertEquals(x, 1.0d, "weakCompareAndSwapAcquire double");
 257         }
 258 
 259         {
 260             boolean success = false;
 261             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 262                 success = UNSAFE.weakCompareAndSwapDoubleRelease(base, offset, 1.0d, 2.0d);
 263             }
 264             assertEquals(success, true, "weakCompareAndSwapRelease double");
 265             double x = UNSAFE.getDouble(base, offset);
 266             assertEquals(x, 2.0d, "weakCompareAndSwapRelease double");
 267         }
 268 
 269         {
 270             boolean success = false;
 271             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 272                 success = UNSAFE.weakCompareAndSwapDoubleVolatile(base, offset, 2.0d, 1.0d);
 273             }
 274             assertEquals(success, true, "weakCompareAndSwapVolatile double");
 275             double x = UNSAFE.getDouble(base, offset);
 276             assertEquals(x, 1.0d, "weakCompareAndSwapVolatile double");
 277         }
 278 
 279         UNSAFE.putDouble(base, offset, 2.0d);
 280 
 281         // Compare set and get
 282         {
 283             double o = UNSAFE.getAndSetDouble(base, offset, 1.0d);
 284             assertEquals(o, 2.0d, "getAndSet double");
 285             double x = UNSAFE.getDouble(base, offset);
 286             assertEquals(x, 1.0d, "getAndSet double value");
 287         }
 288 
 289         UNSAFE.putDouble(base, offset, 1.0d);
 290 
 291         // get and add, add and get
 292         {
 293             double o = UNSAFE.getAndAddDouble(base, offset, 2.0d);
 294             assertEquals(o, 1.0d, "getAndAdd double");
 295             double x = UNSAFE.getDouble(base, offset);
 296             assertEquals(x, (double)(1.0d + 2.0d), "getAndAdd double");
 297         }
 298     }
 299 
 300     static void testAccess(long address) {
 301         // Plain
 302         {
 303             UNSAFE.putDouble(address, 1.0d);
 304             double x = UNSAFE.getDouble(address);
 305             assertEquals(x, 1.0d, "set double value");
 306         }
 307     }
 308 }
 309 
   1 /*
   2  * Copyright (c) 2015, 2017, 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  */


 163 
 164         // Lazy
 165         {
 166             UNSAFE.putDoubleRelease(base, offset, 1.0d);
 167             double x = UNSAFE.getDoubleAcquire(base, offset);
 168             assertEquals(x, 1.0d, "putRelease double value");
 169         }
 170 
 171         // Opaque
 172         {
 173             UNSAFE.putDoubleOpaque(base, offset, 2.0d);
 174             double x = UNSAFE.getDoubleOpaque(base, offset);
 175             assertEquals(x, 2.0d, "putOpaque double value");
 176         }
 177 
 178 
 179         UNSAFE.putDouble(base, offset, 1.0d);
 180 
 181         // Compare
 182         {
 183             boolean r = UNSAFE.compareAndSetDouble(base, offset, 1.0d, 2.0d);
 184             assertEquals(r, true, "success compareAndSet double");
 185             double x = UNSAFE.getDouble(base, offset);
 186             assertEquals(x, 2.0d, "success compareAndSet double value");
 187         }
 188 
 189         {
 190             boolean r = UNSAFE.compareAndSetDouble(base, offset, 1.0d, 3.0d);
 191             assertEquals(r, false, "failing compareAndSet double");
 192             double x = UNSAFE.getDouble(base, offset);
 193             assertEquals(x, 2.0d, "failing compareAndSet double value");
 194         }
 195 
 196         // Advanced compare
 197         {
 198             double r = UNSAFE.compareAndExchangeDouble(base, offset, 2.0d, 1.0d);
 199             assertEquals(r, 2.0d, "success compareAndExchange double");
 200             double x = UNSAFE.getDouble(base, offset);
 201             assertEquals(x, 1.0d, "success compareAndExchange double value");
 202         }
 203 
 204         {
 205             double r = UNSAFE.compareAndExchangeDouble(base, offset, 2.0d, 3.0d);
 206             assertEquals(r, 1.0d, "failing compareAndExchange double");
 207             double x = UNSAFE.getDouble(base, offset);
 208             assertEquals(x, 1.0d, "failing compareAndExchange double value");
 209         }
 210 
 211         {
 212             double r = UNSAFE.compareAndExchangeDoubleAcquire(base, offset, 1.0d, 2.0d);
 213             assertEquals(r, 1.0d, "success compareAndExchangeAcquire double");
 214             double x = UNSAFE.getDouble(base, offset);
 215             assertEquals(x, 2.0d, "success compareAndExchangeAcquire double value");
 216         }
 217 
 218         {
 219             double r = UNSAFE.compareAndExchangeDoubleAcquire(base, offset, 1.0d, 3.0d);
 220             assertEquals(r, 2.0d, "failing compareAndExchangeAcquire double");
 221             double x = UNSAFE.getDouble(base, offset);
 222             assertEquals(x, 2.0d, "failing compareAndExchangeAcquire double value");
 223         }
 224 
 225         {
 226             double r = UNSAFE.compareAndExchangeDoubleRelease(base, offset, 2.0d, 1.0d);
 227             assertEquals(r, 2.0d, "success compareAndExchangeRelease double");
 228             double x = UNSAFE.getDouble(base, offset);
 229             assertEquals(x, 1.0d, "success compareAndExchangeRelease double value");
 230         }
 231 
 232         {
 233             double r = UNSAFE.compareAndExchangeDoubleRelease(base, offset, 2.0d, 3.0d);
 234             assertEquals(r, 1.0d, "failing compareAndExchangeRelease double");
 235             double x = UNSAFE.getDouble(base, offset);
 236             assertEquals(x, 1.0d, "failing compareAndExchangeRelease double value");
 237         }
 238 
 239         {
 240             boolean success = false;
 241             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 242                 success = UNSAFE.weakCompareAndSetDoublePlain(base, offset, 1.0d, 2.0d);
 243             }
 244             assertEquals(success, true, "weakCompareAndSetPlain double");
 245             double x = UNSAFE.getDouble(base, offset);
 246             assertEquals(x, 2.0d, "weakCompareAndSetPlain double value");
 247         }
 248 
 249         {
 250             boolean success = false;
 251             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 252                 success = UNSAFE.weakCompareAndSetDoubleAcquire(base, offset, 2.0d, 1.0d);
 253             }
 254             assertEquals(success, true, "weakCompareAndSetAcquire double");
 255             double x = UNSAFE.getDouble(base, offset);
 256             assertEquals(x, 1.0d, "weakCompareAndSetAcquire double");
 257         }
 258 
 259         {
 260             boolean success = false;
 261             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 262                 success = UNSAFE.weakCompareAndSetDoubleRelease(base, offset, 1.0d, 2.0d);
 263             }
 264             assertEquals(success, true, "weakCompareAndSetRelease double");
 265             double x = UNSAFE.getDouble(base, offset);
 266             assertEquals(x, 2.0d, "weakCompareAndSetRelease double");
 267         }
 268 
 269         {
 270             boolean success = false;
 271             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 272                 success = UNSAFE.weakCompareAndSetDouble(base, offset, 2.0d, 1.0d);
 273             }
 274             assertEquals(success, true, "weakCompareAndSet double");
 275             double x = UNSAFE.getDouble(base, offset);
 276             assertEquals(x, 1.0d, "weakCompareAndSet double");
 277         }
 278 
 279         UNSAFE.putDouble(base, offset, 2.0d);
 280 
 281         // Compare set and get
 282         {
 283             double o = UNSAFE.getAndSetDouble(base, offset, 1.0d);
 284             assertEquals(o, 2.0d, "getAndSet double");
 285             double x = UNSAFE.getDouble(base, offset);
 286             assertEquals(x, 1.0d, "getAndSet double value");
 287         }
 288 
 289         UNSAFE.putDouble(base, offset, 1.0d);
 290 
 291         // get and add, add and get
 292         {
 293             double o = UNSAFE.getAndAddDouble(base, offset, 2.0d);
 294             assertEquals(o, 1.0d, "getAndAdd double");
 295             double x = UNSAFE.getDouble(base, offset);
 296             assertEquals(x, (double)(1.0d + 2.0d), "getAndAdd double");
 297         }
 298     }
 299 
 300     static void testAccess(long address) {
 301         // Plain
 302         {
 303             UNSAFE.putDouble(address, 1.0d);
 304             double x = UNSAFE.getDouble(address);
 305             assertEquals(x, 1.0d, "set double value");
 306         }
 307     }
 308 }

< prev index next >