Skip to content

Commit f8057d2

Browse files
committed
Documented SyncVar
Since we used it in the DocRunner and noticed it could have better documentation. Review by @heathermiller.
1 parent c85b4a4 commit f8057d2

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/library/scala/concurrent/SyncVar.scala

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class SyncVar[A] {
5353
value
5454
}
5555

56+
/** Waits for this SyncVar to become defined and returns
57+
* the result */
5658
def take(): A = synchronized {
5759
try get
5860
finally unsetVal()
@@ -64,41 +66,45 @@ class SyncVar[A] {
6466
* the SyncVar.
6567
*
6668
* @param timeout the amount of milliseconds to wait, 0 means forever
67-
* @return `None` if variable is undefined after `timeout`, `Some(value)` otherwise
69+
* @return the value or a throws an exception if the timeout occurs
70+
* @throws NoSuchElementException on timeout
6871
*/
6972
def take(timeout: Long): A = synchronized {
7073
try get(timeout).get
7174
finally unsetVal()
7275
}
7376

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

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

91+
/** Checks whether a value is stored in the synchronized variable */
8692
def isSet: Boolean = synchronized {
8793
isDefined
8894
}
8995

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

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

110-
// `unsetVal` exists so as to retroactively deprecate `unset` without
116+
// `unsetVal` exists so as to retroactively deprecate `unset` without
111117
// deprecation warnings where we use `unset` internally. The
112118
// implementation of `unset` was moved to `unsetVal` to achieve this
113119
private def unsetVal(): Unit = synchronized {
114120
isDefined = false
115121
value = None
116-
notifyAll()
122+
notifyAll()
117123
}
118124

119125
}

0 commit comments

Comments
 (0)