Class: TCellAgent::BoundedQueue
- Inherits:
-
Object
- Object
- TCellAgent::BoundedQueue
- Defined in:
- lib/tcell_agent/utils/queue_with_timeout.rb
Instance Method Summary collapse
- #full? ⇒ Boolean
-
#initialize(max_size = :infinite) ⇒ BoundedQueue
constructor
A new instance of BoundedQueue.
- #pop(timeout = :never, &timeout_policy) ⇒ Object
- #push(obj, timeout = :never, &timeout_policy) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(max_size = :infinite) ⇒ BoundedQueue
11 12 13 14 15 16 17 |
# File 'lib/tcell_agent/utils/queue_with_timeout.rb', line 11 def initialize(max_size = :infinite) @lock = Mutex.new @items = [] @item_available = ConditionVariable.new @max_size = max_size @space_available = ConditionVariable.new end |
Instance Method Details
#full? ⇒ Boolean
50 51 52 53 |
# File 'lib/tcell_agent/utils/queue_with_timeout.rb', line 50 def full? return false if @max_size == :infinite @max_size <= @items.size end |
#pop(timeout = :never, &timeout_policy) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/tcell_agent/utils/queue_with_timeout.rb', line 35 def pop(timeout = :never, &timeout_policy) timeout_policy ||= ->{nil} wait_for_condition( @item_available, ->{@items.any?}, timeout, timeout_policy) do item = @items.shift @space_available.signal item end end |
#push(obj, timeout = :never, &timeout_policy) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/tcell_agent/utils/queue_with_timeout.rb', line 19 def push(obj, timeout=:never, &timeout_policy) timeout_policy ||= -> do raise "Push timed out" end wait_for_condition( @space_available, ->{!full?}, timeout, timeout_policy) do @items.push(obj) @item_available.signal end end |
#size ⇒ Object
55 56 57 |
# File 'lib/tcell_agent/utils/queue_with_timeout.rb', line 55 def size @items.size end |