196 |
196 |
197 UNSAFE.putInt(base, offset, 0x01234567); |
197 UNSAFE.putInt(base, offset, 0x01234567); |
198 |
198 |
199 // Compare |
199 // Compare |
200 { |
200 { |
201 boolean r = UNSAFE.compareAndSwapInt(base, offset, 0x01234567, 0x89ABCDEF); |
201 boolean r = UNSAFE.compareAndSetInt(base, offset, 0x01234567, 0x89ABCDEF); |
202 assertEquals(r, true, "success compareAndSwap int"); |
202 assertEquals(r, true, "success compareAndSet int"); |
203 int x = UNSAFE.getInt(base, offset); |
203 int x = UNSAFE.getInt(base, offset); |
204 assertEquals(x, 0x89ABCDEF, "success compareAndSwap int value"); |
204 assertEquals(x, 0x89ABCDEF, "success compareAndSet int value"); |
205 } |
205 } |
206 |
206 |
207 { |
207 { |
208 boolean r = UNSAFE.compareAndSwapInt(base, offset, 0x01234567, 0xCAFEBABE); |
208 boolean r = UNSAFE.compareAndSetInt(base, offset, 0x01234567, 0xCAFEBABE); |
209 assertEquals(r, false, "failing compareAndSwap int"); |
209 assertEquals(r, false, "failing compareAndSet int"); |
210 int x = UNSAFE.getInt(base, offset); |
210 int x = UNSAFE.getInt(base, offset); |
211 assertEquals(x, 0x89ABCDEF, "failing compareAndSwap int value"); |
211 assertEquals(x, 0x89ABCDEF, "failing compareAndSet int value"); |
212 } |
212 } |
213 |
213 |
214 // Advanced compare |
214 // Advanced compare |
215 { |
215 { |
216 int r = UNSAFE.compareAndExchangeIntVolatile(base, offset, 0x89ABCDEF, 0x01234567); |
216 int r = UNSAFE.compareAndExchangeInt(base, offset, 0x89ABCDEF, 0x01234567); |
217 assertEquals(r, 0x89ABCDEF, "success compareAndExchangeVolatile int"); |
217 assertEquals(r, 0x89ABCDEF, "success compareAndExchange int"); |
218 int x = UNSAFE.getInt(base, offset); |
218 int x = UNSAFE.getInt(base, offset); |
219 assertEquals(x, 0x01234567, "success compareAndExchangeVolatile int value"); |
219 assertEquals(x, 0x01234567, "success compareAndExchange int value"); |
220 } |
220 } |
221 |
221 |
222 { |
222 { |
223 int r = UNSAFE.compareAndExchangeIntVolatile(base, offset, 0x89ABCDEF, 0xCAFEBABE); |
223 int r = UNSAFE.compareAndExchangeInt(base, offset, 0x89ABCDEF, 0xCAFEBABE); |
224 assertEquals(r, 0x01234567, "failing compareAndExchangeVolatile int"); |
224 assertEquals(r, 0x01234567, "failing compareAndExchange int"); |
225 int x = UNSAFE.getInt(base, offset); |
225 int x = UNSAFE.getInt(base, offset); |
226 assertEquals(x, 0x01234567, "failing compareAndExchangeVolatile int value"); |
226 assertEquals(x, 0x01234567, "failing compareAndExchange int value"); |
227 } |
227 } |
228 |
228 |
229 { |
229 { |
230 int r = UNSAFE.compareAndExchangeIntAcquire(base, offset, 0x01234567, 0x89ABCDEF); |
230 int r = UNSAFE.compareAndExchangeIntAcquire(base, offset, 0x01234567, 0x89ABCDEF); |
231 assertEquals(r, 0x01234567, "success compareAndExchangeAcquire int"); |
231 assertEquals(r, 0x01234567, "success compareAndExchangeAcquire int"); |
255 } |
255 } |
256 |
256 |
257 { |
257 { |
258 boolean success = false; |
258 boolean success = false; |
259 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
259 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
260 success = UNSAFE.weakCompareAndSwapInt(base, offset, 0x01234567, 0x89ABCDEF); |
260 success = UNSAFE.weakCompareAndSetIntPlain(base, offset, 0x01234567, 0x89ABCDEF); |
261 } |
261 } |
262 assertEquals(success, true, "weakCompareAndSwap int"); |
262 assertEquals(success, true, "weakCompareAndSetPlain int"); |
263 int x = UNSAFE.getInt(base, offset); |
263 int x = UNSAFE.getInt(base, offset); |
264 assertEquals(x, 0x89ABCDEF, "weakCompareAndSwap int value"); |
264 assertEquals(x, 0x89ABCDEF, "weakCompareAndSetPlain int value"); |
265 } |
265 } |
266 |
266 |
267 { |
267 { |
268 boolean success = false; |
268 boolean success = false; |
269 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
269 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
270 success = UNSAFE.weakCompareAndSwapIntAcquire(base, offset, 0x89ABCDEF, 0x01234567); |
270 success = UNSAFE.weakCompareAndSetIntAcquire(base, offset, 0x89ABCDEF, 0x01234567); |
271 } |
271 } |
272 assertEquals(success, true, "weakCompareAndSwapAcquire int"); |
272 assertEquals(success, true, "weakCompareAndSetAcquire int"); |
273 int x = UNSAFE.getInt(base, offset); |
273 int x = UNSAFE.getInt(base, offset); |
274 assertEquals(x, 0x01234567, "weakCompareAndSwapAcquire int"); |
274 assertEquals(x, 0x01234567, "weakCompareAndSetAcquire int"); |
275 } |
275 } |
276 |
276 |
277 { |
277 { |
278 boolean success = false; |
278 boolean success = false; |
279 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
279 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
280 success = UNSAFE.weakCompareAndSwapIntRelease(base, offset, 0x01234567, 0x89ABCDEF); |
280 success = UNSAFE.weakCompareAndSetIntRelease(base, offset, 0x01234567, 0x89ABCDEF); |
281 } |
281 } |
282 assertEquals(success, true, "weakCompareAndSwapRelease int"); |
282 assertEquals(success, true, "weakCompareAndSetRelease int"); |
283 int x = UNSAFE.getInt(base, offset); |
283 int x = UNSAFE.getInt(base, offset); |
284 assertEquals(x, 0x89ABCDEF, "weakCompareAndSwapRelease int"); |
284 assertEquals(x, 0x89ABCDEF, "weakCompareAndSetRelease int"); |
285 } |
285 } |
286 |
286 |
287 { |
287 { |
288 boolean success = false; |
288 boolean success = false; |
289 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
289 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
290 success = UNSAFE.weakCompareAndSwapIntVolatile(base, offset, 0x89ABCDEF, 0x01234567); |
290 success = UNSAFE.weakCompareAndSetInt(base, offset, 0x89ABCDEF, 0x01234567); |
291 } |
291 } |
292 assertEquals(success, true, "weakCompareAndSwapVolatile int"); |
292 assertEquals(success, true, "weakCompareAndSet int"); |
293 int x = UNSAFE.getInt(base, offset); |
293 int x = UNSAFE.getInt(base, offset); |
294 assertEquals(x, 0x01234567, "weakCompareAndSwapVolatile int"); |
294 assertEquals(x, 0x01234567, "weakCompareAndSet int"); |
295 } |
295 } |
296 |
296 |
297 UNSAFE.putInt(base, offset, 0x89ABCDEF); |
297 UNSAFE.putInt(base, offset, 0x89ABCDEF); |
298 |
298 |
299 // Compare set and get |
299 // Compare set and get |