src/Pure/System/event_bus.scala
author wenzelm
Fri, 25 Nov 2011 21:29:11 +0100
changeset 45635 d9cf3520083c
parent 43406 40c67d894be4
child 45667 546d78f0d81f
permissions -rw-r--r--
explicit change_parser thread, which avoids undirected Future.fork with its tendency towards hundreds of worker threads;

/*  Title:      Pure/System/event_bus.scala
    Author:     Makarius

Generic event bus with multiple receiving actors.
*/

package isabelle

import scala.actors.Actor, Actor._
import scala.collection.mutable.ListBuffer


class Event_Bus[Event]
{
  /* receivers */

  private val receivers = new ListBuffer[Actor]

  def += (r: Actor) { synchronized { receivers += r } }
  def + (r: Actor): Event_Bus[Event] = { this += r; this }

  def += (f: Event => Unit) {
    this += actor { loop { react { case x => f(x.asInstanceOf[Event]) } } }
  }

  def + (f: Event => Unit): Event_Bus[Event] = { this += f; this }

  def -= (r: Actor) { synchronized { receivers -= r } }
  def - (r: Actor) = { this -= r; this }


  /* event invocation */

  def event(x: Event) { synchronized { receivers.foreach(_ ! x) } }
}