Class: N::Pool
- Inherits:
-
Array
- Object
- Array
- N::Pool
- Includes:
- MonitorMixin
- Defined in:
- lib/n/utils/pool.rb
Overview
Pool
Implemente 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.
25 26 27 28 |
# File 'lib/n/utils/pool.rb', line 25 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.
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/n/utils/pool.rb', line 50 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.
41 42 43 44 45 46 |
# File 'lib/n/utils/pool.rb', line 41 def pop synchronize do @cv.wait_while { empty? } super end end |
#push(obj) ⇒ Object
Add, restore an object to the pool.
32 33 34 35 36 37 |
# File 'lib/n/utils/pool.rb', line 32 def push(obj) synchronize do super @cv.signal() end end |