|
4 | 4 |
|
5 | 5 | module Concurrent |
6 | 6 |
|
7 | | - # Portable/generic (but not very memory or scheduling-efficient) fallback |
8 | | - class MutexAtomic #:nodoc: all |
| 7 | + # @!macro atomic_reference |
| 8 | + class MutexAtomic |
9 | 9 | include Concurrent::AtomicDirectUpdate |
10 | 10 | include Concurrent::AtomicNumericCompareAndSetWrapper |
11 | 11 |
|
| 12 | + # @!macro atomic_reference_method_initialize |
12 | 13 | def initialize(value = nil) |
13 | 14 | @mutex = Mutex.new |
14 | 15 | @value = value |
15 | 16 | end |
16 | 17 |
|
| 18 | + # @!macro [attach] atomic_reference_method_get |
| 19 | + # |
| 20 | + # Gets the current value. |
| 21 | + # |
| 22 | + # @return [Object] the current value |
17 | 23 | def get |
18 | 24 | @mutex.synchronize { @value } |
19 | 25 | end |
20 | | - alias value get |
| 26 | + alias_method :value, :get |
21 | 27 |
|
| 28 | + # @!macro [attach] atomic_reference_method_set |
| 29 | + # |
| 30 | + # Sets to the given value. |
| 31 | + # |
| 32 | + # @param [Object] new_value the new value |
| 33 | + # |
| 34 | + # @return [Object] the new value |
22 | 35 | def set(new_value) |
23 | 36 | @mutex.synchronize { @value = new_value } |
24 | 37 | end |
25 | | - alias value= set |
| 38 | + alias_method :value=, :set |
26 | 39 |
|
| 40 | + # @!macro [attach] atomic_reference_method_get_and_set |
| 41 | + # |
| 42 | + # Atomically sets to the given value and returns the old value. |
| 43 | + # |
| 44 | + # @param [Object] new_value the new value |
| 45 | + # |
| 46 | + # @return [Object] the old value |
27 | 47 | def get_and_set(new_value) |
28 | 48 | @mutex.synchronize do |
29 | 49 | old_value = @value |
30 | 50 | @value = new_value |
31 | 51 | old_value |
32 | 52 | end |
33 | 53 | end |
34 | | - alias swap get_and_set |
| 54 | + alias_method :swap, :get_and_set |
35 | 55 |
|
36 | | - def _compare_and_set(old_value, new_value) |
| 56 | + # @!macro [attach] atomic_reference_method_compare_and_set |
| 57 | + # |
| 58 | + # Atomically sets the value to the given updated value if |
| 59 | + # the current value == the expected value. |
| 60 | + # |
| 61 | + # @param [Object] old_value the expected value |
| 62 | + # @param [Object] new_value the new value |
| 63 | + # |
| 64 | + # @return [Boolean] `true` if successful. A `false` return indicates |
| 65 | + # that the actual value was not equal to the expected value. |
| 66 | + def _compare_and_set(old_value, new_value) #:nodoc: |
37 | 67 | return false unless @mutex.try_lock |
38 | 68 | begin |
39 | 69 | return false unless @value.equal? old_value |
|
0 commit comments