Skip to content

Commit 0ea374c

Browse files
committed
Use IsolatedExecutionState across Active Support
Ref: rails#43596 This allow users to declare wether their unit of work is isolated by fibers or by threads. `PerThreadRegistry` and `thread_mattr_accessor` were intentionally left out as they require documentation change. I'll submit them in separate pull requests.
1 parent 3b01e92 commit 0ea374c

File tree

11 files changed

+54
-26
lines changed

11 files changed

+54
-26
lines changed

activesupport/lib/active_support/core_ext/date/calculations.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ class << self
1717
# If <tt>Date.beginning_of_week</tt> has not been set for the current request, returns the week start specified in <tt>config.beginning_of_week</tt>.
1818
# If no config.beginning_of_week was specified, returns :monday.
1919
def beginning_of_week
20-
Thread.current[:beginning_of_week] || beginning_of_week_default || :monday
20+
::ActiveSupport::IsolatedExecutionState[:beginning_of_week] || beginning_of_week_default || :monday
2121
end
2222

2323
# Sets <tt>Date.beginning_of_week</tt> to a week start (e.g. :monday) for current request/thread.
2424
#
2525
# This method accepts any of the following day symbols:
2626
# :monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday
2727
def beginning_of_week=(week_start)
28-
Thread.current[:beginning_of_week] = find_beginning_of_week!(week_start)
28+
::ActiveSupport::IsolatedExecutionState[:beginning_of_week] = find_beginning_of_week!(week_start)
2929
end
3030

3131
# Returns week start day symbol (e.g. :monday), or raises an +ArgumentError+ for invalid day symbol.

activesupport/lib/active_support/core_ext/time/zones.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class << self
1212
# Returns the TimeZone for the current request, if this has been set (via Time.zone=).
1313
# If <tt>Time.zone</tt> has not been set for the current request, returns the TimeZone specified in <tt>config.time_zone</tt>.
1414
def zone
15-
Thread.current[:time_zone] || zone_default
15+
::ActiveSupport::IsolatedExecutionState[:time_zone] || zone_default
1616
end
1717

1818
# Sets <tt>Time.zone</tt> to a TimeZone object for the current request/thread.
@@ -39,7 +39,7 @@ def zone
3939
# end
4040
# end
4141
def zone=(time_zone)
42-
Thread.current[:time_zone] = find_zone!(time_zone)
42+
::ActiveSupport::IsolatedExecutionState[:time_zone] = find_zone!(time_zone)
4343
end
4444

4545
# Allows override of <tt>Time.zone</tt> locally inside supplied block;

activesupport/lib/active_support/execution_context.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def clear
4646

4747
private
4848
def store
49-
Thread.current[:active_support_execution_context] ||= {}
49+
IsolatedExecutionState[:active_support_execution_context] ||= {}
5050
end
5151
end
5252
end

activesupport/lib/active_support/execution_wrapper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ def self.inherited(other) # :nodoc:
113113
self.active = Concurrent::Hash.new
114114

115115
def self.active? # :nodoc:
116-
@active[Thread.current]
116+
@active[IsolatedExecutionState.unique_id]
117117
end
118118

119119
def run! # :nodoc:
120-
self.class.active[Thread.current] = true
120+
self.class.active[IsolatedExecutionState.unique_id] = true
121121
run
122122
end
123123

@@ -132,7 +132,7 @@ def run # :nodoc:
132132
def complete!
133133
complete
134134
ensure
135-
self.class.active.delete Thread.current
135+
self.class.active.delete(IsolatedExecutionState.unique_id)
136136
end
137137

138138
def complete # :nodoc:

activesupport/lib/active_support/isolated_execution_state.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def isolation_level=(level)
2525
end
2626
end
2727

28+
def unique_id
29+
self[:__id__] ||= Object.new
30+
end
31+
2832
def [](key)
2933
current[key]
3034
end

activesupport/lib/active_support/logger_thread_safe_level.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ def #{severity.downcase}? # def debug?
1818
end
1919

2020
def local_level
21-
# Note: Thread#[] is fiber-local
22-
Thread.current[:logger_thread_safe_level]
21+
IsolatedExecutionState[:logger_thread_safe_level]
2322
end
2423

