Project

General

Profile

Bug #5258 » queue.patch

nagachika (Tomoyuki Chikanaga), 09/03/2011 08:08 PM

View differences:

lib/thread.rb
#
def pop(non_block=false)
@mutex.synchronize{
while true
if @que.empty?
raise ThreadError, "queue empty" if non_block
# @waiting.include? check is necessary for avoiding a race against
# Thread.wakeup [Bug 5195]
@waiting.push Thread.current unless @waiting.include?(Thread.current)
@mutex.sleep
else
return @que.shift
begin
while true
if @que.empty?
raise ThreadError, "queue empty" if non_block
# @waiting.include? check is necessary for avoiding a race against
# Thread.wakeup [Bug 5195]
@waiting.push Thread.current unless @waiting.include?(Thread.current)
@mutex.sleep
else
return @que.shift
end
end
ensure
@waiting.delete(Thread.current)
end
}
end
......
#
def push(obj)
@mutex.synchronize{
while true
break if @que.length < @max
@queue_wait.push Thread.current
@mutex.sleep
begin
while true
break if @que.length < @max
@queue_wait.push Thread.current
@mutex.sleep
end
ensure
@queue_wait.delete(Thread.current)
end
@que.push obj
test/thread/test_queue.rb
assert_raise(ArgumentError) { q.max = -1 }
assert_equal(1, q.max)
end
def test_queue_pop_interrupt
q = Queue.new
t1 = Thread.new { q.pop }
sleep 0.01 until t1.stop?
t1.kill.join
assert_equal(0, q.num_waiting)
end
def test_sized_queue_pop_interrupt
q = SizedQueue.new(1)
t1 = Thread.new { q.pop }
sleep 0.01 until t1.stop?
t1.kill.join
assert_equal(0, q.num_waiting)
end
def test_sized_queue_push_interrupt
q = SizedQueue.new(1)
q.push(1)
t1 = Thread.new { q.push(2) }
sleep 0.01 until t1.stop?
t1.kill.join
assert_equal(0, q.num_waiting)
end
end
(2-2/2)