diff options
author | Aleksandar Pokopec <aleksandar.prokopec@epfl.ch> | 2011-02-14 16:46:33 +0000 |
---|---|---|
committer | Aleksandar Pokopec <aleksandar.prokopec@epfl.ch> | 2011-02-14 16:46:33 +0000 |
commit | 1887da061703caf5dba4591c6a845bbd442d113b (patch) | |
tree | 40f3b80429ce44c522571f90b54ead0ad244ea86 /src/library/scala/concurrent/SyncVar.scala | |
parent | 274be9370407d482c1c50a4b9e4343a1bd8ec85a (diff) | |
download | scala-1887da061703caf5dba4591c6a845bbd442d113b.tar.gz scala-1887da061703caf5dba4591c6a845bbd442d113b.tar.bz2 scala-1887da061703caf5dba4591c6a845bbd442d113b.zip |
Refixing SyncVars, fixes #4094.
No review.
Diffstat (limited to 'src/library/scala/concurrent/SyncVar.scala')
-rw-r--r-- | src/library/scala/concurrent/SyncVar.scala | 50 |
1 files changed, 17 insertions, 33 deletions
diff --git a/src/library/scala/concurrent/SyncVar.scala b/src/library/scala/concurrent/SyncVar.scala index bf39fef27c..eebdd9b528 100644 --- a/src/library/scala/concurrent/SyncVar.scala +++ b/src/library/scala/concurrent/SyncVar.scala @@ -15,61 +15,45 @@ package scala.concurrent * @version 1.0, 10/03/2003 */ class SyncVar[A] { - @volatile private var isDefined: Boolean = false - @volatile private var value: A = _ + private var isDefined: Boolean = false + private var value: A = _ def get = synchronized { while (!isDefined) wait() value } - /** Like Object.wait but reports millis elapsed. + /** 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. */ - private def waitMeasuringElapsed(timeout: Long): Long = { + private def waitMeasuringElapsed(timeout: Long): Long = if (timeout <= 0) 0 else { val start = System.currentTimeMillis wait(timeout) - System.currentTimeMillis - start + val elapsed = System.currentTimeMillis - start + if (elapsed < 0) 0 else elapsed } + /** Waits for this SyncVar to become defined at least for + * `timeout` milliseconds (possibly more), and gets its + * value. + * + * @param timeout the amount of milliseconds to wait, 0 means forever + * @return `None` if variable is undefined after `timeout`, `Some(value)` otherwise + */ def get(timeout: Long): Option[A] = synchronized { /** Defending against the system clock going backward * by counting time elapsed directly. Loop required * to deal with spurious wakeups. */ var rest = timeout - while (!isDefined && rest >= 0) { - val elapsed = waitMeasuringElapsed(timeout) - if (!isDefined && elapsed > 0) - rest -= elapsed + while (!isDefined && rest > 0) { + val elapsed = waitMeasuringElapsed(rest) + rest -= elapsed } if (isDefined) Some(value) else None } - // /** Waits for this SyncVar to become defined at least for - // * `timeout` milliseconds (possibly more), and gets its - // * value. - // * - // * @param timeout the amount of milliseconds to wait - // * @return `None` if variable is undefined after `timeout`, `Some(value)` otherwise - // */ - // def get(timeout: Long): Option[A] = synchronized { - // if (timeout == 0L) Some(get) - // else { - // val start = System.currentTimeMillis - // var left = timeout - // while (!isDefined && left > 0) { - // wait(left) - // if (!isDefined) { - // val elapsed = System.currentTimeMillis - start - // left = timeout - elapsed - // } - // } - // if (isDefined) Some(value) - // else None - // } - // } - def take() = synchronized { try get finally unset() |