diff options
author | Philipp Haller <hallerp@gmail.com> | 2009-04-17 10:22:36 +0000 |
---|---|---|
committer | Philipp Haller <hallerp@gmail.com> | 2009-04-17 10:22:36 +0000 |
commit | c516c42d42a5b7a9979d9acf30cee05e9016a4fb (patch) | |
tree | c901bad277fc253b46ec4a1857f8d00e3875bca5 /src | |
parent | cc3c2f72dfe906ed25ebea6ab71ca5d5454b8bbe (diff) | |
download | scala-c516c42d42a5b7a9979d9acf30cee05e9016a4fb.tar.gz scala-c516c42d42a5b7a9979d9acf30cee05e9016a4fb.tar.bz2 scala-c516c42d42a5b7a9979d9acf30cee05e9016a4fb.zip |
Added SchedulerService class that allows subcla...
Added SchedulerService class that allows subclasses to define custom
shutdown actions.
Diffstat (limited to 'src')
-rw-r--r-- | src/actors/scala/actors/SchedulerService.scala | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/src/actors/scala/actors/SchedulerService.scala b/src/actors/scala/actors/SchedulerService.scala new file mode 100644 index 0000000000..f885e30b02 --- /dev/null +++ b/src/actors/scala/actors/SchedulerService.scala @@ -0,0 +1,94 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2005-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id: $ + +package scala.actors + +import java.lang.{Runnable, Thread, InterruptedException} + +/** + * The abstract <code>SchedulerService</code> class allows + * subclasses to implement a custom <code>onShutdown</code> + * method, which is invoked when the runtime system has detected + * that all actors have been terminated. + * + * @version 0.9.18 + * @author Philipp Haller + */ +abstract class SchedulerService(daemon: Boolean) extends Thread with IScheduler { + setDaemon(daemon) + + def this() = + this(false) + + /** The <code>ActorGC</code> instance that keeps track of the + * live actor objects that are managed by <code>this</code> + * scheduler. + */ + val actorGC = new ActorGC + + private var terminating = false + + def printActorDump {} + + protected val CHECK_FREQ = 100 + + def onLockup(handler: () => Unit) = + lockupHandler = handler + + def onLockup(millis: Int)(handler: () => Unit) = { + //LOCKUP_CHECK_FREQ = millis / CHECK_FREQ + lockupHandler = handler + } + + private var lockupHandler: () => Unit = null + + def onShutdown(): Unit + + override def run() { + try { + while (!terminating) { + this.synchronized { + try { + wait(CHECK_FREQ) + } catch { + case _: InterruptedException => + if (terminating) throw new QuitException + } + + actorGC.gc() + + if (actorGC.allTerminated) { + throw new QuitException + } + } // sync + } // while (!terminating) + } catch { + case _: QuitException => + Debug.info(this+": initiating shutdown...") + // invoke shutdown hook + onShutdown() + // allow thread to exit + } + } + + /** Submits a <code>Runnable</code> for execution. + * + * @param task the task to be executed + */ + def execute(task: Runnable): Unit = + execute { task.run() } + + /** Shuts down the scheduler. + */ + def shutdown(): Unit = synchronized { + terminating = true + } + +} |