summaryrefslogtreecommitdiff
path: root/src/actors-migration/scala/actors/migration/MigrationSystem.scala
blob: 3dcb38e63498b60637949f0da4d434991e789c1e (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
package scala.actors.migration

import scala.actors._
import scala.collection._

object MigrationSystem {

  private[migration] val contextStack = new ThreadLocal[immutable.Stack[Boolean]] {
    override def initialValue() = immutable.Stack[Boolean]()
  }

  private[this] def withCleanContext(block: => ActorRef): ActorRef = {
    // push clean marker
    val old = contextStack.get
    contextStack.set(old.push(true))
    try {
      val instance = block

      if (instance eq null)
        throw new Exception("Actor instance passed to actorOf can't be 'null'")

      instance
    } finally {
      val stackAfter = contextStack.get
      if (stackAfter.nonEmpty)
        contextStack.set(if (!stackAfter.head) stackAfter.pop.pop else stackAfter.pop)
    }
  }

  def actorOf(props: Props): ActorRef = withCleanContext {
    val creator = props.creator()
    val r = new InternalActorRef(creator)
    creator.start()
    r
  }

}