src/Pure/ML-Systems/multithreading_polyml.ML
author wenzelm
Mon, 27 Jul 2009 17:36:30 +0200
changeset 32230 9f6461b1c9cc
parent 32186 8026b73cd357
child 32286 1fb5db48002d
permissions -rw-r--r--
interruptible: Thread.testInterrupt before changing thread attributes;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/ML-Systems/multithreading_polyml.ML
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
     3
31311
b82e55f51dcc more precise version information;
wenzelm
parents: 30612
diff changeset
     4
Multithreading in Poly/ML 5.2.1 or later (cf. polyml/basis/Thread.sml).
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
     5
*)
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
     6
25704
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
     7
signature MULTITHREADING_POLYML =
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
     8
sig
26083
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
     9
  val interruptible: ('a -> 'b) -> 'a -> 'b
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
    10
  val uninterruptible: ((('c -> 'd) -> 'c -> 'd) -> 'a -> 'b) -> 'a -> 'b
26098
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
    11
  val system_out: string -> string * int
25704
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
    12
  structure TimeLimit: TIME_LIMIT
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
    13
end;
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
    14
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
    15
signature BASIC_MULTITHREADING =
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
    16
sig
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
    17
  include BASIC_MULTITHREADING
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
    18
  include MULTITHREADING_POLYML
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
    19
end;
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
    20
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    21
signature MULTITHREADING =
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    22
sig
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    23
  include MULTITHREADING
25704
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
    24
  include MULTITHREADING_POLYML
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    25
end;
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    26
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
    27
structure Multithreading: MULTITHREADING =
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
    28
struct
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
    29
32185
57ecfab3bcfe added Multithreading.real_time;
wenzelm
parents: 32184
diff changeset
    30
(* tracing *)
24069
8a15a04e36f6 tuned msgs;
wenzelm
parents: 24066
diff changeset
    31
24119
06965b38c5e9 tracing: level;
wenzelm
parents: 24109
diff changeset
    32
val trace = ref 0;
28465
db8667d84dd2 tracing: ignore failure of any kind;
wenzelm
parents: 28443
diff changeset
    33
24119
06965b38c5e9 tracing: level;
wenzelm
parents: 24109
diff changeset
    34
fun tracing level msg =
28465
db8667d84dd2 tracing: ignore failure of any kind;
wenzelm
parents: 28443
diff changeset
    35
  if level > ! trace then ()
db8667d84dd2 tracing: ignore failure of any kind;
wenzelm
parents: 28443
diff changeset
    36
  else (TextIO.output (TextIO.stdErr, (">>> " ^ msg () ^ "\n")); TextIO.flushOut TextIO.stdErr)
db8667d84dd2 tracing: ignore failure of any kind;
wenzelm
parents: 28443
diff changeset
    37
    handle _ (*sic*) => ();
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
    38
32186
8026b73cd357 tuned tracing;
wenzelm
parents: 32185
diff changeset
    39
fun tracing_time detailed time =
32184
cfa0ef0c0c5f simplified/unified Multithreading.tracing_time;
wenzelm
parents: 32107
diff changeset
    40
  tracing
32186
8026b73cd357 tuned tracing;
wenzelm
parents: 32185
diff changeset
    41
   (if not detailed then 5
8026b73cd357 tuned tracing;
wenzelm
parents: 32185
diff changeset
    42
    else if Time.>= (time, Time.fromMilliseconds 1000) then 1
32184
cfa0ef0c0c5f simplified/unified Multithreading.tracing_time;
wenzelm
parents: 32107
diff changeset
    43
    else if Time.>= (time, Time.fromMilliseconds 100) then 2
cfa0ef0c0c5f simplified/unified Multithreading.tracing_time;
wenzelm
parents: 32107
diff changeset
    44
    else if Time.>= (time, Time.fromMilliseconds 10) then 3
cfa0ef0c0c5f simplified/unified Multithreading.tracing_time;
wenzelm
parents: 32107
diff changeset
    45
    else if Time.>= (time, Time.fromMilliseconds 1) then 4 else 5);
