src/Pure/Concurrent/isabelle_thread.ML
author wenzelm
Sun, 24 Sep 2023 15:07:40 +0200
changeset 78688 ff7db9055002
parent 78681 38fe769658be
child 78701 c7b2697094ac
permissions -rw-r--r--
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
78688
ff7db9055002 clarified signature;
wenzelm
parents: 78681
diff changeset
    27
  val try: (unit -> 'a) -> 'a option
ff7db9055002 clarified signature;
wenzelm
parents: 78681
diff changeset
    28
  val can: (unit -> 'a) -> bool
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
    29
end;
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
    30
71692
f8e52c0152fe clarified names;
wenzelm
parents: 64557
diff changeset
    31
structure Isabelle_Thread: ISABELLE_THREAD =
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
    32
struct
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
    33
78648
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    34
(* abstract type *)
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    35
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
    36
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
    37
with
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    38
  val make = T;
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    39
  fun dest (T args) = args;
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    40
end;
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    41
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    42
val get_thread = #thread o dest;
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    43
val get_name = #name o dest;
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    44
val get_id = #id o dest;
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    45
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
    46
val equal = Thread.Thread.equal o apply2 get_thread;
78648
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    47
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    48
fun print t =
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    49
  (case get_name t of "" => "ML" | a => "Isabelle." ^ a) ^
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    50
    "-" ^ string_of_int (get_id t);
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    51
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    52
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    53
(* self *)
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    54
78648
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    55
val make_id = Counter.make ();
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    56
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    57
local
78648
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    58
  val self_var = Thread_Data.var () : T Thread_Data.var;
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    59
in
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    60
78678
wenzelm
parents: 78677
diff changeset
    61
fun init_self args =
wenzelm
parents: 78677
diff changeset
    62
  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
    63
78648
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    64
fun self () =
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    65
  (case Thread_Data.get self_var of
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    66
    SOME t => t
78678
wenzelm
parents: 78677
diff changeset
    67
  | NONE => init_self {thread = Thread.Thread.self (), name = "", id = make_id ()});
78648
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    68
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    69
fun is_self t = equal (t, self ());
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    70
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    71
end;
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    72
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    73
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    74
(* fork *)
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    75
71883
44ba78056790 clarified signature;
wenzelm
parents: 71694
diff changeset
    76
fun stack_limit () =
44ba78056790 clarified signature;
wenzelm
parents: 71694
diff changeset
    77
  let
44ba78056790 clarified signature;
wenzelm
parents: 71694
diff changeset
    78
    val threads_stack_limit =
44ba78056790 clarified signature;
wenzelm
parents: 71694
diff changeset
    79
      Real.floor (Options.default_real "threads_stack_limit" * 1024.0 * 1024.0 * 1024.0);
44ba78056790 clarified signature;
wenzelm
parents: 71694
diff changeset
    80
  in if threads_stack_limit <= 0 then NONE else SOME threads_stack_limit end;
44ba78056790 clarified signature;
wenzelm
parents: 71694
diff changeset
    81
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    82
type params = {name: string, stack_limit: int option, interrupts: bool};
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    83
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    84
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
    85
  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
    86
  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
    87
    (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
    88
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    89
fun fork (params: params) body =
78648
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
    90
  let
78677
1b9e0f74addb more robust: prefer linear data flow;
wenzelm
parents: 78650
diff changeset
    91
    val self = Single_Assignment.var "self";
1b9e0f74addb more robust: prefer linear data flow;
wenzelm
parents: 78650
diff changeset
    92
    fun main () =
1b9e0f74addb more robust: prefer linear data flow;
wenzelm
parents: 78650
diff changeset
    93
      let
78678
wenzelm
parents: 78677
diff changeset
    94
        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
    95
        val _ = Single_Assignment.assign self t;
1b9e0f74addb more robust: prefer linear data flow;
wenzelm
parents: 78650
diff changeset
    96
      in body () end;
1b9e0f74addb more robust: prefer linear data flow;
wenzelm
parents: 78650
diff changeset
    97
    val _ = Thread.Thread.fork (main, attributes params);
1b9e0f74addb more robust: prefer linear data flow;
wenzelm
parents: 78650
diff changeset
    98
  in Single_Assignment.await self end;
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
    99
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
   100
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
   101
(* join *)
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
   102
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
   103
val is_active = Thread.Thread.isActive o get_thread;
78648
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
   104
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
   105
fun join t =
852ec09aef13 more explicit type Isabelle_Thread.T;
wenzelm
parents: 78647
diff changeset
   106
  while is_active t
52583
0a7240d88e09 explicit shutdown of message output thread;
wenzelm
parents: 49894
diff changeset
   107
  do OS.Process.sleep (seconds 0.1);
0a7240d88e09 explicit shutdown of message output thread;
wenzelm
parents: 49894
diff changeset
   108
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
   109
78681
38fe769658be clarified modules;
wenzelm
parents: 78678
diff changeset
   110
(* interrupts *)
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
   111
78681
38fe769658be clarified modules;
wenzelm
parents: 78678
diff changeset
   112
val interrupt = Thread.Thread.Interrupt;
38fe769658be clarified modules;
wenzelm
parents: 78678
diff changeset
   113
val interrupt_exn = Exn.Exn interrupt;
38fe769658be clarified modules;
wenzelm
parents: 78678
diff changeset
   114
38fe769658be clarified modules;
wenzelm
parents: 78678
diff changeset
   115
fun interrupt_self () = raise interrupt;
38fe769658be clarified modules;
wenzelm
parents: 78678
diff changeset
   116
38fe769658be clarified modules;
wenzelm
parents: 78678
diff changeset
   117
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
   118
  Thread.Thread.interrupt (get_thread t) handle Thread.Thread _ => ();
28550
422e9bd169ac added fail-safe interrupt;
wenzelm
parents: 28241
diff changeset
   119
78688
ff7db9055002 clarified signature;
wenzelm
parents: 78681
diff changeset
   120
fun try e = Basics.try e ();
ff7db9055002 clarified signature;
wenzelm
parents: 78681
diff changeset
   121
fun can e = Basics.can e ();
ff7db9055002 clarified signature;
wenzelm
parents: 78681
diff changeset
   122
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
   123
end;