< prev index next >

test/compiler/unsafe/X-UnsafeAccessTest.java.template

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  */
  23 
  24 /*
  25  * @test
  26  * @bug 8143628
  27  * @summary Test unsafe access for $type$
  28  *



  29  * @modules $module$/$package$

  30  * @run testng/othervm -Diters=100   -Xint                   compiler.unsafe.$Qualifier$UnsafeAccessTest$Type$
  31  * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 compiler.unsafe.$Qualifier$UnsafeAccessTest$Type$
  32  * @run testng/othervm -Diters=20000 -XX:-TieredCompilation  compiler.unsafe.$Qualifier$UnsafeAccessTest$Type$
  33  * @run testng/othervm -Diters=20000                         compiler.unsafe.$Qualifier$UnsafeAccessTest$Type$
  34  */
  35 
  36 package compiler.unsafe;
  37 
  38 import org.testng.annotations.Test;
  39 
  40 import java.lang.reflect.Field;
  41 
  42 import static org.testng.Assert.*;
  43 
  44 public class $Qualifier$UnsafeAccessTest$Type$ {
  45     static final int ITERS = Integer.getInteger("iters", 1);
  46     static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
  47 
  48     static final $package$.Unsafe UNSAFE;
  49 


 202 
 203         {
 204             UNSAFE.put$Type$Unaligned(base, offset, $value1$, true);
 205             $type$ x = UNSAFE.get$Type$Unaligned(base, offset, true);
 206             assertEquals(x, $value1$, "putUnaligned big endian $type$ value");
 207         }
 208 
 209         {
 210             UNSAFE.put$Type$Unaligned(base, offset, $value2$, false);
 211             $type$ x = UNSAFE.get$Type$Unaligned(base, offset, false);
 212             assertEquals(x, $value2$, "putUnaligned little endian $type$ value");
 213         }
 214 #end[Unaligned]
 215 #end[JdkInternalMisc]
 216 
 217 #if[CAS]
 218         UNSAFE.put$Type$(base, offset, $value1$);
 219 
 220         // Compare
 221         {




 222             boolean r = UNSAFE.compareAndSwap$Type$(base, offset, $value1$, $value2$);
 223             assertEquals(r, true, "success compareAndSwap $type$");

 224             $type$ x = UNSAFE.get$Type$(base, offset);



 225             assertEquals(x, $value2$, "success compareAndSwap $type$ value");

 226         }
 227 
 228         {




 229             boolean r = UNSAFE.compareAndSwap$Type$(base, offset, $value1$, $value3$);
 230             assertEquals(r, false, "failing compareAndSwap $type$");

 231             $type$ x = UNSAFE.get$Type$(base, offset);



 232             assertEquals(x, $value2$, "failing compareAndSwap $type$ value");

 233         }
 234 
 235 #if[JdkInternalMisc]
 236         // Advanced compare
 237         {
 238             $type$ r = UNSAFE.compareAndExchange$Type$Volatile(base, offset, $value2$, $value1$);
 239             assertEquals(r, $value2$, "success compareAndExchangeVolatile $type$");
 240             $type$ x = UNSAFE.get$Type$(base, offset);
 241             assertEquals(x, $value1$, "success compareAndExchangeVolatile $type$ value");
 242         }
 243 
 244         {
 245             $type$ r = UNSAFE.compareAndExchange$Type$Volatile(base, offset, $value2$, $value3$);
 246             assertEquals(r, $value1$, "failing compareAndExchangeVolatile $type$");
 247             $type$ x = UNSAFE.get$Type$(base, offset);
 248             assertEquals(x, $value1$, "failing compareAndExchangeVolatile $type$ value");
 249         }
 250 
 251         {
 252             $type$ r = UNSAFE.compareAndExchange$Type$Acquire(base, offset, $value1$, $value2$);
 253             assertEquals(r, $value1$, "success compareAndExchangeAcquire $type$");
 254             $type$ x = UNSAFE.get$Type$(base, offset);
 255             assertEquals(x, $value2$, "success compareAndExchangeAcquire $type$ value");
 256         }
 257 
 258         {
 259             $type$ r = UNSAFE.compareAndExchange$Type$Acquire(base, offset, $value1$, $value3$);
 260             assertEquals(r, $value2$, "failing compareAndExchangeAcquire $type$");
 261             $type$ x = UNSAFE.get$Type$(base, offset);
 262             assertEquals(x, $value2$, "failing compareAndExchangeAcquire $type$ value");
 263         }
 264 
 265         {
 266             $type$ r = UNSAFE.compareAndExchange$Type$Release(base, offset, $value2$, $value1$);
 267             assertEquals(r, $value2$, "success compareAndExchangeRelease $type$");
 268             $type$ x = UNSAFE.get$Type$(base, offset);
 269             assertEquals(x, $value1$, "success compareAndExchangeRelease $type$ value");
 270         }
 271 
 272         {
 273             $type$ r = UNSAFE.compareAndExchange$Type$Release(base, offset, $value2$, $value3$);
 274             assertEquals(r, $value1$, "failing compareAndExchangeRelease $type$");
 275             $type$ x = UNSAFE.get$Type$(base, offset);
 276             assertEquals(x, $value1$, "failing compareAndExchangeRelease $type$ value");
 277         }
 278 
 279         {
 280             boolean success = false;
 281             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 282                 success = UNSAFE.weakCompareAndSwap$Type$(base, offset, $value1$, $value2$);
 283             }
 284             assertEquals(success, true, "weakCompareAndSwap $type$");
 285             $type$ x = UNSAFE.get$Type$(base, offset);
 286             assertEquals(x, $value2$, "weakCompareAndSwap $type$ value");
 287         }
 288 
 289         {
 290             boolean success = false;
 291             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 292                 success = UNSAFE.weakCompareAndSwap$Type$Acquire(base, offset, $value2$, $value1$);
 293             }
 294             assertEquals(success, true, "weakCompareAndSwapAcquire $type$");
 295             $type$ x = UNSAFE.get$Type$(base, offset);
 296             assertEquals(x, $value1$, "weakCompareAndSwapAcquire $type$");
 297         }
 298 
 299         {
 300             boolean success = false;
 301             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 302                 success = UNSAFE.weakCompareAndSwap$Type$Release(base, offset, $value1$, $value2$);
 303             }
 304             assertEquals(success, true, "weakCompareAndSwapRelease $type$");
 305             $type$ x = UNSAFE.get$Type$(base, offset);
 306             assertEquals(x, $value2$, "weakCompareAndSwapRelease $type$");
 307         }
 308 
 309         {
 310             boolean success = false;
 311             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 312                 success = UNSAFE.weakCompareAndSwap$Type$Volatile(base, offset, $value2$, $value1$);
 313             }
 314             assertEquals(success, true, "weakCompareAndSwapVolatile $type$");
 315             $type$ x = UNSAFE.get$Type$(base, offset);
 316             assertEquals(x, $value1$, "weakCompareAndSwapVolatile $type$");
 317         }
 318 
 319 #end[JdkInternalMisc]
 320         UNSAFE.put$Type$(base, offset, $value2$);
 321 
 322         // Compare set and get
 323         {
 324             $type$ o = UNSAFE.getAndSet$Type$(base, offset, $value1$);
 325             assertEquals(o, $value2$, "getAndSet $type$");
 326             $type$ x = UNSAFE.get$Type$(base, offset);
 327             assertEquals(x, $value1$, "getAndSet $type$ value");
 328         }
 329 #end[CAS]
 330 
 331 #if[AtomicAdd]
 332         UNSAFE.put$Type$(base, offset, $value1$);
 333 
 334         // get and add, add and get
 335         {
 336             $type$ o = UNSAFE.getAndAdd$Type$(base, offset, $value2$);
 337             assertEquals(o, $value1$, "getAndAdd $type$");
 338             $type$ x = UNSAFE.get$Type$(base, offset);
 339             assertEquals(x, ($type$)($value1$ + $value2$), "getAndAdd $type$");
 340         }
 341 #end[AtomicAdd]
 342     }
 343 
 344 #if[!Object]
 345 #if[!boolean]
 346     static void testAccess(long address) {
 347         // Plain
 348         {
 349             UNSAFE.put$Type$(address, $value1$);
 350             $type$ x = UNSAFE.get$Type$(address);
 351             assertEquals(x, $value1$, "set $type$ value");
 352         }
 353     }
 354 #end[!boolean]
 355 #end[!Object]
 356 }
 357 
   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  */
  23 
  24 /*
  25  * @test
  26  * @bug 8143628
  27  * @summary Test unsafe access for $type$
  28  *
  29 #if[JdkInternalMisc]
  30  * @modules $module$/$package$:+open
  31 #else[JdkInternalMisc]
  32  * @modules $module$/$package$
  33 #end[JdkInternalMisc]
  34  * @run testng/othervm -Diters=100   -Xint                   compiler.unsafe.$Qualifier$UnsafeAccessTest$Type$
  35  * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 compiler.unsafe.$Qualifier$UnsafeAccessTest$Type$
  36  * @run testng/othervm -Diters=20000 -XX:-TieredCompilation  compiler.unsafe.$Qualifier$UnsafeAccessTest$Type$
  37  * @run testng/othervm -Diters=20000                         compiler.unsafe.$Qualifier$UnsafeAccessTest$Type$
  38  */
  39 
  40 package compiler.unsafe;
  41 
  42 import org.testng.annotations.Test;
  43 
  44 import java.lang.reflect.Field;
  45 
  46 import static org.testng.Assert.*;
  47 
  48 public class $Qualifier$UnsafeAccessTest$Type$ {
  49     static final int ITERS = Integer.getInteger("iters", 1);
  50     static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
  51 
  52     static final $package$.Unsafe UNSAFE;
  53 


 206 
 207         {
 208             UNSAFE.put$Type$Unaligned(base, offset, $value1$, true);
 209             $type$ x = UNSAFE.get$Type$Unaligned(base, offset, true);
 210             assertEquals(x, $value1$, "putUnaligned big endian $type$ value");
 211         }
 212 
 213         {
 214             UNSAFE.put$Type$Unaligned(base, offset, $value2$, false);
 215             $type$ x = UNSAFE.get$Type$Unaligned(base, offset, false);
 216             assertEquals(x, $value2$, "putUnaligned little endian $type$ value");
 217         }
 218 #end[Unaligned]
 219 #end[JdkInternalMisc]
 220 
 221 #if[CAS]
 222         UNSAFE.put$Type$(base, offset, $value1$);
 223 
 224         // Compare
 225         {
 226 #if[JdkInternalMisc]
 227             boolean r = UNSAFE.compareAndSet$Type$(base, offset, $value1$, $value2$);
 228             assertEquals(r, true, "success compareAndSet $type$");
 229 #else[JdkInternalMisc]
 230             boolean r = UNSAFE.compareAndSwap$Type$(base, offset, $value1$, $value2$);
 231             assertEquals(r, true, "success compareAndSwap $type$");
 232 #end[JdkInternalMisc]
 233             $type$ x = UNSAFE.get$Type$(base, offset);
 234 #if[JdkInternalMisc]
 235             assertEquals(x, $value2$, "success compareAndSet $type$ value");
 236 #else[JdkInternalMisc]
 237             assertEquals(x, $value2$, "success compareAndSwap $type$ value");
 238 #end[JdkInternalMisc]
 239         }
 240 
 241         {
 242 #if[JdkInternalMisc]
 243             boolean r = UNSAFE.compareAndSet$Type$(base, offset, $value1$, $value3$);
 244             assertEquals(r, false, "failing compareAndSet $type$");
 245 #else[JdkInternalMisc]
 246             boolean r = UNSAFE.compareAndSwap$Type$(base, offset, $value1$, $value3$);
 247             assertEquals(r, false, "failing compareAndSwap $type$");
 248 #end[JdkInternalMisc]
 249             $type$ x = UNSAFE.get$Type$(base, offset);
 250 #if[JdkInternalMisc]
 251             assertEquals(x, $value2$, "failing compareAndSet $type$ value");
 252 #else[JdkInternalMisc]
 253             assertEquals(x, $value2$, "failing compareAndSwap $type$ value");
 254 #end[JdkInternalMisc]
 255         }
 256 
 257 #if[JdkInternalMisc]
 258         // Advanced compare
 259         {
 260             $type$ r = UNSAFE.compareAndExchange$Type$(base, offset, $value2$, $value1$);
 261             assertEquals(r, $value2$, "success compareAndExchange $type$");
 262             $type$ x = UNSAFE.get$Type$(base, offset);
 263             assertEquals(x, $value1$, "success compareAndExchange $type$ value");
 264         }
 265 
 266         {
 267             $type$ r = UNSAFE.compareAndExchange$Type$(base, offset, $value2$, $value3$);
 268             assertEquals(r, $value1$, "failing compareAndExchange $type$");
 269             $type$ x = UNSAFE.get$Type$(base, offset);
 270             assertEquals(x, $value1$, "failing compareAndExchange $type$ value");
 271         }
 272 
 273         {
 274             $type$ r = UNSAFE.compareAndExchange$Type$Acquire(base, offset, $value1$, $value2$);
 275             assertEquals(r, $value1$, "success compareAndExchangeAcquire $type$");
 276             $type$ x = UNSAFE.get$Type$(base, offset);
 277             assertEquals(x, $value2$, "success compareAndExchangeAcquire $type$ value");
 278         }
 279 
 280         {
 281             $type$ r = UNSAFE.compareAndExchange$Type$Acquire(base, offset, $value1$, $value3$);
 282             assertEquals(r, $value2$, "failing compareAndExchangeAcquire $type$");
 283             $type$ x = UNSAFE.get$Type$(base, offset);
 284             assertEquals(x, $value2$, "failing compareAndExchangeAcquire $type$ value");
 285         }
 286 
 287         {
 288             $type$ r = UNSAFE.compareAndExchange$Type$Release(base, offset, $value2$, $value1$);
 289             assertEquals(r, $value2$, "success compareAndExchangeRelease $type$");
 290             $type$ x = UNSAFE.get$Type$(base, offset);
 291             assertEquals(x, $value1$, "success compareAndExchangeRelease $type$ value");
 292         }
 293 
 294         {
 295             $type$ r = UNSAFE.compareAndExchange$Type$Release(base, offset, $value2$, $value3$);
 296             assertEquals(r, $value1$, "failing compareAndExchangeRelease $type$");
 297             $type$ x = UNSAFE.get$Type$(base, offset);
 298             assertEquals(x, $value1$, "failing compareAndExchangeRelease $type$ value");
 299         }
 300 
 301         {
 302             boolean success = false;
 303             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 304                 success = UNSAFE.weakCompareAndSet$Type$Plain(base, offset, $value1$, $value2$);
 305             }
 306             assertEquals(success, true, "weakCompareAndSetPlain $type$");
 307             $type$ x = UNSAFE.get$Type$(base, offset);
 308             assertEquals(x, $value2$, "weakCompareAndSetPlain $type$ value");
 309         }
 310 
 311         {
 312             boolean success = false;
 313             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 314                 success = UNSAFE.weakCompareAndSet$Type$Acquire(base, offset, $value2$, $value1$);
 315             }
 316             assertEquals(success, true, "weakCompareAndSetAcquire $type$");
 317             $type$ x = UNSAFE.get$Type$(base, offset);
 318             assertEquals(x, $value1$, "weakCompareAndSetAcquire $type$");
 319         }
 320 
 321         {
 322             boolean success = false;
 323             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 324                 success = UNSAFE.weakCompareAndSet$Type$Release(base, offset, $value1$, $value2$);
 325             }
 326             assertEquals(success, true, "weakCompareAndSetRelease $type$");
 327             $type$ x = UNSAFE.get$Type$(base, offset);
 328             assertEquals(x, $value2$, "weakCompareAndSetRelease $type$");
 329         }
 330 
 331         {
 332             boolean success = false;
 333             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 334                 success = UNSAFE.weakCompareAndSet$Type$(base, offset, $value2$, $value1$);
 335             }
 336             assertEquals(success, true, "weakCompareAndSet $type$");
 337             $type$ x = UNSAFE.get$Type$(base, offset);
 338             assertEquals(x, $value1$, "weakCompareAndSet $type$");
 339         }
 340 
 341 #end[JdkInternalMisc]
 342         UNSAFE.put$Type$(base, offset, $value2$);
 343 
 344         // Compare set and get
 345         {
 346             $type$ o = UNSAFE.getAndSet$Type$(base, offset, $value1$);
 347             assertEquals(o, $value2$, "getAndSet $type$");
 348             $type$ x = UNSAFE.get$Type$(base, offset);
 349             assertEquals(x, $value1$, "getAndSet $type$ value");
 350         }
 351 #end[CAS]
 352 
 353 #if[AtomicAdd]
 354         UNSAFE.put$Type$(base, offset, $value1$);
 355 
 356         // get and add, add and get
 357         {
 358             $type$ o = UNSAFE.getAndAdd$Type$(base, offset, $value2$);
 359             assertEquals(o, $value1$, "getAndAdd $type$");
 360             $type$ x = UNSAFE.get$Type$(base, offset);
 361             assertEquals(x, ($type$)($value1$ + $value2$), "getAndAdd $type$");
 362         }
 363 #end[AtomicAdd]
 364     }
 365 
 366 #if[!Object]
 367 #if[!boolean]
 368     static void testAccess(long address) {
 369         // Plain
 370         {
 371             UNSAFE.put$Type$(address, $value1$);
 372             $type$ x = UNSAFE.get$Type$(address);
 373             assertEquals(x, $value1$, "set $type$ value");
 374         }
 375     }
 376 #end[!boolean]
 377 #end[!Object]
 378 }

< prev index next >