ruby-changes:53606
From: shugo <ko1@a...>
Date: Tue, 20 Nov 2018 12:02:00 +0900 (JST)
Subject: [ruby-changes:53606] shugo:r65822 (trunk): lib/monitor.rb: prevent to initialize MonitorMixin twice
shugo 2018-11-20 12:01:55 +0900 (Tue, 20 Nov 2018) New Revision: 65822 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=65822 Log: lib/monitor.rb: prevent to initialize MonitorMixin twice Suggested by Benoit Daloze. [ruby-core:88504] [Feature #15000] Added files: trunk/test/monitor/test_monitor_mixin.rb Modified files: trunk/lib/monitor.rb Index: test/monitor/test_monitor_mixin.rb =================================================================== --- test/monitor/test_monitor_mixin.rb (nonexistent) +++ test/monitor/test_monitor_mixin.rb (revision 65822) @@ -0,0 +1,37 @@ https://github.com/ruby/ruby/blob/trunk/test/monitor/test_monitor_mixin.rb#L1 +# frozen_string_literal: false +require 'test/unit' +require 'monitor' + +class TestMonitorMixin < Test::Unit::TestCase + def test_cond + a = "foo" + a.extend(MonitorMixin) + cond = a.new_cond + queue1 = Queue.new + th = Thread.start do + queue1.deq + a.synchronize do + a.replace("bar") + cond.signal + end + end + th2 = Thread.start do + a.synchronize do + queue1.enq(nil) + assert_equal("foo", a) + result1 = cond.wait + assert_equal(true, result1) + assert_equal("bar", a) + end + end + assert_join_threads([th, th2]) + end + + def test_initialize_twice + a = Object.new + a.extend(MonitorMixin) + assert_raise(ThreadError) do + a.send(:mon_initialize) + end + end +end Index: lib/monitor.rb =================================================================== --- lib/monitor.rb (revision 65821) +++ lib/monitor.rb (revision 65822) @@ -251,9 +251,12 @@ module MonitorMixin https://github.com/ruby/ruby/blob/trunk/lib/monitor.rb#L251 # 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_mutex) + raise ThreadError, "already initialized" + end + @mon_mutex = Thread::Mutex.new @mon_owner = nil @mon_count = 0 - @mon_mutex = Thread::Mutex.new end def mon_check_owner -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/