diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2012-12-14 11:12:50 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2012-12-14 11:12:50 -0800 |
commit | 49219293e589093a3ceff9279fd4803271da082d (patch) | |
tree | eda4cbbcf56a41b6dd1b24f5b899b706d2b2cc25 /activesupport | |
parent | ce130ef78cc0ac244db63c1ce52f7eb8b2d8c9d8 (diff) | |
parent | 45448a578877f6a753492113d72cc3512a6f1720 (diff) | |
download | rails-49219293e589093a3ceff9279fd4803271da082d.tar.gz rails-49219293e589093a3ceff9279fd4803271da082d.tar.bz2 rails-49219293e589093a3ceff9279fd4803271da082d.zip |
Merge pull request #8510 from thedarkone/thread_safety_improvements
Thread safety improvements
Diffstat (limited to 'activesupport')
5 files changed, 15 insertions, 10 deletions
diff --git a/activesupport/activesupport.gemspec b/activesupport/activesupport.gemspec index a4216d2cb4..4c9e59dbd2 100644 --- a/activesupport/activesupport.gemspec +++ b/activesupport/activesupport.gemspec @@ -24,4 +24,5 @@ Gem::Specification.new do |s| s.add_dependency 'multi_json', '~> 1.3' s.add_dependency 'tzinfo', '~> 0.3.33' s.add_dependency 'minitest', '~> 4.1' + s.add_dependency 'thread_safe','~> 0.1' end diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index efd351d741..fff4c776a9 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -1,5 +1,6 @@ require 'set' require 'thread' +require 'thread_safe' require 'pathname' require 'active_support/core_ext/module/aliasing' require 'active_support/core_ext/module/attribute_accessors' @@ -517,7 +518,7 @@ module ActiveSupport #:nodoc: class ClassCache def initialize - @store = Hash.new + @store = ThreadSafe::Cache.new end def empty? diff --git a/activesupport/lib/active_support/inflector/inflections.rb b/activesupport/lib/active_support/inflector/inflections.rb index 6f259a093b..9cf4b2b2ba 100644 --- a/activesupport/lib/active_support/inflector/inflections.rb +++ b/activesupport/lib/active_support/inflector/inflections.rb @@ -1,3 +1,4 @@ +require 'thread_safe' require 'active_support/core_ext/array/prepend_and_append' require 'active_support/i18n' @@ -24,9 +25,10 @@ module ActiveSupport # singularization rules that is runs. This guarantees that your rules run # before any of the rules that may already have been loaded. class Inflections + @__instance__ = ThreadSafe::Cache.new + def self.instance(locale = :en) - @__instance__ ||= Hash.new { |h, k| h[k] = new } - @__instance__[locale] + @__instance__[locale] ||= new end attr_reader :plurals, :singulars, :uncountables, :humans, :acronyms, :acronym_regex diff --git a/activesupport/lib/active_support/key_generator.rb b/activesupport/lib/active_support/key_generator.rb index 6beb2b6afa..71654dbb87 100644 --- a/activesupport/lib/active_support/key_generator.rb +++ b/activesupport/lib/active_support/key_generator.rb @@ -1,4 +1,4 @@ -require 'mutex_m' +require 'thread_safe' require 'openssl' module ActiveSupport @@ -28,16 +28,14 @@ module ActiveSupport class CachingKeyGenerator def initialize(key_generator) @key_generator = key_generator - @cache_keys = {}.extend(Mutex_m) + @cache_keys = ThreadSafe::Cache.new end # Returns a derived key suitable for use. The default key_size is chosen # to be compatible with the default settings of ActiveSupport::MessageVerifier. # i.e. OpenSSL::Digest::SHA1#block_length def generate_key(salt, key_size=64) - @cache_keys.synchronize do - @cache_keys["#{salt}#{key_size}"] ||= @key_generator.generate_key(salt, key_size) - end + @cache_keys["#{salt}#{key_size}"] ||= @key_generator.generate_key(salt, key_size) end end diff --git a/activesupport/lib/active_support/notifications/fanout.rb b/activesupport/lib/active_support/notifications/fanout.rb index 2e5bcf4639..7588fdb67c 100644 --- a/activesupport/lib/active_support/notifications/fanout.rb +++ b/activesupport/lib/active_support/notifications/fanout.rb @@ -1,4 +1,5 @@ require 'mutex_m' +require 'thread_safe' module ActiveSupport module Notifications @@ -11,7 +12,7 @@ module ActiveSupport def initialize @subscribers = [] - @listeners_for = {} + @listeners_for = ThreadSafe::Cache.new super end @@ -44,7 +45,9 @@ module ActiveSupport end def listeners_for(name) - synchronize do + # this is correctly done double-checked locking (ThreadSafe::Cache's lookups have volatile semantics) + @listeners_for[name] || synchronize do + # use synchronisation when accessing @subscribers @listeners_for[name] ||= @subscribers.select { |s| s.subscribed_to?(name) } end end |