cfa0ef0c0c5f simplified/unified Multithreading.tracing_time;
wenzelm
parents: 32107
diff changeset
    46
32185
57ecfab3bcfe added Multithreading.real_time;
wenzelm
parents: 32184
diff changeset
    47
fun real_time f x =
57ecfab3bcfe added Multithreading.real_time;
wenzelm
parents: 32184
diff changeset
    48
  let
57ecfab3bcfe added Multithreading.real_time;
wenzelm
parents: 32184
diff changeset
    49
    val timer = Timer.startRealTimer ();
57ecfab3bcfe added Multithreading.real_time;
wenzelm
parents: 32184
diff changeset
    50
    val () = f x;
57ecfab3bcfe added Multithreading.real_time;
wenzelm
parents: 32184
diff changeset
    51
    val time = Timer.checkRealTimer timer;
57ecfab3bcfe added Multithreading.real_time;
wenzelm
parents: 32184
diff changeset
    52
  in time end;
57ecfab3bcfe added Multithreading.real_time;
wenzelm
parents: 32184
diff changeset
    53
57ecfab3bcfe added Multithreading.real_time;
wenzelm
parents: 32184
diff changeset
    54
57ecfab3bcfe added Multithreading.real_time;
wenzelm
parents: 32184
diff changeset
    55
(* options *)
32184
cfa0ef0c0c5f simplified/unified Multithreading.tracing_time;
wenzelm
parents: 32107
diff changeset
    56
23981
03b71bf91318 added trace flag, official tracing operation;
wenzelm
parents: 23973
diff changeset
    57
val available = true;
25775
90525e67ede7 added Multithreading.max_threads_value, which maps a value of 0 to number of CPUs;
wenzelm
parents: 25735
diff changeset
    58
31897
15d55d07de8b default to maximum max_threads;
wenzelm
parents: 31630
diff changeset
    59
val max_threads = ref 0;
23973
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
    60
31630
2f8ed0dca3bd back to default -M max, with more robust interpretation of corresponding max_threads value;
wenzelm
parents: 31311
diff changeset
    61
val tested_platform =
2f8ed0dca3bd back to default -M max, with more robust interpretation of corresponding max_threads value;
wenzelm
parents: 31311
diff changeset
    62
  let val ml_platform = getenv "ML_PLATFORM"
2f8ed0dca3bd back to default -M max, with more robust interpretation of corresponding max_threads value;
wenzelm
parents: 31311
diff changeset
    63
  in String.isSuffix "linux" ml_platform orelse String.isSuffix "darwin" ml_platform end;
2f8ed0dca3bd back to default -M max, with more robust interpretation of corresponding max_threads value;
wenzelm
parents: 31311
diff changeset
    64
25775
90525e67ede7 added Multithreading.max_threads_value, which maps a value of 0 to number of CPUs;
wenzelm
parents: 25735
diff changeset
    65
fun max_threads_value () =
31630
2f8ed0dca3bd back to default -M max, with more robust interpretation of corresponding max_threads value;
wenzelm
parents: 31311
diff changeset
    66
  let val m = ! max_threads in
2f8ed0dca3bd back to default -M max, with more robust interpretation of corresponding max_threads value;
wenzelm
parents: 31311
diff changeset
    67
    if m > 0 then m
2f8ed0dca3bd back to default -M max, with more robust interpretation of corresponding max_threads value;
wenzelm
parents: 31311
diff changeset
    68
    else if not tested_platform then 1
2f8ed0dca3bd back to default -M max, with more robust interpretation of corresponding max_threads value;
wenzelm
parents: 31311
diff changeset
    69
    else Int.min (Int.max (Thread.numProcessors (), 1), 8)
2f8ed0dca3bd back to default -M max, with more robust interpretation of corresponding max_threads value;
wenzelm
parents: 31311
diff changeset
    70
  end;
25775
90525e67ede7 added Multithreading.max_threads_value, which maps a value of 0 to number of CPUs;
wenzelm
parents: 25735
diff changeset
    71
