Class SizedQueue
In: lib/thread.rb
Parent: Queue

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

Methods

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

Public Class methods

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

[Source]

     # File lib/thread.rb, line 334
334:   def initialize(max)
335:     raise ArgumentError, "queue size must be positive" unless max > 0
336:     @max = max
337:     @queue_wait = []
338:     @queue_wait.taint           # enable tainted comunication
339:     super()
340:   end

Public Instance methods

<<(obj)

Alias for push

deq(*args)

Alias for pop

enq(obj)

Alias for push

Returns the maximum size of the queue.

[Source]

     # File lib/thread.rb, line 345
345:   def max
346:     @max
347:   end

Sets the maximum size of the queue.

[Source]

     # File lib/thread.rb, line 352
352:   def max=(max)
353:     Thread.critical = true
354:     if max <= @max
355:       @max = max
356:       Thread.critical = false
357:     else
358:       diff = max - @max
359:       @max = max
360:       Thread.critical = false
361:       diff.times do
362:         begin
363:           t = @queue_wait.shift
364:           t.run if t
365:         rescue ThreadError
366:           retry
367:         end
368:       end
369:     end
370:     max
371:   end

[Source]

     # File lib/thread.rb, line 407
407:   def num_waiting
408:     @waiting.size + @queue_wait.size
409:   end

[Source]

     # File lib/thread.rb, line 385
385:   def pop(*args)
386:     retval = super
387:     Thread.critical = true
388:     if @que.length < @max
389:       begin
390:         t = @queue_wait.shift
391:         t.wakeup if t
392:       rescue ThreadError
393:         retry
394:       ensure
395:         Thread.critical = false
396:       end
397:       begin
398:         t.run if t
399:       rescue ThreadError
400:       end
401:     end
402:     retval
403:   end

[Source]

     # File lib/thread.rb, line 373
373:   def push(obj)
374:     Thread.critical = true
375:     while @que.length >= @max
376:       @queue_wait.push Thread.current
377:       Thread.stop
378:       Thread.critical = true
379:     end
380:     super
381:   end
shift(*args)

Alias for pop

[Validate]