Class: WorkQueue::ThreadSafeWrapper
- Inherits:
-
Object
- Object
- WorkQueue::ThreadSafeWrapper
- Includes:
- MonitorMixin
- Defined in:
- lib/work_queue.rb
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#initialize(queue) ⇒ ThreadSafeWrapper
constructor
A new instance of ThreadSafeWrapper.
- #push(task, force:) ⇒ Object
- #shift(block:) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(queue) ⇒ ThreadSafeWrapper
Returns a new instance of ThreadSafeWrapper.
12 13 14 15 16 17 |
# File 'lib/work_queue.rb', line 12 def initialize(queue) mon_initialize @queue = queue @has_items = new_cond end |
Instance Method Details
#empty? ⇒ Boolean
42 43 44 |
# File 'lib/work_queue.rb', line 42 def empty? synchronize { @queue.empty? } end |
#push(task, force:) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/work_queue.rb', line 19 def push(task, force:) synchronize do previously_empty = @queue.empty? @queue.push(task, force: force) @has_items.signal if previously_empty end end |
#shift(block:) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/work_queue.rb', line 28 def shift(block:) synchronize do loop do if task = @queue.shift break task elsif block @has_items.wait else break nil end end end end |
#size ⇒ Object
46 47 48 |
# File 'lib/work_queue.rb', line 46 def size synchronize { @queue.size } end |