Class: Pool
Instance Method Summary collapse
-
#initialize ⇒ Pool
constructor
A new instance of Pool.
-
#obtain ⇒ Object
Obtains an object, passes it to a block for processing and restores it to the pool.
-
#pop ⇒ Object
Obtain an object from the pool.
-
#push(obj) ⇒ Object
Add, restore an object to the pool.
Methods included from DupReplaceSnapshotMixin
#restore_snapshot, #take_snapshot
Constructor Details
#initialize ⇒ Pool
Returns a new instance of Pool.
46 47 48 49 |
# File 'lib/mega/pool.rb', line 46 def initialize super @cv = new_cond() end |
Instance Method Details
#obtain ⇒ Object
Obtains an object, passes it to a block for processing and restores it to the pool.
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/mega/pool.rb', line 72 def obtain result = nil begin obj = pop() result = yield(obj) ensure push(obj) end return result end |
#pop ⇒ Object
Obtain an object from the pool.
62 63 64 65 66 67 |
# File 'lib/mega/pool.rb', line 62 def pop synchronize do @cv.wait_while { empty? } super end end |
#push(obj) ⇒ Object
Add, restore an object to the pool.
53 54 55 56 57 58 |
# File 'lib/mega/pool.rb', line 53 def push(obj) synchronize do super @cv.signal() end end |