src/Pure/Concurrent/isabelle_thread.ML
author wenzelm
Thu, 21 Sep 2023 23:45:03 +0200
changeset 78681 38fe769658be
parent 78678 5b2391321bab
child 78688 ff7db9055002
permissions -rw-r--r--
clarified modules; clarified signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
71692
f8e52c0152fe clarified names;
wenzelm
parents: 64557
diff changeset
     1
(*  Title:      Pure/Concurrent/isabelle_thread.ML
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
     3
71692
f8e52c0152fe clarified names;
wenzelm
parents: 64557
diff changeset
     4
Isabelle-specific thread management.
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
     5
*)
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
     6
71692
f8e52c0152fe clarified names;
wenzelm
parents: 64557
diff changeset
     7
signature ISABELLE_THREAD =
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
     8
sig
78648
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
     9
  type T
78650
47d0c333d155 clarified signature: retain original Poly/ML names Thread.Thread, Thread.Mutex, Thread.ConditionVar and de-emphasize them for Isabelle/ML;
wenzelm
parents: 78648
diff changeset
    10
  val get_thread: T -> Thread.Thread.thread
78648
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    11
  val get_name: T -> string
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    12
  val get_id: T -> int
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    13
  val equal: T * T -> bool
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    14
  val print: T -> string
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    15
  val self: unit -> T
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    16
  val is_self: T -> bool
71883
44ba78056790 clarified signature;
wenzelm
parents: 71694
diff changeset
    17
  val stack_limit: unit -> int option
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    18
  type params = {name: string, stack_limit: int option, interrupts: bool}
78650
47d0c333d155 clarified signature: retain original Poly/ML names Thread.Thread, Thread.Mutex, Thread.ConditionVar and de-emphasize them for Isabelle/ML;
wenzelm
parents: 78648
diff changeset
    19
  val attributes: params -> Thread.Thread.threadAttribute list
78648
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    20
  val fork: params -> (unit -> unit) -> T
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    21
  val is_active: T -> bool
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    22
  val join: T -> unit
78681
38fe769658be clarified modules;
wenzelm
parents: 78678
diff changeset
    23
  val interrupt: exn
38fe769658be clarified modules;
wenzelm
parents: 78678
diff changeset
    24
  val interrupt_exn: 'a Exn.result
38fe769658be clarified modules;
wenzelm
parents: 78678
diff changeset
    25
  val interrupt_self: unit -> 'a
38fe769658be clarified modules;
wenzelm
parents: 78678
diff changeset
    26
  val interrupt_other: T -> unit
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
    27
end;
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
    28
71692
f8e52c0152fe clarified names;
wenzelm
parents: 64557
diff changeset
    29
structure Isabelle_Thread: ISABELLE_THREAD =
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
    30
struct
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
    31
78648
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    32
(* abstract type *)
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    33
78650
47d0c333d155 clarified signature: retain original Poly/ML names Thread.Thread, Thread.Mutex, Thread.ConditionVar and de-emphasize them for Isabelle/ML;
wenzelm
parents: 78648
diff changeset
    34
abstype T = T of {thread: Thread.Thread.thread, name: string, id: int}
78648
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    35
with
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    36
  val make = T;
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    37
  fun dest (T args) = args;
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    38
end;
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    39
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    40
val get_thread = #thread o dest;
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    41
val get_name = #name o dest;
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    42
val get_id = #id o dest;
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    43
78650
47d0c333d155 clarified signature: retain original Poly/ML names Thread.Thread, Thread.Mutex, Thread.ConditionVar and de-emphasize them for Isabelle/ML;
wenzelm
parents: 78648
diff changeset
    44
val equal = Thread.Thread.equal o apply2 get_thread;
78648
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    45
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    46
fun print t =
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    47
  (case get_name t of "" => "ML" | a => "Isabelle." ^ a) ^
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    48
    "-" ^ string_of_int (get_id t);
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    49
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    50
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    51
(* self *)
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    52
78648
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    53
val make_id = Counter.make ();
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    54
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    55
local
78648
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    56
  val self_var = Thread_Data.var () : T Thread_Data.var;
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    57
in
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    58
78678
wenzelm
parents: 78677
diff changeset
    59
fun init_self args =
wenzelm
parents: 78677
diff changeset
    60
  let val t = make args in Thread_Data.put self_var (SOME t); t end;
60830
f56e189350b2 separate channel for debugger output;
wenzelm
parents: 60829
diff changeset
    61
78648
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    62
fun self () =
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    63
  (case Thread_Data.get self_var of
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    64
    SOME t => t
78678
wenzelm
parents: 78677
diff changeset
    65
  | NONE => init_self {thread = Thread.Thread.self (), name = "", id = make_id ()});
78648
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    66
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    67
fun is_self t = equal (t, self ());
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    68
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    69
end;
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    70
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    71
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    72
(* fork *)
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    73
71883
44ba78056790 clarified signature;
wenzelm
parents: 71694
diff changeset
    74
