< prev index next >

test/compiler/unsafe/JdkInternalMiscUnsafeAccessTestFloat.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.putFloatRelease(base, offset, 1.0f);
 167             float x = UNSAFE.getFloatAcquire(base, offset);
 168             assertEquals(x, 1.0f, "putRelease float value");
 169         }
 170 
 171         // Opaque
 172         {
 173             UNSAFE.putFloatOpaque(base, offset, 2.0f);
 174             float x = UNSAFE.getFloatOpaque(base, offset);
 175             assertEquals(x, 2.0f, "putOpaque float value");
 176         }
 177 
 178 
 179         UNSAFE.putFloat(base, offset, 1.0f);
 180 
 181         // Compare
 182         {
 183             boolean r = UNSAFE.compareAndSwapFloat(base, offset, 1.0f, 2.0f);
 184             assertEquals(r, true, "success compareAndSwap float");
 185             float x = UNSAFE.getFloat(base, offset);
 186             assertEquals(x, 2.0f, "success compareAndSwap float value");
 187         }
 188 
 189         {
 190             boolean r = UNSAFE.compareAndSwapFloat(base, offset, 1.0f, 3.0f);
 191             assertEquals(r, false, "failing compareAndSwap float");
 192             float x = UNSAFE.getFloat(base, offset);
 193             assertEquals(x, 2.0f, "failing compareAndSwap float value");
 194         }
 195 
 196         // Advanced compare
 197         {
 198             float r = UNSAFE.compareAndExchangeFloatVolatile(base, offset, 2.0f, 1.0f);
 199             assertEquals(r, 2.0f, "success compareAndExchangeVolatile float");
 200             float x = UNSAFE.getFloat(base, offset);
 201             assertEquals(x, 1.0f, "success compareAndExchangeVolatile float value");
 202         }
 203 
 204         {
 205             float r = UNSAFE.compareAndExchangeFloatVolatile(base, offset, 2.0f, 3.0f);
 206             assertEquals(r, 1.0f, "failing compareAndExchangeVolatile float");
 207             float x = UNSAFE.getFloat(base, offset);
 208             assertEquals(x, 1.0f, "failing compareAndExchangeVolatile float value");
 209         }
 210 
 211         {
 212             float r = UNSAFE.compareAndExchangeFloatAcquire(base, offset, 1.0f, 2.0f);
 213             assertEquals(r, 1.0f, "success compareAndExchangeAcquire float");
 214             float x = UNSAFE.getFloat(base, offset);
 215             assertEquals(x, 2.0f, "success compareAndExchangeAcquire float value");
 216         }
 217 
 218         {
 219             float r = UNSAFE.compareAndExchangeFloatAcquire(base, offset, 1.0f, 3.0f);
 220             assertEquals(r, 2.0f, "failing compareAndExchangeAcquire float");
 221             float x = UNSAFE.getFloat(base, offset);
 222             assertEquals(x, 2.0f, "failing compareAndExchangeAcquire float value");
 223         }
 224 
 225         {
 226             float r = UNSAFE.compareAndExchangeFloatRelease(base, offset, 2.0f, 1.0f);
 227             assertEquals(r, 2.0f, "success compareAndExchangeRelease float");
 228             float x = UNSAFE.getFloat(base, offset);
 229             assertEquals(x, 1.0f, "success compareAndExchangeRelease float value");
 230         }
 231 
 232         {
 233             float r = UNSAFE.compareAndExchangeFloatRelease(base, offset, 2.0f, 3.0f);
 234             assertEquals(r, 1.0f, "failing compareAndExchangeRelease float");
 235             float x = UNSAFE.getFloat(base, offset);
 236             assertEquals(x, 1.0f, "failing compareAndExchangeRelease float value");
 237         }
 238 
 239         {
 240             boolean success = false;
 241             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 242                 success = UNSAFE.weakCompareAndSwapFloat(base, offset, 1.0f, 2.0f);
 243             }
 244             assertEquals(success, true, "weakCompareAndSwap float");
 245             float x = UNSAFE.getFloat(base, offset);
 246             assertEquals(x, 2.0f, "weakCompareAndSwap float value");
 247         }
 248 
 249         {
 250             boolean success = false;
 251             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 252                 success = UNSAFE.weakCompareAndSwapFloatAcquire(base, offset, 2.0f, 1.0f);
 253             }
 254             assertEquals(success, true, "weakCompareAndSwapAcquire float");
 255             float x = UNSAFE.getFloat(base, offset);
 256             assertEquals(x, 1.0f, "weakCompareAndSwapAcquire float");
 257         }
 258 
 259         {
 260             boolean success = false;
 261             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 262                 success = UNSAFE.weakCompareAndSwapFloatRelease(base, offset, 1.0f, 2.0f);
 263             }
 264             assertEquals(success, true, "weakCompareAndSwapRelease float");
 265             float x = UNSAFE.getFloat(base, offset);
 266             assertEquals(x, 2.0f, "weakCompareAndSwapRelease float");
 267         }
 268 
 269         {
 270             boolean success = false;
 271             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 272                 success = UNSAFE.weakCompareAndSwapFloatVolatile(base, offset, 2.0f, 1.0f);
 273             }
 274             assertEquals(success, true, "weakCompareAndSwapVolatile float");
 275             float x = UNSAFE.getFloat(base, offset);
 276             assertEquals(x, 1.0f, "weakCompareAndSwapVolatile float");
 277         }
 278 
 279         UNSAFE.putFloat(base, offset, 2.0f);
 280 
 281         // Compare set and get
 282         {
 283             float o = UNSAFE.getAndSetFloat(base, offset, 1.0f);
 284             assertEquals(o, 2.0f, "getAndSet float");
 285             float x = UNSAFE.getFloat(base, offset);
 286             assertEquals(x, 1.0f, "getAndSet float value");
 287         }
 288 
 289         UNSAFE.putFloat(base, offset, 1.0f);
 290 
 291         // get and add, add and get
 292         {
 293             float o = UNSAFE.getAndAddFloat(base, offset, 2.0f);
 294             assertEquals(o, 1.0f, "getAndAdd float");
 295             float x = UNSAFE.getFloat(base, offset);
 296             assertEquals(x, (float)(1.0f + 2.0f), "getAndAdd float");
 297         }
 298     }
 299 
 300     static void testAccess(long address) {
 301         // Plain
 302         {
 303             UNSAFE.putFloat(address, 1.0f);
 304             float x = UNSAFE.getFloat(address);
 305             assertEquals(x, 1.0f, "set float 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.putFloatRelease(base, offset, 1.0f);
 167             float x = UNSAFE.getFloatAcquire(base, offset);
 168             assertEquals(x, 1.0f, "putRelease float value");
 169         }
 170 
 171         // Opaque
 172         {
 173             UNSAFE.putFloatOpaque(base, offset, 2.0f);
 174             float x = UNSAFE.getFloatOpaque(base, offset);
 175             assertEquals(x, 2.0f, "putOpaque float value");
 176         }
 177 
 178 
 179         UNSAFE.putFloat(base, offset, 1.0f);
 180 
 181         // Compare
 182         {
 183             boolean r = UNSAFE.compareAndSetFloat(base, offset, 1.0f, 2.0f);
 184             assertEquals(r, true, "success compareAndSet float");
 185             float x = UNSAFE.getFloat(base, offset);
 186             assertEquals(x, 2.0f, "success compareAndSet float value");
 187         }
 188 
 189         {
 190             boolean r = UNSAFE.compareAndSetFloat(base, offset, 1.0f, 3.0f);
 191             assertEquals(r, false, "failing compareAndSet float");
 192             float x = UNSAFE.getFloat(base, offset);
 193             assertEquals(x, 2.0f, "failing compareAndSet float value");
 194         }
 195 
 196         // Advanced compare
 197         {
 198             float r = UNSAFE.compareAndExchangeFloat(base, offset, 2.0f, 1.0f);
 199             assertEquals(r, 2.0f, "success compareAndExchange float");
 200             float x = UNSAFE.getFloat(base, offset);
 201             assertEquals(x, 1.0f, "success compareAndExchange float value");
 202         }
 203 
 204         {
 205             float r = UNSAFE.compareAndExchangeFloat(base, offset, 2.0f, 3.0f);
 206             assertEquals(r, 1.0f, "failing compareAndExchange float");
 207             float x = UNSAFE.getFloat(base, offset);
 208             assertEquals(x, 1.0f, "failing compareAndExchange float value");
 209         }
 210 
 211         {
 212             float r = UNSAFE.compareAndExchangeFloatAcquire(base, offset, 1.0f, 2.0f);
 213             assertEquals(r, 1.0f, "success compareAndExchangeAcquire float");
 214             float x = UNSAFE.getFloat(base, offset);
 215             assertEquals(x, 2.0f, "success compareAndExchangeAcquire float value");
 216         }
 217 
 218         {
 219             float r = UNSAFE.compareAndExchangeFloatAcquire(base, offset, 1.0f, 3.0f);
 220             assertEquals(r, 2.0f, "failing compareAndExchangeAcquire float");
 221             float x = UNSAFE.getFloat(base, offset);
 222             assertEquals(x, 2.0f, "failing compareAndExchangeAcquire float value");
 223         }
 224 
 225         {
 226             float r = UNSAFE.compareAndExchangeFloatRelease(base, offset, 2.0f, 1.0f);
 227             assertEquals(r, 2.0f, "success compareAndExchangeRelease float");
 228             float x = UNSAFE.getFloat(base, offset);
 229             assertEquals(x, 1.0f, "success compareAndExchangeRelease float value");
 230         }
 231 
 232         {
 233             float r = UNSAFE.compareAndExchangeFloatRelease(base, offset, 2.0f, 3.0f);
 234             assertEquals(r, 1.0f, "failing compareAndExchangeRelease float");
 235             float x = UNSAFE.getFloat(base, offset);
 236             assertEquals(x, 1.0f, "failing compareAndExchangeRelease float value");
 237         }
 238 
 239         {
 240             boolean success = false;
 241             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 242                 success = UNSAFE.weakCompareAndSetFloatPlain(base, offset, 1.0f, 2.0f);
 243             }
 244             assertEquals(success, true, "weakCompareAndSetPlain float");
 245             float x = UNSAFE.getFloat(base, offset);
 246             assertEquals(x, 2.0f, "weakCompareAndSetPlain float value");
 247         }
 248 
 249         {
 250             boolean success = false;
 251             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 252                 success = UNSAFE.weakCompareAndSetFloatAcquire(base, offset, 2.0f, 1.0f);
 253             }
 254             assertEquals(success, true, "weakCompareAndSetAcquire float");
 255             float x = UNSAFE.getFloat(base, offset);
 256             assertEquals(x, 1.0f, "weakCompareAndSetAcquire float");
 257         }
 258 
 259         {
 260             boolean success = false;
 261             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 262                 success = UNSAFE.weakCompareAndSetFloatRelease(base, offset, 1.0f, 2.0f);
 263             }
 264             assertEquals(success, true, "weakCompareAndSetRelease float");
 265             float x = UNSAFE.getFloat(base, offset);
 266             assertEquals(x, 2.0f, "weakCompareAndSetRelease float");
 267         }
 268 
 269         {
 270             boolean success = false;
 271             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 272                 success = UNSAFE.weakCompareAndSetFloat(base, offset, 2.0f, 1.0f);
 273             }
 274             assertEquals(success, true, "weakCompareAndSet float");
 275             float x = UNSAFE.getFloat(base, offset);
 276             assertEquals(x, 1.0f, "weakCompareAndSet float");
 277         }
 278 
 279         UNSAFE.putFloat(base, offset, 2.0f);
 280 
 281         // Compare set and get
 282         {
 283             float o = UNSAFE.getAndSetFloat(base, offset, 1.0f);
 284             assertEquals(o, 2.0f, "getAndSet float");
 285             float x = UNSAFE.getFloat(base, offset);
 286             assertEquals(x, 1.0f, "getAndSet float value");
 287         }
 288 
 289         UNSAFE.putFloat(base, offset, 1.0f);
 290 
 291         // get and add, add and get
 292         {
 293             float o = UNSAFE.getAndAddFloat(base, offset, 2.0f);
 294             assertEquals(o, 1.0f, "getAndAdd float");
 295             float x = UNSAFE.getFloat(base, offset);
 296             assertEquals(x, (float)(1.0f + 2.0f), "getAndAdd float");
 297         }
 298     }
 299 
 300     static void testAccess(long address) {
 301         // Plain
 302         {
 303             UNSAFE.putFloat(address, 1.0f);
 304             float x = UNSAFE.getFloat(address);
 305             assertEquals(x, 1.0f, "set float value");
 306         }
 307     }
 308 }

< prev index next >