@@ -53,6 +53,8 @@ class SyncVar[A] {
53
53
value
54
54
}
55
55
56
+ /** Waits for this SyncVar to become defined and returns
57
+ * the result */
56
58
def take (): A = synchronized {
57
59
try get
58
60
finally unsetVal()
@@ -64,41 +66,45 @@ class SyncVar[A] {
64
66
* the SyncVar.
65
67
*
66
68
* @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
68
71
*/
69
72
def take (timeout : Long ): A = synchronized {
70
73
try get(timeout).get
71
74
finally unsetVal()
72
75
}
73
76
74
77
// 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
76
79
// whether or not the SyncVar is already defined. So, set has been
77
80
// deprecated in order to eventually be able to make "setting" private
78
81
@ deprecated(" Use `put` instead, as `set` is potentionally error-prone" , " 2.10.0" )
79
82
def set (x : A ): Unit = setVal(x)
80
83
84
+ /** Places a value in the SyncVar. If the SyncVar already has a stored value,
85
+ * it waits until another thread takes it */
81
86
def put (x : A ): Unit = synchronized {
82
87
while (isDefined) wait()
83
88
setVal(x)
84
89
}
85
90
91
+ /** Checks whether a value is stored in the synchronized variable */
86
92
def isSet : Boolean = synchronized {
87
93
isDefined
88
94
}
89
95
90
96
// 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
92
98
// 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
94
100
@ deprecated(" Use `take` instead, as `unset` is potentionally error-prone" , " 2.10.0" )
95
101
def unset (): Unit = synchronized {
96
102
isDefined = false
97
103
value = None
98
104
notifyAll()
99
105
}
100
106
101
- // `setVal` exists so as to retroactively deprecate `set` without
107
+ // `setVal` exists so as to retroactively deprecate `set` without
102
108
// deprecation warnings where we use `set` internally. The
103
109
// implementation of `set` was moved to `setVal` to achieve this
104
110
private def setVal (x : A ): Unit = synchronized {
@@ -107,13 +113,13 @@ class SyncVar[A] {
107
113
notifyAll()
108
114
}
109
115
110
- // `unsetVal` exists so as to retroactively deprecate `unset` without
116
+ // `unsetVal` exists so as to retroactively deprecate `unset` without
111
117
// deprecation warnings where we use `unset` internally. The
112
118
// implementation of `unset` was moved to `unsetVal` to achieve this
113
119
private def unsetVal (): Unit = synchronized {
114
120
isDefined = false
115
121
value = None
116
- notifyAll()
122
+ notifyAll()
117
123
}
118
124
119
125
}
0 commit comments