Class: N::Pool
Overview
Pool
Generalized object pool implementation. Implemented as a thread safe stack. Exclusive locking is needed both for push and pop.
INVESTIGATE: Could use the SizedQueue/Queue.
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.
Constructor Details
#initialize ⇒ Pool
Returns a new instance of Pool.
22 23 24 25 |
# File 'lib/glue/pool.rb', line 22 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.
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/glue/pool.rb', line 47 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.
38 39 40 41 42 43 |
# File 'lib/glue/pool.rb', line 38 def pop synchronize do @cv.wait_while { empty? } super end end |
#push(obj) ⇒ Object
Add, restore an object to the pool.
29 30 31 32 33 34 |
# File 'lib/glue/pool.rb', line 29 def push(obj) synchronize do super @cv.signal() end end |