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 */
139 @Test
140 public void testArrayOffHeapDirect() {
141 int size = 10;
142 long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
143 try {
144 for (int c = 0; c < ITERS; c++) {
145 for (int i = 0; i < size; i++) {
146 testAccess((((long) i) << ARRAY_SHIFT) + address);
147 }
148 }
149 } finally {
150 UNSAFE.freeMemory(address);
151 }
152 }
153 #end[!boolean]
154 #end[!Object]
155
156 static void testAccess(Object base, long offset) {
157 // Plain
158 {
159 UNSAFE.put$Type$(base, offset, $value1$);
160 $type$ x = UNSAFE.get$Type$(base, offset);
161 assertEquals(x, $value1$, "set $type$ value");
162 }
163
164 // Volatile
165 {
166 UNSAFE.put$Type$Volatile(base, offset, $value2$);
167 $type$ x = UNSAFE.get$Type$Volatile(base, offset);
168 assertEquals(x, $value2$, "putVolatile $type$ value");
169 }
170
171 #if[!JdkInternalMisc]
172 #if[Ordered]
173 // Lazy
174 {
175 UNSAFE.putOrdered$Type$(base, offset, $value1$);
176 $type$ x = UNSAFE.get$Type$Volatile(base, offset);
177 assertEquals(x, $value1$, "putRelease $type$ value");
178 }
179 #end[Ordered]
180 #end[!JdkInternalMisc]
181
182 #if[JdkInternalMisc]
183 // Lazy
184 {
185 UNSAFE.put$Type$Release(base, offset, $value1$);
186 $type$ x = UNSAFE.get$Type$Acquire(base, offset);
187 assertEquals(x, $value1$, "putRelease $type$ value");
188 }
189
190 // Opaque
191 {
192 UNSAFE.put$Type$Opaque(base, offset, $value2$);
193 $type$ x = UNSAFE.get$Type$Opaque(base, offset);
194 assertEquals(x, $value2$, "putOpaque $type$ value");
195 }
196 #end[JdkInternalMisc]
197
198 #if[JdkInternalMisc]
199 #if[Unaligned]
200 // Unaligned
201 {
202 UNSAFE.put$Type$Unaligned(base, offset, $value2$);
203 $type$ x = UNSAFE.get$Type$Unaligned(base, offset);
204 assertEquals(x, $value2$, "putUnaligned $type$ value");
205 }
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 }
|
1 /*
2 * Copyright (c) 2015, 2019, 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 */
139 @Test
140 public void testArrayOffHeapDirect() {
141 int size = 10;
142 long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
143 try {
144 for (int c = 0; c < ITERS; c++) {
145 for (int i = 0; i < size; i++) {
146 testAccess((((long) i) << ARRAY_SHIFT) + address);
147 }
148 }
149 } finally {
150 UNSAFE.freeMemory(address);
151 }
152 }
153 #end[!boolean]
154 #end[!Object]
155
156 static void testAccess(Object base, long offset) {
157 // Plain
158 {
159 UNSAFE.put$MethodAffix$(base, offset, $value1$);
160 $type$ x = UNSAFE.get$MethodAffix$(base, offset);
161 assertEquals(x, $value1$, "set $type$ value");
162 }
163
164 // Volatile
165 {
166 UNSAFE.put$MethodAffix$Volatile(base, offset, $value2$);
167 $type$ x = UNSAFE.get$MethodAffix$Volatile(base, offset);
168 assertEquals(x, $value2$, "putVolatile $type$ value");
169 }
170
171 #if[!JdkInternalMisc]
172 #if[Ordered]
173 // Lazy
174 {
175 UNSAFE.putOrdered$MethodAffix$(base, offset, $value1$);
176 $type$ x = UNSAFE.get$MethodAffix$Volatile(base, offset);
177 assertEquals(x, $value1$, "putRelease $type$ value");
178 }
179 #end[Ordered]
180 #end[!JdkInternalMisc]
181
182 #if[JdkInternalMisc]
183 // Lazy
184 {
185 UNSAFE.put$MethodAffix$Release(base, offset, $value1$);
186 $type$ x = UNSAFE.get$MethodAffix$Acquire(base, offset);
187 assertEquals(x, $value1$, "putRelease $type$ value");
188 }
189
190 // Opaque
191 {
192 UNSAFE.put$MethodAffix$Opaque(base, offset, $value2$);
193 $type$ x = UNSAFE.get$MethodAffix$Opaque(base, offset);
194 assertEquals(x, $value2$, "putOpaque $type$ value");
195 }
196 #end[JdkInternalMisc]
197
198 #if[JdkInternalMisc]
199 #if[Unaligned]
200 // Unaligned
201 {
202 UNSAFE.put$MethodAffix$Unaligned(base, offset, $value2$);
203 $type$ x = UNSAFE.get$MethodAffix$Unaligned(base, offset);
204 assertEquals(x, $value2$, "putUnaligned $type$ value");
205 }
206
207 {
208 UNSAFE.put$MethodAffix$Unaligned(base, offset, $value1$, true);
209 $type$ x = UNSAFE.get$MethodAffix$Unaligned(base, offset, true);
210 assertEquals(x, $value1$, "putUnaligned big endian $type$ value");
211 }
212
213 {
214 UNSAFE.put$MethodAffix$Unaligned(base, offset, $value2$, false);
215 $type$ x = UNSAFE.get$MethodAffix$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$MethodAffix$(base, offset, $value1$);
223
224 // Compare
225 {
226 #if[JdkInternalMisc]
227 boolean r = UNSAFE.compareAndSet$MethodAffix$(base, offset, $value1$, $value2$);
228 assertEquals(r, true, "success compareAndSet $type$");
229 #else[JdkInternalMisc]
230 boolean r = UNSAFE.compareAndSwap$MethodAffix$(base, offset, $value1$, $value2$);
231 assertEquals(r, true, "success compareAndSwap $type$");
232 #end[JdkInternalMisc]
233 $type$ x = UNSAFE.get$MethodAffix$(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$MethodAffix$(base, offset, $value1$, $value3$);
244 assertEquals(r, false, "failing compareAndSet $type$");
245 #else[JdkInternalMisc]
246 boolean r = UNSAFE.compareAndSwap$MethodAffix$(base, offset, $value1$, $value3$);
247 assertEquals(r, false, "failing compareAndSwap $type$");
248 #end[JdkInternalMisc]
249 $type$ x = UNSAFE.get$MethodAffix$(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$MethodAffix$(base, offset, $value2$, $value1$);
261 assertEquals(r, $value2$, "success compareAndExchange $type$");
262 $type$ x = UNSAFE.get$MethodAffix$(base, offset);
263 assertEquals(x, $value1$, "success compareAndExchange $type$ value");
264 }
265
266 {
267 $type$ r = UNSAFE.compareAndExchange$MethodAffix$(base, offset, $value2$, $value3$);
268 assertEquals(r, $value1$, "failing compareAndExchange $type$");
269 $type$ x = UNSAFE.get$MethodAffix$(base, offset);
270 assertEquals(x, $value1$, "failing compareAndExchange $type$ value");
271 }
272
273 {
274 $type$ r = UNSAFE.compareAndExchange$MethodAffix$Acquire(base, offset, $value1$, $value2$);
275 assertEquals(r, $value1$, "success compareAndExchangeAcquire $type$");
276 $type$ x = UNSAFE.get$MethodAffix$(base, offset);
277 assertEquals(x, $value2$, "success compareAndExchangeAcquire $type$ value");
278 }
279
280 {
281 $type$ r = UNSAFE.compareAndExchange$MethodAffix$Acquire(base, offset, $value1$, $value3$);
282 assertEquals(r, $value2$, "failing compareAndExchangeAcquire $type$");
283 $type$ x = UNSAFE.get$MethodAffix$(base, offset);
284 assertEquals(x, $value2$, "failing compareAndExchangeAcquire $type$ value");
285 }
286
287 {
288 $type$ r = UNSAFE.compareAndExchange$MethodAffix$Release(base, offset, $value2$, $value1$);
289 assertEquals(r, $value2$, "success compareAndExchangeRelease $type$");
290 $type$ x = UNSAFE.get$MethodAffix$(base, offset);
291 assertEquals(x, $value1$, "success compareAndExchangeRelease $type$ value");
292 }
293
294 {
295 $type$ r = UNSAFE.compareAndExchange$MethodAffix$Release(base, offset, $value2$, $value3$);
296 assertEquals(r, $value1$, "failing compareAndExchangeRelease $type$");
297 $type$ x = UNSAFE.get$MethodAffix$(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$MethodAffix$Plain(base, offset, $value1$, $value2$);
305 }
306 assertEquals(success, true, "weakCompareAndSetPlain $type$");
307 $type$ x = UNSAFE.get$MethodAffix$(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$MethodAffix$Acquire(base, offset, $value2$, $value1$);
315 }
316 assertEquals(success, true, "weakCompareAndSetAcquire $type$");
317 $type$ x = UNSAFE.get$MethodAffix$(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$MethodAffix$Release(base, offset, $value1$, $value2$);
325 }
326 assertEquals(success, true, "weakCompareAndSetRelease $type$");
327 $type$ x = UNSAFE.get$MethodAffix$(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$MethodAffix$(base, offset, $value2$, $value1$);
335 }
336 assertEquals(success, true, "weakCompareAndSet $type$");
337 $type$ x = UNSAFE.get$MethodAffix$(base, offset);
338 assertEquals(x, $value1$, "weakCompareAndSet $type$");
339 }
340
341 #end[JdkInternalMisc]
342 UNSAFE.put$MethodAffix$(base, offset, $value2$);
343
344 // Compare set and get
345 {
346 $type$ o = UNSAFE.getAndSet$MethodAffix$(base, offset, $value1$);
347 assertEquals(o, $value2$, "getAndSet $type$");
348 $type$ x = UNSAFE.get$MethodAffix$(base, offset);
349 assertEquals(x, $value1$, "getAndSet $type$ value");
350 }
351 #end[CAS]
352
353 #if[AtomicAdd]
354 UNSAFE.put$MethodAffix$(base, offset, $value1$);
355
356 // get and add, add and get
357 {
358 $type$ o = UNSAFE.getAndAdd$MethodAffix$(base, offset, $value2$);
359 assertEquals(o, $value1$, "getAndAdd $type$");
360 $type$ x = UNSAFE.get$MethodAffix$(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$MethodAffix$(address, $value1$);
372 $type$ x = UNSAFE.get$MethodAffix$(address);
373 assertEquals(x, $value1$, "set $type$ value");
374 }
375 }
376 #end[!boolean]
377 #end[!Object]
378 }
|