concurrent-ruby 1.1.9 → 1.1.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +14 -0
  3. data/Gemfile +2 -7
  4. data/README.md +17 -21
  5. data/Rakefile +2 -12
  6. data/ext/concurrent-ruby/com/concurrent_ruby/ext/JavaAtomicFixnumLibrary.java +0 -0
  7. data/ext/concurrent-ruby/com/concurrent_ruby/ext/JavaSemaphoreLibrary.java +52 -22
  8. data/lib/concurrent-ruby/concurrent/async.rb +1 -0
  9. data/lib/concurrent-ruby/concurrent/atomic/atomic_reference.rb +1 -0
  10. data/lib/concurrent-ruby/concurrent/atomic/event.rb +2 -2
  11. data/lib/concurrent-ruby/concurrent/atomic/mutex_semaphore.rb +18 -2
  12. data/lib/concurrent-ruby/concurrent/atomic/reentrant_read_write_lock.rb +4 -6
  13. data/lib/concurrent-ruby/concurrent/atomic/semaphore.rb +26 -5
  14. data/lib/concurrent-ruby/concurrent/concurrent_ruby.jar +0 -0
  15. data/lib/concurrent-ruby/concurrent/executor/abstract_executor_service.rb +16 -13
  16. data/lib/concurrent-ruby/concurrent/executor/fixed_thread_pool.rb +13 -3
  17. data/lib/concurrent-ruby/concurrent/executor/java_executor_service.rb +1 -1
  18. data/lib/concurrent-ruby/concurrent/executor/java_thread_pool_executor.rb +4 -0
  19. data/lib/concurrent-ruby/concurrent/executor/ruby_executor_service.rb +10 -4
  20. data/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb +26 -37
  21. data/lib/concurrent-ruby/concurrent/executor/safe_task_executor.rb +5 -5
  22. data/lib/concurrent-ruby/concurrent/map.rb +0 -1
  23. data/lib/concurrent-ruby/concurrent/scheduled_task.rb +29 -16
  24. data/lib/concurrent-ruby/concurrent/synchronization/lockable_object.rb +1 -3
  25. data/lib/concurrent-ruby/concurrent/timer_task.rb +11 -33
  26. data/lib/concurrent-ruby/concurrent/tvar.rb +20 -60
  27. data/lib/concurrent-ruby/concurrent/utility/monotonic_time.rb +67 -35
  28. data/lib/concurrent-ruby/concurrent/utility/processor_counter.rb +2 -35
  29. data/lib/concurrent-ruby/concurrent/version.rb +1 -1
  30. data/lib/concurrent-ruby/concurrent-ruby.rb +5 -1
  31. metadata +7 -7
@@ -93,13 +93,8 @@ module Concurrent
93
93
  end
94
94
 
95
95
  # @!visibility private
96
- def ready_worker(worker)
97
- synchronize { ns_ready_worker worker }
98
- end
99
-
100
- # @!visibility private
101
- def worker_not_old_enough(worker)
102
- synchronize { ns_worker_not_old_enough worker }
96
+ def ready_worker(worker, last_message)
97
+ synchronize { ns_ready_worker worker, last_message }
103
98
  end
104
99
 
105
100
  # @!visibility private
@@ -112,6 +107,11 @@ module Concurrent
112
107
  synchronize { @completed_task_count += 1 }
113
108
  end
114
109
 
110
+ # @!macro thread_pool_executor_method_prune_pool
111
+ def prune_pool
112
+ synchronize { ns_prune_pool }
113
+ end
114
+
115
115
  private
116
116
 
117
117
  # @!visibility private
@@ -156,10 +156,11 @@ module Concurrent
156
156
  if ns_assign_worker(*args, &task) || ns_enqueue(*args, &task)
157
157
  @scheduled_task_count += 1
158
158
  else
159
- handle_fallback(*args, &task)
159
+ return fallback_action(*args, &task)
160
160
  end
161
161
 
162
162
  ns_prune_pool if @next_gc_time < Concurrent.monotonic_time
163
+ nil
163
164
  end
164
165
 
165
166
  # @!visibility private
@@ -192,7 +193,7 @@ module Concurrent
192
193
  # @!visibility private
193
194
  def ns_assign_worker(*args, &task)
194
195
  # keep growing if the pool is not at the minimum yet
