Class: QuackConcurrency::Queue
- Inherits:
-
Object
- Object
- QuackConcurrency::Queue
- Defined in:
- lib/quack_concurrency/queue.rb
Overview
This is a duck type for ::Thread::Queue
. It is intended to be a drop in replacement for it’s core counterpart. Valuable if ::Thread::Queue
has not been implemented.
Instance Method Summary collapse
-
#clear ⇒ self
Removes all objects from it.
-
#close ⇒ self
Closes it.
-
#closed? ⇒ Boolean
Checks if it is closed.
-
#empty? ⇒ Boolean
Checks if it is empty.
-
#initialize ⇒ Queue
constructor
Creates a new Queue concurrency tool.
-
#length ⇒ Integer
(also: #size)
Returns the length of it.
-
#num_waiting ⇒ Integer
Returns the number of threads waiting on it.
-
#pop(non_block = false) ⇒ Object
(also: #deq, #shift)
Retrieves an item from it.
-
#push(item = nil) ⇒ self
(also: #<<, #enq)
Pushes the given object to it.
Constructor Details
Instance Method Details
#clear ⇒ self
Removes all objects from it.
20 21 22 23 |
# File 'lib/quack_concurrency/queue.rb', line 20 def clear @mutex.synchronize { @items.clear } self end |
#close ⇒ self
33 34 35 36 37 38 39 40 |
# File 'lib/quack_concurrency/queue.rb', line 33 def close @mutex.synchronize do return if closed? @closed = true @waiter.resume_all end self end |
#closed? ⇒ Boolean
Checks if it is closed.
44 45 46 |
# File 'lib/quack_concurrency/queue.rb', line 44 def closed? @closed end |
#empty? ⇒ Boolean
Checks if it is empty.
50 51 52 |
# File 'lib/quack_concurrency/queue.rb', line 50 def empty? @items.empty? end |
#length ⇒ Integer Also known as: size
Returns the length of it.
56 57 58 |
# File 'lib/quack_concurrency/queue.rb', line 56 def length @items.length end |
#num_waiting ⇒ Integer
Returns the number of threads waiting on it.
63 64 65 |
# File 'lib/quack_concurrency/queue.rb', line 63 def num_waiting @pop_mutex.waiting_threads_count + @waiter.waiting_threads_count end |
#pop(non_block = false) ⇒ Object Also known as: deq, shift
Note:
If it is empty, the method will block until an item is available.
Retrieves an item from it. If non_block
is true
, a ThreadError
will be raised.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/quack_concurrency/queue.rb', line 73 def pop(non_block = false) @pop_mutex.lock do @mutex.synchronize do if empty? return if closed? raise ThreadError if non_block @mutex.unlock @waiter.wait @mutex.lock return if closed? end @items.shift end end end |
#push(item = nil) ⇒ self Also known as: <<, enq
Pushes the given object to it.
94 95 96 97 98 99 100 101 |
# File 'lib/quack_concurrency/queue.rb', line 94 def push(item = nil) @mutex.synchronize do raise ClosedQueueError if closed? @items.push(item) @waiter.resume_next end self end |