-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathThreads.scala
More file actions
36 lines (34 loc) · 772 Bytes
/
Threads.scala
File metadata and controls
36 lines (34 loc) · 772 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* sbt -- Simple Build Tool
* Copyright 2009 Mark Harrah
*/
package xsbt
import scala.concurrent.SyncVar
/** Runs provided code in a new Thread and returns the Thread instance. */
private object Spawn
{
def apply(f: => Unit): Thread = apply(f, false)
def apply(f: => Unit, daemon: Boolean): Thread =
{
val thread = new Thread() { override def run() = { f } }
thread.setDaemon(daemon)
thread.start()
thread
}
}
private object Future
{
def apply[T](f: => T): () => T =
{
val result = new SyncVar[Either[Throwable, T]]
def run: Unit =
try { result.set(Right(f)) }
catch { case e: Exception => result.set(Left(e)) }
Spawn(run)
() =>
result.get match
{
case Right(value) => value
case Left(exception) => throw exception
}
}
}