In Files

  • thread.rb

Class/Module Index [+]

Quicksearch

SizedQueue

This class represents queues of specified size capacity. The push operation may be blocked if the capacity is full.

See Queue for an example of how a SizedQueue works.

Public Class Methods

new(max) click to toggle source

Creates a fixed-length queue with a maximum size of max.

 
               # File thread.rb, line 247
def initialize(max)
  raise ArgumentError, "queue size must be positive" unless max > 0
  @max = max
  @queue_wait = []
  @queue_wait.taint           # enable tainted comunication
  super()
end
            

Public Instance Methods

<<(obj) click to toggle source

Alias of push

Alias for: push
deq(*args) click to toggle source

Alias of pop

Alias for: pop
enq(obj) click to toggle source

Alias of push

Alias for: push
max() click to toggle source

Returns the maximum size of the queue.

 
               # File thread.rb, line 258
def max
  @max
end
            
max=(max) click to toggle source

Sets the maximum size of the queue.

 
               # File thread.rb, line 265
def max=(max)
  diff = nil
  @mutex.synchronize {
    if max <= @max
      @max = max
    else
      diff = max - @max
      @max = max
    end
  }
  if diff
    diff.times do
      begin
        t = @queue_wait.shift
        t.run if t
      rescue ThreadError
        retry
      end
    end
  end
  max
end
            
num_waiting() click to toggle source

Returns the number of threads waiting on the queue.

 
               # File thread.rb, line 351
def num_waiting
  @waiting.size + @queue_wait.size
end
            
pop(*args) click to toggle source

Retrieves data from the queue and runs a waiting thread, if any.

 
               # File thread.rb, line 323
def pop(*args)
  retval = super
  @mutex.synchronize {
    if @que.length < @max
      begin
        t = @queue_wait.shift
        t.wakeup if t
      rescue ThreadError
        retry
      end
    end
  }
  retval
end
            
Also aliased as: shift, deq
push(obj) click to toggle source

Pushes obj to the queue. If there is no space left in the queue, waits until space becomes available.

 
               # File thread.rb, line 292
def push(obj)
  @mutex.synchronize{
    while true
      break if @que.length < @max
      @queue_wait.push Thread.current
      @mutex.sleep
    end

    @que.push obj
    begin
      t = @waiting.shift
      t.wakeup if t
    rescue ThreadError
      retry
    end
  }
end
            
Also aliased as: <<, enq
shift(*args) click to toggle source

Alias of pop

Alias for: pop