mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #12398 from thedarkone/thread-safety-stuff
Thread safety fixes
This commit is contained in:
commit
f90a5888ba
3 changed files with 35 additions and 25 deletions
|
@ -9,8 +9,8 @@ module ActionDispatch
|
|||
attr_reader :memos
|
||||
|
||||
def initialize
|
||||
@regexp_states = Hash.new { |h,k| h[k] = {} }
|
||||
@string_states = Hash.new { |h,k| h[k] = {} }
|
||||
@regexp_states = {}
|
||||
@string_states = {}
|
||||
@accepting = {}
|
||||
@memos = Hash.new { |h,k| h[k] = [] }
|
||||
end
|
||||
|
@ -111,14 +111,8 @@ module ActionDispatch
|
|||
end
|
||||
|
||||
def []=(from, to, sym)
|
||||
case sym
|
||||
when String
|
||||
@string_states[from][sym] = to
|
||||
when Regexp
|
||||
@regexp_states[from][sym] = to
|
||||
else
|
||||
raise ArgumentError, 'unknown symbol: %s' % sym.class
|
||||
end
|
||||
to_mappings = states_hash_for(sym)[from] ||= {}
|
||||
to_mappings[sym] = to
|
||||
end
|
||||
|
||||
def states
|
||||
|
@ -137,18 +131,35 @@ module ActionDispatch
|
|||
|
||||
private
|
||||
|
||||
def states_hash_for(sym)
|
||||
case sym
|
||||
when String
|
||||
@string_states
|
||||
when Regexp
|
||||
@regexp_states
|
||||
else
|
||||
raise ArgumentError, 'unknown symbol: %s' % sym.class
|
||||
end
|
||||
end
|
||||
|
||||
def move_regexp(t, a)
|
||||
return [] if t.empty?
|
||||
|
||||
t.map { |s|
|
||||
@regexp_states[s].map { |re, v| re === a ? v : nil }
|
||||
if states = @regexp_states[s]
|
||||
states.map { |re, v| re === a ? v : nil }
|
||||
end
|
||||
}.flatten.compact.uniq
|
||||
end
|
||||
|
||||
def move_string(t, a)
|
||||
return [] if t.empty?
|
||||
|
||||
t.map { |s| @string_states[s][a] }.compact
|
||||
t.map do |s|
|
||||
if states = @string_states[s]
|
||||
states[a]
|
||||
end
|
||||
end.compact
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
# encoding: utf-8
|
||||
|
||||
require 'thread_safe'
|
||||
|
||||
module ActionDispatch
|
||||
module Journey # :nodoc:
|
||||
module Visitors # :nodoc:
|
||||
class Visitor # :nodoc:
|
||||
DISPATCH_CACHE = Hash.new { |h,k|
|
||||
DISPATCH_CACHE = ThreadSafe::Cache.new { |h,k|
|
||||
h[k] = :"visit_#{k}"
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
require 'active_support/core_ext/enumerable'
|
||||
require 'mutex_m'
|
||||
require 'thread_safe'
|
||||
|
||||
module ActiveRecord
|
||||
# = Active Record Attribute Methods
|
||||
|
@ -29,23 +30,18 @@ module ActiveRecord
|
|||
}
|
||||
|
||||
class AttributeMethodCache
|
||||
include Mutex_m
|
||||
|
||||
def initialize
|
||||
super
|
||||
@module = Module.new
|
||||
@method_cache = {}
|
||||
@method_cache = ThreadSafe::Cache.new
|
||||
end
|
||||
|
||||
def [](name)
|
||||
synchronize do
|
||||
@method_cache.fetch(name) {
|
||||
safe_name = name.unpack('h*').first
|
||||
temp_method = "__temp__#{safe_name}"
|
||||
ActiveRecord::AttributeMethods::AttrNames.set_name_cache safe_name, name
|
||||
@module.module_eval method_body(temp_method, safe_name), __FILE__, __LINE__
|
||||
@method_cache[name] = @module.instance_method temp_method
|
||||
}
|
||||
@method_cache.compute_if_absent(name) do
|
||||
safe_name = name.unpack('h*').first
|
||||
temp_method = "__temp__#{safe_name}"
|
||||
ActiveRecord::AttributeMethods::AttrNames.set_name_cache safe_name, name
|
||||
@module.module_eval method_body(temp_method, safe_name), __FILE__, __LINE__
|
||||
@module.instance_method temp_method
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue