summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/io/package.scala
blob: 565a3d4bcb84de6b374e63b92790327e388c2988 (plain) (blame)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* NSC -- new Scala compiler
 * Copyright 2005-2011 LAMP/EPFL
 * @author Paul Phillips
 */

package scala.tools.nsc

import java.util.concurrent.{ Future, Callable, Executors, ThreadFactory }
import java.util.{ Timer, TimerTask }
import java.util.jar.{ Attributes }

package object io {
  type JManifest = java.util.jar.Manifest
  type JFile = java.io.File
  private[io] implicit def installManifestOps(m: JManifest) = new ManifestOps(m)
  class ManifestOps(manifest: JManifest) {
    def attrs                                       = manifest.getMainAttributes()
    def apply(name: Attributes.Name)                = "" + attrs.get(name)
    def update(key: Attributes.Name, value: String) = attrs.put(key, value)
  }

  def runnable(body: => Unit): Runnable       = new Runnable { override def run() = body }
  def callable[T](body: => T): Callable[T]    = new Callable[T] { override def call() = body }
  def spawn[T](body: => T): Future[T]         = Executors.newSingleThreadExecutor() submit callable[T](body)
  def submit(runnable: Runnable)              = Executors.newSingleThreadExecutor() submit runnable
  def runnableFn(f: () => Unit): Runnable     = runnable(f())
  def callableFn[T](f: () => T): Callable[T]  = callable(f())
  def spawnFn[T](f: () => T): Future[T]       = spawn(f())

  def newConfiguredExecutor(f: Thread => Unit) = {
    Executors.newCachedThreadPool(new ThreadFactory {
      def newThread(r: Runnable) = {
        val t = Executors.defaultThreadFactory().newThread(r)
        f(t)
        t
      }
    })
  }
  def newDaemonThreadExecutor() = newConfiguredExecutor(_ setDaemon true)

  // Create, start, and return a background thread
  // If isDaemon is true, it is marked as daemon (and will not interfere with JVM shutdown)
  def daemonize(isDaemon: Boolean)(body: => Unit): Thread = {
    val thread = new Thread(runnable(body))
    thread setDaemon isDaemon
    thread.start
    thread
  }

  // Set a timer to execute the given code.
  def timer(seconds: Int)(body: => Unit): Timer = {
    val alarm = new Timer(true) // daemon
    val tt    = new TimerTask { def run() = body }

    alarm.schedule(tt, seconds * 1000)
    alarm
  }
}