diff --git a/ext/monitor/lib/monitor.rb b/ext/monitor/lib/monitor.rb index 821dccadbe..1883cb72f6 100644 --- a/ext/monitor/lib/monitor.rb +++ b/ext/monitor/lib/monitor.rb @@ -208,6 +208,10 @@ module MonitorMixin # Monitor object. # def new_cond + unless defined?(@mon_data) + mon_initialize + @mon_initialized_by_new_cond = true + end return ConditionVariable.new(@mon_data) end @@ -224,8 +228,12 @@ module MonitorMixin # Initializes the MonitorMixin after being included in a class or when an # object has been extended with the MonitorMixin def mon_initialize - if defined?(@mon_data) && @mon_data_owner_object_id == self.object_id - raise ThreadError, "already initialized" + if defined?(@mon_data) + if defined?(@mon_initialized_by_new_cond) + return # already initalized. + elsif @mon_data_owner_object_id == self.object_id + raise ThreadError, "already initialized" + end end @mon_data = ::Monitor.new @mon_data_owner_object_id = self.object_id diff --git a/test/monitor/test_monitor.rb b/test/monitor/test_monitor.rb index 950db917e6..721c848d68 100644 --- a/test/monitor/test_monitor.rb +++ b/test/monitor/test_monitor.rb @@ -223,6 +223,19 @@ class TestMonitor < Test::Unit::TestCase assert_join_threads([th, th2]) end + class NewCondTest + include MonitorMixin + attr_reader :cond + def initialize + @cond = new_cond + super # mon_initialize + end + end + + def test_new_cond_before_initialize + assert NewCondTest.new.cond.instance_variable_get(:@monitor) != nil + end + def test_timedwait cond = @monitor.new_cond b = "foo"