author | wenzelm |
Tue, 04 May 2021 20:02:08 +0200 | |
changeset 73625 | f8f065e20837 |
parent 73578 | 629868f96c81 |
child 78648 | 852ec09aef13 |
permissions | -rw-r--r-- |
52584 | 1 |
(* Title: Pure/System/message_channel.ML |
2 |
Author: Makarius |
|
3 |
||
4 |
Preferably asynchronous channel for Isabelle messages. |
|
5 |
*) |
|
6 |
||
7 |
signature MESSAGE_CHANNEL = |
|
8 |
sig |
|
9 |
type T |
|
10 |
val shutdown: T -> unit |
|
73578 | 11 |
val message: T -> string -> Properties.T -> XML.body list -> unit |
69449 | 12 |
val make: BinIO.outstream -> T |
52584 | 13 |
end; |
14 |
||
15 |
structure Message_Channel: MESSAGE_CHANNEL = |
|
16 |
struct |
|
17 |
||
73578 | 18 |
datatype message = Shutdown | Message of XML.body list; |
19 |
||
20 |
datatype T = Message_Channel of {mbox: message Mailbox.T, thread: Thread.thread}; |
|
52800
1baa5d19ac44
less aggressive flushing: cope with massive amounts of protocol messages, e.g. from threads_trace;
wenzelm
parents:
52584
diff
changeset
|
21 |
|
73578 | 22 |
fun shutdown (Message_Channel {mbox, thread}) = |
23 |
(Mailbox.send mbox Shutdown; |
|
24 |
Mailbox.await_empty mbox; |
|
25 |
Isabelle_Thread.join thread); |
|
26 |
||
27 |
fun message (Message_Channel {mbox, ...}) name raw_props chunks = |
|
52800
1baa5d19ac44
less aggressive flushing: cope with massive amounts of protocol messages, e.g. from threads_trace;
wenzelm
parents:
52584
diff
changeset
|
28 |
let |
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
58857
diff
changeset
|
29 |
val robust_props = map (apply2 YXML.embed_controls) raw_props; |
73559 | 30 |
val header = [XML.Elem ((name, robust_props), [])]; |
73578 | 31 |
in Mailbox.send mbox (Message (header :: chunks)) end; |
52584 | 32 |
|
69449 | 33 |
fun make stream = |
62359 | 34 |
let |
35 |
val mbox = Mailbox.create (); |
|
73578 | 36 |
fun loop () = Mailbox.receive NONE mbox |> dispatch |
37 |
and dispatch [] = loop () |
|
38 |
| dispatch (Shutdown :: _) = () |
|
39 |
| dispatch (Message chunks :: rest) = |
|
40 |
(Byte_Message.write_message_yxml stream chunks; dispatch rest); |
|
62359 | 41 |
val thread = |
73578 | 42 |
Isabelle_Thread.fork {name = "message_channel", stack_limit = NONE, interrupts = false} loop; |
43 |
in Message_Channel {mbox = mbox, thread = thread} end; |
|
52584 | 44 |
|
45 |
end; |