aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r--activesupport/lib/active_support/concurrency/latch.rb27
-rw-r--r--activesupport/lib/active_support/core_ext/class/attribute.rb9
2 files changed, 33 insertions, 3 deletions
diff --git a/activesupport/lib/active_support/concurrency/latch.rb b/activesupport/lib/active_support/concurrency/latch.rb
new file mode 100644
index 0000000000..1507de433e
--- /dev/null
+++ b/activesupport/lib/active_support/concurrency/latch.rb
@@ -0,0 +1,27 @@
+require 'thread'
+require 'monitor'
+
+module ActiveSupport
+ module Concurrency
+ class Latch
+ def initialize(count = 1)
+ @count = count
+ @lock = Monitor.new
+ @cv = @lock.new_cond
+ end
+
+ def release
+ @lock.synchronize do
+ @count -= 1 if @count > 0
+ @cv.broadcast if @count.zero?
+ end
+ end
+
+ def await
+ @lock.synchronize do
+ @cv.wait_while { @count > 0 }
+ end
+ end
+ end
+ end
+end
diff --git a/activesupport/lib/active_support/core_ext/class/attribute.rb b/activesupport/lib/active_support/core_ext/class/attribute.rb
index da0a12136c..7b6f8ab0a1 100644
--- a/activesupport/lib/active_support/core_ext/class/attribute.rb
+++ b/activesupport/lib/active_support/core_ext/class/attribute.rb
@@ -79,12 +79,14 @@ class Class
def self.#{name}=(val)
singleton_class.class_eval do
- redefine_method(:#{name}) { val }
+ remove_possible_method(:#{name})
+ define_method(:#{name}) { val }
end
if singleton_class?
class_eval do
- redefine_method(:#{name}) do
+ remove_possible_method(:#{name})
+ def #{name}
defined?(@#{name}) ? @#{name} : singleton_class.#{name}
end
end
@@ -93,7 +95,8 @@ class Class
end
if instance_reader
- redefine_method(:#{name}) do
+ remove_possible_method :#{name}
+ def #{name}
defined?(@#{name}) ? @#{name} : self.class.#{name}
end