class MultiThink::TimedStack
Public Class Methods
new(size = 0, options)
click to toggle source
# File lib/multithink/timed_stack.rb, line 9 def initialize(size = 0, options) @size = size @options = options @que = [] @checked_out = [] @mutex = Mutex.new @resource = ConditionVariable.new @shutdown_block = nil end
Public Instance Methods
available?()
click to toggle source
# File lib/multithink/timed_stack.rb, line 33 def available? @que.count + @checked_out.count < @size end
empty?()
click to toggle source
# File lib/multithink/timed_stack.rb, line 69 def empty? @que.empty? end
length()
click to toggle source
# File lib/multithink/timed_stack.rb, line 73 def length @que.length end
pop(timeout=0.5)
click to toggle source
# File lib/multithink/timed_stack.rb, line 37 def pop(timeout=0.5) deadline = Time.now + timeout @mutex.synchronize do loop do raise PoolShuttingDownError if @shutdown_block return @que.pop unless @que.empty? if available? new_conn = MultiThink::Connection.new(@options) @checked_out << new_conn return new_conn end to_wait = deadline - Time.now raise Timeout::Error, "Waited #{timeout} sec" if to_wait <= 0 @resource.wait(@mutex, to_wait) end end end
push(obj)
click to toggle source
# File lib/multithink/timed_stack.rb, line 19 def push(obj) @mutex.synchronize do if @shutdown_block @shutdown_block.call(obj) else @que.push obj @checked_out.delete obj end @resource.broadcast end end
Also aliased as: <<
shutdown(&block)
click to toggle source
# File lib/multithink/timed_stack.rb, line 55 def shutdown(&block) raise ArgumentError, "shutdown must receive a block" unless block_given? @mutex.synchronize do @shutdown_block = block @resource.broadcast @que.size.times do conn = @que.pop block.call(conn) end end end