| author | wenzelm | 
| Wed, 27 Mar 2024 15:38:41 +0100 | |
| changeset 80033 | 71d005ffa9fe | 
| parent 78753 | f40b59292288 | 
| child 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 | ||
| 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: 
52584diff
changeset | 28 | let | 
| 59058 
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
 wenzelm parents: 
58857diff
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); | |
| 78753 | 41 | val thread = Isabelle_Thread.fork (Isabelle_Thread.params "message_channel") loop; | 
| 73578 | 42 |   in Message_Channel {mbox = mbox, thread = thread} end;
 | 
| 52584 | 43 | |
| 44 | end; |