src/Pure/System/message_channel.ML
author wenzelm
Fri, 05 Jul 2024 00:18:51 +0200
changeset 80505 e3af424fdd1a
parent 78753 f40b59292288
permissions -rw-r--r--
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52584
5cad4a5f5615 more abstract message channel;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/System/message_channel.ML
5cad4a5f5615 more abstract message channel;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
5cad4a5f5615 more abstract message channel;
wenzelm
parents:
diff changeset
     3
5cad4a5f5615 more abstract message channel;
wenzelm
parents:
diff changeset
     4
Preferably asynchronous channel for Isabelle messages.
5cad4a5f5615 more abstract message channel;
wenzelm
parents:
diff changeset
     5
*)
5cad4a5f5615 more abstract message channel;
wenzelm
parents:
diff changeset
     6
5cad4a5f5615 more abstract message channel;
wenzelm
parents:
diff changeset
     7
signature MESSAGE_CHANNEL =
5cad4a5f5615 more abstract message channel;
wenzelm
parents:
diff changeset
     8
sig
5cad4a5f5615 more abstract message channel;
wenzelm
parents:
diff changeset
     9
  type T
5cad4a5f5615 more abstract message channel;
wenzelm
parents:
diff changeset
    10
  val shutdown: T -> unit
73578
629868f96c81 misc tuning and clarification;
wenzelm
parents: 73577
diff changeset
    11
  val message: T -> string -> Properties.T -> XML.body list -> unit
69449
b516fdf8005c more uniform multi-language operations;
wenzelm
parents: 62359
diff changeset
    12
  val make: BinIO.outstream -> T
52584
5cad4a5f5615 more abstract message channel;
wenzelm
parents:
diff changeset
    13
end;
5cad4a5f5615 more abstract message channel;
wenzelm
parents:
diff changeset
    14
5cad4a5f5615 more abstract message channel;
wenzelm
parents:
diff changeset
    15
structure Message_Channel: MESSAGE_CHANNEL =
5cad4a5f5615 more abstract message channel;
wenzelm
parents:
diff changeset
    16
struct
5cad4a5f5615 more abstract message channel;
wenzelm
parents:
diff changeset
    17
73578
629868f96c81 misc tuning and clarification;
wenzelm
parents: 73577
diff changeset
    18
datatype message = Shutdown | Message of XML.body list;
629868f96c81 misc tuning and clarification;
wenzelm
parents: 73577
diff changeset
    19
78648
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 73578
diff changeset
    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: 52584
diff changeset
    21
73578
629868f96c81 misc tuning and clarification;
wenzelm
parents: 73577
diff changeset
    22
fun shutdown (Message_Channel {mbox, thread}) =
629868f96c81 misc tuning and clarification;
wenzelm
parents: 73577
diff changeset
    23
 (Mailbox.send mbox Shutdown;
629868f96c81 misc tuning and clarification;
wenzelm
parents: 73577
diff changeset
    24
  Mailbox.await_empty mbox;
629868f96c81 misc tuning and clarification;
wenzelm
parents: 73577
diff changeset
    25
  Isabelle_Thread.join thread);
629868f96c81 misc tuning and clarification;
wenzelm
parents: 73577
diff changeset
    26
80505
e3af424fdd1a more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents: 78753
diff 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: 52584
diff changeset
    28
  let
80505
e3af424fdd1a more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents: 78753
diff 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: 78753
diff 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: 78753
diff 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: 78753
diff changeset
    32
  in Mailbox.send mbox (Message (kind :: props_length :: props_chunks @ chunks)) end;
52584
5cad4a5f5615 more abstract message channel;
wenzelm
parents:
diff changeset
    33
69449
b516fdf8005c more uniform multi-language operations;
wenzelm
parents: 62359
diff changeset
    34
fun make stream =
62359
6709e51d5c11 unconditional Multithreading;
wenzelm
parents: 61556
diff changeset
    35
  let
6709e51d5c11 unconditional Multithreading;
wenzelm
parents: 61556
diff changeset
    36
    val mbox = Mailbox.create ();
73578
629868f96c81 misc tuning and clarification;
wenzelm
parents: 73577
diff changeset
    37
    fun loop () = Mailbox.receive NONE mbox |> dispatch
629868f96c81 misc tuning and clarification;
wenzelm
parents: 73577
diff changeset
    38
    and dispatch [] = loop ()
629868f96c81 misc tuning and clarification;
wenzelm
parents: 73577
diff changeset
    39
      | dispatch (Shutdown :: _) = ()
629868f96c81 misc tuning and clarification;
wenzelm
parents: 73577
diff changeset
    40
      | dispatch (Message chunks :: rest) =
629868f96c81 misc tuning and clarification;
wenzelm
parents: 73577
diff changeset
    41
          (Byte_Message.write_message_yxml stream chunks; dispatch rest);
78753
f40b59292288 clarified signature;
wenzelm
parents: 78648
diff changeset
    42
    val thread = Isabelle_Thread.fork (Isabelle_Thread.params "message_channel") loop;
73578
629868f96c81 misc tuning and clarification;
wenzelm
parents: 73577
diff changeset
    43
  in Message_Channel {mbox = mbox, thread = thread} end;
52584
5cad4a5f5615 more abstract message channel;
wenzelm
parents:
diff changeset
    44
5cad4a5f5615 more abstract message channel;
wenzelm
parents:
diff changeset
    45
end;