| author | hoelzl | 
| Thu, 22 Sep 2011 10:02:16 -0400 | |
| changeset 45041 | 0523a6be8ade | 
| parent 43406 | 40c67d894be4 | 
| child 45635 | d9cf3520083c | 
| permissions | -rw-r--r-- | 
| 38428 
c13c95c97e89
event_bus.scala rather belongs to system plumbing;
 wenzelm parents: 
36676diff
changeset | 1 | /* Title: Pure/System/event_bus.scala | 
| 29190 | 2 | Author: Makarius | 
| 3 | ||
| 32539 | 4 | Generic event bus with multiple receiving actors. | 
| 29190 | 5 | */ | 
| 6 | ||
| 7 | package isabelle | |
| 8 | ||
| 32539 | 9 | import scala.actors.Actor, Actor._ | 
| 29191 | 10 | import scala.collection.mutable.ListBuffer | 
| 29190 | 11 | |
| 12 | ||
| 32539 | 13 | class Event_Bus[Event] | 
| 29200 | 14 | {
 | 
| 32539 | 15 | /* receivers */ | 
| 16 | ||
| 17 | private val receivers = new ListBuffer[Actor] | |
| 18 | ||
| 19 |   def += (r: Actor) { synchronized { receivers += r } }
 | |
| 20 |   def + (r: Actor): Event_Bus[Event] = { this += r; this }
 | |
| 29200 | 21 | |
| 32539 | 22 |   def += (f: Event => Unit) {
 | 
| 43406 
40c67d894be4
avoid compiler warning -- this is unchecked anyway;
 wenzelm parents: 
38849diff
changeset | 23 |     this += actor { loop { react { case x => f(x.asInstanceOf[Event]) } } }
 | 
| 32539 | 24 | } | 
| 29190 | 25 | |
| 32539 | 26 |   def + (f: Event => Unit): Event_Bus[Event] = { this += f; this }
 | 
| 29191 | 27 | |
| 32539 | 28 |   def -= (r: Actor) { synchronized { receivers -= r } }
 | 
| 29 |   def - (r: Actor) = { this -= r; this }
 | |
| 29190 | 30 | |
| 29200 | 31 | |
| 32 | /* event invocation */ | |
| 33 | ||
| 32539 | 34 |   def event(x: Event) { synchronized { receivers.foreach(_ ! x) } }
 | 
| 38849 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 35 | |
| 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 36 | |
| 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 37 | /* await global condition -- triggered via bus events */ | 
| 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 38 | |
| 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 39 | def await(cond: => Boolean) | 
| 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 40 |   {
 | 
| 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 41 | case object Wait | 
| 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 42 |     val a = new Actor {
 | 
| 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 43 |       def act {
 | 
| 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 44 |         if (cond) react { case Wait => reply(()); exit(Wait) }
 | 
| 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 45 |         else {
 | 
| 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 46 |           loop {
 | 
| 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 47 |             react {
 | 
| 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 48 | case trigger if trigger != Wait => | 
| 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 49 |                 if (cond) { react { case Wait => reply(()); exit(Wait) } }
 | 
| 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 50 | } | 
| 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 51 | } | 
| 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 52 | } | 
| 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 53 | } | 
| 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 54 | } | 
| 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 55 | this += a | 
| 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 56 | a.start | 
| 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 57 | a !? Wait | 
| 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 58 | this -= a | 
| 
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
 wenzelm parents: 
38428diff
changeset | 59 | } | 
| 29190 | 60 | } |