Class: Litesupport::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/litestack/litesupport.rb

Instance Method Summary collapse

Constructor Details

#initialize(count, &block) ⇒ Pool

Returns a new instance of Pool.



82
83
84
85
86
87
88
89
90
91
# File 'lib/litestack/litesupport.rb', line 82

def initialize(count, &block)
  @count = count
  @block = block
  @resources = Thread::Queue.new
  @mutex = Litesupport::Mutex.new
  @count.times do
    resource = @mutex.synchronize { block.call }
    @resources << resource
  end
end

Instance Method Details

#acquireObject



93
94
95
96
97
98
99
100
101
102
# File 'lib/litestack/litesupport.rb', line 93

def acquire
  result = nil
  resource = @resources.pop
  begin
    result = yield resource
  ensure
    @resources << resource
  end
  result
end