2524
def local_level=(level)
@@ -31,7 +30,7 @@ def local_level=(level)
3130
else
3231
raise ArgumentError, "Invalid log level: #{level.inspect}"
3332
end
34-
Thread.current[:logger_thread_safe_level] = level
33+
IsolatedExecutionState[:logger_thread_safe_level] = level
3534
end
3635

3736
def level

activesupport/lib/active_support/notifications/fanout.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,12 @@ def publish(name, *args)
226226
end
227227

228228
def start(name, id, payload)
229-
timestack = Thread.current[:_timestack] ||= []
229+
timestack = IsolatedExecutionState[:_timestack] ||= []
230230
timestack.push Time.now
231231
end
232232

233233
def finish(name, id, payload)
234-
timestack = Thread.current[:_timestack]
234+
timestack = IsolatedExecutionState[:_timestack]
235235
started = timestack.pop
236236
@delegate.call(name, started, Time.now, id, payload)
237237
end
@@ -243,27 +243,27 @@ def publish(name, *args)
243243
end
244244

245245
def start(name, id, payload)
246-
timestack = Thread.current[:_timestack_monotonic] ||= []
246+
timestack = IsolatedExecutionState[:_timestack_monotonic] ||= []
247247
timestack.push Process.clock_gettime(Process::CLOCK_MONOTONIC)
248248
end
249249

250250
def finish(name, id, payload)
251-
timestack = Thread.current[:_timestack_monotonic]
251+
timestack = IsolatedExecutionState[:_timestack_monotonic]
252252
started = timestack.pop
253253
@delegate.call(name, started, Process.clock_gettime(Process::CLOCK_MONOTONIC), id, payload)
254254
end
255255
end
256256

257257
class EventObject < Evented
258258
def start(name, id, payload)
259-
stack = Thread.current[:_event_stack] ||= []
259+
stack = IsolatedExecutionState[:_event_stack] ||= []
260260
event = build_event name, id, payload
261261
event.start!
262262
stack.push event
263263
end
264264

265265
def finish(name, id, payload)
266-
stack = Thread.current[:_event_stack]
266+
stack = IsolatedExecutionState[:_event_stack]
267267
event = stack.pop
268268
event.payload = payload
269269
event.finish!

activesupport/lib/active_support/tagged_logging.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def clear_tags!
5757
def current_tags
5858
# We use our object ID here to avoid conflicting with other instances
5959
thread_key = @thread_key ||= "activesupport_tagged_logging_tags:#{object_id}"
60-
Thread.current[thread_key] ||= []
60+
IsolatedExecutionState[thread_key] ||= []
6161
end
6262

6363
def tags_text

activesupport/lib/active_support/xml_mini.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,11 @@ def _parse_file(file, entity)
181181
end
182182

183183
def current_thread_backend
184-
Thread.current[:xml_mini_backend]
184+
IsolatedExecutionState[:xml_mini_backend]
185185
end
186186

187187
def current_thread_backend=(name)
188-
Thread.current[:xml_mini_backend] = name && cast_backend_name_to_module(name)
188+
IsolatedExecutionState[:xml_mini_backend] = name && cast_backend_name_to_module(name)
189189
end
190190

191191
def cast_backend_name_to_module(name)

activesupport/test/core_ext/time_with_zone_test.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,13 +1205,11 @@ def test_time_zone_getter_and_setter_with_zone_default_set
12051205

12061206
def test_time_zone_setter_is_thread_safe
12071207
Time.use_zone "Paris" do
1208-
t1 = Thread.new { Time.zone = "Alaska" }.join
1209-
t2 = Thread.new { Time.zone = "Hawaii" }.join
1210-
assert t1.stop?, "Thread 1 did not finish running"
1211-
assert t2.stop?, "Thread 2 did not finish running"
1208+
t1 = Thread.new { Time.zone = "Alaska"; Time.zone }
1209+
t2 = Thread.new { Time.zone = "Hawaii"; Time.zone }
12121210
assert_equal ActiveSupport::TimeZone["Paris"], Time.zone
1213-
assert_equal ActiveSupport::TimeZone["Alaska"], t1[:time_zone]
1214-
assert_equal ActiveSupport::TimeZone["Hawaii"], t2[:time_zone]
1211+
assert_equal ActiveSupport::TimeZone["Alaska"], t1.value
1212+
assert_equal ActiveSupport::TimeZone["Hawaii"], t2.value
12151213
end
12161214
end
12171215

0 commit comments

Comments
 (0)