Class: Slave::ThreadSafe

Inherits:
Object
  • Object
show all
Defined in:
lib/slave.rb,
lib/slave-1.2.1.rb

Overview

ThreadSafe is a delegate wrapper class used for implementing gross thread safety around existing objects. when an object is wrapped with this class as

ts = ThreadSafe.new{ AnyObject.new }

then ts can be used exactly as the normal object would have been, only all calls are now thread safe. this is the mechanism behind the ‘threadsafe’/:threadsafe keyword to Slave#initialize

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ ThreadSafe

Returns a new instance of ThreadSafe.



130
131
132
133
# File 'lib/slave.rb', line 130

def initialize(object)
  @object = object
  @sync = Sync.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a, &b) ⇒ Object



139
140
141
# File 'lib/slave.rb', line 139

def method_missing m, *a, &b
  ex{ @object.send m, *a, &b }
end

Instance Method Details

#classObject



151
152
153
# File 'lib/slave.rb', line 151

def class
  ex{ @object.class }
end

#exObject



135
136
137
# File 'lib/slave.rb', line 135

def ex
  @sync.synchronize{ yield }
end

#inspectObject



147
148
149
# File 'lib/slave.rb', line 147

def inspect
  ex{ @object.inspect }
end

#respond_to?(*a, &b) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/slave.rb', line 143

def respond_to? *a, &b 
  ex{ @object.respond_to? *a, &b }
end