Class: AsyncFutures::SynchronizedDelegator
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- AsyncFutures::SynchronizedDelegator
- Defined in:
- lib/async_futures/synchronized_delegator.rb
Overview
A Delegator that synchronizes all calls for a delegated object.
This does not guarantee concurrency safety under all circumstances,
but it does make it easier to be safe.
For example,
any method that returns self is potentially unsafe if chained afterward
(because the chained operations can happen outside the synchronized mutex,
and thus are not safe for concurrency).
The only argument to new is the object to be wrapped.
For concurrency safety, it should no longer be directly accessed
outside the delegator
unless you are know you are in a situation
where the object will not be accessed concurrently.
Instance Method Summary collapse
-
#clone(freeze: nil) ⇒ Object
A synchronized implementation of
clone. -
#dup ⇒ Object
A synchronized implementation of
dup. -
#freeze ⇒ Object
A synchronized implementation of
freeze. -
#initialize(obj) ⇒ SynchronizedDelegator
constructor
A new instance of SynchronizedDelegator.
-
#method_missing(name, *args, **kwargs) ⇒ Object
Like regular
method_missing, but all calls are synchronized. - #respond_to_missing?(name, include_private = false) ⇒ Boolean
Constructor Details
#initialize(obj) ⇒ SynchronizedDelegator
Returns a new instance of SynchronizedDelegator.
21 22 23 24 |
# File 'lib/async_futures/synchronized_delegator.rb', line 21 def initialize(obj) @mutex = Thread::Mutex.new super end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, **kwargs) ⇒ Object
Like regular method_missing, but all calls are synchronized.
27 28 29 30 31 32 |
# File 'lib/async_futures/synchronized_delegator.rb', line 27 def method_missing(name, *args, **kwargs, &) @mutex.synchronize do # SimpleDelegator#method_missing forwards to __getobj__ super end end |
Instance Method Details
#clone(freeze: nil) ⇒ Object
A synchronized implementation of clone.
Uses a custom implementation of initialize_clone
that creates and assigns a new mutex
to the clone.
The internal mutex will also be properly frozen
if the original delegator is frozen.
46 47 48 49 50 |
# File 'lib/async_futures/synchronized_delegator.rb', line 46 def clone(freeze: nil) @mutex.synchronize do super end end |
#dup ⇒ Object
A synchronized implementation of dup.
Uses a custom implementation of initialize_dup
that creates and assigns a new mutex
to the duplicate.
57 58 59 60 61 |
# File 'lib/async_futures/synchronized_delegator.rb', line 57 def dup @mutex.synchronize do super end end |
#freeze ⇒ Object
A synchronized implementation of freeze.
Also freezes private mutex.
66 67 68 69 70 71 |
# File 'lib/async_futures/synchronized_delegator.rb', line 66 def freeze @mutex.synchronize do @mutex.freeze super end end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
34 35 36 37 |
# File 'lib/async_futures/synchronized_delegator.rb', line 34 def respond_to_missing?(name, include_private = false) # keep respond_to? consistent super end |