< prev index next >

test/compiler/unsafe/JdkInternalMiscUnsafeAccessTestInt.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             int x = UNSAFE.getIntUnaligned(base, offset);
 182             assertEquals(x, 0x89ABCDEF, "putUnaligned int value");
 183         }
 184 
 185         {
 186             UNSAFE.putIntUnaligned(base, offset, 0x01234567, true);
 187             int x = UNSAFE.getIntUnaligned(base, offset, true);
 188             assertEquals(x, 0x01234567, "putUnaligned big endian int value");
 189         }
 190 
 191         {
 192             UNSAFE.putIntUnaligned(base, offset, 0x89ABCDEF, false);
 193             int x = UNSAFE.getIntUnaligned(base, offset, false);
 194             assertEquals(x, 0x89ABCDEF, "putUnaligned little endian int value");
 195         }
 196 
 197         UNSAFE.putInt(base, offset, 0x01234567);
 198 
 199         // Compare
 200         {
 201             boolean r = UNSAFE.compareAndSwapInt(base, offset, 0x01234567, 0x89ABCDEF);
 202             assertEquals(r, true, "success compareAndSwap int");
 203             int x = UNSAFE.getInt(base, offset);
 204             assertEquals(x, 0x89ABCDEF, "success compareAndSwap int value");
 205         }
 206 
 207         {
 208             boolean r = UNSAFE.compareAndSwapInt(base, offset, 0x01234567, 0xCAFEBABE);
 209             assertEquals(r, false, "failing compareAndSwap int");
 210             int x = UNSAFE.getInt(base, offset);
 211             assertEquals(x, 0x89ABCDEF, "failing compareAndSwap int value");
 212         }
 213 
 214         // Advanced compare
 215         {
 216             int r = UNSAFE.compareAndExchangeIntVolatile(base, offset, 0x89ABCDEF, 0x01234567);
 217             assertEquals(r, 0x89ABCDEF, "success compareAndExchangeVolatile int");
 218             int x = UNSAFE.getInt(base, offset);
 219             assertEquals(x, 0x01234567, "success compareAndExchangeVolatile int value");
 220         }
 221 
 222         {
 223             int r = UNSAFE.compareAndExchangeIntVolatile(base, offset, 0x89ABCDEF, 0xCAFEBABE);
 224             assertEquals(r, 0x01234567, "failing compareAndExchangeVolatile int");
 225             int x = UNSAFE.getInt(base, offset);
 226             assertEquals(x, 0x01234567, "failing compareAndExchangeVolatile int value");
 227         }
 228 
 229         {
 230             int r = UNSAFE.compareAndExchangeIntAcquire(base, offset, 0x01234567, 0x89ABCDEF);
 231             assertEquals(r, 0x01234567, "success compareAndExchangeAcquire int");
 232             int x = UNSAFE.getInt(base, offset);
 233             assertEquals(x, 0x89ABCDEF, "success compareAndExchangeAcquire int value");
 234         }
 235 
 236         {
 237             int r = UNSAFE.compareAndExchangeIntAcquire(base, offset, 0x01234567, 0xCAFEBABE);
 238             assertEquals(r, 0x89ABCDEF, "failing compareAndExchangeAcquire int");
 239             int x = UNSAFE.getInt(base, offset);
 240             assertEquals(x, 0x89ABCDEF, "failing compareAndExchangeAcquire int value");
 241         }
 242 
 243         {
 244             int r = UNSAFE.compareAndExchangeIntRelease(base, offset, 0x89ABCDEF, 0x01234567);
 245             assertEquals(r, 0x89ABCDEF, "success compareAndExchangeRelease int");
 246             int x = UNSAFE.getInt(base, offset);
 247             assertEquals(x, 0x01234567, "success compareAndExchangeRelease int value");
 248         }
 249 
 250         {
 251             int r = UNSAFE.compareAndExchangeIntRelease(base, offset, 0x89ABCDEF, 0xCAFEBABE);
 252             assertEquals(r, 0x01234567, "failing compareAndExchangeRelease int");
 253             int x = UNSAFE.getInt(base, offset);
 254             assertEquals(x, 0x01234567, "failing compareAndExchangeRelease int value");
 255         }
 256 
 257         {
 258             boolean success = false;
 259             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 260                 success = UNSAFE.weakCompareAndSwapInt(base, offset, 0x01234567, 0x89ABCDEF);
 261             }
 262             assertEquals(success, true, "weakCompareAndSwap int");
 263             int x = UNSAFE.getInt(base, offset);
 264             assertEquals(x, 0x89ABCDEF, "weakCompareAndSwap int value");
 265         }
 266 
 267         {
 268             boolean success = false;
 269             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 270                 success = UNSAFE.weakCompareAndSwapIntAcquire(base, offset, 0x89ABCDEF, 0x01234567);
 271             }
 272             assertEquals(success, true, "weakCompareAndSwapAcquire int");
 273             int x = UNSAFE.getInt(base, offset);
 274             assertEquals(x, 0x01234567, "weakCompareAndSwapAcquire int");
 275         }
 276 
 277         {
 278             boolean success = false;
 279             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 280                 success = UNSAFE.weakCompareAndSwapIntRelease(base, offset, 0x01234567, 0x89ABCDEF);
 281             }
 282             assertEquals(success, true, "weakCompareAndSwapRelease int");
 283             int x = UNSAFE.getInt(base, offset);
 284             assertEquals(x, 0x89ABCDEF, "weakCompareAndSwapRelease int");
 285         }
 286 
 287         {
 288             boolean success = false;
 289             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 290                 success = UNSAFE.weakCompareAndSwapIntVolatile(base, offset, 0x89ABCDEF, 0x01234567);
 291             }
 292             assertEquals(success, true, "weakCompareAndSwapVolatile int");
 293             int x = UNSAFE.getInt(base, offset);
 294             assertEquals(x, 0x01234567, "weakCompareAndSwapVolatile int");
 295         }
 296 
 297         UNSAFE.putInt(base, offset, 0x89ABCDEF);
 298 
 299         // Compare set and get
 300         {
 301             int o = UNSAFE.getAndSetInt(base, offset, 0x01234567);
 302             assertEquals(o, 0x89ABCDEF, "getAndSet int");
 303             int x = UNSAFE.getInt(base, offset);
 304             assertEquals(x, 0x01234567, "getAndSet int value");
 305         }
 306 
 307         UNSAFE.putInt(base, offset, 0x01234567);
 308 
 309         // get and add, add and get
 310         {
 311             int o = UNSAFE.getAndAddInt(base, offset, 0x89ABCDEF);
 312             assertEquals(o, 0x01234567, "getAndAdd int");
 313             int x = UNSAFE.getInt(base, offset);
 314             assertEquals(x, (int)(0x01234567 + 0x89ABCDEF), "getAndAdd int");
 315         }
 316     }
 317 
 318     static void testAccess(long address) {
 319         // Plain
 320         {
 321             UNSAFE.putInt(address, 0x01234567);
 322             int x = UNSAFE.getInt(address);
 323             assertEquals(x, 0x01234567, "set int 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             int x = UNSAFE.getIntUnaligned(base, offset);
 182             assertEquals(x, 0x89ABCDEF, "putUnaligned int value");
 183         }
 184 
 185         {
 186             UNSAFE.putIntUnaligned(base, offset, 0x01234567, true);
 187             int x = UNSAFE.getIntUnaligned(base, offset, true);
 188             assertEquals(x, 0x01234567, "putUnaligned big endian int value");
 189         }
 190 
 191         {
 192             UNSAFE.putIntUnaligned(base, offset, 0x89ABCDEF, false);
 193             int x = UNSAFE.getIntUnaligned(base, offset, false);
 194             assertEquals(x, 0x89ABCDEF, "putUnaligned little endian int value");
 195         }
 196 
 197         UNSAFE.putInt(base, offset, 0x01234567);
 198 
 199         // Compare
 200         {
 201             boolean r = UNSAFE.compareAndSetInt(base, offset, 0x01234567, 0x89ABCDEF);
 202             assertEquals(r, true, "success compareAndSet int");
 203             int x = UNSAFE.getInt(base, offset);
 204             assertEquals(x, 0x89ABCDEF, "success compareAndSet int value");
 205         }
 206 
 207         {
 208             boolean r = UNSAFE.compareAndSetInt(base, offset, 0x01234567, 0xCAFEBABE);
 209             assertEquals(r, false, "failing compareAndSet int");
 210             int x = UNSAFE.getInt(base, offset);
 211             assertEquals(x, 0x89ABCDEF, "failing compareAndSet int value");
 212         }
 213 
 214         // Advanced compare
 215         {
 216             int r = UNSAFE.compareAndExchangeInt(base, offset, 0x89ABCDEF, 0x01234567);
 217             assertEquals(r, 0x89ABCDEF, "success compareAndExchange int");
 218             int x = UNSAFE.getInt(base, offset);
 219             assertEquals(x, 0x01234567, "success compareAndExchange int value");
 220         }
 221 
 222         {
 223             int r = UNSAFE.compareAndExchangeInt(base, offset, 0x89ABCDEF, 0xCAFEBABE);
 224             assertEquals(r, 0x01234567, "failing compareAndExchange int");
 225             int x = UNSAFE.getInt(base, offset);
 226             assertEquals(x, 0x01234567, "failing compareAndExchange int value");
 227         }
 228 
 229         {
 230             int r = UNSAFE.compareAndExchangeIntAcquire(base, offset, 0x01234567, 0x89ABCDEF);
 231             assertEquals(r, 0x01234567, "success compareAndExchangeAcquire int");
 232             int x = UNSAFE.getInt(base, offset);
 233             assertEquals(x, 0x89ABCDEF, "success compareAndExchangeAcquire int value");
 234         }
 235 
 236         {
 237             int r = UNSAFE.compareAndExchangeIntAcquire(base, offset, 0x01234567, 0xCAFEBABE);
 238             assertEquals(r, 0x89ABCDEF, "failing compareAndExchangeAcquire int");
 239             int x = UNSAFE.getInt(base, offset);
 240             assertEquals(x, 0x89ABCDEF, "failing compareAndExchangeAcquire int value");
 241         }
 242 
 243         {
 244             int r = UNSAFE.compareAndExchangeIntRelease(base, offset, 0x89ABCDEF, 0x01234567);
 245             assertEquals(r, 0x89ABCDEF, "success compareAndExchangeRelease int");
 246             int x = UNSAFE.getInt(base, offset);
 247             assertEquals(x, 0x01234567, "success compareAndExchangeRelease int value");
 248         }
 249 
 250         {
 251             int r = UNSAFE.compareAndExchangeIntRelease(base, offset, 0x89ABCDEF, 0xCAFEBABE);
 252             assertEquals(r, 0x01234567, "failing compareAndExchangeRelease int");
 253             int x = UNSAFE.getInt(base, offset);
 254             assertEquals(x, 0x01234567, "failing compareAndExchangeRelease int value");
 255         }
 256 
 257         {
 258             boolean success = false;
 259             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 260                 success = UNSAFE.weakCompareAndSetIntPlain(base, offset, 0x01234567, 0x89ABCDEF);
 261             }
 262             assertEquals(success, true, "weakCompareAndSetPlain int");
 263             int x = UNSAFE.getInt(base, offset);
 264             assertEquals(x, 0x89ABCDEF, "weakCompareAndSetPlain int value");
 265         }
 266 
 267         {
 268             boolean success = false;
 269             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 270                 success = UNSAFE.weakCompareAndSetIntAcquire(base, offset, 0x89ABCDEF, 0x01234567);
 271             }
 272             assertEquals(success, true, "weakCompareAndSetAcquire int");
 273             int x = UNSAFE.getInt(base, offset);
 274             assertEquals(x, 0x01234567, "weakCompareAndSetAcquire int");
 275         }
 276 
 277         {
 278             boolean success = false;
 279             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 280                 success = UNSAFE.weakCompareAndSetIntRelease(base, offset, 0x01234567, 0x89ABCDEF);
 281             }
 282             assertEquals(success, true, "weakCompareAndSetRelease int");
 283             int x = UNSAFE.getInt(base, offset);
 284             assertEquals(x, 0x89ABCDEF, "weakCompareAndSetRelease int");
 285         }
 286 
 287         {
 288             boolean success = false;
 289             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 290                 success = UNSAFE.weakCompareAndSetInt(base, offset, 0x89ABCDEF, 0x01234567);
 291             }
 292             assertEquals(success, true, "weakCompareAndSet int");
 293             int x = UNSAFE.getInt(base, offset);
 294             assertEquals(x, 0x01234567, "weakCompareAndSet int");
 295         }
 296 
 297         UNSAFE.putInt(base, offset, 0x89ABCDEF);
 298 
 299         // Compare set and get
 300         {
 301             int o = UNSAFE.getAndSetInt(base, offset, 0x01234567);
 302             assertEquals(o, 0x89ABCDEF, "getAndSet int");
 303             int x = UNSAFE.getInt(base, offset);
 304             assertEquals(x, 0x01234567, "getAndSet int value");
 305         }
 306 
 307         UNSAFE.putInt(base, offset, 0x01234567);
 308 
 309         // get and add, add and get
 310         {
 311             int o = UNSAFE.getAndAddInt(base, offset, 0x89ABCDEF);
 312             assertEquals(o, 0x01234567, "getAndAdd int");
 313             int x = UNSAFE.getInt(base, offset);
 314             assertEquals(x, (int)(0x01234567 + 0x89ABCDEF), "getAndAdd int");
 315         }
 316     }
 317 
 318     static void testAccess(long address) {
 319         // Plain
 320         {
 321             UNSAFE.putInt(address, 0x01234567);
 322             int x = UNSAFE.getInt(address);
 323             assertEquals(x, 0x01234567, "set int value");
 324         }
 325     }
 326 }

< prev index next >