src/Pure/ML-Systems/multithreading_polyml.ML
author haftmann
Fri, 25 Sep 2009 09:50:31 +0200
changeset 32705 04ce6bb14d85
parent 32295 400cc493d466
child 32738 15bb09ca0378
permissions -rw-r--r--
merged
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
(* options *)
32184
cfa0ef0c0c5f simplified/unified Multithreading.tracing_time;
wenzelm
parents: 32107
diff changeset
    31
23981
03b71bf91318 added trace flag, official tracing operation;
wenzelm
parents: 23973
diff changeset
    32
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
    33
31897
15d55d07de8b default to maximum max_threads;
wenzelm
parents: 31630
diff changeset
    34
val max_threads = ref 0;
23973
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
    35
31630
2f8ed0dca3bd back to default -M max, with more robust interpretation of corresponding max_threads value;
wenzelm
parents: 31311
diff changeset
    36
val tested_platform =
2f8ed0dca3bd back to default -M max, with more robust interpretation of corresponding max_threads value;
wenzelm
parents: 31311
diff changeset
    37
  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
    38
  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
    39
25775
90525e67ede7 added Multithreading.max_threads_value, which maps a value of 0 to number of CPUs;
wenzelm
parents: 25735
diff changeset
    40
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
    41
  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
    42
    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
    43
    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
    44
    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
    45
  end;
25775
90525e67ede7 added Multithreading.max_threads_value, which maps a value of 0 to number of CPUs;
wenzelm
parents: 25735
diff changeset
    46
28555
d59712ee942c added enabled;
wenzelm
parents: 28466
diff changeset
    47
fun enabled () = max_threads_value () > 1;
d59712ee942c added enabled;
wenzelm
parents: 28466
diff changeset
    48
23973
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
    49
24069
8a15a04e36f6 tuned msgs;
wenzelm
parents: 24066
diff changeset
    50
(* misc utils *)
8a15a04e36f6 tuned msgs;
wenzelm
parents: 24066
diff changeset
    51
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    52
fun show "" = "" | show name = " " ^ name;
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    53
fun show' "" = "" | show' name = " [" ^ name ^ "]";
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    54
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
    55
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
    56
  let val is = TextIO.openIn name
26504
6e87c0a60104 before close: Exn.capture/release;
wenzelm
parents: 26493
diff changeset
    57
  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
    58
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
    59
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
    60
  let val os = TextIO.openOut name
26504
6e87c0a60104 before close: Exn.capture/release;
wenzelm
parents: 26493
diff changeset
    61
  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
    62
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    63
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    64
(* thread attributes *)
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    65
28161
7718587e510e added sync_interrupts, regular_interrupts;
wenzelm
parents: 28150
diff changeset
    66
val no_interrupts =
7718587e510e added sync_interrupts, regular_interrupts;
wenzelm
parents: 28150
diff changeset
    67
  [Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptDefer];
7718587e510e added sync_interrupts, regular_interrupts;
wenzelm
parents: 28150
diff changeset
    68
32295
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
    69
val public_interrupts =
28161
7718587e510e added sync_interrupts, regular_interrupts;
wenzelm
parents: 28150
diff changeset
    70
  [Thread.EnableBroadcastInterrupt true, Thread.InterruptState Thread.InterruptAsynchOnce];
7718587e510e added sync_interrupts, regular_interrupts;
wenzelm
parents: 28150
diff changeset
    71
32295
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
    72
