diff options
Diffstat (limited to 'src/compiler/scala/tools/nsc/util/package.scala')
-rw-r--r-- | src/compiler/scala/tools/nsc/util/package.scala | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/compiler/scala/tools/nsc/util/package.scala b/src/compiler/scala/tools/nsc/util/package.scala index 92d4eab54f..c38b2c5031 100644 --- a/src/compiler/scala/tools/nsc/util/package.scala +++ b/src/compiler/scala/tools/nsc/util/package.scala @@ -11,6 +11,28 @@ package object util { /** Apply a function and return the passed value */ def returning[T](x: T)(f: T => Unit): T = { f(x) ; x } + /** All living threads. */ + def allThreads(): List[Thread] = { + val num = Thread.activeCount() + val tarray = new Array[Thread](num) + val got = Thread.enumerate(tarray) + + tarray take got toList + } + + /** Execute code and then wait for all Threads created during its + * execution to complete. + */ + def waitingForThreads[T](body: => T) = { + val ts1 = allThreads() + val result = body + val ts2 = allThreads() + val newThreads = (ts2.toSet -- ts1) filterNot (_.isDaemon()) + + newThreads foreach (_.join()) + result + } + /** Generate a string using a routine that wants to write on a stream. */ def stringFromWriter(writer: PrintWriter => Unit): String = { val stringWriter = new StringWriter() |