< prev index next >

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


 134 
 135         // Lazy
 136         {
 137             UNSAFE.putObjectRelease(base, offset, "foo");
 138             Object x = UNSAFE.getObjectAcquire(base, offset);
 139             assertEquals(x, "foo", "putRelease Object value");
 140         }
 141 
 142         // Opaque
 143         {
 144             UNSAFE.putObjectOpaque(base, offset, "bar");
 145             Object x = UNSAFE.getObjectOpaque(base, offset);
 146             assertEquals(x, "bar", "putOpaque Object value");
 147         }
 148 
 149 
 150         UNSAFE.putObject(base, offset, "foo");
 151 
 152         // Compare
 153         {
 154             boolean r = UNSAFE.compareAndSwapObject(base, offset, "foo", "bar");
 155             assertEquals(r, true, "success compareAndSwap Object");
 156             Object x = UNSAFE.getObject(base, offset);
 157             assertEquals(x, "bar", "success compareAndSwap Object value");
 158         }
 159 
 160         {
 161             boolean r = UNSAFE.compareAndSwapObject(base, offset, "foo", "baz");
 162             assertEquals(r, false, "failing compareAndSwap Object");
 163             Object x = UNSAFE.getObject(base, offset);
 164             assertEquals(x, "bar", "failing compareAndSwap Object value");
 165         }
 166 
 167         // Advanced compare
 168         {
 169             Object r = UNSAFE.compareAndExchangeObjectVolatile(base, offset, "bar", "foo");
 170             assertEquals(r, "bar", "success compareAndExchangeVolatile Object");
 171             Object x = UNSAFE.getObject(base, offset);
 172             assertEquals(x, "foo", "success compareAndExchangeVolatile Object value");
 173         }
 174 
 175         {
 176             Object r = UNSAFE.compareAndExchangeObjectVolatile(base, offset, "bar", "baz");
 177             assertEquals(r, "foo", "failing compareAndExchangeVolatile Object");
 178             Object x = UNSAFE.getObject(base, offset);
 179             assertEquals(x, "foo", "failing compareAndExchangeVolatile Object value");
 180         }
 181 
 182         {
 183             Object r = UNSAFE.compareAndExchangeObjectAcquire(base, offset, "foo", "bar");
 184             assertEquals(r, "foo", "success compareAndExchangeAcquire Object");
 185             Object x = UNSAFE.getObject(base, offset);
 186             assertEquals(x, "bar", "success compareAndExchangeAcquire Object value");
 187         }
 188 
 189         {
 190             Object r = UNSAFE.compareAndExchangeObjectAcquire(base, offset, "foo", "baz");
 191             assertEquals(r, "bar", "failing compareAndExchangeAcquire Object");
 192             Object x = UNSAFE.getObject(base, offset);
 193             assertEquals(x, "bar", "failing compareAndExchangeAcquire Object value");
 194         }
 195 
 196         {
 197             Object r = UNSAFE.compareAndExchangeObjectRelease(base, offset, "bar", "foo");
 198             assertEquals(r, "bar", "success compareAndExchangeRelease Object");
 199             Object x = UNSAFE.getObject(base, offset);
 200             assertEquals(x, "foo", "success compareAndExchangeRelease Object value");
 201         }
 202 
 203         {
 204             Object r = UNSAFE.compareAndExchangeObjectRelease(base, offset, "bar", "baz");
 205             assertEquals(r, "foo", "failing compareAndExchangeRelease Object");
 206             Object x = UNSAFE.getObject(base, offset);
 207             assertEquals(x, "foo", "failing compareAndExchangeRelease Object value");
 208         }
 209 
 210         {
 211             boolean success = false;
 212             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 213                 success = UNSAFE.weakCompareAndSwapObject(base, offset, "foo", "bar");
 214             }
 215             assertEquals(success, true, "weakCompareAndSwap Object");
 216             Object x = UNSAFE.getObject(base, offset);
 217             assertEquals(x, "bar", "weakCompareAndSwap Object value");
 218         }
 219 
 220         {
 221             boolean success = false;
 222             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 223                 success = UNSAFE.weakCompareAndSwapObjectAcquire(base, offset, "bar", "foo");
 224             }
 225             assertEquals(success, true, "weakCompareAndSwapAcquire Object");
 226             Object x = UNSAFE.getObject(base, offset);
 227             assertEquals(x, "foo", "weakCompareAndSwapAcquire Object");
 228         }
 229 
 230         {
 231             boolean success = false;
 232             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 233                 success = UNSAFE.weakCompareAndSwapObjectRelease(base, offset, "foo", "bar");
 234             }
 235             assertEquals(success, true, "weakCompareAndSwapRelease Object");
 236             Object x = UNSAFE.getObject(base, offset);
 237             assertEquals(x, "bar", "weakCompareAndSwapRelease Object");
 238         }
 239 
 240         {
 241             boolean success = false;
 242             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 243                 success = UNSAFE.weakCompareAndSwapObjectVolatile(base, offset, "bar", "foo");
 244             }
 245             assertEquals(success, true, "weakCompareAndSwapVolatile Object");
 246             Object x = UNSAFE.getObject(base, offset);
 247             assertEquals(x, "foo", "weakCompareAndSwapVolatile Object");
 248         }
 249 
 250         UNSAFE.putObject(base, offset, "bar");
 251 
 252         // Compare set and get
 253         {
 254             Object o = UNSAFE.getAndSetObject(base, offset, "foo");
 255             assertEquals(o, "bar", "getAndSet Object");
 256             Object x = UNSAFE.getObject(base, offset);
 257             assertEquals(x, "foo", "getAndSet Object value");
 258         }
 259 
 260     }
 261 
 262 }
 263 
   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  */


 134 
 135         // Lazy
 136         {
 137             UNSAFE.putObjectRelease(base, offset, "foo");
 138             Object x = UNSAFE.getObjectAcquire(base, offset);
 139             assertEquals(x, "foo", "putRelease Object value");
 140         }
 141 
 142         // Opaque
 143         {
 144             UNSAFE.putObjectOpaque(base, offset, "bar");
 145             Object x = UNSAFE.getObjectOpaque(base, offset);
 146             assertEquals(x, "bar", "putOpaque Object value");
 147         }
 148 
 149 
 150         UNSAFE.putObject(base, offset, "foo");
 151 
 152         // Compare
 153         {
 154             boolean r = UNSAFE.compareAndSetObject(base, offset, "foo", "bar");
 155             assertEquals(r, true, "success compareAndSet Object");
 156             Object x = UNSAFE.getObject(base, offset);
 157             assertEquals(x, "bar", "success compareAndSet Object value");
 158         }
 159 
 160         {
 161             boolean r = UNSAFE.compareAndSetObject(base, offset, "foo", "baz");
 162             assertEquals(r, false, "failing compareAndSet Object");
 163             Object x = UNSAFE.getObject(base, offset);
 164             assertEquals(x, "bar", "failing compareAndSet Object value");
 165         }
 166 
 167         // Advanced compare
 168         {
 169             Object r = UNSAFE.compareAndExchangeObject(base, offset, "bar", "foo");
 170             assertEquals(r, "bar", "success compareAndExchange Object");
 171             Object x = UNSAFE.getObject(base, offset);
 172             assertEquals(x, "foo", "success compareAndExchange Object value");
 173         }
 174 
 175         {
 176             Object r = UNSAFE.compareAndExchangeObject(base, offset, "bar", "baz");
 177             assertEquals(r, "foo", "failing compareAndExchange Object");
 178             Object x = UNSAFE.getObject(base, offset);
 179             assertEquals(x, "foo", "failing compareAndExchange Object value");
 180         }
 181 
 182         {
 183             Object r = UNSAFE.compareAndExchangeObjectAcquire(base, offset, "foo", "bar");
 184             assertEquals(r, "foo", "success compareAndExchangeAcquire Object");
 185             Object x = UNSAFE.getObject(base, offset);
 186             assertEquals(x, "bar", "success compareAndExchangeAcquire Object value");
 187         }
 188 
 189         {
 190             Object r = UNSAFE.compareAndExchangeObjectAcquire(base, offset, "foo", "baz");
 191             assertEquals(r, "bar", "failing compareAndExchangeAcquire Object");
 192             Object x = UNSAFE.getObject(base, offset);
 193             assertEquals(x, "bar", "failing compareAndExchangeAcquire Object value");
 194         }
 195 
 196         {
 197             Object r = UNSAFE.compareAndExchangeObjectRelease(base, offset, "bar", "foo");
 198             assertEquals(r, "bar", "success compareAndExchangeRelease Object");
 199             Object x = UNSAFE.getObject(base, offset);
 200             assertEquals(x, "foo", "success compareAndExchangeRelease Object value");
 201         }
 202 
 203         {
 204             Object r = UNSAFE.compareAndExchangeObjectRelease(base, offset, "bar", "baz");
 205             assertEquals(r, "foo", "failing compareAndExchangeRelease Object");
 206             Object x = UNSAFE.getObject(base, offset);
 207             assertEquals(x, "foo", "failing compareAndExchangeRelease Object value");
 208         }
 209 
 210         {
 211             boolean success = false;
 212             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 213                 success = UNSAFE.weakCompareAndSetObjectPlain(base, offset, "foo", "bar");
 214             }
 215             assertEquals(success, true, "weakCompareAndSetPlain Object");
 216             Object x = UNSAFE.getObject(base, offset);
 217             assertEquals(x, "bar", "weakCompareAndSetPlain Object value");
 218         }
 219 
 220         {
 221             boolean success = false;
 222             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 223                 success = UNSAFE.weakCompareAndSetObjectAcquire(base, offset, "bar", "foo");
 224             }
 225             assertEquals(success, true, "weakCompareAndSetAcquire Object");
 226             Object x = UNSAFE.getObject(base, offset);
 227             assertEquals(x, "foo", "weakCompareAndSetAcquire Object");
 228         }
 229 
 230         {
 231             boolean success = false;
 232             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 233                 success = UNSAFE.weakCompareAndSetObjectRelease(base, offset, "foo", "bar");
 234             }
 235             assertEquals(success, true, "weakCompareAndSetRelease Object");
 236             Object x = UNSAFE.getObject(base, offset);
 237             assertEquals(x, "bar", "weakCompareAndSetRelease Object");
 238         }
 239 
 240         {
 241             boolean success = false;
 242             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 243                 success = UNSAFE.weakCompareAndSetObject(base, offset, "bar", "foo");
 244             }
 245             assertEquals(success, true, "weakCompareAndSet Object");
 246             Object x = UNSAFE.getObject(base, offset);
 247             assertEquals(x, "foo", "weakCompareAndSet Object");
 248         }
 249 
 250         UNSAFE.putObject(base, offset, "bar");
 251 
 252         // Compare set and get
 253         {
 254             Object o = UNSAFE.getAndSetObject(base, offset, "foo");
 255             assertEquals(o, "bar", "getAndSet Object");
 256             Object x = UNSAFE.getObject(base, offset);
 257             assertEquals(x, "foo", "getAndSet Object value");
 258         }
 259 
 260     }
 261 
 262 }

< prev index next >