Class: Contender::Pool::TaskQueue
- Inherits:
-
Object
- Object
- Contender::Pool::TaskQueue
- Defined in:
- lib/contender/pool/task_queue.rb
Overview
Simple thread-safe queue that holds tasks waiting for execution
Instance Method Summary collapse
-
#backlog ⇒ Integer
Performs a volatile read of the size of the task queue.
-
#dequeue(non_block = false) ⇒ Task
Dequeues the first task in line.
-
#empty? ⇒ Boolean
Returns true if the task queue is empty.
-
#enqueue(task) ⇒ undefined
Enqueues the given task for execution.
- #initialize ⇒ undefined constructor
-
#purge ⇒ undefined
Removes any tasks that are queued for execution.
-
#wakeup ⇒ undefined
Wakes up any threads waiting for tasks to be queued.
Constructor Details
#initialize ⇒ undefined
6 7 8 9 10 |
# File 'lib/contender/pool/task_queue.rb', line 6 def initialize @condition = ConditionVariable.new @mutex = Mutex.new @tasks = Array.new end |
Instance Method Details
#backlog ⇒ Integer
Performs a volatile read of the size of the task queue
16 17 18 |
# File 'lib/contender/pool/task_queue.rb', line 16 def backlog @tasks.size end |
#dequeue(non_block = false) ⇒ Task
Dequeues the first task in line
If this operation is performed as non-blocking and the queue is empty, then the result of the dequeue will be nil.
If this operation is performed as blocking, then it will wait until a task is added to the queue. In the case that the thread pool is shutdown during while a worker is waiting for the operation to complete, nil will be returned immediately.
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/contender/pool/task_queue.rb', line 62 def dequeue(non_block = false) @mutex.synchronize do if @tasks.empty? return if non_block @condition.wait @mutex end @tasks.shift end end |
#empty? ⇒ Boolean
Returns true if the task queue is empty
24 25 26 |
# File 'lib/contender/pool/task_queue.rb', line 24 def empty? @tasks.empty? end |
#enqueue(task) ⇒ undefined
Enqueues the given task for execution
43 44 45 46 47 48 |
# File 'lib/contender/pool/task_queue.rb', line 43 def enqueue(task) @mutex.synchronize do @tasks.push task @condition.signal end end |
#purge ⇒ undefined
Removes any tasks that are queued for execution
32 33 34 35 36 |
# File 'lib/contender/pool/task_queue.rb', line 32 def purge @mutex.synchronize do @tasks.clear end end |
#wakeup ⇒ undefined
Wakes up any threads waiting for tasks to be queued
75 76 77 |
# File 'lib/contender/pool/task_queue.rb', line 75 def wakeup @condition.broadcast end |