merge revision(s) 44595: [Backport #9342]

* ext/thread/thread.c (rb_szqueue_clear): notify SZQUEUE_WAITERS
	  on SizedQueue#clear. [ruby-core:59462] [Bug #9342]

	* test/thread/test_queue.rb: add test. the patch is from
	  Justin Collins.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_0_0@44899 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nagachika 2014-02-09 16:07:41 +00:00
parent f0e5252835
commit 08e1428b7b
4 changed files with 88 additions and 1 deletions

View file

@ -1,3 +1,11 @@
Mon Feb 10 00:42:12 2014 Masaki Matsushita <glass.saga@gmail.com>
* ext/thread/thread.c (rb_szqueue_clear): notify SZQUEUE_WAITERS
on SizedQueue#clear. [ruby-core:59462] [Bug #9342]
* test/thread/test_queue.rb: add test. the patch is from
Justin Collins.
Mon Feb 10 00:27:33 2014 Nobuyoshi Nakada <nobu@ruby-lang.org> Mon Feb 10 00:27:33 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
* parse.y (local_push_gen, local_pop_gen): save cmdarg_stack to * parse.y (local_push_gen, local_pop_gen): save cmdarg_stack to

View file

@ -318,6 +318,18 @@ class SizedQueue < Queue
end end
end end
#
# Removes all objects from the queue.
#
def clear
super
@mutex.synchronize do
@max.times do
@enque_cond.signal
end
end
end
# #
# Alias of push # Alias of push
# #

View file

@ -108,4 +108,71 @@ class TestQueue < Test::Unit::TestCase
end end
} }
end 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 test_sized_queue_throttle
q = SizedQueue.new(1)
i = 0
consumer = Thread.new do
while q.pop
i += 1
Thread.pass
end
end
nprod = 4
npush = 100
producer = nprod.times.map do
Thread.new do
npush.times { q.push(true) }
end
end
producer.each(&:join)
q.push(nil)
consumer.join
assert_equal(nprod * npush, i)
end
def test_queue_thread_raise
q = Queue.new
th1 = Thread.new do
begin
q.pop
rescue RuntimeError
sleep
end
end
th2 = Thread.new do
sleep 0.1
q.pop
end
sleep 0.1
th1.raise
sleep 0.1
q << :s
assert_nothing_raised(TimeoutError) do
timeout(1) { th2.join }
end
end
end end

View file

@ -1,6 +1,6 @@
#define RUBY_VERSION "2.0.0" #define RUBY_VERSION "2.0.0"
#define RUBY_RELEASE_DATE "2014-02-10" #define RUBY_RELEASE_DATE "2014-02-10"
#define RUBY_PATCHLEVEL 398 #define RUBY_PATCHLEVEL 399
#define RUBY_RELEASE_YEAR 2014 #define RUBY_RELEASE_YEAR 2014
#define RUBY_RELEASE_MONTH 2 #define RUBY_RELEASE_MONTH 2