diff options
author | Simon Ochsenreither <simon@ochsenreither.de> | 2013-12-11 14:05:42 +0100 |
---|---|---|
committer | Simon Ochsenreither <simon@ochsenreither.de> | 2013-12-13 23:01:35 +0100 |
commit | a3a5e4a6f548bf8b9d2874a5d6578c8364a1984f (patch) | |
tree | 1a5d3a35c351448ca3e026b18d264732677afac8 /src/library | |
parent | b345b42cac64aa97e3bbcc6f14ef8f08214ab56f (diff) | |
download | scala-a3a5e4a6f548bf8b9d2874a5d6578c8364a1984f.tar.gz scala-a3a5e4a6f548bf8b9d2874a5d6578c8364a1984f.tar.bz2 scala-a3a5e4a6f548bf8b9d2874a5d6578c8364a1984f.zip |
SI-7546 Use likely monotonic clock source for durations
System.currentTimeMillis is affected by wall-clock time, which means
things can appear to jump back in time.
Use System.nanoTime which is more likely¹ to be monotonic instead.
¹ See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6458294
"nanoTime affected by system clock change on Linux (RH9) or in
general lacks monotonicity".
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/concurrent/SyncVar.scala | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/src/library/scala/concurrent/SyncVar.scala b/src/library/scala/concurrent/SyncVar.scala index 76d21c3dbf..d5dc3d7e3f 100644 --- a/src/library/scala/concurrent/SyncVar.scala +++ b/src/library/scala/concurrent/SyncVar.scala @@ -8,6 +8,8 @@ package scala.concurrent +import java.util.concurrent.TimeUnit + /** A class to provide safe concurrent access to a mutable cell. * All methods are synchronized. * @@ -23,14 +25,16 @@ class SyncVar[A] { value.get } - /** Waits `timeout` millis. If `timeout <= 0` just returns 0. If the system clock - * went backward, it will return 0, so it never returns negative results. - */ + /** Waits `timeout` millis. If `timeout <= 0` just returns 0. + * It never returns negative results. + */ private def waitMeasuringElapsed(timeout: Long): Long = if (timeout <= 0) 0 else { - val start = System.currentTimeMillis + val start = System.nanoTime() wait(timeout) - val elapsed = System.currentTimeMillis - start - if (elapsed < 0) 0 else elapsed + val elapsed = System.nanoTime() - start + // nanoTime should be monotonic, but it's not possible to rely on that. + // See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6458294. + if (elapsed < 0) 0 else TimeUnit.NANOSECONDS.toMillis(elapsed) } /** Waits for this SyncVar to become defined at least for |