195
- worker = (@ready.pop if @pool.size >= @min_length) || ns_add_busy_worker
196
+ worker, _ = (@ready.pop if @pool.size >= @min_length) || ns_add_busy_worker
196
197
  if worker
197
198
  worker << [task, args]
198
199
  true
@@ -223,7 +224,7 @@ module Concurrent
223
224
  def ns_worker_died(worker)
224
225
  ns_remove_busy_worker worker
225
226
  replacement_worker = ns_add_busy_worker
226
- ns_ready_worker replacement_worker, false if replacement_worker
227
+ ns_ready_worker replacement_worker, Concurrent.monotonic_time, false if replacement_worker
227
228
  end
228
229
 
229
230
  # creates new worker which has to receive work to do after it's added
@@ -242,29 +243,21 @@ module Concurrent
242
243
  # handle ready worker, giving it new job or assigning back to @ready
243
244
  #
244
245
  # @!visibility private
245
- def ns_ready_worker(worker, success = true)
246
+ def ns_ready_worker(worker, last_message, success = true)
246
247
  task_and_args = @queue.shift
247
248
  if task_and_args
248
249
  worker << task_and_args
249
250
  else
250
251
  # stop workers when !running?, do not return them to @ready
251
252
  if running?
252
- @ready.push(worker)
253
+ raise unless last_message
254
+ @ready.push([worker, last_message])
253
255
  else
254
256
  worker.stop
255
257
  end
256
258
  end
257
259
  end
258
260
 
259
- # returns back worker to @ready which was not idle for enough time
260
- #
261
- # @!visibility private
262
- def ns_worker_not_old_enough(worker)
263
- # let's put workers coming from idle_test back to the start (as the oldest worker)
264
- @ready.unshift(worker)
265
- true
266
- end
267
-
268
261
  # removes a worker which is not in not tracked in @ready
269
262
  #
270
263
  # @!visibility private
@@ -278,10 +271,17 @@ module Concurrent
278
271
  #
279
272
  # @!visibility private
280
273
  def ns_prune_pool
281
- return if @pool.size <= @min_length
282
-
283
- last_used = @ready.shift
284
- last_used << :idle_test if last_used
274
+ now = Concurrent.monotonic_time
275
+ stopped_workers = 0
276
+ while !@ready.empty? && (@pool.size - stopped_workers > @min_length)
277
+ worker, last_message = @ready.first
278
+ if now - last_message > self.idletime
279
+ stopped_workers += 1
280
+ @ready.shift
281
+ worker << :stop
282
+ else break
283
+ end
284
+ end
285
285
 
286
286
  @next_gc_time = Concurrent.monotonic_time + @gc_interval
287
287
  end
@@ -330,19 +330,10 @@ module Concurrent
330
330
 
331
331
  def create_worker(queue, pool, idletime)
332
332
  Thread.new(queue, pool, idletime) do |my_queue, my_pool, my_idletime|
333
- last_message = Concurrent.monotonic_time
334
333
  catch(:stop) do
335
334
  loop do
336
335
 
337
336
  case message = my_queue.pop
338
- when :idle_test
339
- if (Concurrent.monotonic_time - last_message) > my_idletime
340
- my_pool.remove_busy_worker(self)
341
- throw :stop
342
- else
343
- my_pool.worker_not_old_enough(self)
344
- end
345
-
346
337
  when :stop
347
338
  my_pool.remove_busy_worker(self)
348
339
  throw :stop
@@ -350,9 +341,7 @@ module Concurrent
350
341
  else
351
342
  task, args = message
352
343
  run_task my_pool, task, args
353
- last_message = Concurrent.monotonic_time
354
-
355
- my_pool.ready_worker(self)
344
+ my_pool.ready_worker(self, Concurrent.monotonic_time)
356
345
  end
357
346
  end
358
347
  end
@@ -16,10 +16,10 @@ module Concurrent
16
16
 
17
17
  # @return [Array]
18
18
  def execute(*args)
19
- synchronize do
20
- success = false
21
- value = reason = nil
19
+ success = true
20
+ value = reason = nil
22
21
 
22
+ synchronize do
23
23
  begin
24
24
  value = @task.call(*args)
25
25
  success = true
@@ -27,9 +27,9 @@ module Concurrent
27
27
  reason = ex