28555
d59712ee942c added enabled;
wenzelm
parents: 28466
diff changeset
    72
fun enabled () = max_threads_value () > 1;
d59712ee942c added enabled;
wenzelm
parents: 28466
diff changeset
    73
23973
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
    74
24069
8a15a04e36f6 tuned msgs;
wenzelm
parents: 24066
diff changeset
    75
(* misc utils *)
8a15a04e36f6 tuned msgs;
wenzelm
parents: 24066
diff changeset
    76
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    77
fun show "" = "" | show name = " " ^ name;
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    78
fun show' "" = "" | show' name = " [" ^ name ^ "]";
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    79
26098
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
    80
fun read_file name =
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
    81
  let val is = TextIO.openIn name
26504
6e87c0a60104 before close: Exn.capture/release;
wenzelm
parents: 26493
diff changeset
    82
  in Exn.release (Exn.capture TextIO.inputAll is before TextIO.closeIn is) end;
26098
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
    83
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
    84
fun write_file name txt =
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
    85
  let val os = TextIO.openOut name
26504
6e87c0a60104 before close: Exn.capture/release;
wenzelm
parents: 26493
diff changeset
    86
  in Exn.release (Exn.capture TextIO.output (os, txt) before TextIO.closeOut os) end;
26098
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
    87
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    88
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    89
(* thread attributes *)
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    90
28161
7718587e510e added sync_interrupts, regular_interrupts;
wenzelm
parents: 28150
diff changeset
    91
val no_interrupts =
7718587e510e added sync_interrupts, regular_interrupts;
wenzelm
parents: 28150
diff changeset
    92
  [Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptDefer];
7718587e510e added sync_interrupts, regular_interrupts;
wenzelm
parents: 28150
diff changeset
    93
7718587e510e added sync_interrupts, regular_interrupts;
wenzelm
parents: 28150
diff changeset
    94
val regular_interrupts =
7718587e510e added sync_interrupts, regular_interrupts;
wenzelm
parents: 28150
diff changeset
    95
  [Thread.EnableBroadcastInterrupt true, Thread.InterruptState Thread.InterruptAsynchOnce];
7718587e510e added sync_interrupts, regular_interrupts;
wenzelm
parents: 28150
diff changeset
    96
30612
cb6421b6a18f future_job: do not inherit attributes, but enforce restricted interrupts -- attempt to prevent interrupt race conditions;
wenzelm
parents: 30602
diff changeset
    97
val restricted_interrupts =
cb6421b6a18f future_job: do not inherit attributes, but enforce restricted interrupts -- attempt to prevent interrupt race conditions;
wenzelm
parents: 30602
diff changeset
    98
  [Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptAsynchOnce];
cb6421b6a18f future_job: do not inherit attributes, but enforce restricted interrupts -- attempt to prevent interrupt race conditions;
wenzelm
parents: 30602
diff changeset
    99
28466
6e35fbfc32b8 with_attributes: enforces InterruptAsynch => InterruptAsynchOnce to avoid race condition;
wenzelm
parents: 28465
diff changeset
   100
val safe_interrupts = map
6e35fbfc32b8 with_attributes: enforces InterruptAsynch => InterruptAsynchOnce to avoid race condition;
wenzelm
parents: 28465
diff changeset
   101
  (fn Thread.InterruptState Thread.InterruptAsynch =>
6e35fbfc32b8 with_attributes: enforces InterruptAsynch => InterruptAsynchOnce to avoid race condition;
wenzelm
parents: 28465
diff changeset
   102
      Thread.InterruptState Thread.InterruptAsynchOnce
6e35fbfc32b8 with_attributes: enforces InterruptAsynch => InterruptAsynchOnce to avoid race condition;
wenzelm
parents: 28465
diff changeset
   103
    | x => x);
6e35fbfc32b8 with_attributes: enforces InterruptAsynch => InterruptAsynchOnce to avoid race condition;
wenzelm
parents: 28465
diff changeset
   104
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   105
fun with_attributes new_atts f x =
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   106
  let
