23
24 /*
25 * @test
26 * @bug 8143628
27 * @summary Test unsafe access for long
28 * @modules jdk.unsupported/sun.misc
29 * @run testng/othervm -Diters=100 -Xint SunMiscUnsafeAccessTestLong
30 * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 SunMiscUnsafeAccessTestLong
31 * @run testng/othervm -Diters=20000 -XX:-TieredCompilation SunMiscUnsafeAccessTestLong
32 * @run testng/othervm -Diters=20000 SunMiscUnsafeAccessTestLong
33 */
34
35 import org.testng.annotations.Test;
36
37 import java.lang.reflect.Field;
38
39 import static org.testng.Assert.*;
40
41 public class SunMiscUnsafeAccessTestLong {
42 static final int ITERS = Integer.getInteger("iters", 1);
43
44 static final sun.misc.Unsafe UNSAFE;
45
46 static final long V_OFFSET;
47
48 static final Object STATIC_V_BASE;
49
50 static final long STATIC_V_OFFSET;
51
52 static int ARRAY_OFFSET;
53
54 static int ARRAY_SHIFT;
55
56 static {
57 try {
58 Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
59 f.setAccessible(true);
60 UNSAFE = (sun.misc.Unsafe) f.get(null);
61 } catch (Exception e) {
62 throw new RuntimeException("Unable to get Unsafe instance.", e);
148 long x = UNSAFE.getLong(base, offset);
149 assertEquals(x, 1L, "set long value");
150 }
151
152 // Volatile
153 {
154 UNSAFE.putLongVolatile(base, offset, 2L);
155 long x = UNSAFE.getLongVolatile(base, offset);
156 assertEquals(x, 2L, "putVolatile long value");
157 }
158
159 // Lazy
160 {
161 UNSAFE.putOrderedLong(base, offset, 1L);
162 long x = UNSAFE.getLongVolatile(base, offset);
163 assertEquals(x, 1L, "putRelease long value");
164 }
165
166
167
168
169 UNSAFE.putLong(base, offset, 1L);
170
171 // Compare
172 {
173 boolean r = UNSAFE.compareAndSwapLong(base, offset, 1L, 2L);
174 assertEquals(r, true, "success compareAndSwap long");
175 long x = UNSAFE.getLong(base, offset);
176 assertEquals(x, 2L, "success compareAndSwap long value");
177 }
178
179 {
180 boolean r = UNSAFE.compareAndSwapLong(base, offset, 1L, 3L);
181 assertEquals(r, false, "failing compareAndSwap long");
182 long x = UNSAFE.getLong(base, offset);
183 assertEquals(x, 2L, "failing compareAndSwap long value");
184 }
185
186
187
188 // Compare set and get
189 {
190 long o = UNSAFE.getAndSetLong(base, offset, 1L);
191 assertEquals(o, 2L, "getAndSet long");
192 long x = UNSAFE.getLong(base, offset);
193 assertEquals(x, 1L, "getAndSet long value");
194 }
195
196 UNSAFE.putLong(base, offset, 1L);
197
198 // get and add, add and get
199 {
200 long o = UNSAFE.getAndAddLong(base, offset, 2L);
201 assertEquals(o, 1L, "getAndAdd long");
202 long x = UNSAFE.getLong(base, offset);
203 assertEquals(x, 1L + 2L, "weakCompareAndSwapRelease long");
204 }
205 }
206
207 static void testAccess(long address) {
208 // Plain
209 {
210 UNSAFE.putLong(address, 1L);
211 long x = UNSAFE.getLong(address);
212 assertEquals(x, 1L, "set long value");
213 }
214 }
215 }
216
217
|
23
24 /*
25 * @test
26 * @bug 8143628
27 * @summary Test unsafe access for long
28 * @modules jdk.unsupported/sun.misc
29 * @run testng/othervm -Diters=100 -Xint SunMiscUnsafeAccessTestLong
30 * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 SunMiscUnsafeAccessTestLong
31 * @run testng/othervm -Diters=20000 -XX:-TieredCompilation SunMiscUnsafeAccessTestLong
32 * @run testng/othervm -Diters=20000 SunMiscUnsafeAccessTestLong
33 */
34
35 import org.testng.annotations.Test;
36
37 import java.lang.reflect.Field;
38
39 import static org.testng.Assert.*;
40
41 public class SunMiscUnsafeAccessTestLong {
42 static final int ITERS = Integer.getInteger("iters", 1);
43 static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
44
45 static final sun.misc.Unsafe UNSAFE;
46
47 static final long V_OFFSET;
48
49 static final Object STATIC_V_BASE;
50
51 static final long STATIC_V_OFFSET;
52
53 static int ARRAY_OFFSET;
54
55 static int ARRAY_SHIFT;
56
57 static {
58 try {
59 Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
60 f.setAccessible(true);
61 UNSAFE = (sun.misc.Unsafe) f.get(null);
62 } catch (Exception e) {
63 throw new RuntimeException("Unable to get Unsafe instance.", e);
149 long x = UNSAFE.getLong(base, offset);
150 assertEquals(x, 1L, "set long value");
151 }
152
153 // Volatile
154 {
155 UNSAFE.putLongVolatile(base, offset, 2L);
156 long x = UNSAFE.getLongVolatile(base, offset);
157 assertEquals(x, 2L, "putVolatile long value");
158 }
159
160 // Lazy
161 {
162 UNSAFE.putOrderedLong(base, offset, 1L);
163 long x = UNSAFE.getLongVolatile(base, offset);
164 assertEquals(x, 1L, "putRelease long value");
165 }
166
167
168
169 UNSAFE.putLong(base, offset, 1L);
170
171 // Compare
172 {
173 boolean r = UNSAFE.compareAndSwapLong(base, offset, 1L, 2L);
174 assertEquals(r, true, "success compareAndSwap long");
175 long x = UNSAFE.getLong(base, offset);
176 assertEquals(x, 2L, "success compareAndSwap long value");
177 }
178
179 {
180 boolean r = UNSAFE.compareAndSwapLong(base, offset, 1L, 3L);
181 assertEquals(r, false, "failing compareAndSwap long");
182 long x = UNSAFE.getLong(base, offset);
183 assertEquals(x, 2L, "failing compareAndSwap long value");
184 }
185
186
187 // Compare set and get
188 {
189 long o = UNSAFE.getAndSetLong(base, offset, 1L);
190 assertEquals(o, 2L, "getAndSet long");
191 long x = UNSAFE.getLong(base, offset);
192 assertEquals(x, 1L, "getAndSet long value");
193 }
194
195 UNSAFE.putLong(base, offset, 1L);
196
197 // get and add, add and get
198 {
199 long o = UNSAFE.getAndAddLong(base, offset, 2L);
200 assertEquals(o, 1L, "getAndAdd long");
201 long x = UNSAFE.getLong(base, offset);
202 assertEquals(x, 1L + 2L, "getAndAdd long");
203 }
204 }
205
206 static void testAccess(long address) {
207 // Plain
208 {
209 UNSAFE.putLong(address, 1L);
210 long x = UNSAFE.getLong(address);
211 assertEquals(x, 1L, "set long value");
212 }
213 }
214 }
215
|