val private_interrupts =
30612
cb6421b6a18f future_job: do not inherit attributes, but enforce restricted interrupts -- attempt to prevent interrupt race conditions;
wenzelm
parents: 30602
diff changeset
    73
  [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
    74
32295
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
    75
val sync_interrupts = map
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
    76
  (fn x as Thread.InterruptState Thread.InterruptDefer => x
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
    77
    | Thread.InterruptState _ => Thread.InterruptState Thread.InterruptSynch
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
    78
    | x => x);
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
    79
28466
6e35fbfc32b8 with_attributes: enforces InterruptAsynch => InterruptAsynchOnce to avoid race condition;
wenzelm
parents: 28465
diff changeset
    80
val safe_interrupts = map
6e35fbfc32b8 with_attributes: enforces InterruptAsynch => InterruptAsynchOnce to avoid race condition;
wenzelm
parents: 28465
diff changeset
    81
  (fn Thread.InterruptState Thread.InterruptAsynch =>
6e35fbfc32b8 with_attributes: enforces InterruptAsynch => InterruptAsynchOnce to avoid race condition;
wenzelm
parents: 28465
diff changeset
    82
      Thread.InterruptState Thread.InterruptAsynchOnce
6e35fbfc32b8 with_attributes: enforces InterruptAsynch => InterruptAsynchOnce to avoid race condition;
wenzelm
parents: 28465
diff changeset
    83
    | x => x);
6e35fbfc32b8 with_attributes: enforces InterruptAsynch => InterruptAsynchOnce to avoid race condition;
wenzelm
parents: 28465
diff changeset
    84
32295
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
    85
fun with_attributes new_atts e =
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    86
  let
29550
67ec51c032cb with_attributes: make double sure that unsafe attributes are avoided;
wenzelm
parents: 28555
diff changeset
    87
    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
    88
    val result = Exn.capture (fn () =>
32295
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
    89
      (Thread.setAttributes (safe_interrupts new_atts); e orig_atts)) ();
30602
1bd90b76477a with_attributes: canonical capture/release scheme (potentially iron out race condition);
wenzelm
parents: 29564
diff changeset
    90
    val _ = Thread.setAttributes orig_atts;
28466
6e35fbfc32b8 with_attributes: enforces InterruptAsynch => InterruptAsynchOnce to avoid race condition;
wenzelm
parents: 28465
diff changeset
    91
  in Exn.release result end;
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    92
32286
1fb5db48002d added Multithreading.sync_wait, which turns enabled interrupts to sync ones, to ensure that wait will reaquire its lock when interrupted;
wenzelm
parents: 32230
diff changeset
    93
32295
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
    94
(* portable wrappers *)
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
    95
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
    96
fun interruptible f x = with_attributes public_interrupts (fn _ => f x);
32286
1fb5db48002d added Multithreading.sync_wait, which turns enabled interrupts to sync ones, to ensure that wait will reaquire its lock when interrupted;
wenzelm
parents: 32230
diff changeset
    97
32295
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
    98
fun uninterruptible f x =
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
    99
  with_attributes no_interrupts (fn atts =>
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   100
    f (fn g => fn y => with_attributes atts (fn _ => g y)) x);
24668
4058b7b0925c added interrupt_timeout;
wenzelm
parents: 24297
diff changeset
   101
24688
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
   102
32286
1fb5db48002d added Multithreading.sync_wait, which turns enabled interrupts to sync ones, to ensure that wait will reaquire its lock when interrupted;
wenzelm
parents: 32230
diff changeset
   103
(* synchronous wait *)
1fb5db48002d added Multithreading.sync_wait, which turns enabled interrupts to sync ones, to ensure that wait will reaquire its lock when interrupted;
wenzelm
parents: 32230
diff changeset
   104
32295
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   105
fun sync_wait opt_atts time cond lock =
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   106
  with_attributes
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   107
    (sync_interrupts (case opt_atts of SOME atts => atts | NONE => Thread.getAttributes ()))
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   108
    (fn _ =>
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   109
      (case time of
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   110
        SOME t => Exn.Result (ConditionVar.waitUntil (cond, lock, t))
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   111
      | NONE => (ConditionVar.wait (cond, lock); Exn.Result true))
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   112
      handle exn => Exn.Exn exn);
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   113
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   114
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   115
(* tracing *)
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   116
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   117
val trace = ref 0;
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   118
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   119
fun tracing level msg =
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   120
  if level > ! trace then ()
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   121
  else uninterruptible (fn _ => fn () =>
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   122
    (TextIO.output (TextIO.stdErr, (">>> " ^ msg () ^ "\n")); TextIO.flushOut TextIO.stdErr)
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   123
      handle _ (*sic*) => ()) ();
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   124
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   125
fun tracing_time detailed time =
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   126
  tracing
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   127
   (if not detailed then 5
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   128
    else if Time.>= (time, Time.fromMilliseconds 1000) then 1
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   129
    else if Time.>= (time, Time.fromMilliseconds 100) then 2
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   130
    else if Time.>= (time, Time.fromMilliseconds 10) then 3
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   131
    else if Time.>= (time, Time.fromMilliseconds 1) then 4 else 5);
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   132
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   133
fun real_time f x =
32286
1fb5db48002d added Multithreading.sync_wait, which turns enabled interrupts to sync ones, to ensure that wait will reaquire its lock when interrupted;
wenzelm
parents: 32230
diff changeset
   134
  let
32295
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   135
    val timer = Timer.startRealTimer ();
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   136
    val () = f x;
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   137
    val time = Timer.checkRealTimer timer;
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   138
  in time end;
32286
1fb5db48002d added Multithreading.sync_wait, which turns enabled interrupts to sync ones, to ensure that wait will reaquire its lock when interrupted;
wenzelm
parents: 32230
diff changeset
   139
1fb5db48002d added Multithreading.sync_wait, which turns enabled interrupts to sync ones, to ensure that wait will reaquire its lock when interrupted;
wenzelm
parents: 32230
diff changeset
   140
24688
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
   141
(* 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
   142
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
   143
structure TimeLimit =
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
   144
struct
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
   145
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
   146
exception TimeOut;
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
   147
26083
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   148
fun timeLimit time f x = uninterruptible (fn restore_attributes => fn () =>
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   149
  let
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   150
    val worker = Thread.self ();
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   151
    val timeout = ref false;
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   152
    val watchdog = Thread.fork (fn () =>
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   153
      (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
   154
26083
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   155
    val result = Exn.capture (restore_attributes f) x;
28443
de653f1ad78b more robust treatment of Interrupt (cf. exn.ML);
wenzelm
parents: 28398
diff changeset
   156
    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
   157
26083
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   158
    val _ = Thread.interrupt watchdog handle Thread _ => ();
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   159
  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
   160
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
   161
end;
24668
4058b7b0925c added interrupt_timeout;
wenzelm
parents: 24297
diff changeset
   162
24069
8a15a04e36f6 tuned msgs;
wenzelm
parents: 24066
diff changeset
   163
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
   164
(* 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
   165
32295
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   166
fun system_out script = with_attributes no_interrupts (fn orig_atts =>
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
   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 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
   169
    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
   170
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 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
   172
    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
   173
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
    (*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
   175
    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
   176
    val result = ref Wait;
32295
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   177
    val lock = Mutex.mutex ();
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   178
    val cond = ConditionVar.conditionVar ();
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
   179
    fun set_result res =
32295
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   180
      (Mutex.lock lock; result := res; ConditionVar.signal cond; Mutex.unlock lock);
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
32295
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   182
    val _ = Mutex.lock lock;
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
   183
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
    (*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
   185
    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
   186
      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
   187
        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
   188
          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
   189
            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
   190
        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
   191
          (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
   192
            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
   193
          | 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
   194
          | 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
   195
          | 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
   196
              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
   197
              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
   198
          | Posix.Process.W_STOPPED s => Result (512 + LargeWord.toInt (Posix.Signal.toWord s)));
28398
9aa3216e5f31 tuned comments;
wenzelm
parents: 28254
diff changeset
   199
      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
   200
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
    (*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
   202
    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
   203
      (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
   204
        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
   205
          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
   206
            (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
   207
              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
   208
      | 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
   209
      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
   210
        (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
   211
32295
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   212
    val _ =
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   213
      while ! result = Wait do
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   214
        let val res =
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   215
          sync_wait (SOME orig_atts)
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   216
            (SOME (Time.+ (Time.now (), Time.fromMilliseconds 100))) cond lock
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   217
        in case res of Exn.Exn Exn.Interrupt => kill 10 | _ => () 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
   218
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   219
    (*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
   220
    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
   221
    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
   222
    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
   223
    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
   224
    val _ = Thread.interrupt system_thread handle Thread _ => ();
28443
de653f1ad78b more robust treatment of Interrupt (cf. exn.ML);
wenzelm
parents: 28398
diff changeset
   225
    val rc = (case ! result of Signal => raise Exn.Interrupt | Result rc => rc);
32295
400cc493d466 renamed Multithreading.regular_interrupts to Multithreading.public_interrupts;
wenzelm
parents: 32286
diff changeset
   226
  in (output, rc) 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
   227
b59d33f73aed added system_out (back to multithreaded version -- still suffers from non-interruptible wait in Poly/ML 5.1);
wenzelm
parents: 26083
diff changeset
   228
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   229
(* critical section -- may be nested within the same thread *)
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   230
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   231
local
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   232
24063
736c03ae92f5 more informative tracing;
wenzelm
parents: 24060
diff changeset
   233
val critical_lock = Mutex.mutex ();
736c03ae92f5 more informative tracing;
wenzelm
parents: 24060
diff changeset
   234
val critical_thread = ref (NONE: Thread.thread option);
736c03ae92f5 more informative tracing;
wenzelm
parents: 24060
diff changeset
   235
val critical_name = ref "";
736c03ae92f5 more informative tracing;
wenzelm
parents: 24060
diff changeset
   236
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   237
in
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   238
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   239
fun self_critical () =
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   240
  (case ! critical_thread of
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   241
    NONE => false
28150
cbc2cbfc840c added no_interrupts;
wenzelm
parents: 28124
diff changeset
   242
  | SOME t => Thread.equal (t, Thread.self ()));
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   243
23991
d4417ba26706 renamed CRITICAL' to NAMED_CRITICAL;
wenzelm
parents: 23981
diff changeset
   244
fun NAMED_CRITICAL name e =
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   245
  if self_critical () then e ()
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   246
  else
32184
cfa0ef0c0c5f simplified/unified Multithreading.tracing_time;
wenzelm
parents: 32107
diff changeset
   247
    Exn.release (uninterruptible (fn restore_attributes => fn () =>
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   248
      let
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   249
        val name' = ! critical_name;
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   250
        val _ =
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   251
          if Mutex.trylock critical_lock then ()
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   252
          else
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   253
            let
32184
cfa0ef0c0c5f simplified/unified Multithreading.tracing_time;
wenzelm
parents: 32107
diff changeset
   254
              val _ = tracing 5 (fn () => "CRITICAL" ^ show name ^ show' name' ^ ": waiting");
32185
57ecfab3bcfe added Multithreading.real_time;
wenzelm
parents: 32184
diff changeset
   255
              val time = real_time Mutex.lock critical_lock;
32186
8026b73cd357 tuned tracing;
wenzelm
parents: 32185
diff changeset
   256
              val _ = tracing_time true time (fn () =>
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   257
                "CRITICAL" ^ show name ^ show' name' ^ ": passed after " ^ Time.toString time);
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   258
            in () end;
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   259
        val _ = critical_thread := SOME (Thread.self ());
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   260
        val _ = critical_name := name;
26083
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   261
        val result = Exn.capture (restore_attributes e) ();
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   262
        val _ = critical_name := "";
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   263
        val _ = critical_thread := NONE;
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   264
        val _ = Mutex.unlock critical_lock;
32184
cfa0ef0c0c5f simplified/unified Multithreading.tracing_time;
wenzelm
parents: 32107
diff changeset
   265
      in result end) ());
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   266
23991
d4417ba26706 renamed CRITICAL' to NAMED_CRITICAL;
wenzelm
parents: 23981
diff changeset
   267
fun CRITICAL e = NAMED_CRITICAL "" e;
23981
03b71bf91318 added trace flag, official tracing operation;
wenzelm
parents: 23973
diff changeset
   268
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   269
end;
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   270
23973
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
   271
25704
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   272
(* serial numbers *)
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   273
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   274
local
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   275
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   276
val serial_lock = Mutex.mutex ();
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   277
val serial_count = ref 0;
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   278
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   279
in
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   280
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   281
val serial = uninterruptible (fn _ => fn () =>
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   282
  let
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   283
    val _ = Mutex.lock serial_lock;
28124
10a1f1f4c6ae moved Multithreading.task/schedule to Concurrent/schedule.ML;
wenzelm
parents: 26504
diff changeset
   284
    val _ = serial_count := ! serial_count + 1;
10a1f1f4c6ae moved Multithreading.task/schedule to Concurrent/schedule.ML;
wenzelm
parents: 26504
diff changeset
   285
    val res = ! serial_count;
25704
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   286
    val _ = Mutex.unlock serial_lock;
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   287
  in res end);
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   288
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   289
end;
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   290
25704
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   291
end;
24688
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
   292
32286
1fb5db48002d added Multithreading.sync_wait, which turns enabled interrupts to sync ones, to ensure that wait will reaquire its lock when interrupted;
wenzelm
parents: 32230
diff changeset
   293
structure Basic_Multithreading: BASIC_MULTITHREADING = Multithreading;
1fb5db48002d added Multithreading.sync_wait, which turns enabled interrupts to sync ones, to ensure that wait will reaquire its lock when interrupted;
wenzelm
parents: 32230
diff changeset
   294
open Basic_Multithreading;