29550
67ec51c032cb with_attributes: make double sure that unsafe attributes are avoided;
wenzelm
parents: 28555
diff changeset
   107
    val orig_atts = safe_interrupts (Thread.getAttributes ());
30602
1bd90b76477a with_attributes: canonical capture/release scheme (potentially iron out race condition);
wenzelm
parents: 29564
diff changeset
   108
    val result = Exn.capture (fn () =>
1bd90b76477a with_attributes: canonical capture/release scheme (potentially iron out race condition);
wenzelm
parents: 29564
diff changeset
   109
      (Thread.setAttributes (safe_interrupts new_atts); f orig_atts x)) ();
1bd90b76477a with_attributes: canonical capture/release scheme (potentially iron out race condition);
wenzelm
parents: 29564
diff changeset
   110
    val _ = Thread.setAttributes orig_atts;
28466
6e35fbfc32b8 with_attributes: enforces InterruptAsynch => InterruptAsynchOnce to avoid race condition;
wenzelm
parents: 28465
diff changeset
   111
  in Exn.release result end;
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   112
32230
9f6461b1c9cc interruptible: Thread.testInterrupt before changing thread attributes;
wenzelm
parents: 32186
diff changeset
   113
fun interruptible f x =
9f6461b1c9cc interruptible: Thread.testInterrupt before changing thread attributes;
wenzelm
parents: 32186
diff changeset
   114
  (Thread.testInterrupt (); with_attributes regular_interrupts (fn _ => fn x => f x) x);
28150
cbc2cbfc840c added no_interrupts;
wenzelm
parents: 28124
diff changeset
   115
26083
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   116
fun uninterruptible f =
32107
47d0da617fcc future_job: tight scope for interrupts, to prevent shooting ourselves in the foot via cancel_group;
wenzelm
parents: 31897
diff changeset
   117
  with_attributes no_interrupts (fn atts => fn x =>
47d0da617fcc future_job: tight scope for interrupts, to prevent shooting ourselves in the foot via cancel_group;
wenzelm
parents: 31897
diff changeset
   118
    f (fn g => with_attributes atts (fn _ => fn y => g y)) x);
24668
4058b7b0925c added interrupt_timeout;
wenzelm
parents: 24297
diff changeset
   119
24688
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
   120
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
   121
(* execution with time limit *)
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
   122
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
   123
structure TimeLimit =
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
   124
struct
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
   125
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
   126
exception TimeOut;
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
   127
26083
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   128
fun timeLimit time f x = uninterruptible (fn restore_attributes => fn () =>
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   129
  let
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   130
    val worker = Thread.self ();
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   131
    val timeout = ref false;
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   132
    val watchdog = Thread.fork (fn () =>
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   133
      (OS.Process.sleep time; timeout := true; Thread.interrupt worker), []);
24688
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
   134
26083
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   135
    val result = Exn.capture (restore_attributes f) x;
28443
de653f1ad78b more robust treatment of Interrupt (cf. exn.ML);
wenzelm
parents: 28398
diff changeset
   136
    val was_timeout = (case result of Exn.Exn Exn.Interrupt => ! timeout | _ => false);
24688
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
   137
26083
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   138
    val _ = Thread.interrupt watchdog handle Thread _ => ();
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   139
  in if was_timeout then raise TimeOut else Exn.release result end) ();
24688
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
   140
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
   141
end;
24668
4058b7b0925c added interrupt_timeout;
wenzelm
parents: 24297
diff changeset
   142
24069
8a15a04e36f6 tuned msgs;
wenzelm
parents: 24066
diff changeset
   143
26098
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   144
(* system shell processes, with propagation of interrupts *)
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   145
28254
d67ba23e0277 multithreading for Poly/ML 5.1 is no longer supported;
wenzelm
parents: 28169
diff changeset
   146
