Class: ThrottleQueue::PriorityQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/throttle-queue.rb

Overview

:nodoc: all

Instance Method Summary collapse

Constructor Details

#initializePriorityQueue

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

Returns:

  • (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

#popObject



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

#shutdownObject



170
171
172
173
# File 'lib/throttle-queue.rb', line 170

def shutdown
  @shutdown = true
  @received.signal
end

#shutdown?Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/throttle-queue.rb', line 175

def shutdown?
  @shutdown
end