aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorshugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-11-20 03:01:55 +0000
committershugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-11-20 03:01:55 +0000
commit8d68f422dc1a42453fabd697f004e35c618ce3ea (patch)
tree80ebfcafddf9bd9b52113b76943c55671788cddb
parent59676f6d030ff06d6e577860150ed1aa3aa1046a (diff)
downloadruby-8d68f422dc1a42453fabd697f004e35c618ce3ea.tar.gz
lib/monitor.rb: prevent to initialize MonitorMixin twice
Suggested by Benoit Daloze. [ruby-core:88504] [Feature #15000] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65822 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--lib/monitor.rb5
-rw-r--r--test/monitor/test_monitor_mixin.rb37
2 files changed, 41 insertions, 1 deletions
diff --git a/lib/monitor.rb b/lib/monitor.rb
index 288ed755ea..86c779f184 100644
--- a/lib/monitor.rb
+++ b/lib/monitor.rb
@@ -251,9 +251,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_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
diff --git a/test/monitor/test_monitor_mixin.rb b/test/monitor/test_monitor_mixin.rb
new file mode 100644
index 0000000000..24ad6b78f3
--- /dev/null
+++ b/test/monitor/test_monitor_mixin.rb
@@ -0,0 +1,37 @@
+# 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