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.



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

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



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

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