sbt/launcher-package/integration-test/src/test/scala/SyncVar.scala

39 lines
1.0 KiB
Scala

package sbt.internal
// minimal copy of scala.concurrent.SyncVar since that version deprecated put and unset
private[sbt] final class SyncVar[A] {
private var isDefined: Boolean = false
private var value: Option[A] = None
/** Waits until a value is set and then gets it. Does not clear the value */
def get: A = synchronized {
while (!isDefined) wait()
value.get
}
/** Waits until a value is set, gets it, and finally clears the value. */
def take(): A = synchronized {
try get finally unset()
}
/** Sets the value, whether or not it is currently defined. */
def set(x: A): Unit = synchronized {
isDefined = true
value = Some(x)
notifyAll()
}
/** Sets the value, first waiting until it is undefined if it is currently defined. */
def put(x: A): Unit = synchronized {
while (isDefined) wait()
set(x)
}
/** Clears the value, whether or not it is current defined. */
def unset(): Unit = synchronized {
isDefined = false
value = None
notifyAll()
}
}