< prev index next >

test/compiler/unsafe/JdkInternalMiscUnsafeAccessTestLong.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  */


 181             long x = UNSAFE.getLongUnaligned(base, offset);
 182             assertEquals(x, 0xCAFEBABECAFEBABEL, "putUnaligned long value");
 183         }
 184 
 185         {
 186             UNSAFE.putLongUnaligned(base, offset, 0x0123456789ABCDEFL, true);
 187             long x = UNSAFE.getLongUnaligned(base, offset, true);
 188             assertEquals(x, 0x0123456789ABCDEFL, "putUnaligned big endian long value");
 189         }
 190 
 191         {
 192             UNSAFE.putLongUnaligned(base, offset, 0xCAFEBABECAFEBABEL, false);
 193             long x = UNSAFE.getLongUnaligned(base, offset, false);
 194             assertEquals(x, 0xCAFEBABECAFEBABEL, "putUnaligned little endian long value");
 195         }
 196 
 197         UNSAFE.putLong(base, offset, 0x0123456789ABCDEFL);
 198 
 199         // Compare
 200         {
 201             boolean r = UNSAFE.compareAndSwapLong(base, offset, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 202             assertEquals(r, true, "success compareAndSwap long");
 203             long x = UNSAFE.getLong(base, offset);
 204             assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndSwap long value");
 205         }
 206 
 207         {
 208             boolean r = UNSAFE.compareAndSwapLong(base, offset, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL);
 209             assertEquals(r, false, "failing compareAndSwap long");
 210             long x = UNSAFE.getLong(base, offset);
 211             assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndSwap long value");
 212         }
 213 
 214         // Advanced compare
 215         {
 216             long r = UNSAFE.compareAndExchangeLongVolatile(base, offset, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 217             assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchangeVolatile long");
 218             long x = UNSAFE.getLong(base, offset);
 219             assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchangeVolatile long value");
 220         }
 221 
 222         {
 223             long r = UNSAFE.compareAndExchangeLongVolatile(base, offset, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL);
 224             assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchangeVolatile long");
 225             long x = UNSAFE.getLong(base, offset);
 226             assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchangeVolatile long value");
 227         }
 228 
 229         {
 230             long r = UNSAFE.compareAndExchangeLongAcquire(base, offset, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 231             assertEquals(r, 0x0123456789ABCDEFL, "success compareAndExchangeAcquire long");
 232             long x = UNSAFE.getLong(base, offset);
 233             assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndExchangeAcquire long value");
 234         }
 235 
 236         {
 237             long r = UNSAFE.compareAndExchangeLongAcquire(base, offset, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL);
 238             assertEquals(r, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long");
 239             long x = UNSAFE.getLong(base, offset);
 240             assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long value");
 241         }
 242 
 243         {
 244             long r = UNSAFE.compareAndExchangeLongRelease(base, offset, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 245             assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchangeRelease long");
 246             long x = UNSAFE.getLong(base, offset);
 247             assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchangeRelease long value");
 248         }
 249 
 250         {
 251             long r = UNSAFE.compareAndExchangeLongRelease(base, offset, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL);
 252             assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long");
 253             long x = UNSAFE.getLong(base, offset);
 254             assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long value");
 255         }
 256 
 257         {
 258             boolean success = false;
 259             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 260                 success = UNSAFE.weakCompareAndSwapLong(base, offset, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 261             }
 262             assertEquals(success, true, "weakCompareAndSwap long");
 263             long x = UNSAFE.getLong(base, offset);
 264             assertEquals(x, 0xCAFEBABECAFEBABEL, "weakCompareAndSwap long value");
 265         }
 266 
 267         {
 268             boolean success = false;
 269             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 270                 success = UNSAFE.weakCompareAndSwapLongAcquire(base, offset, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 271             }
 272             assertEquals(success, true, "weakCompareAndSwapAcquire long");
 273             long x = UNSAFE.getLong(base, offset);
 274             assertEquals(x, 0x0123456789ABCDEFL, "weakCompareAndSwapAcquire long");
 275         }
 276 
 277         {
 278             boolean success = false;
 279             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 280                 success = UNSAFE.weakCompareAndSwapLongRelease(base, offset, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 281             }
 282             assertEquals(success, true, "weakCompareAndSwapRelease long");
 283             long x = UNSAFE.getLong(base, offset);
 284             assertEquals(x, 0xCAFEBABECAFEBABEL, "weakCompareAndSwapRelease long");
 285         }
 286 
 287         {
 288             boolean success = false;
 289             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 290                 success = UNSAFE.weakCompareAndSwapLongVolatile(base, offset, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 291             }
 292             assertEquals(success, true, "weakCompareAndSwapVolatile long");
 293             long x = UNSAFE.getLong(base, offset);
 294             assertEquals(x, 0x0123456789ABCDEFL, "weakCompareAndSwapVolatile long");
 295         }
 296 
 297         UNSAFE.putLong(base, offset, 0xCAFEBABECAFEBABEL);
 298 
 299         // Compare set and get
 300         {
 301             long o = UNSAFE.getAndSetLong(base, offset, 0x0123456789ABCDEFL);
 302             assertEquals(o, 0xCAFEBABECAFEBABEL, "getAndSet long");
 303             long x = UNSAFE.getLong(base, offset);
 304             assertEquals(x, 0x0123456789ABCDEFL, "getAndSet long value");
 305         }
 306 
 307         UNSAFE.putLong(base, offset, 0x0123456789ABCDEFL);
 308 
 309         // get and add, add and get
 310         {
 311             long o = UNSAFE.getAndAddLong(base, offset, 0xCAFEBABECAFEBABEL);
 312             assertEquals(o, 0x0123456789ABCDEFL, "getAndAdd long");
 313             long x = UNSAFE.getLong(base, offset);
 314             assertEquals(x, (long)(0x0123456789ABCDEFL + 0xCAFEBABECAFEBABEL), "getAndAdd long");
 315         }
 316     }
 317 
 318     static void testAccess(long address) {
 319         // Plain
 320         {
 321             UNSAFE.putLong(address, 0x0123456789ABCDEFL);
 322             long x = UNSAFE.getLong(address);
 323             assertEquals(x, 0x0123456789ABCDEFL, "set long value");
 324         }
 325     }
 326 }
 327 
   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  */


 181             long x = UNSAFE.getLongUnaligned(base, offset);
 182             assertEquals(x, 0xCAFEBABECAFEBABEL, "putUnaligned long value");
 183         }
 184 
 185         {
 186             UNSAFE.putLongUnaligned(base, offset, 0x0123456789ABCDEFL, true);
 187             long x = UNSAFE.getLongUnaligned(base, offset, true);
 188             assertEquals(x, 0x0123456789ABCDEFL, "putUnaligned big endian long value");
 189         }
 190 
 191         {
 192             UNSAFE.putLongUnaligned(base, offset, 0xCAFEBABECAFEBABEL, false);
 193             long x = UNSAFE.getLongUnaligned(base, offset, false);
 194             assertEquals(x, 0xCAFEBABECAFEBABEL, "putUnaligned little endian long value");
 195         }
 196 
 197         UNSAFE.putLong(base, offset, 0x0123456789ABCDEFL);
 198 
 199         // Compare
 200         {
 201             boolean r = UNSAFE.compareAndSetLong(base, offset, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 202             assertEquals(r, true, "success compareAndSet long");
 203             long x = UNSAFE.getLong(base, offset);
 204             assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndSet long value");
 205         }
 206 
 207         {
 208             boolean r = UNSAFE.compareAndSetLong(base, offset, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL);
 209             assertEquals(r, false, "failing compareAndSet long");
 210             long x = UNSAFE.getLong(base, offset);
 211             assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndSet long value");
 212         }
 213 
 214         // Advanced compare
 215         {
 216             long r = UNSAFE.compareAndExchangeLong(base, offset, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 217             assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchange long");
 218             long x = UNSAFE.getLong(base, offset);
 219             assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchange long value");
 220         }
 221 
 222         {
 223             long r = UNSAFE.compareAndExchangeLong(base, offset, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL);
 224             assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchange long");
 225             long x = UNSAFE.getLong(base, offset);
 226             assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchange long value");
 227         }
 228 
 229         {
 230             long r = UNSAFE.compareAndExchangeLongAcquire(base, offset, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 231             assertEquals(r, 0x0123456789ABCDEFL, "success compareAndExchangeAcquire long");
 232             long x = UNSAFE.getLong(base, offset);
 233             assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndExchangeAcquire long value");
 234         }
 235 
 236         {
 237             long r = UNSAFE.compareAndExchangeLongAcquire(base, offset, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL);
 238             assertEquals(r, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long");
 239             long x = UNSAFE.getLong(base, offset);
 240             assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long value");
 241         }
 242 
 243         {
 244             long r = UNSAFE.compareAndExchangeLongRelease(base, offset, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 245             assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchangeRelease long");
 246             long x = UNSAFE.getLong(base, offset);
 247             assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchangeRelease long value");
 248         }
 249 
 250         {
 251             long r = UNSAFE.compareAndExchangeLongRelease(base, offset, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL);
 252             assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long");
 253             long x = UNSAFE.getLong(base, offset);
 254             assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long value");
 255         }
 256 
 257         {
 258             boolean success = false;
 259             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 260                 success = UNSAFE.weakCompareAndSetLongPlain(base, offset, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 261             }
 262             assertEquals(success, true, "weakCompareAndSetPlain long");
 263             long x = UNSAFE.getLong(base, offset);
 264             assertEquals(x, 0xCAFEBABECAFEBABEL, "weakCompareAndSetPlain long value");
 265         }
 266 
 267         {
 268             boolean success = false;
 269             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 270                 success = UNSAFE.weakCompareAndSetLongAcquire(base, offset, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 271             }
 272             assertEquals(success, true, "weakCompareAndSetAcquire long");
 273             long x = UNSAFE.getLong(base, offset);
 274             assertEquals(x, 0x0123456789ABCDEFL, "weakCompareAndSetAcquire long");
 275         }
 276 
 277         {
 278             boolean success = false;
 279             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 280                 success = UNSAFE.weakCompareAndSetLongRelease(base, offset, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL);
 281             }
 282             assertEquals(success, true, "weakCompareAndSetRelease long");
 283             long x = UNSAFE.getLong(base, offset);
 284             assertEquals(x, 0xCAFEBABECAFEBABEL, "weakCompareAndSetRelease long");
 285         }
 286 
 287         {
 288             boolean success = false;
 289             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 290                 success = UNSAFE.weakCompareAndSetLong(base, offset, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL);
 291             }
 292             assertEquals(success, true, "weakCompareAndSet long");
 293             long x = UNSAFE.getLong(base, offset);
 294             assertEquals(x, 0x0123456789ABCDEFL, "weakCompareAndSet long");
 295         }
 296 
 297         UNSAFE.putLong(base, offset, 0xCAFEBABECAFEBABEL);
 298 
 299         // Compare set and get
 300         {
 301             long o = UNSAFE.getAndSetLong(base, offset, 0x0123456789ABCDEFL);
 302             assertEquals(o, 0xCAFEBABECAFEBABEL, "getAndSet long");
 303             long x = UNSAFE.getLong(base, offset);
 304             assertEquals(x, 0x0123456789ABCDEFL, "getAndSet long value");
 305         }
 306 
 307         UNSAFE.putLong(base, offset, 0x0123456789ABCDEFL);
 308 
 309         // get and add, add and get
 310         {
 311             long o = UNSAFE.getAndAddLong(base, offset, 0xCAFEBABECAFEBABEL);
 312             assertEquals(o, 0x0123456789ABCDEFL, "getAndAdd long");
 313             long x = UNSAFE.getLong(base, offset);
 314             assertEquals(x, (long)(0x0123456789ABCDEFL + 0xCAFEBABECAFEBABEL), "getAndAdd long");
 315         }
 316     }
 317 
 318     static void testAccess(long address) {
 319         // Plain
 320         {
 321             UNSAFE.putLong(address, 0x0123456789ABCDEFL);
 322             long x = UNSAFE.getLong(address);
 323             assertEquals(x, 0x0123456789ABCDEFL, "set long value");
 324         }
 325     }
 326 }

< prev index next >