[前][次][番号順一覧][スレッド一覧]

ruby-changes:32516

From: glass <ko1@a...>
Date: Tue, 14 Jan 2014 21:14:56 +0900 (JST)
Subject: [ruby-changes:32516] glass:r44595 (trunk): * ext/thread/thread.c (rb_szqueue_clear): notify SZQUEUE_WAITERS

glass	2014-01-14 21:14:48 +0900 (Tue, 14 Jan 2014)

  New Revision: 44595

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=44595

  Log:
    * 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.

  Modified files:
    trunk/ChangeLog
    trunk/ext/thread/thread.c
    trunk/test/thread/test_queue.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 44594)
+++ ChangeLog	(revision 44595)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Jan 14 21:07:22 2014  Masaki Matsushita  <glass.saga@g...>
+
+	* 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.
+
 Tue Jan 14 15:58:43 2014  Nobuyoshi Nakada  <nobu@r...>
 
 	* ext/bigdecimal/bigdecimal.c (CLASS_NAME): macro to wrap
Index: ext/thread/thread.c
===================================================================
--- ext/thread/thread.c	(revision 44594)
+++ ext/thread/thread.c	(revision 44595)
@@ -503,6 +503,20 @@ rb_szqueue_pop(int argc, VALUE *argv, VA https://github.com/ruby/ruby/blob/trunk/ext/thread/thread.c#L503
 }
 
 /*
+ * Document-method: Queue#clear
+ *
+ * Removes all objects from the queue.
+ */
+
+static VALUE
+rb_szqueue_clear(VALUE self)
+{
+    rb_ary_clear(GET_QUEUE_QUE(self));
+    wakeup_all_threads(GET_SZQUEUE_WAITERS(self));
+    return self;
+}
+
+/*
  * Document-method: SizedQueue#num_waiting
  *
  * Returns the number of threads waiting on the queue.
@@ -586,6 +600,7 @@ Init_thread(void) https://github.com/ruby/ruby/blob/trunk/ext/thread/thread.c#L600
     rb_define_method(rb_cSizedQueue, "max=", rb_szqueue_max_set, 1);
     rb_define_method(rb_cSizedQueue, "push", rb_szqueue_push, 1);
     rb_define_method(rb_cSizedQueue, "pop", rb_szqueue_pop, -1);
+    rb_define_method(rb_cSizedQueue, "clear", rb_szqueue_clear, 0);
     rb_define_method(rb_cSizedQueue, "num_waiting", rb_szqueue_num_waiting, 0);
 
     /* Alias for #push. */
Index: test/thread/test_queue.rb
===================================================================
--- test/thread/test_queue.rb	(revision 44594)
+++ test/thread/test_queue.rb	(revision 44595)
@@ -122,6 +122,28 @@ class TestQueue < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/thread/test_queue.rb#L122
     assert_same q, retval
   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_push_return_value
     q = SizedQueue.new(1)
     retval = q.push(1)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]