Skip to content

Commit a8d112b

Browse files
author
Charlie Somerville
committed
* ext/thread/thread.c (rb_szqueue_push): check GET_SZQUEUE_WAITERS
instead of GET_QUEUE_WAITERS to prevent deadlock. Patch by Eric Wong. [Bug ruby#9302] [ruby-core:59324] * test/thread/test_queue.rb: add test git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44583 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 6013958 commit a8d112b

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

ext/thread/thread.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ static VALUE
459459
rb_szqueue_push(VALUE self, VALUE obj)
460460
{
461461
struct waiting_delete args;
462-
args.waiting = GET_QUEUE_WAITERS(self);
462+
args.waiting = GET_SZQUEUE_WAITERS(self);
463463
args.th = rb_thread_current();
464464

465465
while (queue_length(self) >= GET_SZQUEUE_ULONGMAX(self)) {

test/thread/test_queue.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,29 @@ def test_sized_queue_clear_return_value
134134
assert_same q, retval
135135
end
136136

137+
def test_sized_queue_throttle
138+
q = SizedQueue.new(1)
139+
i = 0
140+
consumer = Thread.new do
141+
while q.pop
142+
i += 1
143+
Thread.pass
144+
end
145+
end
146+
nprod = 4
147+
npush = 100
148+
149+
producer = nprod.times.map do
150+
Thread.new do
151+
npush.times { q.push(true) }
152+
end
153+
end
154+
producer.each(&:join)
155+
q.push(nil)
156+
consumer.join
157+
assert_equal(nprod * npush, i)
158+
end
159+
137160
def test_queue_thread_raise
138161
q = Queue.new
139162
th1 = Thread.new do

0 commit comments

Comments
 (0)