annotate hotspot/test/compiler/unsafe/X-UnsafeAccessTest.java.template @ 45055:1c7bbc426b1e

8159995: Rename internal Unsafe.compare methods Reviewed-by: psandoz, dholmes Contributed-by: ron.pressler@oracle.com
author psandoz
date Thu, 11 May 2017 12:55:56 -0700
parents c2304140ed64
children
rev   line source
psandoz@35103 1 /*
psandoz@45055 2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
psandoz@35103 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
psandoz@35103 4 *
psandoz@35103 5 * This code is free software; you can redistribute it and/or modify it
psandoz@35103 6 * under the terms of the GNU General Public License version 2 only, as
psandoz@35103 7 * published by the Free Software Foundation.
psandoz@35103 8 *
psandoz@35103 9 * This code is distributed in the hope that it will be useful, but WITHOUT
psandoz@35103 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
psandoz@35103 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
psandoz@35103 12 * version 2 for more details (a copy is included in the LICENSE file that
psandoz@35103 13 * accompanied this code).
psandoz@35103 14 *
psandoz@35103 15 * You should have received a copy of the GNU General Public License version
psandoz@35103 16 * 2 along with this work; if not, write to the Free Software Foundation,
psandoz@35103 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
psandoz@35103 18 *
psandoz@35103 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
psandoz@35103 20 * or visit www.oracle.com if you need additional information or have any
psandoz@35103 21 * questions.
psandoz@35103 22 */
psandoz@35103 23
psandoz@35103 24 /*
psandoz@35103 25 * @test
psandoz@35103 26 * @bug 8143628
psandoz@35103 27 * @summary Test unsafe access for $type$
tpivovarova@40059 28 *
psandoz@45055 29 #if[JdkInternalMisc]
psandoz@45055 30 * @modules $module$/$package$:+open
psandoz@45055 31 #else[JdkInternalMisc]
chegar@36851 32 * @modules $module$/$package$
psandoz@45055 33 #end[JdkInternalMisc]
tpivovarova@40059 34 * @run testng/othervm -Diters=100 -Xint compiler.unsafe.$Qualifier$UnsafeAccessTest$Type$
tpivovarova@40059 35 * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 compiler.unsafe.$Qualifier$UnsafeAccessTest$Type$
tpivovarova@40059 36 * @run testng/othervm -Diters=20000 -XX:-TieredCompilation compiler.unsafe.$Qualifier$UnsafeAccessTest$Type$
tpivovarova@40059 37 * @run testng/othervm -Diters=20000 compiler.unsafe.$Qualifier$UnsafeAccessTest$Type$
psandoz@35103 38 */
psandoz@35103 39
tpivovarova@40059 40 package compiler.unsafe;
tpivovarova@40059 41
psandoz@35103 42 import org.testng.annotations.Test;
psandoz@35103 43
psandoz@35103 44 import java.lang.reflect.Field;
psandoz@35103 45
psandoz@35103 46 import static org.testng.Assert.*;
psandoz@35103 47
psandoz@35103 48 public class $Qualifier$UnsafeAccessTest$Type$ {
psandoz@35103 49 static final int ITERS = Integer.getInteger("iters", 1);
shade@38234 50 static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
psandoz@35103 51
psandoz@35103 52 static final $package$.Unsafe UNSAFE;
psandoz@35103 53
psandoz@35103 54 static final long V_OFFSET;
psandoz@35103 55
psandoz@35103 56 static final Object STATIC_V_BASE;
psandoz@35103 57
psandoz@35103 58 static final long STATIC_V_OFFSET;
psandoz@35103 59
psandoz@35103 60 static int ARRAY_OFFSET;
psandoz@35103 61
psandoz@35103 62 static int ARRAY_SHIFT;
psandoz@35103 63
psandoz@35103 64 static {
psandoz@35103 65 try {
psandoz@35103 66 Field f = $package$.Unsafe.class.getDeclaredField("theUnsafe");
psandoz@35103 67 f.setAccessible(true);
psandoz@35103 68 UNSAFE = ($package$.Unsafe) f.get(null);
psandoz@35103 69 } catch (Exception e) {
psandoz@35103 70 throw new RuntimeException("Unable to get Unsafe instance.", e);
psandoz@35103 71 }
psandoz@35103 72
psandoz@35103 73 try {
psandoz@35103 74 Field staticVField = $Qualifier$UnsafeAccessTest$Type$.class.getDeclaredField("static_v");
psandoz@35103 75 STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField);
psandoz@35103 76 STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField);
psandoz@35103 77 } catch (Exception e) {
psandoz@35103 78 throw new RuntimeException(e);
psandoz@35103 79 }
psandoz@35103 80
psandoz@35103 81 try {
psandoz@35103 82 Field vField = $Qualifier$UnsafeAccessTest$Type$.class.getDeclaredField("v");
psandoz@35103 83 V_OFFSET = UNSAFE.objectFieldOffset(vField);
psandoz@35103 84 } catch (Exception e) {
psandoz@35103 85 throw new RuntimeException(e);
psandoz@35103 86 }
psandoz@35103 87
psandoz@35103 88 ARRAY_OFFSET = UNSAFE.arrayBaseOffset($type$[].class);
psandoz@35103 89 int ascale = UNSAFE.arrayIndexScale($type$[].class);
psandoz@35103 90 ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
psandoz@35103 91 }
psandoz@35103 92
psandoz@35103 93 static $type$ static_v;
psandoz@35103 94
psandoz@35103 95 $type$ v;
psandoz@35103 96
psandoz@35103 97 @Test
psandoz@35103 98 public void testFieldInstance() {
psandoz@35103 99 $Qualifier$UnsafeAccessTest$Type$ t = new $Qualifier$UnsafeAccessTest$Type$();
psandoz@35103 100 for (int c = 0; c < ITERS; c++) {
psandoz@35103 101 testAccess(t, V_OFFSET);
psandoz@35103 102 }
psandoz@35103 103 }
psandoz@35103 104
psandoz@35103 105 @Test
psandoz@35103 106 public void testFieldStatic() {
psandoz@35103 107 for (int c = 0; c < ITERS; c++) {
psandoz@35103 108 testAccess(STATIC_V_BASE, STATIC_V_OFFSET);
psandoz@35103 109 }
psandoz@35103 110 }
psandoz@35103 111
psandoz@35103 112 @Test
psandoz@35103 113 public void testArray() {
psandoz@35103 114 $type$[] array = new $type$[10];
psandoz@35103 115 for (int c = 0; c < ITERS; c++) {
psandoz@35103 116 for (int i = 0; i < array.length; i++) {
psandoz@35103 117 testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET);
psandoz@35103 118 }
psandoz@35103 119 }
psandoz@35103 120 }
psandoz@35103 121
psandoz@35103 122 #if[!Object]
psandoz@35103 123 #if[!boolean]
psandoz@35103 124 @Test
psandoz@35103 125 public void testArrayOffHeap() {
psandoz@35103 126 int size = 10;
psandoz@35103 127 long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
psandoz@35103 128 try {
psandoz@35103 129 for (int c = 0; c < ITERS; c++) {
psandoz@35103 130 for (int i = 0; i < size; i++) {
psandoz@35103 131 testAccess(null, (((long) i) << ARRAY_SHIFT) + address);
psandoz@35103 132 }
psandoz@35103 133 }
psandoz@35103 134 } finally {
psandoz@35103 135 UNSAFE.freeMemory(address);
psandoz@35103 136 }
psandoz@35103 137 }
psandoz@35103 138
psandoz@35103 139 @Test
psandoz@35103 140 public void testArrayOffHeapDirect() {
psandoz@35103 141 int size = 10;
psandoz@35103 142 long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
psandoz@35103 143 try {
psandoz@35103 144 for (int c = 0; c < ITERS; c++) {
psandoz@35103 145 for (int i = 0; i < size; i++) {
psandoz@35103 146 testAccess((((long) i) << ARRAY_SHIFT) + address);
psandoz@35103 147 }
psandoz@35103 148 }
psandoz@35103 149 } finally {
psandoz@35103 150 UNSAFE.freeMemory(address);
psandoz@35103 151 }
psandoz@35103 152 }
psandoz@35103 153 #end[!boolean]
psandoz@35103 154 #end[!Object]
psandoz@35103 155
psandoz@35103 156 static void testAccess(Object base, long offset) {
psandoz@35103 157 // Plain
psandoz@35103 158 {
psandoz@35103 159 UNSAFE.put$Type$(base, offset, $value1$);
psandoz@35103 160 $type$ x = UNSAFE.get$Type$(base, offset);
psandoz@35103 161 assertEquals(x, $value1$, "set $type$ value");
psandoz@35103 162 }
psandoz@35103 163
psandoz@35103 164 // Volatile
psandoz@35103 165 {
psandoz@35103 166 UNSAFE.put$Type$Volatile(base, offset, $value2$);
psandoz@35103 167 $type$ x = UNSAFE.get$Type$Volatile(base, offset);
psandoz@35103 168 assertEquals(x, $value2$, "putVolatile $type$ value");
psandoz@35103 169 }
psandoz@35103 170
shade@36827 171 #if[!JdkInternalMisc]
psandoz@35103 172 #if[Ordered]
psandoz@35103 173 // Lazy
psandoz@35103 174 {
psandoz@35103 175 UNSAFE.putOrdered$Type$(base, offset, $value1$);
psandoz@35103 176 $type$ x = UNSAFE.get$Type$Volatile(base, offset);
psandoz@35103 177 assertEquals(x, $value1$, "putRelease $type$ value");
psandoz@35103 178 }
psandoz@35103 179 #end[Ordered]
shade@36827 180 #end[!JdkInternalMisc]
psandoz@35103 181
psandoz@35103 182 #if[JdkInternalMisc]
shade@36316 183 // Lazy
shade@36316 184 {
shade@36316 185 UNSAFE.put$Type$Release(base, offset, $value1$);
shade@36316 186 $type$ x = UNSAFE.get$Type$Acquire(base, offset);
shade@36316 187 assertEquals(x, $value1$, "putRelease $type$ value");
shade@36316 188 }
shade@36316 189
shade@36316 190 // Opaque
shade@36316 191 {
shade@36316 192 UNSAFE.put$Type$Opaque(base, offset, $value2$);
shade@36316 193 $type$ x = UNSAFE.get$Type$Opaque(base, offset);
shade@36316 194 assertEquals(x, $value2$, "putOpaque $type$ value");
shade@36316 195 }
shade@36316 196 #end[JdkInternalMisc]
shade@36316 197
shade@36316 198 #if[JdkInternalMisc]
psandoz@35103 199 #if[Unaligned]
psandoz@35103 200 // Unaligned
psandoz@35103 201 {
psandoz@35103 202 UNSAFE.put$Type$Unaligned(base, offset, $value2$);
psandoz@35103 203 $type$ x = UNSAFE.get$Type$Unaligned(base, offset);
psandoz@35103 204 assertEquals(x, $value2$, "putUnaligned $type$ value");
psandoz@35103 205 }
psandoz@35103 206
psandoz@35103 207 {
psandoz@35103 208 UNSAFE.put$Type$Unaligned(base, offset, $value1$, true);
psandoz@35103 209 $type$ x = UNSAFE.get$Type$Unaligned(base, offset, true);
psandoz@35103 210 assertEquals(x, $value1$, "putUnaligned big endian $type$ value");
psandoz@35103 211 }
psandoz@35103 212
psandoz@35103 213 {
psandoz@35103 214 UNSAFE.put$Type$Unaligned(base, offset, $value2$, false);
psandoz@35103 215 $type$ x = UNSAFE.get$Type$Unaligned(base, offset, false);
psandoz@35103 216 assertEquals(x, $value2$, "putUnaligned little endian $type$ value");
psandoz@35103 217 }
psandoz@35103 218 #end[Unaligned]
psandoz@35103 219 #end[JdkInternalMisc]
psandoz@35103 220
psandoz@35103 221 #if[CAS]
psandoz@35103 222 UNSAFE.put$Type$(base, offset, $value1$);
psandoz@35103 223
psandoz@35103 224 // Compare
psandoz@35103 225 {
psandoz@45055 226 #if[JdkInternalMisc]
psandoz@45055 227 boolean r = UNSAFE.compareAndSet$Type$(base, offset, $value1$, $value2$);
psandoz@45055 228 assertEquals(r, true, "success compareAndSet $type$");
psandoz@45055 229 #else[JdkInternalMisc]
psandoz@35103 230 boolean r = UNSAFE.compareAndSwap$Type$(base, offset, $value1$, $value2$);
psandoz@35103 231 assertEquals(r, true, "success compareAndSwap $type$");
psandoz@45055 232 #end[JdkInternalMisc]
psandoz@35103 233 $type$ x = UNSAFE.get$Type$(base, offset);
psandoz@45055 234 #if[JdkInternalMisc]
psandoz@45055 235 assertEquals(x, $value2$, "success compareAndSet $type$ value");
psandoz@45055 236 #else[JdkInternalMisc]
psandoz@35103 237 assertEquals(x, $value2$, "success compareAndSwap $type$ value");
psandoz@45055 238 #end[JdkInternalMisc]
psandoz@35103 239 }
psandoz@35103 240
psandoz@35103 241 {
psandoz@45055 242 #if[JdkInternalMisc]
psandoz@45055 243 boolean r = UNSAFE.compareAndSet$Type$(base, offset, $value1$, $value3$);
psandoz@45055 244 assertEquals(r, false, "failing compareAndSet $type$");
psandoz@45055 245 #else[JdkInternalMisc]
psandoz@35103 246 boolean r = UNSAFE.compareAndSwap$Type$(base, offset, $value1$, $value3$);
psandoz@35103 247 assertEquals(r, false, "failing compareAndSwap $type$");
psandoz@45055 248 #end[JdkInternalMisc]
psandoz@35103 249 $type$ x = UNSAFE.get$Type$(base, offset);
psandoz@45055 250 #if[JdkInternalMisc]
psandoz@45055 251 assertEquals(x, $value2$, "failing compareAndSet $type$ value");
psandoz@45055 252 #else[JdkInternalMisc]
psandoz@35103 253 assertEquals(x, $value2$, "failing compareAndSwap $type$ value");
psandoz@45055 254 #end[JdkInternalMisc]
psandoz@35103 255 }
psandoz@35103 256
shade@36316 257 #if[JdkInternalMisc]
shade@36316 258 // Advanced compare
shade@36316 259 {
psandoz@45055 260 $type$ r = UNSAFE.compareAndExchange$Type$(base, offset, $value2$, $value1$);
psandoz@45055 261 assertEquals(r, $value2$, "success compareAndExchange $type$");
shade@36316 262 $type$ x = UNSAFE.get$Type$(base, offset);
psandoz@45055 263 assertEquals(x, $value1$, "success compareAndExchange $type$ value");
shade@36316 264 }
shade@36316 265
shade@36316 266 {
psandoz@45055 267 $type$ r = UNSAFE.compareAndExchange$Type$(base, offset, $value2$, $value3$);
psandoz@45055 268 assertEquals(r, $value1$, "failing compareAndExchange $type$");
shade@36316 269 $type$ x = UNSAFE.get$Type$(base, offset);
psandoz@45055 270 assertEquals(x, $value1$, "failing compareAndExchange $type$ value");
shade@36316 271 }
shade@36316 272
shade@36316 273 {
shade@36316 274 $type$ r = UNSAFE.compareAndExchange$Type$Acquire(base, offset, $value1$, $value2$);
shade@36316 275 assertEquals(r, $value1$, "success compareAndExchangeAcquire $type$");
shade@36316 276 $type$ x = UNSAFE.get$Type$(base, offset);
shade@36316 277 assertEquals(x, $value2$, "success compareAndExchangeAcquire $type$ value");
shade@36316 278 }
shade@36316 279
shade@36316 280 {
shade@36316 281 $type$ r = UNSAFE.compareAndExchange$Type$Acquire(base, offset, $value1$, $value3$);
shade@36316 282 assertEquals(r, $value2$, "failing compareAndExchangeAcquire $type$");
shade@36316 283 $type$ x = UNSAFE.get$Type$(base, offset);
shade@36316 284 assertEquals(x, $value2$, "failing compareAndExchangeAcquire $type$ value");
shade@36316 285 }
shade@36316 286
shade@36316 287 {
shade@36316 288 $type$ r = UNSAFE.compareAndExchange$Type$Release(base, offset, $value2$, $value1$);
shade@36316 289 assertEquals(r, $value2$, "success compareAndExchangeRelease $type$");
shade@36316 290 $type$ x = UNSAFE.get$Type$(base, offset);
shade@36316 291 assertEquals(x, $value1$, "success compareAndExchangeRelease $type$ value");
shade@36316 292 }
shade@36316 293
shade@36316 294 {
shade@36316 295 $type$ r = UNSAFE.compareAndExchange$Type$Release(base, offset, $value2$, $value3$);
shade@36316 296 assertEquals(r, $value1$, "failing compareAndExchangeRelease $type$");
shade@36316 297 $type$ x = UNSAFE.get$Type$(base, offset);
shade@36316 298 assertEquals(x, $value1$, "failing compareAndExchangeRelease $type$ value");
shade@36316 299 }
shade@36316 300
shade@36316 301 {
shade@38234 302 boolean success = false;
shade@38234 303 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
psandoz@45055 304 success = UNSAFE.weakCompareAndSet$Type$Plain(base, offset, $value1$, $value2$);
shade@38234 305 }
psandoz@45055 306 assertEquals(success, true, "weakCompareAndSetPlain $type$");
shade@36316 307 $type$ x = UNSAFE.get$Type$(base, offset);
psandoz@45055 308 assertEquals(x, $value2$, "weakCompareAndSetPlain $type$ value");
shade@36316 309 }
shade@36316 310
shade@36316 311 {
shade@38234 312 boolean success = false;
shade@38234 313 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
psandoz@45055 314 success = UNSAFE.weakCompareAndSet$Type$Acquire(base, offset, $value2$, $value1$);
shade@38234 315 }
psandoz@45055 316 assertEquals(success, true, "weakCompareAndSetAcquire $type$");
shade@36316 317 $type$ x = UNSAFE.get$Type$(base, offset);
psandoz@45055 318 assertEquals(x, $value1$, "weakCompareAndSetAcquire $type$");
shade@36316 319 }
shade@36316 320
shade@36316 321 {
shade@38234 322 boolean success = false;
shade@38234 323 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
psandoz@45055 324 success = UNSAFE.weakCompareAndSet$Type$Release(base, offset, $value1$, $value2$);
shade@38234 325 }
psandoz@45055 326 assertEquals(success, true, "weakCompareAndSetRelease $type$");
shade@36316 327 $type$ x = UNSAFE.get$Type$(base, offset);
psandoz@45055 328 assertEquals(x, $value2$, "weakCompareAndSetRelease $type$");
shade@36316 329 }
shade@38281 330
shade@38281 331 {
shade@38281 332 boolean success = false;
shade@38281 333 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
psandoz@45055 334 success = UNSAFE.weakCompareAndSet$Type$(base, offset, $value2$, $value1$);
shade@38281 335 }
psandoz@45055 336 assertEquals(success, true, "weakCompareAndSet $type$");
shade@38281 337 $type$ x = UNSAFE.get$Type$(base, offset);
psandoz@45055 338 assertEquals(x, $value1$, "weakCompareAndSet $type$");
shade@38281 339 }
shade@38281 340
shade@36316 341 #end[JdkInternalMisc]
shade@38281 342 UNSAFE.put$Type$(base, offset, $value2$);
shade@36316 343
psandoz@35103 344 // Compare set and get
psandoz@35103 345 {
psandoz@35103 346 $type$ o = UNSAFE.getAndSet$Type$(base, offset, $value1$);
psandoz@35103 347 assertEquals(o, $value2$, "getAndSet $type$");
psandoz@35103 348 $type$ x = UNSAFE.get$Type$(base, offset);
psandoz@35103 349 assertEquals(x, $value1$, "getAndSet $type$ value");
psandoz@35103 350 }
psandoz@35103 351 #end[CAS]
psandoz@35103 352
psandoz@35103 353 #if[AtomicAdd]
psandoz@35103 354 UNSAFE.put$Type$(base, offset, $value1$);
psandoz@35103 355
psandoz@35103 356 // get and add, add and get
psandoz@35103 357 {
psandoz@35103 358 $type$ o = UNSAFE.getAndAdd$Type$(base, offset, $value2$);
psandoz@35103 359 assertEquals(o, $value1$, "getAndAdd $type$");
psandoz@35103 360 $type$ x = UNSAFE.get$Type$(base, offset);
shade@39419 361 assertEquals(x, ($type$)($value1$ + $value2$), "getAndAdd $type$");
psandoz@35103 362 }
psandoz@35103 363 #end[AtomicAdd]
psandoz@35103 364 }
psandoz@35103 365
psandoz@35103 366 #if[!Object]
psandoz@35103 367 #if[!boolean]
psandoz@35103 368 static void testAccess(long address) {
psandoz@35103 369 // Plain
psandoz@35103 370 {
psandoz@35103 371 UNSAFE.put$Type$(address, $value1$);
psandoz@35103 372 $type$ x = UNSAFE.get$Type$(address);
psandoz@35103 373 assertEquals(x, $value1$, "set $type$ value");
psandoz@35103 374 }
psandoz@35103 375 }
psandoz@35103 376 #end[!boolean]
psandoz@35103 377 #end[!Object]
shade@36316 378 }