Skip to content

Commit 8cf4242

Browse files
authored
Merge pull request rails#49674 from Shopify/drop-mutex-m
Drop dependency on `mutex_m`
2 parents 633eb5a + bcdeea5 commit 8cf4242

File tree

6 files changed

+13
-24
lines changed

6 files changed

+13
-24
lines changed

Gemfile.lock

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ PATH
9191
drb
9292
i18n (>= 1.6, < 2)
9393
minitest (>= 5.1)
94-
mutex_m
9594
tzinfo (~> 2.0)
9695
rails (7.2.0.alpha)
9796
actioncable (= 7.2.0.alpha)
@@ -342,7 +341,6 @@ GEM
342341
msgpack (1.7.0)
343342
multi_json (1.15.0)
344343
multipart-post (2.2.3)
345-
mutex_m (0.1.2)
346344
mysql2 (0.5.5)
347345
net-http-persistent (4.0.1)
348346
connection_pool (~> 2.2)

actionpack/lib/action_controller/metal/params_wrapper.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,7 @@ module ParamsWrapper
8484

8585
EXCLUDE_PARAMETERS = %w(authenticity_token _method utf8)
8686

87-
require "mutex_m"
88-
8987
class Options < Struct.new(:name, :format, :include, :exclude, :klass, :model) # :nodoc:
90-
include Mutex_m
91-
9288
def self.from_hash(hash)
9389
name = hash[:name]
9490
format = Array(hash[:format])
@@ -99,6 +95,7 @@ def self.from_hash(hash)
9995

10096
def initialize(name, format, include, exclude, klass, model) # :nodoc:
10197
super
98+
@mutex = Mutex.new
10299
@include_set = include
103100
@name_set = name
104101
end
@@ -111,7 +108,7 @@ def include
111108
return super if @include_set
112109

113110
m = model
114-
synchronize do
111+
@mutex.synchronize do
115112
return super if @include_set
116113

117114
@include_set = true
@@ -144,7 +141,7 @@ def name
144141
return super if @name_set
145142

146143
m = model
147-
synchronize do
144+
@mutex.synchronize do
148145
return super if @name_set
149146

150147
@name_set = true

activerecord/lib/active_record/attribute_methods.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# frozen_string_literal: true
22

3-
require "mutex_m"
43
require "active_support/core_ext/enumerable"
54

65
module ActiveRecord
@@ -24,7 +23,7 @@ module AttributeMethods
2423
RESTRICTED_CLASS_METHODS = %w(private public protected allocate new name parent superclass)
2524

2625
class GeneratedAttributeMethods < Module # :nodoc:
27-
include Mutex_m
26+
LOCK = Monitor.new
2827
end
2928

3029
class << self
@@ -68,7 +67,7 @@ def generate_alias_attributes # :nodoc:
6867
superclass.generate_alias_attributes unless superclass == Base
6968
return if @alias_attributes_mass_generated
7069

71-
generated_attribute_methods.synchronize do
70+
GeneratedAttributeMethods::LOCK.synchronize do
7271
return if @alias_attributes_mass_generated
7372
ActiveSupport::CodeGenerator.batch(generated_attribute_methods, __FILE__, __LINE__) do |code_generator|
7473
aliases_by_attribute_name.each do |old_name, new_names|
@@ -127,7 +126,7 @@ def define_attribute_methods # :nodoc:
127126
return false if @attribute_methods_generated
128127
# Use a mutex; we don't want two threads simultaneously trying to define
129128
# attribute methods.
130-
generated_attribute_methods.synchronize do
129+
GeneratedAttributeMethods::LOCK.synchronize do
131130
return false if @attribute_methods_generated
132131
superclass.define_attribute_methods unless base_class?
133132
super(attribute_names)
@@ -136,7 +135,7 @@ def define_attribute_methods # :nodoc:
136135
end
137136

138137
def undefine_attribute_methods # :nodoc:
139-
generated_attribute_methods.synchronize do
138+
GeneratedAttributeMethods::LOCK.synchronize do
140139
super if defined?(@attribute_methods_generated) && @attribute_methods_generated
141140
@attribute_methods_generated = false
142141
@alias_attributes_mass_generated = false

activerecord/lib/active_record/relation/delegation.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# frozen_string_literal: true
22

3-
require "mutex_m"
43
require "active_support/core_ext/module/delegation"
54

65
module ActiveRecord
@@ -67,10 +66,10 @@ def generated_relation_methods
6766
end
6867

6968
class GeneratedRelationMethods < Module # :nodoc:
70-
include Mutex_m
69+
MUTEX = Mutex.new
7170

7271
def generate_method(method)
73-
synchronize do
72+
MUTEX.synchronize do
7473
return if method_defined?(method)
7574

7675
if /\A[a-zA-Z_]\w*[!?]?\z/.match?(method) && !DELEGATION_RESERVED_METHOD_NAMES.include?(method.to_s)

activesupport/activesupport.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,5 @@ Gem::Specification.new do |s|
4141
s.add_dependency "minitest", ">= 5.1"
4242
s.add_dependency "base64"
4343
s.add_dependency "drb"
44-
s.add_dependency "mutex_m"
4544
s.add_dependency "bigdecimal"
4645
end

activesupport/lib/active_support/notifications/fanout.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# frozen_string_literal: true
22

3-
require "mutex_m"
43
require "concurrent/map"
54
require "set"
65
require "active_support/core_ext/object/try"
@@ -45,15 +44,13 @@ def iterate_guarding_exceptions(listeners)
4544
#
4645
# This class is thread safe. All methods are reentrant.
4746
class Fanout
48-
include Mutex_m
49-
5047
def initialize
48+
@mutex = Mutex.new
5149
@string_subscribers = Concurrent::Map.new { |h, k| h.compute_if_absent(k) { [] } }
5250
@other_subscribers = []
5351
@all_listeners_for = Concurrent::Map.new
5452
@groups_for = Concurrent::Map.new
5553
@silenceable_groups_for = Concurrent::Map.new
56-
super
5754
end
5855

5956
def inspect # :nodoc:
@@ -63,7 +60,7 @@ def inspect # :nodoc:
6360

6461
def subscribe(pattern = nil, callable = nil, monotonic: false, &block)
6562
subscriber = Subscribers.new(pattern, callable || block, monotonic)
66-
synchronize do
63+
@mutex.synchronize do
6764
case pattern
6865
when String
6966
@string_subscribers[pattern] << subscriber
@@ -79,7 +76,7 @@ def subscribe(pattern = nil, callable = nil, monotonic: false, &block)
7976
end
8077

8178
def unsubscribe(subscriber_or_name)
82-
synchronize do
79+
@mutex.synchronize do
8380
case subscriber_or_name
8481
when String
8582
@string_subscribers[subscriber_or_name].clear
@@ -294,7 +291,7 @@ def publish_event(event)
294291

295292
def all_listeners_for(name)
296293
# this is correctly done double-checked locking (Concurrent::Map's lookups have volatile semantics)
297-
@all_listeners_for[name] || synchronize do
294+
@all_listeners_for[name] || @mutex.synchronize do
298295
# use synchronisation when accessing @subscribers
299296
@all_listeners_for[name] ||=
300297
@string_subscribers[name] + @other_subscribers.select { |s| s.subscribed_to?(name) }

0 commit comments

Comments
 (0)