Class | SizedQueue
In: | thread.rb
Parent: | Queue

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

<< deq enq max max= new num_waiting pop push shift

### Public Class methods

new(max)

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

[Source]

    # File thread.rb, line 379
      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)

Alias for push

deq(*args)

Alias for pop

enq(obj)

Alias for push

max()

Returns the maximum size of the queue.

[Source]

    # File thread.rb, line 390
      def max
        @max
      end

max=(max)

Sets the maximum size of the queue.

[Source]

    # File thread.rb, line 397
      def max=(max)
        Thread.critical = true
        if max <= @max
          @max = max
          Thread.critical = false
        else
          diff = max - @max
          @max = max
          Thread.critical = false
          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.

[Source]

    # File thread.rb, line 478
      def num_waiting
        @waiting.size + @queue_wait.size
      end

pop(*args)

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

[Source]

    # File thread.rb, line 445
      def pop(*args)
        retval = super
        Thread.critical = true
        if @que.length < @max
          begin
            t = @queue_wait.shift
            t.wakeup if t
          rescue ThreadError
            retry
          ensure
            Thread.critical = false
          end
          begin
            t.run if t
          rescue ThreadError
          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.

[Source]

    # File thread.rb, line 422
      def push(obj)
        Thread.critical = true
        while @que.length >= @max
          @queue_wait.push Thread.current
          Thread.stop
          Thread.critical = true
        end
        super
      end

shift(*args)

Alias for pop

[Validate]