Skip to content

Commit 608bb09

Browse files
committed
All global class variables are now private constants.
1 parent 4bdd77f commit 608bb09

File tree

4 files changed

+49
-47
lines changed

4 files changed

+49
-47
lines changed

lib/concurrent/configuration.rb

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,46 @@ module Concurrent
1010

1111
# Suppresses all output when used for logging.
1212
NULL_LOGGER = lambda { |level, progname, message = nil, &block| }
13+
private_constant :NULL_LOGGER
1314

14-
# initialize the global executors
15-
class << self
15+
# @!visibility private
16+
GLOBAL_LOGGER = Atomic.new(NULL_LOGGER)
17+
private_constant :GLOBAL_LOGGER
1618

17-
# @!visibility private
18-
@@global_logger = Atomic.new(NULL_LOGGER)
19+
# @!visibility private
20+
AUTO_TERMINATE_GLOBAL_EXECUTORS = AtomicBoolean.new(true)
21+
private_constant :AUTO_TERMINATE_GLOBAL_EXECUTORS
1922

20-
# @!visibility private
21-
@@auto_terminate_global_executors = AtomicBoolean.new(true)
23+
# @!visibility private
24+
AUTO_TERMINATE_ALL_EXECUTORS = AtomicBoolean.new(true)
25+
private_constant :AUTO_TERMINATE_ALL_EXECUTORS
2226

23-
# @!visibility private
24-
@@auto_terminate_all_executors = AtomicBoolean.new(true)
25-
26-
# @!visibility private
27-
@@global_fast_executor = LazyReference.new do
28-
Concurrent.new_fast_executor(
29-
stop_on_exit: @@auto_terminate_global_executors.value)
30-
end
27+
# @!visibility private
28+
GLOBAL_FAST_EXECUTOR = LazyReference.new do
29+
Concurrent.new_fast_executor(
30+
stop_on_exit: AUTO_TERMINATE_GLOBAL_EXECUTORS.value)
31+
end
32+
private_constant :GLOBAL_FAST_EXECUTOR
3133

32-
# @!visibility private
33-
@@global_io_executor = LazyReference.new do
34-
Concurrent.new_io_executor(
35-
stop_on_exit: @@auto_terminate_global_executors.value)
36-
end
34+
# @!visibility private
35+
GLOBAL_IO_EXECUTOR = LazyReference.new do
36+
Concurrent.new_io_executor(
37+
stop_on_exit: AUTO_TERMINATE_GLOBAL_EXECUTORS.value)
38+
end
39+
private_constant :GLOBAL_IO_EXECUTOR
3740

38-
# @!visibility private
39-
@@global_timer_set = LazyReference.new do
40-
TimerSet.new(stop_on_exit: @@auto_terminate_global_executors.value)
41-
end
41+
# @!visibility private
42+
GLOBAL_TIMER_SET = LazyReference.new do
43+
TimerSet.new(stop_on_exit: AUTO_TERMINATE_GLOBAL_EXECUTORS.value)
4244
end
45+
private_constant :GLOBAL_TIMER_SET
4346

4447
def self.global_logger
45-
@@global_logger.value
48+
GLOBAL_LOGGER.value
4649
end
4750

4851
def self.global_logger=(value)
49-
@@global_logger.value = value
52+
GLOBAL_LOGGER.value = value
5053
end
5154

5255
# Defines if global executors should be auto-terminated with an
@@ -64,7 +67,7 @@ def self.global_logger=(value)
6467
# application and even then it should be used only when necessary.
6568
#
6669
def self.disable_auto_termination_of_global_executors!
67-
@@auto_terminate_global_executors.make_false
70+
AUTO_TERMINATE_GLOBAL_EXECUTORS.make_false
6871
end
6972

7073
# Defines if global executors should be auto-terminated with an
@@ -85,7 +88,7 @@ def self.disable_auto_termination_of_global_executors!
8588
# application exit using an `at_exit` handler; false when no auto-termination
8689
# will occur.
8790
def self.auto_terminate_global_executors?
88-
@@auto_terminate_global_executors.value
91+
AUTO_TERMINATE_GLOBAL_EXECUTORS.value
8992
end
9093

9194
# Defines if *ALL* executors should be auto-terminated with an
@@ -105,7 +108,7 @@ def self.auto_terminate_global_executors?
105108
# gem. It should *only* be used from within the main application.
106109
# And even then it should be used only when necessary.
107110
def self.disable_auto_termination_of_all_executors!
108-
@@auto_terminate_all_executors.make_false
111+
AUTO_TERMINATE_ALL_EXECUTORS.make_false
109112
end
110113