fun system_out script = uninterruptible (fn restore_attributes => fn () =>
26098
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   147
  let
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   148
    val script_name = OS.FileSys.tmpName ();
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   149
    val _ = write_file script_name script;
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   150
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   151
    val pid_name = OS.FileSys.tmpName ();
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   152
    val output_name = OS.FileSys.tmpName ();
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   153
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   154
    (*result state*)
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   155
    datatype result = Wait | Signal | Result of int;
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   156
    val result = ref Wait;
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   157
    val result_mutex = Mutex.mutex ();
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   158
    val result_cond = ConditionVar.conditionVar ();
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   159
    fun set_result res =
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   160
      (Mutex.lock result_mutex; result := res; Mutex.unlock result_mutex;
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   161
        ConditionVar.signal result_cond);
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   162
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   163
    val _ = Mutex.lock result_mutex;
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   164
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   165
    (*system thread*)
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   166
    val system_thread = Thread.fork (fn () =>
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   167
      let
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   168
        val status =
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   169
          OS.Process.system ("perl -w \"$ISABELLE_HOME/lib/scripts/system.pl\" group " ^
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   170
            script_name ^ " " ^ pid_name ^ " " ^ output_name);
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   171
        val res =
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   172
          (case Posix.Process.fromStatus status of
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   173
            Posix.Process.W_EXITED => Result 0
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   174
          | Posix.Process.W_EXITSTATUS 0wx82 => Signal
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   175
          | Posix.Process.W_EXITSTATUS w => Result (Word8.toInt w)
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   176
          | Posix.Process.W_SIGNALED s =>
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   177
              if s = Posix.Signal.int then Signal
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   178
              else Result (256 + LargeWord.toInt (Posix.Signal.toWord s))
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   179
          | Posix.Process.W_STOPPED s => Result (512 + LargeWord.toInt (Posix.Signal.toWord s)));
28398
9aa3216e5f31 tuned comments;
wenzelm
parents: 28254
diff changeset
   180
      in set_result res end handle _ (*sic*) => set_result (Result 2), []);
26098
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   181
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   182
    (*main thread -- proxy for interrupts*)
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   183
    fun kill n =
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   184
      (case Int.fromString (read_file pid_name) of
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   185
        SOME pid =>
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   186
          Posix.Process.kill
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   187
            (Posix.Process.K_GROUP (Posix.Process.wordToPid (LargeWord.fromInt pid)),
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   188
              Posix.Signal.int)
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   189
      | NONE => ())
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   190
      handle OS.SysErr _ => () | IO.Io _ =>
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   191
        (OS.Process.sleep (Time.fromMilliseconds 100); if n > 0 then kill (n - 1) else ());
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   192
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   193
    val _ = while ! result = Wait do
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   194
      restore_attributes (fn () =>
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   195
        (ConditionVar.waitUntil (result_cond, result_mutex, Time.now () + Time.fromMilliseconds 100); ())
28443
de653f1ad78b more robust treatment of Interrupt (cf. exn.ML);
wenzelm
parents: 28398
diff changeset
   196
          handle Exn.Interrupt => kill 10) ();
26098
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   197
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   198
    (*cleanup*)
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   199
    val output = read_file output_name handle IO.Io _ => "";
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   200
    val _ = OS.FileSys.remove script_name handle OS.SysErr _ => ();
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   201
    val _ = OS.FileSys.remove pid_name handle OS.SysErr _ => ();
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   202
    val _ = OS.FileSys.remove output_name handle OS.SysErr _ => ();
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   203
    val _ = Thread.interrupt system_thread handle Thread _ => ();
28443
de653f1ad78b more robust treatment of Interrupt (cf. exn.ML);
wenzelm
parents: 28398
diff changeset
   204
    val rc = (case ! result of Signal => raise Exn.Interrupt | Result rc => rc);
26098
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   205
  in (output, rc) end) ();
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   206
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   207
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   208
(* critical section -- may be nested within the same thread *)
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   209
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   210
local
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   211
24063
736c03ae92f5 more informative tracing;
wenzelm
parents: 24060
diff changeset
   212
