@@ -13,7 +13,7 @@ module AtomicDirectUpdate
13
13
# Pass the current value to the given block, replacing it
14
14
# with the block's result. May retry if the value changes
15
15
# during the block's execution.
16
- #
16
+ #
17
17
# @yield [Object] Calculate a new value for the atomic reference using
18
18
# given (old) value
19
19
# @yieldparam [Object] old_value the starting value of the atomic reference
@@ -27,17 +27,45 @@ def update
27
27
# @!macro [attach] atomic_reference_method_try_update
28
28
#
29
29
# Pass the current value to the given block, replacing it
30
+ # with the block's result. Return nil if the update fails.
31
+ #
32
+ # @yield [Object] Calculate a new value for the atomic reference using
33
+ # given (old) value
34
+ # @yieldparam [Object] old_value the starting value of the atomic reference
35
+ #
36
+ # @note This method was altered to avoid raising an exception by default.
37
+ # Instead, this method now returns `nil` in case of failure. For more info,
38
+ # please see: https://github.com/ruby-concurrency/concurrent-ruby/pull/336
39
+ #
40
+ # @return [Object] the new value, or nil if update failed
41
+ def try_update
42
+ old_value = get
43
+ new_value = yield old_value
44
+
45
+ return unless compare_and_set old_value , new_value
46
+
47
+ new_value
48
+ end
49
+
50
+ # @!macro [attach] atomic_reference_method_try_update!
51
+ #
52
+ # Pass the current value to the given block, replacing it
30
53
# with the block's result. Raise an exception if the update
31
54
# fails.
32
- #
55
+ #
33
56
# @yield [Object] Calculate a new value for the atomic reference using
34
57
# given (old) value
35
58
# @yieldparam [Object] old_value the starting value of the atomic reference
36
59
#
60
+ # @note This behavior mimics the behavior of the original
61
+ # `AtomicReference#try_update` API. The reason this was changed was to
62
+ # avoid raising exceptions (which are inherently slow) by default. For more
63
+ # info: https://github.com/ruby-concurrency/concurrent-ruby/pull/336
64
+ #
37
65
# @return [Object] the new value
38
66
#
39
67
# @raise [Concurrent::ConcurrentUpdateError] if the update fails
40
- def try_update
68
+ def try_update!
41
69
old_value = get
42
70
new_value = yield old_value
43
71
unless compare_and_set ( old_value , new_value )
0 commit comments