Class: Knj::Threadsafe::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/knj/threadsafe.rb

Overview

Instances of this class proxies calls to a given-object by using a mutex or monitor.

Examples

threadsafe_array = Knj::Threadsafe::Proxy.new(:obj => []) threadsafe_array << 5 ret = threadsafe_array

threadsafe_array = Knj::Threadsafe::Proxy.new(:obj => [], :monitor => true)

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Proxy

Spawn needed vars.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/knj/threadsafe.rb', line 21

def initialize(args)
  if args[:monitor]
    @mutex = Monitor.new
  elsif args[:mutex]
    @mutex = args[:mutex]
  else
    @mutex = Mutex.new
  end
  
  @obj = args[:obj]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Proxies all calls to this object through the mutex.



34
35
36
37
38
# File 'lib/knj/threadsafe.rb', line 34

def method_missing(method_name, *args, &block)
  @mutex.synchronize do
    @obj.__send__(method_name, *args, &block)
  end
end