val critical_lock = Mutex.mutex ();
736c03ae92f5 more informative tracing;
wenzelm
parents: 24060
diff changeset
   213
val critical_thread = ref (NONE: Thread.thread option);
736c03ae92f5 more informative tracing;
wenzelm
parents: 24060
diff changeset
   214
val critical_name = ref "";
736c03ae92f5 more informative tracing;
wenzelm
parents: 24060
diff changeset
   215
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   216
in
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   217
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   218
fun self_critical () =
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   219
  (case ! critical_thread of
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   220
    NONE => false
28150
cbc2cbfc840c added no_interrupts;
wenzelm
parents: 28124
diff changeset
   221
  | SOME t => Thread.equal (t, Thread.self ()));
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   222
23991
d4417ba26706 renamed CRITICAL' to NAMED_CRITICAL;
wenzelm
parents: 23981
diff changeset
   223
fun NAMED_CRITICAL name e =
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   224
  if self_critical () then e ()
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   225
  else
32184
cfa0ef0c0c5f simplified/unified Multithreading.tracing_time;
wenzelm
parents: 32107
diff changeset
   226
    Exn.release (uninterruptible (fn restore_attributes => fn () =>
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   227
      let
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   228
        val name' = ! critical_name;
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   229
        val _ =
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   230
          if Mutex.trylock critical_lock then ()
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   231
          else
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   232
            let
32184
cfa0ef0c0c5f simplified/unified Multithreading.tracing_time;
wenzelm
parents: 32107
diff changeset
   233
              val _ = tracing 5 (fn () => "CRITICAL" ^ show name ^ show' name' ^ ": waiting");
32185
57ecfab3bcfe added Multithreading.real_time;
wenzelm
parents: 32184
diff changeset
   234
              val time = real_time Mutex.lock critical_lock;
32186
8026b73cd357 tuned tracing;
wenzelm
parents: 32185
diff changeset
   235
              val _ = tracing_time true time (fn () =>
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   236
                "CRITICAL" ^ show name ^ show' name' ^ ": passed after " ^ Time.toString time);
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   237
            in () end;
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   238
        val _ = critical_thread := SOME (Thread.self ());
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   239
        val _ = critical_name := name;
26083
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   240
        val result = Exn.capture (restore_attributes e) ();
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   241
        val _ = critical_name := "";
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   242
        val _ = critical_thread := NONE;
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   243
        val _ = Mutex.unlock critical_lock;
32184
cfa0ef0c0c5f simplified/unified Multithreading.tracing_time;
wenzelm
parents: 32107
diff changeset
   244
      in result end) ());
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   245
23991
d4417ba26706 renamed CRITICAL' to NAMED_CRITICAL;
wenzelm
parents: 23981
diff changeset
   246
fun CRITICAL e = NAMED_CRITICAL "" e;
23981
03b71bf91318 added trace flag, official tracing operation;
wenzelm
parents: 23973
diff changeset
   247
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   248
end;
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   249
23973
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
   250
25704
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   251
(* serial numbers *)
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   252
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   253
local
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   254
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   255
val serial_lock = Mutex.mutex ();
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   256
val serial_count = ref 0;
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   257
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   258
in
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   259
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   260
val serial = uninterruptible (fn _ => fn () =>
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   261
  let
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   262
    val _ = Mutex.lock serial_lock;
28124
10a1f1f4c6ae moved Multithreading.task/schedule to Concurrent/schedule.ML;
wenzelm
parents: 26504
diff changeset
   263
    val _ = serial_count := ! serial_count + 1;
10a1f1f4c6ae moved Multithreading.task/schedule to Concurrent/schedule.ML;
wenzelm
parents: 26504
diff changeset
   264
    val res = ! serial_count;
25704
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   265
    val _ = Mutex.unlock serial_lock;
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   266
  in res end);
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   267
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   268
end;
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   269
25704
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   270
end;
24688
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
   271
25704
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   272
structure BasicMultithreading: BASIC_MULTITHREADING = Multithreading;
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   273
open BasicMultithreading;