From: sheepman Date: 2008-01-19T00:56:13+09:00 Subject: [ruby-dev:33176] Re: should initialize the group of threads こんばんは sheepman です。 フリーズした ThreadGroup に所属するスレッドからは スレッドを生成できないという仕様があることを思い出しました。 この場所でチェックすべきかは分かりませんがパッチとテストです。 Index: thread.c =================================================================== --- thread.c (revision 15114) +++ thread.c (working copy) @@ -375,15 +375,20 @@ thread_create_core(VALUE thval, VALUE ar { rb_thread_t *th; - GetThreadPtr(thval, th); - + if (OBJ_FROZEN(GET_THREAD()->thgroup)) { + rb_raise(rb_eThreadError, + "can't start a new thread (frozen ThreadGroup)"); + } + + GetThreadPtr(thval, th); /* setup thread environment */ th->first_args = args; th->first_proc = fn ? Qfalse : rb_block_proc(); th->first_func = fn; th->priority = GET_THREAD()->priority; - + th->thgroup = GET_THREAD()->thgroup; + native_mutex_initialize(&th->interrupt_lock); /* kick thread */ st_insert(th->vm->living_threads, thval, (st_data_t) th->thread_id); Index: test/ruby/test_thread.rb =================================================================== --- test/ruby/test_thread.rb (revision 15114) +++ test/ruby/test_thread.rb (working copy) @@ -22,3 +22,36 @@ class TestThread < Test::Unit::TestCase end end +class TestThreadGroup < Test::Unit::TestCase + def test_thread_init + thgrp = ThreadGroup.new + Thread.new{ + thgrp.add(Thread.current) + assert_equal(thgrp, Thread.new{sleep 1}.group) + }.join + end + + def test_frozen_thgroup + thgrp = ThreadGroup.new + Thread.new{ + thgrp.add(Thread.current) + thgrp.freeze + assert_raise(ThreadError) do + Thread.new{1}.join + end + }.join + end + + def test_enclosed_thgroup + thgrp = ThreadGroup.new + thgrp.enclose + Thread.new{ + assert_raise(ThreadError) do + thgrp.add(Thread.current) + end + assert_nothing_raised do + Thread.new{1}.join + end + }.join + end +end -- sheepman / TAMURA Takashi sheepman@sheepman.sakura.ne.jp