Module: Concurrent::Dereferenceable
- Included in:
- Agent, MVar, Obligation, TimerTask
- Defined in:
- lib/concurrent/dereferenceable.rb
Overview
Object references in Ruby are mutable. This can lead to serious problems when the #value of a concurrent object is a mutable reference. Which is always the case unless the value is a Fixnum, Symbol, or similar “primitive” data type. Most classes in this library that expose a #value getter method do so using the Dereferenceable mixin module.
Objects with this mixin can be configured with a few options that can help protect the program from potentially dangerous operations.
-
:dup_on_derefwhen true will call the#dupmethod on thevalueobject every time the#valuemethod is called (default: false) -
:freeze_on_derefwhen true will call the#freezemethod on thevalueobject every time the#valuemethod is called (default: false) -
:copy_on_derefwhen given aProcobject theProcwill be run every time the#valuemethod is called. TheProcwill be given the currentvalueas its only parameter and the result returned by the block will be the return value of the#valuecall. Whennilthis option will be ignored (default: nil)
Instance Method Summary collapse
-
#value ⇒ Object
(also: #deref)
Return the value this object represents after applying the options specified by the
#set_deref_optionsmethod.
Instance Method Details
#value ⇒ Object Also known as: deref
Return the value this object represents after applying the options specified by the #set_deref_options method.
When multiple deref options are set the order of operations is strictly defined. The order of deref operations is:
-
:copy_on_deref -
:dup_on_deref -
:freeze_on_deref
Because of this ordering there is no need to #freeze an object created by a provided :copy_on_deref block. Simply set :freeze_on_deref to true. Setting both :dup_on_deref to true and :freeze_on_deref to true is as close to the behavior of a “pure” functional language (like Erlang, Clojure, or Haskell) as we are likely to get in Ruby.
This method is thread-safe and synchronized with the internal #mutex.
35 36 37 38 39 40 |
# File 'lib/concurrent/dereferenceable.rb', line 35 def value mutex.lock (@value) ensure mutex.unlock end |