1
- require 'concurrent/synchronization'
2
-
3
1
module Concurrent
4
2
5
3
# @!macro monotonic_get_time
@@ -14,77 +12,8 @@ module Concurrent
14
12
# starting point
15
13
#
16
14
# @!macro monotonic_clock_warning
17
- if defined? ( Process ::CLOCK_MONOTONIC )
18
-
19
- def monotonic_time ( unit = :float_second )
20
- Process . clock_gettime ( Process ::CLOCK_MONOTONIC , unit )
21
- end
22
-
23
- elsif Concurrent . on_jruby?
24
-
25
- # @!visibility private
26
- TIME_UNITS = Hash . new { |_hash , key | raise ArgumentError , "unexpected unit: #{ key } " } . compare_by_identity
27
- TIME_UNITS . merge! (
28
- second : 1_000_000_000 ,
29
- millisecond : 1_000_000 ,
30
- microsecond : 1_000 ,
31
- nanosecond : 1 ,
32
- float_second : 1_000_000_000.0 ,
33
- float_millisecond : 1_000_000.0 ,
34
- float_microsecond : 1_000.0 ,
35
- )
36
- TIME_UNITS . freeze
37
- private_constant :TIME_UNITS
38
-
39
- def monotonic_time ( unit = :float_second )
40
- java . lang . System . nanoTime ( ) / TIME_UNITS [ unit ]
41
- end
42
-
43
- else
44
-
45
- class_definition = Class . new ( Synchronization ::LockableObject ) do
46
- def initialize
47
- @last_time = Time . now . to_f
48
- @time_units = Hash . new { |_hash , key | raise ArgumentError , "unexpected unit: #{ key } " } . compare_by_identity
49
- @time_units . merge! (
50
- second : [ nil , true ] ,
51
- millisecond : [ 1_000 , true ] ,
52
- microsecond : [ 1_000_000 , true ] ,
53
- nanosecond : [ 1_000_000_000 , true ] ,
54
- float_second : [ nil , false ] ,
55
- float_millisecond : [ 1_000.0 , false ] ,
56
- float_microsecond : [ 1_000_000.0 , false ] ,
57
- )
58
- super ( )
59
- end
60
-
61
- # @!visibility private
62
- def get_time ( unit )
63
- synchronize do
64
- now = Time . now . to_f
65
- if @last_time < now
66
- @last_time = now
67
- else # clock has moved back in time
68
- @last_time += 0.000_001
69
- end
70
- scale , to_int = @time_units [ unit ]
71
- now *= scale if scale
72
- now = now . to_i if to_int
73
- now
74
- end
75
- end
76
- end
77
-
78
- # Clock that cannot be set and represents monotonic time since
79
- # some unspecified starting point.
80
- #
81
- # @!visibility private
82
- GLOBAL_MONOTONIC_CLOCK = class_definition . new
83
- private_constant :GLOBAL_MONOTONIC_CLOCK
84
-
85
- def monotonic_time ( unit = :float_second )
86
- GLOBAL_MONOTONIC_CLOCK . get_time ( unit )
87
- end
15
+ def monotonic_time ( unit = :float_second )
16
+ Process . clock_gettime ( Process ::CLOCK_MONOTONIC , unit )
88
17
end
89
18
module_function :monotonic_time
90
19
end
0 commit comments