|
| 1 | +module Concurrent |
| 2 | + module Concern |
| 3 | + |
| 4 | + # Object references in Ruby are mutable. This can lead to serious problems when |
| 5 | + # the `#value` of a concurrent object is a mutable reference. Which is always the |
| 6 | + # case unless the value is a `Fixnum`, `Symbol`, or similar "primitive" data type. |
| 7 | + # Most classes in this library that expose a `#value` getter method do so using the |
| 8 | + # `Dereferenceable` mixin module. |
| 9 | + # |
| 10 | + # @!macro copy_options |
| 11 | + module Dereferenceable |
| 12 | + |
| 13 | + # Return the value this object represents after applying the options specified |
| 14 | + # by the `#set_deref_options` method. |
| 15 | + # |
| 16 | + # @return [Object] the current value of the object |
| 17 | + def value |
| 18 | + mutex.synchronize { apply_deref_options(@value) } |
| 19 | + end |
| 20 | + alias_method :deref, :value |
| 21 | + |
| 22 | + protected |
| 23 | + |
| 24 | + # Set the internal value of this object |
| 25 | + # |
| 26 | + # @param [Object] value the new value |
| 27 | + def value=(value) |
| 28 | + mutex.synchronize{ @value = value } |
| 29 | + end |
| 30 | + |
| 31 | + # A mutex lock used for synchronizing thread-safe operations. Methods defined |
| 32 | + # by `Dereferenceable` are synchronized using the `Mutex` returned from this |
| 33 | + # method. Operations performed by the including class that operate on the |
| 34 | + # `@value` instance variable should be locked with this `Mutex`. |
| 35 | + # |
| 36 | + # @return [Mutex] the synchronization object |
| 37 | + def mutex |
| 38 | + @mutex |
| 39 | + end |
| 40 | + |
| 41 | + # Initializes the internal `Mutex`. |
| 42 | + # |
| 43 | + # @note This method *must* be called from within the constructor of the including class. |
| 44 | + # |
| 45 | + # @see #mutex |
| 46 | + def init_mutex(mutex = Mutex.new) |
| 47 | + @mutex = mutex |
| 48 | + end |
| 49 | + |
| 50 | + # @!macro [attach] dereferenceable_set_deref_options |
| 51 | + # Set the options which define the operations #value performs before |
| 52 | + # returning data to the caller (dereferencing). |
| 53 | + # |
| 54 | + # @note Most classes that include this module will call `#set_deref_options` |
| 55 | + # from within the constructor, thus allowing these options to be set at |
| 56 | + # object creation. |
| 57 | + # |
| 58 | + # @param [Hash] opts the options defining dereference behavior. |
| 59 | + # @option opts [String] :dup_on_deref (false) call `#dup` before returning the data |
| 60 | + # @option opts [String] :freeze_on_deref (false) call `#freeze` before returning the data |
| 61 | + # @option opts [String] :copy_on_deref (nil) call the given `Proc` passing |
| 62 | + # the internal value and returning the value returned from the proc |
| 63 | + def set_deref_options(opts = {}) |
| 64 | + mutex.synchronize{ ns_set_deref_options(opts) } |
| 65 | + end |
| 66 | + |
| 67 | + # @!macro dereferenceable_set_deref_options |
| 68 | + # @!visibility private |
| 69 | + def ns_set_deref_options(opts) |
| 70 | + @dup_on_deref = opts[:dup_on_deref] || opts[:dup] |
| 71 | + @freeze_on_deref = opts[:freeze_on_deref] || opts[:freeze] |
| 72 | + @copy_on_deref = opts[:copy_on_deref] || opts[:copy] |
| 73 | + @do_nothing_on_deref = !(@dup_on_deref || @freeze_on_deref || @copy_on_deref) |
| 74 | + nil |
| 75 | + end |
| 76 | + |
| 77 | + # @!visibility private |
| 78 | + def apply_deref_options(value) |
| 79 | + return nil if value.nil? |
| 80 | + return value if @do_nothing_on_deref |
| 81 | + value = @copy_on_deref.call(value) if @copy_on_deref |
| 82 | + value = value.dup if @dup_on_deref |
| 83 | + value = value.freeze if @freeze_on_deref |
| 84 | + value |
| 85 | + end |
| 86 | + end |
| 87 | + end |
| 88 | +end |
0 commit comments