Skip to content

Commit 5914f5a

Browse files
committed
Changed fallback global monotonic clock from class var to constant.
1 parent 00be418 commit 5914f5a

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

lib/concurrent/utility/monotonic_time.rb

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,31 @@ def monotonic_time
1111
else
1212

1313
require 'thread'
14-
class << self
1514

16-
# @!visibility private
17-
@@global_monotonic_clock = Class.new {
18-
def initialize
19-
@mutex = Mutex.new
20-
@correction = 0
21-
@last_time = Time.now.to_f
22-
end
23-
def get_time
24-
@mutex.synchronize do
25-
@correction ||= 0 # compensating any back time shifts
26-
now = Time.now.to_f
27-
corrected_now = now + @correction
28-
if @last_time < corrected_now
29-
return @last_time = corrected_now
30-
else
31-
@correction += @last_time - corrected_now + 0.000_001
32-
return @last_time = @correction + now
33-
end
15+
# @!visibility private
16+
GLOBAL_MONOTONIC_CLOCK = Class.new {
17+
def initialize
18+
@mutex = Mutex.new
19+
@correction = 0
20+
@last_time = Time.now.to_f
21+
end
22+
def get_time
23+
@mutex.synchronize do
24+
@correction ||= 0 # compensating any back time shifts
25+
now = Time.now.to_f
26+
corrected_now = now + @correction
27+
if @last_time < corrected_now
28+
return @last_time = corrected_now
29+
else
30+
@correction += @last_time - corrected_now + 0.000_001
31+
return @last_time = @correction + now
3432
end
3533
end
36-
}.new
37-
end
34+
end
35+
}.new
3836

3937
def monotonic_time
40-
@@global_monotonic_clock.get_time
38+
GLOBAL_MONOTONIC_CLOCK.get_time
4139
end
4240
end
4341

0 commit comments

Comments
 (0)