summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormichelou <michelou@epfl.ch>2007-07-11 14:52:34 +0000
committermichelou <michelou@epfl.ch>2007-07-11 14:52:34 +0000
commit95a9b8dc2e7b0050e6254ac114e4d2ea58512d96 (patch)
treee7cec4387f745d9d13f8299b2b99ea4a6a6ae9e5 /src
parent5540988eb49493dddbc9cb00ccddb6c8e0694f43 (diff)
downloadscala-95a9b8dc2e7b0050e6254ac114e4d2ea58512d96.tar.gz
scala-95a9b8dc2e7b0050e6254ac114e4d2ea58512d96.tar.bz2
scala-95a9b8dc2e7b0050e6254ac114e4d2ea58512d96.zip
added Stepan's patch ops/SyncVar
Diffstat (limited to 'src')
-rw-r--r--src/library/scala/concurrent/SyncVar.scala33
-rw-r--r--src/library/scala/concurrent/ops.scala20
2 files changed, 36 insertions, 17 deletions
diff --git a/src/library/scala/concurrent/SyncVar.scala b/src/library/scala/concurrent/SyncVar.scala
index 9b852e4cb5..6b9ca33395 100644
--- a/src/library/scala/concurrent/SyncVar.scala
+++ b/src/library/scala/concurrent/SyncVar.scala
@@ -1,7 +1,7 @@
/* __ *\
** ________ ___ / / ___ Scala API **
-** / __/ __// _ | / / / _ | (c) 2003-2006, LAMP/EPFL **
-** __\ \/ /__/ __ |/ /__/ __ | **
+** / __/ __// _ | / / / _ | (c) 2003-2007, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
@@ -14,24 +14,43 @@ package scala.concurrent
/** The class <code>SyncVar</code> ...
*
- * @author Martin Odersky
+ * @author Martin Odersky, Stepan Koltsov
* @version 1.0, 10/03/2003
*/
-class SyncVar[a] {
+class SyncVar[A] {
private var isDefined: Boolean = false
- private var value: a = _
+ private var value: A = _
+ private var exception: Option[Throwable] = None
def get = synchronized {
while (!isDefined) wait()
- value
+ if (exception.isEmpty) value
+ else throw exception.get
}
- def set(x: a) = synchronized {
+ def set(x: A) = synchronized {
value = x
isDefined = true
+ exception = None
notifyAll()
}
+ private def setException(e: Throwable) = synchronized {
+ exception = Some(e)
+ isDefined = true
+ notifyAll()
+ }
+
+ def setWithCatch(x: => A) = synchronized {
+ try {
+ this set x
+ } catch {
+ case e =>
+ this setException e
+ throw e
+ }
+ }
+
def isSet: Boolean = synchronized {
isDefined
}
diff --git a/src/library/scala/concurrent/ops.scala b/src/library/scala/concurrent/ops.scala
index ed31099c30..0d3c485300 100644
--- a/src/library/scala/concurrent/ops.scala
+++ b/src/library/scala/concurrent/ops.scala
@@ -1,7 +1,7 @@
/* __ *\
** ________ ___ / / ___ Scala API **
-** / __/ __// _ | / / / _ | (c) 2003-2006, LAMP/EPFL **
-** __\ \/ /__/ __ |/ /__/ __ | **
+** / __/ __// _ | / / / _ | (c) 2003-2007, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
@@ -16,7 +16,7 @@ import java.lang.Thread
/** The object <code>ops</code> ...
*
- * @author Martin Odersky
+ * @author Martin Odersky, Stepan Koltsov
* @version 1.0, 12/03/2003
*/
object ops {
@@ -33,9 +33,9 @@ object ops {
* @param p ...
* @return ...
*/
- def future[a](p: => a): () => a = {
- val result = new SyncVar[a]
- spawn { result set p }
+ def future[A](p: => A): () => A = {
+ val result = new SyncVar[A]
+ spawn { result setWithCatch p }
() => result.get
}
@@ -44,9 +44,9 @@ object ops {
* @param yp ...
* @return ...
*/
- def par[a, b](xp: => a, yp: => b): (a, b) = {
- val y = new SyncVar[b]
- spawn { y set yp }
+ def par[A, B](xp: => A, yp: => B): (A, B) = {
+ val y = new SyncVar[B]
+ spawn { y setWithCatch yp }
(xp, y.get)
}
@@ -55,7 +55,7 @@ object ops {
* @param end ...
* @param p ...
*/
- def replicate(start: Int, end: Int)(p: Int => Unit): Unit = {
+ def replicate(start: Int, end: Int)(p: Int => Unit) {
if (start == end)
()
else if (start + 1 == end)