44 import java.util.function.IntBinaryOperator; |
44 import java.util.function.IntBinaryOperator; |
45 import java.util.function.IntUnaryOperator; |
45 import java.util.function.IntUnaryOperator; |
46 import jdk.internal.misc.Unsafe; |
46 import jdk.internal.misc.Unsafe; |
47 import jdk.internal.reflect.CallerSensitive; |
47 import jdk.internal.reflect.CallerSensitive; |
48 import jdk.internal.reflect.Reflection; |
48 import jdk.internal.reflect.Reflection; |
|
49 import java.lang.invoke.VarHandle; |
49 |
50 |
50 /** |
51 /** |
51 * A reflection-based utility that enables atomic updates to |
52 * A reflection-based utility that enables atomic updates to |
52 * designated {@code volatile int} fields of designated classes. |
53 * designated {@code volatile int} fields of designated classes. |
53 * This class is designed for use in atomic data structures in which |
54 * This class is designed for use in atomic data structures in which |
273 } while (!compareAndSet(obj, prev, next)); |
274 } while (!compareAndSet(obj, prev, next)); |
274 return next; |
275 return next; |
275 } |
276 } |
276 |
277 |
277 /** |
278 /** |
278 * Atomically updates the field of the given object managed by this updater |
279 * Atomically updates (with memory effects as specified by {@link |
279 * with the results of applying the given function, returning the previous |
280 * VarHandle#compareAndSet}) the field of the given object managed |
280 * value. The function should be side-effect-free, since it may be |
281 * by this updater with the results of applying the given |
281 * re-applied when attempted updates fail due to contention among threads. |
282 * function, returning the previous value. The function should be |
|
283 * side-effect-free, since it may be re-applied when attempted |
|
284 * updates fail due to contention among threads. |
282 * |
285 * |
283 * @param obj An object whose field to get and set |
286 * @param obj An object whose field to get and set |
284 * @param updateFunction a side-effect-free function |
287 * @param updateFunction a side-effect-free function |
285 * @return the previous value |
288 * @return the previous value |
286 * @since 1.8 |
289 * @since 1.8 |
293 } while (!compareAndSet(obj, prev, next)); |
296 } while (!compareAndSet(obj, prev, next)); |
294 return prev; |
297 return prev; |
295 } |
298 } |
296 |
299 |
297 /** |
300 /** |
298 * Atomically updates the field of the given object managed by this updater |
301 * Atomically updates (with memory effects as specified by {@link |
299 * with the results of applying the given function, returning the updated |
302 * VarHandle#compareAndSet}) the field of the given object managed |
300 * value. The function should be side-effect-free, since it may be |
303 * by this updater with the results of applying the given |
301 * re-applied when attempted updates fail due to contention among threads. |
304 * function, returning the updated value. The function should be |
|
305 * side-effect-free, since it may be re-applied when attempted |
|
306 * updates fail due to contention among threads. |
302 * |
307 * |
303 * @param obj An object whose field to get and set |
308 * @param obj An object whose field to get and set |
304 * @param updateFunction a side-effect-free function |
309 * @param updateFunction a side-effect-free function |
305 * @return the updated value |
310 * @return the updated value |
306 * @since 1.8 |
311 * @since 1.8 |
313 } while (!compareAndSet(obj, prev, next)); |
318 } while (!compareAndSet(obj, prev, next)); |
314 return next; |
319 return next; |
315 } |
320 } |
316 |
321 |
317 /** |
322 /** |
318 * Atomically updates the field of the given object managed by this |
323 * Atomically updates (with memory effects as specified by {@link |
319 * updater with the results of applying the given function to the |
324 * VarHandle#compareAndSet}) the field of the given object managed |
320 * current and given values, returning the previous value. The |
325 * by this updater with the results of applying the given function |
321 * function should be side-effect-free, since it may be re-applied |
326 * to the current and given values, returning the previous value. |
322 * when attempted updates fail due to contention among threads. The |
327 * The function should be side-effect-free, since it may be |
323 * function is applied with the current value as its first argument, |
328 * re-applied when attempted updates fail due to contention among |
324 * and the given update as the second argument. |
329 * threads. The function is applied with the current value as its |
|
330 * first argument, and the given update as the second argument. |
325 * |
331 * |
326 * @param obj An object whose field to get and set |
332 * @param obj An object whose field to get and set |
327 * @param x the update value |
333 * @param x the update value |
328 * @param accumulatorFunction a side-effect-free function of two arguments |
334 * @param accumulatorFunction a side-effect-free function of two arguments |
329 * @return the previous value |
335 * @return the previous value |
338 } while (!compareAndSet(obj, prev, next)); |
344 } while (!compareAndSet(obj, prev, next)); |
339 return prev; |
345 return prev; |
340 } |
346 } |
341 |
347 |
342 /** |
348 /** |
343 * Atomically updates the field of the given object managed by this |
349 * Atomically updates (with memory effects as specified by {@link |
344 * updater with the results of applying the given function to the |
350 * VarHandle#compareAndSet}) the field of the given object managed |
345 * current and given values, returning the updated value. The |
351 * by this updater with the results of applying the given function |
346 * function should be side-effect-free, since it may be re-applied |
352 * to the current and given values, returning the updated value. |
347 * when attempted updates fail due to contention among threads. The |
353 * The function should be side-effect-free, since it may be |
348 * function is applied with the current value as its first argument, |
354 * re-applied when attempted updates fail due to contention among |
349 * and the given update as the second argument. |
355 * threads. The function is applied with the current value as its |
|
356 * first argument, and the given update as the second argument. |
350 * |
357 * |
351 * @param obj An object whose field to get and set |
358 * @param obj An object whose field to get and set |
352 * @param x the update value |
359 * @param x the update value |
353 * @param accumulatorFunction a side-effect-free function of two arguments |
360 * @param accumulatorFunction a side-effect-free function of two arguments |
354 * @return the updated value |
361 * @return the updated value |