| 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.
See Queue for an example of how a SizedQueue works.
Creates a fixed-length queue with a maximum size of max.
# File lib/thread.rb, line 374
374: def initialize(max)
375: raise ArgumentError, "queue size must be positive" unless max > 0
376: @max = max
377: @queue_wait = []
378: @queue_wait.taint # enable tainted comunication
379: super()
380: end
Returns the maximum size of the queue.
# File lib/thread.rb, line 385
385: def max
386: @max
387: end
Sets the maximum size of the queue.
# File lib/thread.rb, line 392
392: def max=(max)
393: Thread.critical = true
394: if max <= @max
395: @max = max
396: Thread.critical = false
397: else
398: diff = max - @max
399: @max = max
400: Thread.critical = false
401: diff.times do
402: begin
403: t = @queue_wait.shift
404: t.run if t
405: rescue ThreadError
406: retry
407: end
408: end
409: end
410: max
411: end
Returns the number of threads waiting on the queue.
# File lib/thread.rb, line 473
473: def num_waiting
474: @waiting.size + @queue_wait.size
475: end
Retrieves data from the queue and runs a waiting thread, if any.
# File lib/thread.rb, line 440
440: def pop(*args)
441: retval = super
442: Thread.critical = true
443: if @que.length < @max
444: begin
445: t = @queue_wait.shift
446: t.wakeup if t
447: rescue ThreadError
448: retry
449: ensure
450: Thread.critical = false
451: end
452: begin
453: t.run if t
454: rescue ThreadError
455: end
456: end
457: retval
458: end
Pushes obj to the queue. If there is no space left in the queue, waits until space becomes available.
# File lib/thread.rb, line 417
417: def push(obj)
418: Thread.critical = true
419: while @que.length >= @max
420: @queue_wait.push Thread.current
421: Thread.stop
422: Thread.critical = true
423: end
424: super
425: end