Class: N::Pool

Inherits:
Array show all
Includes:
MonitorMixin
Defined in:
lib/glue/pool.rb

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

Constructor Details

#initializePool

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

#obtainObject

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

#popObject

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