28
28
  success = false
29
29
  end
30
-
31
- [success, value, reason]
32
30
  end
31
+
32
+ [success, value, reason]
33
33
  end
34
34
  end
35
35
  end
@@ -281,7 +281,6 @@ module Concurrent
281
281
  each_pair { |k, v| return k if v == value }
282
282
  nil
283
283
  end unless method_defined?(:key)
284
- alias_method :index, :key if RUBY_VERSION < '1.9'
285
284
 
286
285
  # Is map empty?
287
286
  # @return [true, false]
@@ -58,29 +58,42 @@ module Concurrent
58
58
  # @example Basic usage
59
59
  #
60
60
  # require 'concurrent'
61
- # require 'thread' # for Queue
62
- # require 'open-uri' # for open(uri)
61
+ # require 'csv'
62
+ # require 'open-uri'
63
63
  #
64
64
  # class Ticker
65
- # def get_year_end_closing(symbol, year)
66
- # uri = "http://ichart.finance.yahoo.com/table.csv?s=#{symbol}&a=11&b=01&c=#{year}&d=11&e=31&f=#{year}&g=m"
67
- # data = open(uri) {|f| f.collect{|line| line.strip } }
68
- # data[1].split(',')[4].to_f
69
- # end
65
+ # def get_year_end_closing(symbol, year, api_key)
66
+ # uri = "https://www.alphavantage.co/query?function=TIME_SERIES_MONTHLY&symbol=#{symbol}&apikey=#{api_key}&datatype=csv"
67
+ # data = []
68
+ # csv = URI.parse(uri).read
69
+ # if csv.include?('call frequency')
70
+ # return :rate_limit_exceeded
71
+ # end
72
+ # CSV.parse(csv, headers: true) do |row|
73
+ # data << row['close'].to_f if row['timestamp'].include?(year.to_s)
74
+ # end
75
+ # year_end = data.first
76
+ # year_end
77
+ # rescue => e
78
+ # p e
79
+ # end
70
80
  # end
71
81
  #
82
+ # api_key = ENV['ALPHAVANTAGE_KEY']
83
+ # abort(error_message) unless api_key
84
+ #
72
85
  # # Future
73
- # price = Concurrent::Future.execute{ Ticker.new.get_year_end_closing('TWTR', 2013) }
86
+ # price = Concurrent::Future.execute{ Ticker.new.get_year_end_closing('TWTR', 2013, api_key) }
74
87
  # price.state #=> :pending
75
- # sleep(1) # do other stuff
76
- # price.value #=> 63.65
77
- # price.state #=> :fulfilled
88
+ # price.pending? #=> true
89
+ # price.value(0) #=> nil (does not block)
78
90
  #
79
- # # ScheduledTask
80
- # task = Concurrent::ScheduledTask.execute(2){ Ticker.new.get_year_end_closing('INTC', 2013) }
81
- # task.state #=> :pending
82
- # sleep(3) # do other stuff
83
- # task.value #=> 25.96
91
+ # sleep(1) # do other stuff
92
+ #
93
+ # price.value #=> 63.65 (after blocking if necessary)
94
+ # price.state #=> :fulfilled
95
+ # price.fulfilled? #=> true
96
+ # price.value #=> 63.65
84
97
  #
85
98
  # @example Successful task execution
86
99
  #
@@ -4,9 +4,7 @@ module Concurrent
4
4
  # @!visibility private
5
5
  # @!macro internal_implementation_note
6
6
  LockableObjectImplementation = case
7
- when Concurrent.on_cruby? && Concurrent.ruby_version(:<=, 1, 9, 3)
8
- MonitorLockableObject
9
- when Concurrent.on_cruby? && Concurrent.ruby_version(:>, 1, 9, 3)
7
+ when Concurrent.on_cruby?
10
8
  MutexLockableObject
11
9
  when Concurrent.on_jruby?
12
10
  JRubyLockableObject
@@ -25,9 +25,7 @@ module Concurrent
25
25
  # Should the task experience an unrecoverable crash only the task thread will
26
26
  # crash. This makes the `TimerTask` very fault tolerant. Additionally, the
27
27
  # `TimerTask` thread can respond to the success or failure of the task,
