Class: MultiThink::TimedStack
- Inherits:
-
Object
- Object
- MultiThink::TimedStack
- Defined in:
- lib/multithink/timed_stack.rb
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#initialize(size = 0, servers) ⇒ TimedStack
constructor
A new instance of TimedStack.
- #length ⇒ Object
- #pop(timeout = 0.5) ⇒ Object
- #push(obj) ⇒ Object (also: #<<)
- #shutdown(&block) ⇒ Object
Constructor Details
#initialize(size = 0, servers) ⇒ TimedStack
Returns a new instance of TimedStack.
9 10 11 12 13 14 |
# File 'lib/multithink/timed_stack.rb', line 9 def initialize(size = 0, servers) @que = Array.new(size) {|s| MultiThink::Connection.new(servers)} @mutex = Mutex.new @resource = ConditionVariable.new @shutdown_block = nil end |
Instance Method Details
#empty? ⇒ Boolean
56 57 58 |
# File 'lib/multithink/timed_stack.rb', line 56 def empty? @que.empty? end |
#length ⇒ Object
60 61 62 |
# File 'lib/multithink/timed_stack.rb', line 60 def length @que.length end |
#pop(timeout = 0.5) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/multithink/timed_stack.rb', line 29 def pop(timeout=0.5) deadline = Time.now + timeout @mutex.synchronize do loop do raise MultiThink::PoolShuttingDownError if @shutdown_block return @que.pop unless @que.empty? 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) ⇒ Object Also known as: <<
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/multithink/timed_stack.rb', line 16 def push(obj) @mutex.synchronize do if @shutdown_block @shutdown_block.call(obj) else @que.push obj end @resource.broadcast end end |
#shutdown(&block) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/multithink/timed_stack.rb', line 42 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 |