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.

Methods
#
C
D
E
M
N
P
S
Class Public methods
new(max)

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

# File ../ruby/lib/thread.rb, line 251
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
Instance Public methods
<<(obj)

Alias of push

Alias for: push
clear()

Removes all objects from the queue and wakes waiting threads, if any.

# File ../ruby/lib/thread.rb, line 362
def clear
  @mutex.synchronize do
    @que.clear
    begin
      until @queue_wait.empty?
        @queue_wait.shift.wakeup
      end
    rescue ThreadError
      retry
    end
  end
end
deq(*args)

Alias of pop

Alias for: pop
enq(obj)

Alias of push

Alias for: push
max()

Returns the maximum size of the queue.

# File ../ruby/lib/thread.rb, line 262
def max
  @max
end
max=(max)

Sets the maximum size of the queue.

# File ../ruby/lib/thread.rb, line 269
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()

Returns the number of threads waiting on the queue.

# File ../ruby/lib/thread.rb, line 355
def num_waiting
  @waiting.size + @queue_wait.size
end
pop(*args)

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

Also aliased as: shift, deq
# File ../ruby/lib/thread.rb, line 327
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
push(obj)

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

Also aliased as: <<, enq
# File ../ruby/lib/thread.rb, line 296
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
shift(*args)

Alias of pop

Alias for: pop