28
- # performing logging or ancillary operations. `TimerTask` can also be
29
- # configured with a timeout value allowing it to kill a task that runs too
30
- # long.
28
+ # performing logging or ancillary operations.
31
29
  #
32
30
  # One other advantage of `TimerTask` is that it forces the business logic to
33
31
  # be completely decoupled from the concurrency logic. The business logic can
@@ -48,9 +46,7 @@ module Concurrent
48
46
  # {http://ruby-doc.org/stdlib-2.0/libdoc/observer/rdoc/Observable.html
49
47
  # Observable} module. On execution the `TimerTask` will notify the observers
50
48
  # with three arguments: time of execution, the result of the block (or nil on
51
- # failure), and any raised exceptions (or nil on success). If the timeout
52
- # interval is exceeded the observer will receive a `Concurrent::TimeoutError`
53
- # object as the third argument.
49
+ # failure), and any raised exceptions (or nil on success).
54
50
  #
55
51
  # @!macro copy_options
56
52
  #
@@ -59,20 +55,18 @@ module Concurrent
59
55
  # task.execute
60
56
  #
61
57
  # task.execution_interval #=> 60 (default)
62
- # task.timeout_interval #=> 30 (default)
63
58
  #
64
59
  # # wait 60 seconds...
65
60
  # #=> 'Boom!'
66
61
  #
67
62
  # task.shutdown #=> true
68
63
  #
69
- # @example Configuring `:execution_interval` and `:timeout_interval`
70
- # task = Concurrent::TimerTask.new(execution_interval: 5, timeout_interval: 5) do
64
+ # @example Configuring `:execution_interval`
65
+ # task = Concurrent::TimerTask.new(execution_interval: 5) do
71
66
  # puts 'Boom!'
72
67
  # end
73
68
  #
74
69
  # task.execution_interval #=> 5
75
- # task.timeout_interval #=> 5
76
70
  #
77
71
  # @example Immediate execution with `:run_now`
78
72
  # task = Concurrent::TimerTask.new(run_now: true){ puts 'Boom!' }
@@ -115,15 +109,13 @@ module Concurrent
115
109
  # def update(time, result, ex)
116
110
  # if result
117
111
  # print "(#{time}) Execution successfully returned #{result}\n"
118
- # elsif ex.is_a?(Concurrent::TimeoutError)
119
- # print "(#{time}) Execution timed out\n"
120
112
  # else
121
113
  # print "(#{time}) Execution failed with error #{ex}\n"
122
114
  # end
123
115
  # end
124
116
  # end
125
117
  #
126
- # task = Concurrent::TimerTask.new(execution_interval: 1, timeout_interval: 1){ 42 }
118
+ # task = Concurrent::TimerTask.new(execution_interval: 1){ 42 }
127
119
  # task.add_observer(TaskObserver.new)
128
120
  # task.execute
129
121
  # sleep 4
@@ -133,7 +125,7 @@ module Concurrent
133
125
  # #=> (2013-10-13 19:09:00 -0400) Execution successfully returned 42
134
126
  # task.shutdown
135
127
  #
136
- # task = Concurrent::TimerTask.new(execution_interval: 1, timeout_interval: 1){ sleep }
128
+ # task = Concurrent::TimerTask.new(execution_interval: 1){ sleep }
137
129
  # task.add_observer(TaskObserver.new)
138
130
  # task.execute
139
131
  #
@@ -169,8 +161,6 @@ module Concurrent
169
161
  # @param [Hash] opts the options defining task execution.
170
162
  # @option opts [Integer] :execution_interval number of seconds between
171
163
  # task executions (default: EXECUTION_INTERVAL)
172
- # @option opts [Integer] :timeout_interval number of seconds a task can
173
- # run before it is considered to have failed (default: TIMEOUT_INTERVAL)
174
164
  # @option opts [Boolean] :run_now Whether to run the task immediately
175
165
  # upon instantiation or to wait until the first # execution_interval
176
166
  # has passed (default: false)
@@ -256,18 +246,14 @@ module Concurrent
256
246
  # @return [Fixnum] Number of seconds the task can run before it is
257
247
  # considered to have failed.
258
248
  def timeout_interval
259
- synchronize { @timeout_interval }
249
+ warn 'TimerTask timeouts are now ignored as these were not able to be implemented correctly'
260
250
  end
261
251
 
262
252
  # @!attribute [rw] timeout_interval
