blob: 9f26ca8d6917b5a1e346585f811b0b93b1d8010d (
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
59
60
61
62
63
64
65
66
67
68
69
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2005-2013, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
package scala.actors
package scheduler
import scala.collection.mutable
private[scheduler] trait TerminationMonitor {
_: IScheduler =>
protected var activeActors = 0
protected val terminationHandlers = new mutable.HashMap[TrackedReactor, () => Unit]
private var started = false
/** newActor is invoked whenever a new actor is started. */
def newActor(a: TrackedReactor) = synchronized {
activeActors += 1
if (!started)
started = true
}
/** Registers a closure to be executed when the specified
* actor terminates.
*
* @param a the actor
* @param f the closure to be registered
*/
def onTerminate(a: TrackedReactor)(f: => Unit): Unit = synchronized {
terminationHandlers += (a -> (() => f))
}
/** Registers that the specified actor has terminated.
*
* @param a the actor that has terminated
*/
def terminated(a: TrackedReactor) = {
// obtain termination handler (if any)
val todo = synchronized {
terminationHandlers.get(a) match {
case Some(handler) =>
terminationHandlers -= a
handler
case None =>
() => { /* do nothing */ }
}
}
// invoke termination handler (if any)
todo()
synchronized {
activeActors -= 1
}
}
/** Checks whether all actors have terminated. */
private[actors] def allActorsTerminated: Boolean = synchronized {
started && activeActors <= 0
}
/** Checks for actors that have become garbage. */
protected def gc() {}
}
|