Class: ThrottleQueue::PriorityQueue
- Inherits:
-
Object
- Object
- ThrottleQueue::PriorityQueue
- Defined in:
- lib/throttle-queue.rb
Overview
:nodoc: all
Instance Method Summary collapse
- #background(id) ⇒ Object
- #empty? ⇒ Boolean
- #foreground(id) ⇒ Object
-
#initialize ⇒ PriorityQueue
constructor
A new instance of PriorityQueue.
- #pop ⇒ Object
- #shutdown ⇒ Object
- #shutdown? ⇒ Boolean
Constructor Details
#initialize ⇒ PriorityQueue
Returns a new instance of PriorityQueue.
162 163 164 165 166 167 168 |
# File 'lib/throttle-queue.rb', line 162 def initialize @mutex = Mutex.new @fg = [] @bg = [] @received = ConditionVariable.new @shutdown = false end |
Instance Method Details
#background(id) ⇒ Object
185 186 187 188 189 190 191 192 |
# File 'lib/throttle-queue.rb', line 185 def background(id) @mutex.synchronize { unless @shutdown || @bg.include?(id) @bg << id @received.signal end } end |
#empty? ⇒ Boolean
179 180 181 182 183 |
# File 'lib/throttle-queue.rb', line 179 def empty? @mutex.synchronize { @fg.empty? and @bg.empty? } end |
#foreground(id) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/throttle-queue.rb', line 194 def foreground(id) @mutex.synchronize { unless @shutdown || @fg.include?(id) @fg << id if @bg.include?(id) @bg.delete id else @received.signal end end } end |
#pop ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/throttle-queue.rb', line 207 def pop @mutex.synchronize { if @fg.empty? and @bg.empty? @received.wait(@mutex) unless @shutdown end if @shutdown elsif ! @fg.empty? @fg.shift else @bg.shift end } end |
#shutdown ⇒ Object
170 171 172 173 |
# File 'lib/throttle-queue.rb', line 170 def shutdown @shutdown = true @received.signal end |
#shutdown? ⇒ Boolean
175 176 177 |
# File 'lib/throttle-queue.rb', line 175 def shutdown? @shutdown end |