263
253
  # @return [Fixnum] Number of seconds the task can run before it is
264
254
  # considered to have failed.
265
255
  def timeout_interval=(value)
266
- if (value = value.to_f) <= 0.0
267
- raise ArgumentError.new('must be greater than zero')
268
- else
269
- synchronize { @timeout_interval = value }
270
- end
256
+ warn 'TimerTask timeouts are now ignored as these were not able to be implemented correctly'
271
257
  end
272
258
 
273
259
  private :post, :<<
@@ -278,7 +264,9 @@ module Concurrent
278
264
  set_deref_options(opts)
279
265
 
280
266
  self.execution_interval = opts[:execution] || opts[:execution_interval] || EXECUTION_INTERVAL
281
- self.timeout_interval = opts[:timeout] || opts[:timeout_interval] || TIMEOUT_INTERVAL
267
+ if opts[:timeout] || opts[:timeout_interval]
268
+ warn 'TimeTask timeouts are now ignored as these were not able to be implemented correctly'
269
+ end
282
270
  @run_now = opts[:now] || opts[:run_now]
283
271
  @executor = Concurrent::SafeTaskExecutor.new(task)
284
272
  @running = Concurrent::AtomicBoolean.new(false)
@@ -308,7 +296,6 @@ module Concurrent
308
296
  # @!visibility private
309
297
  def execute_task(completion)
310
298
  return nil unless @running.true?
311
- ScheduledTask.execute(timeout_interval, args: [completion], &method(:timeout_task))
312
299
  _success, value, reason = @executor.execute(self)
313
300
  if completion.try?
314
301
  self.value = value
@@ -320,14 +307,5 @@ module Concurrent
320
307
  end
321
308
  nil
322
309
  end
323
-
324
- # @!visibility private
325
- def timeout_task(completion)
326
- return unless @running.true?
327
- if completion.try?
328
- schedule_next_task
329
- observers.notify_observers(Time.now, nil, Concurrent::TimeoutError.new)
330
- end
331
- end
332
310
  end
333
311
  end
@@ -15,7 +15,6 @@ module Concurrent
15
15
  # Create a new `TVar` with an initial value.
16
16
  def initialize(value)
17
17
  @value = value
18
- @version = 0
19
18
  @lock = Mutex.new
20
19
  end
21
20
 
@@ -43,16 +42,6 @@ module Concurrent
43
42
  @value = value
44
43
  end
45
44
 
46
- # @!visibility private
47
- def unsafe_version # :nodoc:
48
- @version
49
- end
50
-
51
- # @!visibility private
52
- def unsafe_increment_version # :nodoc:
53
- @version += 1
54
- end
55
-
56
45
  # @!visibility private
57
46
  def unsafe_lock # :nodoc:
58
47
  @lock
@@ -164,53 +153,39 @@ module Concurrent
164
153
 
165
154
  ABORTED = ::Object.new
166
155
 
167
- ReadLogEntry = Struct.new(:tvar, :version)
156
+ OpenEntry = Struct.new(:value, :modified)
168
157
 
169
158
  AbortError = Class.new(StandardError)
170
159
  LeaveError = Class.new(StandardError)
171
160
 
172
161
  def initialize
173
- @read_log = []
174
- @write_log = {}
162
+ @open_tvars = {}
175
163
  end
176
164
 
177
165
  def read(tvar)
178
- Concurrent::abort_transaction unless valid?
179
-
180
- if @write_log.has_key? tvar
181
- @write_log[tvar]
182
- else
183
- @read_log.push(ReadLogEntry.new(tvar, tvar.unsafe_version))
184
- tvar.unsafe_value
185
- end
166
+ entry = open(tvar)
167
+ entry.value
186
168
  end
187
169
 
188
170
  def write(tvar, value)
189
- # Have we already written to this TVar?
171
+ entry = open(tvar)
172
+ entry.modified = true
173
+ entry.value = value
174
+ end
190
175
 
191
- if @write_log.has_key? tvar
192
- # Record the value written
193
- @write_log[tvar] = value
194
- else
195
- # Try to lock the TVar
176
+ def open(tvar)
177
+ entry = @open_tvars[tvar]
196
178
 
179
+ unless entry
197
180
  unless tvar.unsafe_lock.try_lock
