ruby-concurrency / concurrent-ruby

Modern concurrency tools including agents, futures, promises, thread pools, supervisors, and more. Inspired by Erlang, Clojure, Scala, Go, Java, JavaScript, and classic concurrency patterns.
https://ruby-concurrency.github.io/concurrent-ruby/
Other
5.68k stars 418 forks source link

`Concurrent.monotonic_time` accept an optional unit parameter #915

Closed casperisfine closed 3 years ago

casperisfine commented 3 years ago

This is a direct transposition of MRI's clock_gettime unit option.

Tis is a parameter I use quite a lot when writing MRI specific code, but that I miss when using Concurrent.monotonic_time for portability.

More often than not, when you reach for monotonic time, you want to minimize the overhead, so it's appreciable to be able to avoid a / 1_000 when you want milliseconds in the first place.

NB: I'm not too sure how a good way to test the scale of the return value would be.

chrisseaton commented 3 years ago

When is defined?(Process::CLOCK_MONOTONIC) false?

It's not false on JRuby 9.2.17.0 for example. Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond) works fine on JRuby, TruffleRuby, and I'm guessing not even Rubinius but it's hard to build these days.

@headius do you know if this is some kind of ancient fallback?

casperisfine commented 3 years ago

Older MRI I think. Process.clock_gettime was added in 2.3 if my memory serves me right.

chrisseaton commented 3 years ago

Oh right didn't realise it was so recent. I will check against older versions and build up a matrix of what is supported where.

chrisseaton commented 3 years ago

Seems good to merge when CI passes on older Rubies.

I'll look at the TruffleRuby failure that's unrelated.

casperisfine commented 3 years ago

@chrisseaton I believe I fixed the CI (well it has various failures but unrelated to the current PR).

I also think the implementation is now simpler, as for the simple case of Process.clock_getime being present we save one method call.

chrisseaton commented 3 years ago

I'm sorry that CI is in such a mess. We really need to get a grip of it.