summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorglass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-01-14 12:14:48 +0000
committerglass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-01-14 12:14:48 +0000
commit79e69ec7151662ef6fcb149008d4baab4c4a682c (patch)
tree7c9fdec0248d41ea12244ee42acf7894c4db8fca
parent0a8988c132164a3a39508fabb863f82c22f7063a (diff)
* 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/trunk@44595 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog8
-rw-r--r--ext/thread/thread.c15
-rw-r--r--test/thread/test_queue.rb22
3 files changed, 45 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 129ec9f1f5..0161f2a77a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Tue Jan 14 21:07:22 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.
+
Tue Jan 14 15:58:43 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
* ext/bigdecimal/bigdecimal.c (CLASS_NAME): macro to wrap
diff --git a/ext/thread/thread.c b/ext/thread/thread.c
index 208d1172df..5fb51fe756 100644
--- a/ext/thread/thread.c
+++ b/ext/thread/thread.c
@@ -503,6 +503,20 @@ rb_szqueue_pop(int argc, VALUE *argv, VALUE self)
}
/*
+ * 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)
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. */
diff --git a/test/thread/test_queue.rb b/test/thread/test_queue.rb
index 438e1e75e3..bf8344fade 100644
--- a/test/thread/test_queue.rb
+++ b/test/thread/test_queue.rb
@@ -122,6 +122,28 @@ class TestQueue < Test::Unit::TestCase
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)