| author | wenzelm |
| Fri, 13 May 2011 23:24:06 +0200 | |
| changeset 42794 | 07155da3b2f4 |
| parent 38849 | 2f198d107aef |
| child 43406 | 40c67d894be4 |
| permissions | -rw-r--r-- |
|
38428
c13c95c97e89
event_bus.scala rather belongs to system plumbing;
wenzelm
parents:
36676
diff
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) {
|
23 |
this += actor { loop { react { case x: Event => f(x) } } }
|
|
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:
38428
diff
changeset
|
35 |
|
|
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38428
diff
changeset
|
36 |
|
|
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38428
diff
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:
38428
diff
changeset
|
38 |
|
|
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38428
diff
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:
38428
diff
changeset
|
40 |
{
|
|
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38428
diff
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:
38428
diff
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:
38428
diff
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:
38428
diff
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:
38428
diff
changeset
|
45 |
else {
|
|
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38428
diff
changeset
|
46 |
loop {
|
|
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38428
diff
changeset
|
47 |
react {
|
|
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38428
diff
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:
38428
diff
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:
38428
diff
changeset
|
50 |
} |
|
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38428
diff
changeset
|
51 |
} |
|
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38428
diff
changeset
|
52 |
} |
|
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38428
diff
changeset
|
53 |
} |
|
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38428
diff
changeset
|
54 |
} |
|
2f198d107aef
session_actor: await state assigment of previous change before signalling current change, and avoid crash in overrun situations;
wenzelm
parents:
38428
diff
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:
38428
diff
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:
38428
diff
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:
38428
diff
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:
38428
diff
changeset
|
59 |
} |
| 29190 | 60 |
} |