fun stack_limit () =
44ba78056790 clarified signature;
wenzelm
parents: 71694
diff changeset
    75
  let
44ba78056790 clarified signature;
wenzelm
parents: 71694
diff changeset
    76
    val threads_stack_limit =
44ba78056790 clarified signature;
wenzelm
parents: 71694
diff changeset
    77
      Real.floor (Options.default_real "threads_stack_limit" * 1024.0 * 1024.0 * 1024.0);
44ba78056790 clarified signature;
wenzelm
parents: 71694
diff changeset
    78
  in if threads_stack_limit <= 0 then NONE else SOME threads_stack_limit end;
44ba78056790 clarified signature;
wenzelm
parents: 71694
diff changeset
    79
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    80
type params = {name: string, stack_limit: int option, interrupts: bool};
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    81
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    82
fun attributes ({stack_limit, interrupts, ...}: params) =
78650
47d0c333d155 clarified signature: retain original Poly/ML names Thread.Thread, Thread.Mutex, Thread.ConditionVar and de-emphasize them for Isabelle/ML;
wenzelm
parents: 78648
diff changeset
    83
  Thread.Thread.MaximumMLStack stack_limit ::
64557
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62923
diff changeset
    84
  Thread_Attributes.convert_attributes
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62923
diff changeset
    85
    (if interrupts then Thread_Attributes.public_interrupts else Thread_Attributes.no_interrupts);
59468
fe6651760643 explicit threads_stack_limit (for recent Poly/ML SVN versions), which leads to soft interrupt instead of exhaustion of virtual memory, which is particularly relevant for the bigger address space of x86_64;
wenzelm
parents: 59055
diff changeset
    86
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    87
fun fork (params: params) body =
78648
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    88
  let
78677
1b9e0f74addb more robust: prefer linear data flow;
wenzelm
parents: 78650
diff changeset
    89
    val self = Single_Assignment.var "self";
1b9e0f74addb more robust: prefer linear data flow;
wenzelm
parents: 78650
diff changeset
    90
    fun main () =
1b9e0f74addb more robust: prefer linear data flow;
wenzelm
parents: 78650
diff changeset
    91
      let
78678
wenzelm
parents: 78677
diff changeset
    92
        val t = init_self {thread = Thread.Thread.self (), name = #name params, id = make_id ()};
78677
1b9e0f74addb more robust: prefer linear data flow;
wenzelm
parents: 78650
diff changeset
    93
        val _ = Single_Assignment.assign self t;
1b9e0f74addb more robust: prefer linear data flow;
wenzelm
parents: 78650
diff changeset
    94
      in body () end;
1b9e0f74addb more robust: prefer linear data flow;
wenzelm
parents: 78650
diff changeset
    95
    val _ = Thread.Thread.fork (main, attributes params);
1b9e0f74addb more robust: prefer linear data flow;
wenzelm
parents: 78650
diff changeset
    96
  in Single_Assignment.await self end;
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
    97
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    98
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    99
(* join *)
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
   100
78650
47d0c333d155 clarified signature: retain original Poly/ML names Thread.Thread, Thread.Mutex, Thread.ConditionVar and de-emphasize them for Isabelle/ML;
wenzelm
parents: 78648
diff changeset
   101
val is_active = Thread.Thread.isActive o get_thread;
78648
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
   102
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
   103
fun join t =
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
   104
  while is_active t
52583
0a7240d88e09 explicit shutdown of message output thread;
wenzelm
parents: 49894
diff changeset
   105
  do OS.Process.sleep (seconds 0.1);
0a7240d88e09 explicit shutdown of message output thread;
wenzelm
parents: 49894
diff changeset
   106
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
   107
78681
38fe769658be clarified modules;
wenzelm
parents: 78678
diff changeset
   108
(* interrupts *)
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
   109
78681
38fe769658be clarified modules;
wenzelm
parents: 78678
diff changeset
   110
val interrupt = Thread.Thread.Interrupt;
38fe769658be clarified modules;
wenzelm
parents: 78678
diff changeset
   111
val interrupt_exn = Exn.Exn interrupt;
38fe769658be clarified modules;
wenzelm
parents: 78678
diff changeset
   112
38fe769658be clarified modules;
wenzelm
parents: 78678
diff changeset
   113
fun interrupt_self () = raise interrupt;
38fe769658be clarified modules;
wenzelm
parents: 78678
diff changeset
   114
38fe769658be clarified modules;
wenzelm
parents: 78678
diff changeset
   115
fun interrupt_other t =
78650
47d0c333d155 clarified signature: retain original Poly/ML names Thread.Thread, Thread.Mutex, Thread.ConditionVar and de-emphasize them for Isabelle/ML;
wenzelm
parents: 78648
diff changeset
   116
  Thread.Thread.interrupt (get_thread t) handle Thread.Thread _ => ();
28550
422e9bd169ac added fail-safe interrupt;
wenzelm
parents: 28241
diff changeset
   117
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
   118
end;