| author | wenzelm | 
| Fri, 23 Aug 2024 20:21:04 +0200 | |
| changeset 80749 | 232a839ef8e6 | 
| parent 80505 | e3af424fdd1a | 
| 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 | ||
| 78648 | 20 | datatype T = Message_Channel of {mbox: message Mailbox.T, thread: Isabelle_Thread.T};
 | 
| 52800 
1baa5d19ac44
less aggressive flushing: cope with massive amounts of protocol messages, e.g. from threads_trace;
 wenzelm parents: 
52584diff
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 | ||
| 80505 
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
 wenzelm parents: 
78753diff
changeset | 27 | fun message (Message_Channel {mbox, ...}) name props chunks =
 | 
| 52800 
1baa5d19ac44
less aggressive flushing: cope with massive amounts of protocol messages, e.g. from threads_trace;
 wenzelm parents: 
52584diff
changeset | 28 | let | 
| 80505 
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
 wenzelm parents: 
78753diff
changeset | 29 | val kind = XML.Encode.string name; | 
| 
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
 wenzelm parents: 
78753diff
changeset | 30 | val props_length = XML.Encode.int (length props); | 
| 
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
 wenzelm parents: 
78753diff
changeset | 31 | val props_chunks = map (XML.Encode.string o Properties.print_eq) props; | 
| 
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
 wenzelm parents: 
78753diff
changeset | 32 | in Mailbox.send mbox (Message (kind :: props_length :: props_chunks @ chunks)) end; | 
| 52584 | 33 | |
| 69449 | 34 | fun make stream = | 
| 62359 | 35 | let | 
| 36 | val mbox = Mailbox.create (); | |
| 73578 | 37 | fun loop () = Mailbox.receive NONE mbox |> dispatch | 
| 38 | and dispatch [] = loop () | |
| 39 | | dispatch (Shutdown :: _) = () | |
| 40 | | dispatch (Message chunks :: rest) = | |
| 41 | (Byte_Message.write_message_yxml stream chunks; dispatch rest); | |
| 78753 | 42 | val thread = Isabelle_Thread.fork (Isabelle_Thread.params "message_channel") loop; | 
| 73578 | 43 |   in Message_Channel {mbox = mbox, thread = thread} end;
 | 
| 52584 | 44 | |
| 45 | end; |