Class: SynchronizedDelegator

### Home Classes Methods

### In Files

- lib/thread_safe/synchronized_delegator.rb

### Parent

SimpleDelegator

### Methods

- ::new
- #method_missing
- #setup
- #teardown

### Class/Module Index

Quicksearch

- ThreadSafe
- ThreadSafe::Array
- ThreadSafe::AtomicReferenceCacheBackend
- ThreadSafe::AtomicReferenceCacheBackend::Node
- ThreadSafe::AtomicReferenceCacheBackend::Table
- ThreadSafe::Cache
- ThreadSafe::Hash
- ThreadSafe::MriCacheBackend
- ThreadSafe::NonConcurrentCacheBackend
- ThreadSafe::SynchronizedCacheBackend
- ThreadSafe::Util
- ThreadSafe::Util::Adder
- ThreadSafe::Util::CheapLockable
- ThreadSafe::Util::PowerOfTwoTuple
- ThreadSafe::Util::Striped64
- ThreadSafe::Util::Striped64::Cell
- ThreadSafe::Util::Volatile
- ThreadSafe::Util::VolatileTuple
- SynchronizedDelegator
- Threadsafe

No matching classes.

# SynchronizedDelegator

This class provides a trivial way to synchronize all calls to a given object by wrapping it with a `Delegator` that performs `Monitor#enter/exit` calls around the delegated `send`. Example:

    array = [] # not thread-safe on many impls
    array = SynchronizedDelegator.new([]) # thread-safe

A simple `Monitor` provides a very coarse-grained way to synchronize a given object, in that it will cause synchronization for methods that have no need for it, but this is a trivial way to get thread-safety where none may exist currently on some implementations.

This class is currently being considered for inclusion into stdlib, via bugs.ruby-lang.org/issues/8556

### Public Class Methods

new(obj) click to toggle source

    # File lib/thread_safe/synchronized_delegator.rb, line 28
    def initialize(obj)
      __setobj__(obj)
      @monitor = Monitor.new
    end

### Public Instance Methods

method_missing(method, *args, &block) click to toggle source

    # File lib/thread_safe/synchronized_delegator.rb, line 33
    def method_missing(method, *args, &block)
      monitor = @monitor
      begin
        monitor.enter
        super
      ensure
        monitor.exit
      end
    end

setup() click to toggle source

    # File lib/thread_safe/synchronized_delegator.rb, line 19
    def setup
      @old_abort = Thread.abort_on_exception
      Thread.abort_on_exception = true
    end

teardown() click to toggle source

    # File lib/thread_safe/synchronized_delegator.rb, line 24
    def teardown
      Thread.abort_on_exception = @old_abort
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.