< prev index next >

test/compiler/unsafe/JdkInternalMiscUnsafeAccessTestByte.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.putByteRelease(base, offset, (byte)0x01);
 167             byte x = UNSAFE.getByteAcquire(base, offset);
 168             assertEquals(x, (byte)0x01, "putRelease byte value");
 169         }
 170 
 171         // Opaque
 172         {
 173             UNSAFE.putByteOpaque(base, offset, (byte)0x23);
 174             byte x = UNSAFE.getByteOpaque(base, offset);
 175             assertEquals(x, (byte)0x23, "putOpaque byte value");
 176         }
 177 
 178 
 179         UNSAFE.putByte(base, offset, (byte)0x01);
 180 
 181         // Compare
 182         {
 183             boolean r = UNSAFE.compareAndSwapByte(base, offset, (byte)0x01, (byte)0x23);
 184             assertEquals(r, true, "success compareAndSwap byte");
 185             byte x = UNSAFE.getByte(base, offset);
 186             assertEquals(x, (byte)0x23, "success compareAndSwap byte value");
 187         }
 188 
 189         {
 190             boolean r = UNSAFE.compareAndSwapByte(base, offset, (byte)0x01, (byte)0x45);
 191             assertEquals(r, false, "failing compareAndSwap byte");
 192             byte x = UNSAFE.getByte(base, offset);
 193             assertEquals(x, (byte)0x23, "failing compareAndSwap byte value");
 194         }
 195 
 196         // Advanced compare
 197         {
 198             byte r = UNSAFE.compareAndExchangeByteVolatile(base, offset, (byte)0x23, (byte)0x01);
 199             assertEquals(r, (byte)0x23, "success compareAndExchangeVolatile byte");
 200             byte x = UNSAFE.getByte(base, offset);
 201             assertEquals(x, (byte)0x01, "success compareAndExchangeVolatile byte value");
 202         }
 203 
 204         {
 205             byte r = UNSAFE.compareAndExchangeByteVolatile(base, offset, (byte)0x23, (byte)0x45);
 206             assertEquals(r, (byte)0x01, "failing compareAndExchangeVolatile byte");
 207             byte x = UNSAFE.getByte(base, offset);
 208             assertEquals(x, (byte)0x01, "failing compareAndExchangeVolatile byte value");
 209         }
 210 
 211         {
 212             byte r = UNSAFE.compareAndExchangeByteAcquire(base, offset, (byte)0x01, (byte)0x23);
 213             assertEquals(r, (byte)0x01, "success compareAndExchangeAcquire byte");
 214             byte x = UNSAFE.getByte(base, offset);
 215             assertEquals(x, (byte)0x23, "success compareAndExchangeAcquire byte value");
 216         }
 217 
 218         {
 219             byte r = UNSAFE.compareAndExchangeByteAcquire(base, offset, (byte)0x01, (byte)0x45);
 220             assertEquals(r, (byte)0x23, "failing compareAndExchangeAcquire byte");
 221             byte x = UNSAFE.getByte(base, offset);
 222             assertEquals(x, (byte)0x23, "failing compareAndExchangeAcquire byte value");
 223         }
 224 
 225         {
 226             byte r = UNSAFE.compareAndExchangeByteRelease(base, offset, (byte)0x23, (byte)0x01);
 227             assertEquals(r, (byte)0x23, "success compareAndExchangeRelease byte");
 228             byte x = UNSAFE.getByte(base, offset);
 229             assertEquals(x, (byte)0x01, "success compareAndExchangeRelease byte value");
 230         }
 231 
 232         {
 233             byte r = UNSAFE.compareAndExchangeByteRelease(base, offset, (byte)0x23, (byte)0x45);
 234             assertEquals(r, (byte)0x01, "failing compareAndExchangeRelease byte");
 235             byte x = UNSAFE.getByte(base, offset);
 236             assertEquals(x, (byte)0x01, "failing compareAndExchangeRelease byte value");
 237         }
 238 
 239         {
 240             boolean success = false;
 241             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 242                 success = UNSAFE.weakCompareAndSwapByte(base, offset, (byte)0x01, (byte)0x23);
 243             }
 244             assertEquals(success, true, "weakCompareAndSwap byte");
 245             byte x = UNSAFE.getByte(base, offset);
 246             assertEquals(x, (byte)0x23, "weakCompareAndSwap byte value");
 247         }
 248 
 249         {
 250             boolean success = false;
 251             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 252                 success = UNSAFE.weakCompareAndSwapByteAcquire(base, offset, (byte)0x23, (byte)0x01);
 253             }
 254             assertEquals(success, true, "weakCompareAndSwapAcquire byte");
 255             byte x = UNSAFE.getByte(base, offset);
 256             assertEquals(x, (byte)0x01, "weakCompareAndSwapAcquire byte");
 257         }
 258 
 259         {
 260             boolean success = false;
 261             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 262                 success = UNSAFE.weakCompareAndSwapByteRelease(base, offset, (byte)0x01, (byte)0x23);
 263             }
 264             assertEquals(success, true, "weakCompareAndSwapRelease byte");
 265             byte x = UNSAFE.getByte(base, offset);
 266             assertEquals(x, (byte)0x23, "weakCompareAndSwapRelease byte");
 267         }
 268 
 269         {
 270             boolean success = false;
 271             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 272                 success = UNSAFE.weakCompareAndSwapByteVolatile(base, offset, (byte)0x23, (byte)0x01);
 273             }
 274             assertEquals(success, true, "weakCompareAndSwapVolatile byte");
 275             byte x = UNSAFE.getByte(base, offset);
 276             assertEquals(x, (byte)0x01, "weakCompareAndSwapVolatile byte");
 277         }
 278 
 279         UNSAFE.putByte(base, offset, (byte)0x23);
 280 
 281         // Compare set and get
 282         {
 283             byte o = UNSAFE.getAndSetByte(base, offset, (byte)0x01);
 284             assertEquals(o, (byte)0x23, "getAndSet byte");
 285             byte x = UNSAFE.getByte(base, offset);
 286             assertEquals(x, (byte)0x01, "getAndSet byte value");
 287         }
 288 
 289         UNSAFE.putByte(base, offset, (byte)0x01);
 290 
 291         // get and add, add and get
 292         {
 293             byte o = UNSAFE.getAndAddByte(base, offset, (byte)0x23);
 294             assertEquals(o, (byte)0x01, "getAndAdd byte");
 295             byte x = UNSAFE.getByte(base, offset);
 296             assertEquals(x, (byte)((byte)0x01 + (byte)0x23), "getAndAdd byte");
 297         }
 298     }
 299 
 300     static void testAccess(long address) {
 301         // Plain
 302         {
 303             UNSAFE.putByte(address, (byte)0x01);
 304             byte x = UNSAFE.getByte(address);
 305             assertEquals(x, (byte)0x01, "set byte 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.putByteRelease(base, offset, (byte)0x01);
 167             byte x = UNSAFE.getByteAcquire(base, offset);
 168             assertEquals(x, (byte)0x01, "putRelease byte value");
 169         }
 170 
 171         // Opaque
 172         {
 173             UNSAFE.putByteOpaque(base, offset, (byte)0x23);
 174             byte x = UNSAFE.getByteOpaque(base, offset);
 175             assertEquals(x, (byte)0x23, "putOpaque byte value");
 176         }
 177 
 178 
 179         UNSAFE.putByte(base, offset, (byte)0x01);
 180 
 181         // Compare
 182         {
 183             boolean r = UNSAFE.compareAndSetByte(base, offset, (byte)0x01, (byte)0x23);
 184             assertEquals(r, true, "success compareAndSet byte");
 185             byte x = UNSAFE.getByte(base, offset);
 186             assertEquals(x, (byte)0x23, "success compareAndSet byte value");
 187         }
 188 
 189         {
 190             boolean r = UNSAFE.compareAndSetByte(base, offset, (byte)0x01, (byte)0x45);
 191             assertEquals(r, false, "failing compareAndSet byte");
 192             byte x = UNSAFE.getByte(base, offset);
 193             assertEquals(x, (byte)0x23, "failing compareAndSet byte value");
 194         }
 195 
 196         // Advanced compare
 197         {
 198             byte r = UNSAFE.compareAndExchangeByte(base, offset, (byte)0x23, (byte)0x01);
 199             assertEquals(r, (byte)0x23, "success compareAndExchange byte");
 200             byte x = UNSAFE.getByte(base, offset);
 201             assertEquals(x, (byte)0x01, "success compareAndExchange byte value");
 202         }
 203 
 204         {
 205             byte r = UNSAFE.compareAndExchangeByte(base, offset, (byte)0x23, (byte)0x45);
 206             assertEquals(r, (byte)0x01, "failing compareAndExchange byte");
 207             byte x = UNSAFE.getByte(base, offset);
 208             assertEquals(x, (byte)0x01, "failing compareAndExchange byte value");
 209         }
 210 
 211         {
 212             byte r = UNSAFE.compareAndExchangeByteAcquire(base, offset, (byte)0x01, (byte)0x23);
 213             assertEquals(r, (byte)0x01, "success compareAndExchangeAcquire byte");
 214             byte x = UNSAFE.getByte(base, offset);
 215             assertEquals(x, (byte)0x23, "success compareAndExchangeAcquire byte value");
 216         }
 217 
 218         {
 219             byte r = UNSAFE.compareAndExchangeByteAcquire(base, offset, (byte)0x01, (byte)0x45);
 220             assertEquals(r, (byte)0x23, "failing compareAndExchangeAcquire byte");
 221             byte x = UNSAFE.getByte(base, offset);
 222             assertEquals(x, (byte)0x23, "failing compareAndExchangeAcquire byte value");
 223         }
 224 
 225         {
 226             byte r = UNSAFE.compareAndExchangeByteRelease(base, offset, (byte)0x23, (byte)0x01);
 227             assertEquals(r, (byte)0x23, "success compareAndExchangeRelease byte");
 228             byte x = UNSAFE.getByte(base, offset);
 229             assertEquals(x, (byte)0x01, "success compareAndExchangeRelease byte value");
 230         }
 231 
 232         {
 233             byte r = UNSAFE.compareAndExchangeByteRelease(base, offset, (byte)0x23, (byte)0x45);
 234             assertEquals(r, (byte)0x01, "failing compareAndExchangeRelease byte");
 235             byte x = UNSAFE.getByte(base, offset);
 236             assertEquals(x, (byte)0x01, "failing compareAndExchangeRelease byte value");
 237         }
 238 
 239         {
 240             boolean success = false;
 241             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 242                 success = UNSAFE.weakCompareAndSetBytePlain(base, offset, (byte)0x01, (byte)0x23);
 243             }
 244             assertEquals(success, true, "weakCompareAndSetPlain byte");
 245             byte x = UNSAFE.getByte(base, offset);
 246             assertEquals(x, (byte)0x23, "weakCompareAndSetPlain byte value");
 247         }
 248 
 249         {
 250             boolean success = false;
 251             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 252                 success = UNSAFE.weakCompareAndSetByteAcquire(base, offset, (byte)0x23, (byte)0x01);
 253             }
 254             assertEquals(success, true, "weakCompareAndSetAcquire byte");
 255             byte x = UNSAFE.getByte(base, offset);
 256             assertEquals(x, (byte)0x01, "weakCompareAndSetAcquire byte");
 257         }
 258 
 259         {
 260             boolean success = false;
 261             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 262                 success = UNSAFE.weakCompareAndSetByteRelease(base, offset, (byte)0x01, (byte)0x23);
 263             }
 264             assertEquals(success, true, "weakCompareAndSetRelease byte");
 265             byte x = UNSAFE.getByte(base, offset);
 266             assertEquals(x, (byte)0x23, "weakCompareAndSetRelease byte");
 267         }
 268 
 269         {
 270             boolean success = false;
 271             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 272                 success = UNSAFE.weakCompareAndSetByte(base, offset, (byte)0x23, (byte)0x01);
 273             }
 274             assertEquals(success, true, "weakCompareAndSet byte");
 275             byte x = UNSAFE.getByte(base, offset);
 276             assertEquals(x, (byte)0x01, "weakCompareAndSet byte");
 277         }
 278 
 279         UNSAFE.putByte(base, offset, (byte)0x23);
 280 
 281         // Compare set and get
 282         {
 283             byte o = UNSAFE.getAndSetByte(base, offset, (byte)0x01);
 284             assertEquals(o, (byte)0x23, "getAndSet byte");
 285             byte x = UNSAFE.getByte(base, offset);
 286             assertEquals(x, (byte)0x01, "getAndSet byte value");
 287         }
 288 
 289         UNSAFE.putByte(base, offset, (byte)0x01);
 290 
 291         // get and add, add and get
 292         {
 293             byte o = UNSAFE.getAndAddByte(base, offset, (byte)0x23);
 294             assertEquals(o, (byte)0x01, "getAndAdd byte");
 295             byte x = UNSAFE.getByte(base, offset);
 296             assertEquals(x, (byte)((byte)0x01 + (byte)0x23), "getAndAdd byte");
 297         }
 298     }
 299 
 300     static void testAccess(long address) {
 301         // Plain
 302         {
 303             UNSAFE.putByte(address, (byte)0x01);
 304             byte x = UNSAFE.getByte(address);
 305             assertEquals(x, (byte)0x01, "set byte value");
 306         }
 307     }
 308 }

< prev index next >