ruby-changes:32853
From: usa <ko1@a...>
Date: Fri, 14 Feb 2014 12:48:48 +0900 (JST)
Subject: [ruby-changes:32853] usa:r44932 (ruby_1_9_3): * lib/thread.rb (SizedQueue#clear): wake waiting threads when called.
usa 2014-02-14 12:48:42 +0900 (Fri, 14 Feb 2014) New Revision: 44932 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=44932 Log: * lib/thread.rb (SizedQueue#clear): wake waiting threads when called. [Bug #9342] [ruby-core:59462] * test/thread/test_queue.rb: add a test for above. patched by Justin Collins. Modified files: branches/ruby_1_9_3/ChangeLog branches/ruby_1_9_3/lib/thread.rb branches/ruby_1_9_3/test/thread/test_queue.rb branches/ruby_1_9_3/version.h Index: ruby_1_9_3/ChangeLog =================================================================== --- ruby_1_9_3/ChangeLog (revision 44931) +++ ruby_1_9_3/ChangeLog (revision 44932) @@ -1,3 +1,12 @@ https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ChangeLog#L1 +Fri Feb 14 12:45:07 2014 NAKAMURA Usaku <usa@r...> + + * lib/thread.rb (SizedQueue#clear): wake waiting threads when called. + [Bug #9342] [ruby-core:59462] + + * test/thread/test_queue.rb: add a test for above. + + patched by Justin Collins. + Thu Feb 6 09:06:00 2014 Kenta Murata <mrkn@c...> * configure.in (POSTLINK): sign built program using RUBY_CODESIGN Index: ruby_1_9_3/lib/thread.rb =================================================================== --- ruby_1_9_3/lib/thread.rb (revision 44931) +++ ruby_1_9_3/lib/thread.rb (revision 44932) @@ -355,6 +355,22 @@ class SizedQueue < Queue https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/lib/thread.rb#L355 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: Index: ruby_1_9_3/version.h =================================================================== --- ruby_1_9_3/version.h (revision 44931) +++ ruby_1_9_3/version.h (revision 44932) @@ -1,10 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/version.h#L1 #define RUBY_VERSION "1.9.3" -#define RUBY_PATCHLEVEL 515 +#define RUBY_PATCHLEVEL 516 -#define RUBY_RELEASE_DATE "2014-02-06" +#define RUBY_RELEASE_DATE "2014-02-14" #define RUBY_RELEASE_YEAR 2014 #define RUBY_RELEASE_MONTH 2 -#define RUBY_RELEASE_DAY 6 +#define RUBY_RELEASE_DAY 14 #include "ruby/version.h" Index: ruby_1_9_3/test/thread/test_queue.rb =================================================================== --- ruby_1_9_3/test/thread/test_queue.rb (revision 44931) +++ ruby_1_9_3/test/thread/test_queue.rb (revision 44932) @@ -10,6 +10,28 @@ class TestQueue < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/test/thread/test_queue.rb#L10 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) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/