198
- # Someone else is writing to this TVar - abort
199
181
  Concurrent::abort_transaction
200
182
  end
201
183
 
202
- # Record the value written
203
-
204
- @write_log[tvar] = value
205
-
206
- # If we previously read from it, check the version hasn't changed
207
-
208
- @read_log.each do |log_entry|
209
- if log_entry.tvar == tvar and tvar.unsafe_version > log_entry.version
210
- Concurrent::abort_transaction
211
- end
212
- end
184
+ entry = OpenEntry.new(tvar.unsafe_value, false)
185
+ @open_tvars[tvar] = entry
213
186
  end
187
+
188
+ entry
214
189
  end
215
190
 
216
191
  def abort
@@ -218,32 +193,17 @@ module Concurrent
218
193
  end
219
194
 
220
195
  def commit
221
- return false unless valid?
222
-
223
- @write_log.each_pair do |tvar, value|
224
- tvar.unsafe_value = value
225
- tvar.unsafe_increment_version
226
- end
227
-
228
- unlock
229
-
230
- true
231
- end
232
-
233
- def valid?
234
- @read_log.each do |log_entry|
235
- unless @write_log.has_key? log_entry.tvar
236
- if log_entry.tvar.unsafe_version > log_entry.version
237
- return false
238
- end
196
+ @open_tvars.each do |tvar, entry|
197
+ if entry.modified
198
+ tvar.unsafe_value = entry.value
239
199
  end
240
200
  end
241
201
 
242
- true
202
+ unlock
243
203
  end
244
204
 
245
205
  def unlock
246
- @write_log.each_key do |tvar|
206
+ @open_tvars.each_key do |tvar|
247
207
  tvar.unsafe_lock.unlock
248
208
  end
249
209
  end
@@ -2,26 +2,64 @@ require 'concurrent/synchronization'
2
2
 
3
3
  module Concurrent
4
4
 
5
- class_definition = Class.new(Synchronization::LockableObject) do
6
- def initialize
7
- @last_time = Time.now.to_f
8
- super()
5
+ # @!macro monotonic_get_time
6
+ #
7
+ # Returns the current time a tracked by the application monotonic clock.
8
+ #
9
+ # @param [Symbol] unit the time unit to be returned, can be either
10
+ # :float_second, :float_millisecond, :float_microsecond, :second,
11
+ # :millisecond, :microsecond, or :nanosecond default to :float_second.
12
+ #
13
+ # @return [Float] The current monotonic time since some unspecified
14
+ # starting point
15
+ #
16
+ # @!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)
9
21
  end
10
22
 
11
- if defined?(Process::CLOCK_MONOTONIC)
12
- # @!visibility private
13
- def get_time
14
- Process.clock_gettime(Process::CLOCK_MONOTONIC)
15
- end
16
- elsif Concurrent.on_jruby?
17
- # @!visibility private
18
- def get_time
19
- java.lang.System.nanoTime() / 1_000_000_000.0
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()
20
59
  end
21
- else
22
60
 
23
61
  # @!visibility private
24
- def get_time
62
+ def get_time(unit)
25
63
  synchronize do
26
64
  now = Time.now.to_f
27
65
  if @last_time < now
@@ -29,30 +67,24 @@ module Concurrent
29
67
  else # clock has moved back in time
30
68
  @last_time += 0.000_001
31
69
  end
70
+ scale, to_int = @time_units[unit]
71
+ now *= scale if scale
72
+ now = now.to_i if to_int
73
+ now
32
74
  end
33
75
  end
34
-
35
76
  end
36
- end
37
77
 
38
- # Clock that cannot be set and represents monotonic time since
39
- # some unspecified starting point.
40
- #
41
- # @!visibility private
42
- GLOBAL_MONOTONIC_CLOCK = class_definition.new
43
- private_constant :GLOBAL_MONOTONIC_CLOCK
44
-
45
- # @!macro monotonic_get_time
46
- #
47
- # Returns the current time a tracked by the application monotonic clock.
48
- #
49
- # @return [Float] The current monotonic time since some unspecified
50
- # starting point
51
- #
52
- # @!macro monotonic_clock_warning
53
- def monotonic_time
54
- GLOBAL_MONOTONIC_CLOCK.get_time
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
55
88
  end
56
-
57
89
  module_function :monotonic_time
58
90
  end