Bug #9342 » 0001-Wake-waiting-threads-when-SizedQueue-clear-is-called.patch
| lib/thread.rb | ||
|---|---|---|
|
def num_waiting
|
||
|
@waiting.size + @queue_wait.size
|
||
|
end
|
||
|
#
|
||
|
# Removes all objects from the queue and wakes waiting threads, if any.
|
||
|
#
|
||
|
def clear
|
||
|
@mutex.synchronize do
|
||
|
@que.clear
|
||
|
begin
|
||
|
until @queue_wait.empty?
|
||
|
@queue_wait.shift.wakeup
|
||
|
end
|
||
|
rescue ThreadError
|
||
|
retry
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
# Documentation comments:
|
||
| test/thread/test_queue.rb | ||
|---|---|---|
|
grind(5, 1000, 15, SizedQueue, 1000)
|
||
|
end
|
||
|
def test_sized_queue_clear
|
||
|
# Fill queue, then test that SizedQueue#clear wakes up all waiting threads
|
||
|
sq = SizedQueue.new(2)
|
||
|
2.times { sq << 1 }
|
||
|
t1 = Thread.new do
|
||
|
sq << 1
|
||
|
end
|
||
|
t2 = Thread.new do
|
||
|
sq << 1
|
||
|
end
|
||
|
t3 = Thread.new do
|
||
|
Thread.pass
|
||
|
sq.clear
|
||
|
end
|
||
|
[t3, t2, t1].each(&:join)
|
||
|
assert_equal sq.length, 2
|
||
|
end
|
||
|
def grind(num_threads, num_objects, num_iterations, klass, *args)
|
||
|
from_workers = klass.new(*args)
|
||
|
to_workers = klass.new(*args)
|
||