111114
# Defines if *ALL* executors should be auto-terminated with an
@@ -129,21 +132,21 @@ def self.disable_auto_termination_of_all_executors!
129132
# application exit using an `at_exit` handler; false when no auto-termination
130133
# will occur.
131134
def self.auto_terminate_all_executors?
132-
@@auto_terminate_all_executors.value
135+
AUTO_TERMINATE_ALL_EXECUTORS.value
133136
end
134137

135138
# Global thread pool optimized for short, fast *operations*.
136139
#
137140
# @return [ThreadPoolExecutor] the thread pool
138141
def self.global_fast_executor
139-
@@global_fast_executor.value
142+
GLOBAL_FAST_EXECUTOR.value
140143
end
141144

142145
# Global thread pool optimized for long, blocking (IO) *tasks*.
143146
#
144147
# @return [ThreadPoolExecutor] the thread pool
145148
def self.global_io_executor
146-
@@global_io_executor.value
149+
GLOBAL_IO_EXECUTOR.value
147150
end
148151

149152
# Global thread pool user for global *timers*.
@@ -152,7 +155,7 @@ def self.global_io_executor
152155
#
153156
# @see Concurrent::timer
154157
def self.global_timer_set
155-
@@global_timer_set.value
158+
GLOBAL_TIMER_SET.value
156159
end
157160

158161
def self.shutdown_global_executors
@@ -250,17 +253,15 @@ def global_timer_set
250253
# Use the :executor constructor option instead.
251254
def global_task_pool=(executor)
252255
warn '[DEPRECATED] Replacing global thread pools is deprecated. Use the :executor constructor option instead.'
253-
var = Concurrent.class_variable_get(:@@global_io_executor)
254-
var.reconfigure { executor } or
256+
GLOBAL_IO_EXECUTOR.reconfigure { executor } or
255257
raise ConfigurationError.new('global task pool was already set')
256258
end
257259

258260
# @deprecated Replacing global thread pools is deprecated.
259261
# Use the :executor constructor option instead.
260262
def global_operation_pool=(executor)
261263
warn '[DEPRECATED] Replacing global thread pools is deprecated. Use the :executor constructor option instead.'
262-
var = Concurrent.class_variable_get(:@@global_fast_executor)
263-
var.reconfigure { executor } or
264+
GLOBAL_FAST_EXECUTOR.reconfigure { executor } or
264265
raise ConfigurationError.new('global operation pool was already set')
265266
end
266267

lib/concurrent/utility/monotonic_time.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def get_time
3838
end
3939
end
4040
}.new
41+
private_constant :GLOBAL_MONOTONIC_CLOCK
4142

4243
# @!macro [attach] monotonic_get_time
4344
#

spec/concurrent/configuration_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ module Concurrent
33
describe Configuration do
44

55
before(:each) do
6-
Concurrent.class_variable_set(
7-
:@@global_fast_executor,
6+
Concurrent.const_set(
7+
:GLOBAL_FAST_EXECUTOR,
88
Concurrent::LazyReference.new{ Concurrent::ImmediateExecutor.new })
9-
Concurrent.class_variable_set(
10-
:@@global_io_executor,
9+
Concurrent.const_set(
10+
:GLOBAL_IO_EXECUTOR,
1111
Concurrent::LazyReference.new{ Concurrent::ImmediateExecutor.new })
12-
Concurrent.class_variable_set(
13-
:@@global_timer_set,
12+
Concurrent.const_set(
13+
:GLOBAL_TIMER_SET,
1414
Concurrent::LazyReference.new{ Concurrent::ImmediateExecutor.new })
1515
end
1616

spec/support/example_group_extensions.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@ def do_no_reset!
3434
end
3535

3636
GLOBAL_EXECUTORS = [
37-
[:@@global_fast_executor, ->{ LazyReference.new{ Concurrent.new_fast_executor }}],
38-
[:@@global_io_executor, ->{ LazyReference.new{ Concurrent.new_io_executor }}],
39-
[:@@global_timer_set, ->{ LazyReference.new{ Concurrent::TimerSet.new }}],
37+
[:GLOBAL_FAST_EXECUTOR, ->{ LazyReference.new{ Concurrent.new_fast_executor }}],
38+
[:GLOBAL_IO_EXECUTOR, ->{ LazyReference.new{ Concurrent.new_io_executor }}],
39+
[:GLOBAL_TIMER_SET, ->{ LazyReference.new{ Concurrent::TimerSet.new }}],
4040
]
4141

4242
@@killed = false
4343

4444
def reset_gem_configuration
4545
if @@killed
4646
GLOBAL_EXECUTORS.each do |var, factory|
47-
executor = Concurrent.class_variable_get(var).value
47+
executor = Concurrent.const_get(var).value
4848
executor.shutdown
4949
executor.kill
5050
executor = nil
51-
Concurrent.class_variable_set(var, factory.call)
51+
Concurrent.const_set(var, factory.call)
5252
end
5353
@@killed = false
5454
end

0 commit comments

Comments
 (0)