Skip to content

Commit

Permalink
Documented SyncVar
Browse files Browse the repository at this point in the history
Since we used it in the DocRunner and noticed it could have better
documentation.

Review by @heathermiller.
  • Loading branch information
VladUreche committed Jul 2, 2012
1 parent c85b4a4 commit f8057d2
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/library/scala/concurrent/SyncVar.scala
Expand Up @@ -53,6 +53,8 @@ class SyncVar[A] {
value
}

/** Waits for this SyncVar to become defined and returns
* the result */
def take(): A = synchronized {
try get
finally unsetVal()
Expand All @@ -64,41 +66,45 @@ class SyncVar[A] {
* the SyncVar.
*
* @param timeout the amount of milliseconds to wait, 0 means forever
* @return `None` if variable is undefined after `timeout`, `Some(value)` otherwise
* @return the value or a throws an exception if the timeout occurs
* @throws NoSuchElementException on timeout
*/
def take(timeout: Long): A = synchronized {
try get(timeout).get
finally unsetVal()
}

// TODO: this method should be private
// [Heather] the reason why: it doesn't take into consideration
// [Heather] the reason why: it doesn't take into consideration
// whether or not the SyncVar is already defined. So, set has been
// deprecated in order to eventually be able to make "setting" private
@deprecated("Use `put` instead, as `set` is potentionally error-prone", "2.10.0")
def set(x: A): Unit = setVal(x)

/** Places a value in the SyncVar. If the SyncVar already has a stored value,
* it waits until another thread takes it */
def put(x: A): Unit = synchronized {
while (isDefined) wait()
setVal(x)
}

/** Checks whether a value is stored in the synchronized variable */
def isSet: Boolean = synchronized {
isDefined
}

// TODO: this method should be private
// [Heather] the reason why: it doesn't take into consideration
// [Heather] the reason why: it doesn't take into consideration
// whether or not the SyncVar is already defined. So, unset has been
// deprecated in order to eventually be able to make "unsetting" private
// deprecated in order to eventually be able to make "unsetting" private
@deprecated("Use `take` instead, as `unset` is potentionally error-prone", "2.10.0")
def unset(): Unit = synchronized {
isDefined = false
value = None
notifyAll()
}

// `setVal` exists so as to retroactively deprecate `set` without
// `setVal` exists so as to retroactively deprecate `set` without
// deprecation warnings where we use `set` internally. The
// implementation of `set` was moved to `setVal` to achieve this
private def setVal(x: A): Unit = synchronized {
Expand All @@ -107,13 +113,13 @@ class SyncVar[A] {
notifyAll()
}

// `unsetVal` exists so as to retroactively deprecate `unset` without
// `unsetVal` exists so as to retroactively deprecate `unset` without
// deprecation warnings where we use `unset` internally. The
// implementation of `unset` was moved to `unsetVal` to achieve this
private def unsetVal(): Unit = synchronized {
isDefined = false
value = None
notifyAll()
notifyAll()
}

}
Expand Down

1 comment on commit f8057d2

@VladUreche
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.