* 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
This commit is contained in:
glass 2014-01-14 12:14:48 +00:00
Родитель 0a8988c132
Коммит 79e69ec715
3 изменённых файлов: 45 добавлений и 0 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -502,6 +502,20 @@ rb_szqueue_pop(int argc, VALUE *argv, VALUE self)
return szqueue_do_pop(self, should_block);
}
/*
* 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
*
@ -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. */

Просмотреть файл

@ -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)