src/Pure/Concurrent/simple_thread.ML
author wenzelm
Sun, 26 Jul 2015 20:57:35 +0200
changeset 60788 5e2df6bd906c
parent 60764 b610ba36e02c
child 60829 4b16b778ce0d
permissions -rw-r--r--
updated to infer_instantiate;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Concurrent/simple_thread.ML
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
     3
28577
bd2456e0d944 added generic combinator for synchronized evaluation (formerly in future.ML);
wenzelm
parents: 28550
diff changeset
     4
Simplified thread operations.
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
     5
*)
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
     6
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
     7
signature SIMPLE_THREAD =
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
     8
sig
49894
69bfd86cc711 more robust cancel_now: avoid shooting yourself in the foot;
wenzelm
parents: 44112
diff changeset
     9
  val is_self: Thread.thread -> bool
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    10
  val identification: unit -> string option
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    11
  type params = {name: string, stack_limit: int option, interrupts: bool}
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
    12
  val attributes: params -> Thread.threadAttribute list
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
    13
  val fork: params -> (unit -> unit) -> Thread.thread
52583
0a7240d88e09 explicit shutdown of message output thread;
wenzelm
parents: 49894
diff changeset
    14
  val join: Thread.thread -> unit
44112
ef876972fdc1 more explicit Simple_Thread.interrupt_unsynchronized, to emphasize its meaning;
wenzelm
parents: 40232
diff changeset
    15
  val interrupt_unsynchronized: Thread.thread -> unit
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
    16
end;
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
    17
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 33605
diff changeset
    18
structure Simple_Thread: SIMPLE_THREAD =
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
    19
struct
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
    20
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    21
(* self *)
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    22
49894
69bfd86cc711 more robust cancel_now: avoid shooting yourself in the foot;
wenzelm
parents: 44112
diff changeset
    23
fun is_self thread = Thread.equal (Thread.self (), thread);
69bfd86cc711 more robust cancel_now: avoid shooting yourself in the foot;
wenzelm
parents: 44112
diff changeset
    24
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    25
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    26
(* identification *)
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    27
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    28
local
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    29
  val tag = Universal.tag () : string Universal.tag;
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    30
  val count = Counter.make ();
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    31
in
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    32
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    33
fun identification () = Thread.getLocal tag;
33602
d25e6bd6ca95 exported SimpleThread.attributes;
wenzelm
parents: 33220
diff changeset
    34
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    35
fun set_identification name =
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    36
  Thread.setLocal (tag, name ^ ":" ^ string_of_int (count ()));
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    37
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    38
end;
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    39
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    40
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    41
(* fork *)
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    42
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    43
type params = {name: string, stack_limit: int option, interrupts: bool};
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    44
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    45
fun attributes ({stack_limit, interrupts, ...}: params) =
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
    46
  maximum_ml_stack stack_limit @
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
    47
  (if interrupts then Multithreading.public_interrupts else Multithreading.no_interrupts);
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
    48
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    49
fun fork (params: params) body =
39232
69c6d3e87660 more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents: 37216
diff changeset
    50
  Thread.fork (fn () =>
59055
5a7157b8e870 more informative failure of protocol commands, with exception trace;
wenzelm
parents: 59054
diff changeset
    51
    print_exception_trace General.exnMessage tracing (fn () =>
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    52
      (set_identification (#name params); body ())
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    53
        handle exn => if Exn.is_interrupt exn then () (*sic!*) else reraise exn),
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
    54
    attributes params);
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
    55
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    56
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    57
(* join *)
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    58
52583
0a7240d88e09 explicit shutdown of message output thread;
wenzelm
parents: 49894
diff changeset
    59
fun join thread =
0a7240d88e09 explicit shutdown of message output thread;
wenzelm
parents: 49894
diff changeset
    60
  while Thread.isActive thread
0a7240d88e09 explicit shutdown of message output thread;
wenzelm
parents: 49894
diff changeset
    61
  do OS.Process.sleep (seconds 0.1);
0a7240d88e09 explicit shutdown of message output thread;
wenzelm
parents: 49894
diff changeset
    62
60764
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    63
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    64
(* interrupt *)
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    65
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    66
fun interrupt_unsynchronized thread =
b610ba36e02c more explicit thread identification;
wenzelm
parents: 59468
diff changeset
    67
  Thread.interrupt thread handle Thread _ => ();
28550
422e9bd169ac added fail-safe interrupt;
wenzelm
parents: 28241
diff changeset
    68
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
    69
end;