Sha256: f322f1f1f8c568a648003652c9dd9fd1eab14cc893c8e237527e9149c29cb3be

Contents?: true

Size: 932 Bytes

Versions: 6

Compression:

Stored size: 932 Bytes

Contents

# code:
# * George Moschovitis  <gm@navel.gr>
#
# (c) 2004 Navel, all rights reserved.
# $Id: pool.rb 202 2005-01-17 10:44:13Z gmosx $

require "thread"
require "monitor"

module N

# = 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.
#
class Pool < Array
	include MonitorMixin
	
	def initialize
		super
		@cv = new_cond()
	end

	# Add, restore an object to the pool.
	#
	def push(obj)
		synchronize do
			super
			@cv.signal()
		end
	end
	
	# Obtain an object from the pool.
	#
	def pop
		synchronize do
			@cv.wait_while { empty? }
			super
		end
	end

	# Obtains an object, passes it to a block for processing
	# and restores it to the pool.
	def obtain
		result = nil
		
		begin
			obj = pop()

			result = yield(obj)
		ensure
			push(obj)
		end

		return result
	end
	
end

end # module

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
nitro-0.10.0 lib/glue/pool.rb
nitro-0.9.3 lib/glue/pool.rb
nitro-0.9.5 lib/glue/pool.rb
og-0.10.0 lib/glue/pool.rb
og-0.9.3 lib/glue/pool.rb
og-0.9.5 lib/glue/pool.rb