| 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.
Creates a fixed-length queue with a maximum size of max.
# 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
Returns the maximum size of the queue.
# File lib/thread.rb, line 345
345: def max
346: @max
347: end
Sets the maximum size of the queue.
# 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
# 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