src/Pure/ML-Systems/multithreading_polyml.ML
author wenzelm
Sat, 16 Feb 2008 16:44:02 +0100
changeset 26083 abb3f8dd66dc
parent 26074 44c5419cd9f1
child 26098 b59d33f73aed
permissions -rw-r--r--
removed managed_process (cf. General/shell_process.ML); replaced ignore/raise_interrupt by more flexible (un)interruptible combinators; tuned timeLimit: sleep already interruptible by default; schedule: restore attributes of body, instead of forcing interruptible execution;
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
    ID:         $Id$
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
     4
25704
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
     5
Multithreading in Poly/ML 5.1 (cf. polyml/basis/Thread.sml).
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
     6
*)
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
     7
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
     8
open Thread;
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
     9
25704
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
    10
signature MULTITHREADING_POLYML =
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
    11
sig
26083
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
    12
  val interruptible: ('a -> 'b) -> 'a -> 'b
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
    13
  val uninterruptible: ((('c -> 'd) -> 'c -> 'd) -> 'a -> 'b) -> 'a -> 'b
25704
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
    14
  structure TimeLimit: TIME_LIMIT
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
    15
end;
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
    16
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
    17
signature BASIC_MULTITHREADING =
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
    18
sig
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
    19
  include BASIC_MULTITHREADING
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
    20
  include MULTITHREADING_POLYML
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
    21
end;
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
    22
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    23
signature MULTITHREADING =
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    24
sig
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    25
  include MULTITHREADING
25704
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
    26
  include MULTITHREADING_POLYML
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    27
end;
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    28
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
    29
structure Multithreading: MULTITHREADING =
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
    30
struct
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
    31
24072
8b9e5d776ef3 dequeue: wait loop while PROTECTED -- avoids race condition;
wenzelm
parents: 24069
diff changeset
    32
(* options *)
24069
8a15a04e36f6 tuned msgs;
wenzelm
parents: 24066
diff changeset
    33
24119
06965b38c5e9 tracing: level;
wenzelm
parents: 24109
diff changeset
    34
val trace = ref 0;
06965b38c5e9 tracing: level;
wenzelm
parents: 24109
diff changeset
    35
fun tracing level msg =
06965b38c5e9 tracing: level;
wenzelm
parents: 24109
diff changeset
    36
  if level <= ! trace
23981
03b71bf91318 added trace flag, official tracing operation;
wenzelm
parents: 23973
diff changeset
    37
  then (TextIO.output (TextIO.stdErr, (">>> " ^ msg () ^ "\n")); TextIO.flushOut TextIO.stdErr)
03b71bf91318 added trace flag, official tracing operation;
wenzelm
parents: 23973
diff changeset
    38
  else ();
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
    39
23981
03b71bf91318 added trace flag, official tracing operation;
wenzelm
parents: 23973
diff changeset
    40
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
    41
23973
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
    42
val max_threads = ref 1;
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
    43
25775
90525e67ede7 added Multithreading.max_threads_value, which maps a value of 0 to number of CPUs;
wenzelm
parents: 25735
diff changeset
    44
fun max_threads_value () =
90525e67ede7 added Multithreading.max_threads_value, which maps a value of 0 to number of CPUs;
wenzelm
parents: 25735
diff changeset
    45
  let val m = ! max_threads
90525e67ede7 added Multithreading.max_threads_value, which maps a value of 0 to number of CPUs;
wenzelm
parents: 25735
diff changeset
    46
  in if m <= 0 then Thread.numProcessors () else m end;
90525e67ede7 added Multithreading.max_threads_value, which maps a value of 0 to number of CPUs;
wenzelm
parents: 25735
diff changeset
    47
23973
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
    48
24069
8a15a04e36f6 tuned msgs;
wenzelm
parents: 24066
diff changeset
    49
(* misc utils *)
8a15a04e36f6 tuned msgs;
wenzelm
parents: 24066
diff changeset
    50
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    51
fun cons x xs = x :: xs;
24069
8a15a04e36f6 tuned msgs;
wenzelm
parents: 24066
diff changeset
    52
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    53
fun change r f = r := f (! r);
24069
8a15a04e36f6 tuned msgs;
wenzelm
parents: 24066
diff changeset
    54
8a15a04e36f6 tuned msgs;
wenzelm
parents: 24066
diff changeset
    55
fun inc i = (i := ! i + 1; ! i);
8a15a04e36f6 tuned msgs;
wenzelm
parents: 24066
diff changeset
    56
fun dec i = (i := ! i - 1; ! i);
8a15a04e36f6 tuned msgs;
wenzelm
parents: 24066
diff changeset
    57
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    58
fun show "" = "" | show name = " " ^ name;
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    59
fun show' "" = "" | show' name = " [" ^ name ^ "]";
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    60
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    61
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    62
(* thread attributes *)
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    63
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    64
fun with_attributes new_atts f x =
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    65
  let
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    66
    val orig_atts = Thread.getAttributes ();
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    67
    fun restore () = Thread.setAttributes orig_atts;
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    68
  in
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    69
    Exn.release
24214
0482ecc4ef11 (un)interruptible: pass-through original thread attributes;
wenzelm
parents: 24208
diff changeset
    70
    (*RACE for fully asynchronous interrupts!*)
0482ecc4ef11 (un)interruptible: pass-through original thread attributes;
wenzelm
parents: 24208
diff changeset
    71
    (let
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    72
        val _ = Thread.setAttributes new_atts;
24214
0482ecc4ef11 (un)interruptible: pass-through original thread attributes;
wenzelm
parents: 24208
diff changeset
    73
        val result = Exn.capture (f orig_atts) x;
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    74
        val _ = restore ();
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    75
      in result end
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    76
      handle Interrupt => (restore (); Exn.Exn Interrupt))
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    77
  end;
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    78
26083
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
    79
fun interruptible f =
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
    80
  with_attributes
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
    81
    [Thread.EnableBroadcastInterrupt true, Thread.InterruptState Thread.InterruptAsynchOnce]
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
    82
    (fn _ => f);
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
    83
26083
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
    84
fun uninterruptible f =
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
    85
  with_attributes
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
    86
    [Thread.EnableBroadcastInterrupt false, Thread.InterruptState Thread.InterruptDefer]
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
    87
    (fn atts => f (fn g => with_attributes atts (fn _ => g)));
24668
4058b7b0925c added interrupt_timeout;
wenzelm
parents: 24297
diff changeset
    88
24688
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
    89
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
    90
(* 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
    91
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
    92
structure TimeLimit =
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
    93
struct
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
    94
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
    95
exception TimeOut;
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
    96
26083
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
    97
fun timeLimit time f x = uninterruptible (fn restore_attributes => fn () =>
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
    98
  let
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
    99
    val worker = Thread.self ();
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   100
    val timeout = ref false;
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   101
    val watchdog = Thread.fork (fn () =>
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   102
      (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
   103
26083
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   104
    (*RACE! timeout signal vs. external Interrupt*)
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   105
    val result = Exn.capture (restore_attributes f) x;
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   106
    val was_timeout = (case result of 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
   107
26083
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   108
    val _ = Thread.interrupt watchdog handle Thread _ => ();
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   109
  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
   110
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
   111
end;
24668
4058b7b0925c added interrupt_timeout;
wenzelm
parents: 24297
diff changeset
   112
24069
8a15a04e36f6 tuned msgs;
wenzelm
parents: 24066
diff changeset
   113
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   114
(* critical section -- may be nested within the same thread *)
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   115
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   116
local
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   117
24063
736c03ae92f5 more informative tracing;
wenzelm
parents: 24060
diff changeset
   118
val critical_lock = Mutex.mutex ();
736c03ae92f5 more informative tracing;
wenzelm
parents: 24060
diff changeset
   119
val critical_thread = ref (NONE: Thread.thread option);
736c03ae92f5 more informative tracing;
wenzelm
parents: 24060
diff changeset
   120
val critical_name = ref "";
736c03ae92f5 more informative tracing;
wenzelm
parents: 24060
diff changeset
   121
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   122
in
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   123
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   124
fun self_critical () =
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   125
  (case ! critical_thread of
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   126
    NONE => false
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   127
  | SOME id => Thread.equal (id, Thread.self ()));
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   128
23991
d4417ba26706 renamed CRITICAL' to NAMED_CRITICAL;
wenzelm
parents: 23981
diff changeset
   129
fun NAMED_CRITICAL name e =
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   130
  if self_critical () then e ()
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   131
  else
26083
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   132
    uninterruptible (fn restore_attributes => fn () =>
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   133
      let
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   134
        val name' = ! critical_name;
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   135
        val _ =
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   136
          if Mutex.trylock critical_lock then ()
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   137
          else
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   138
            let
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   139
              val timer = Timer.startRealTimer ();
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   140
              val _ = tracing 4 (fn () => "CRITICAL" ^ show name ^ show' name' ^ ": waiting");
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   141
              val _ = Mutex.lock critical_lock;
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   142
              val time = Timer.checkRealTimer timer;
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   143
              val _ = tracing (if Time.> (time, Time.fromMilliseconds 10) then 3 else 4) (fn () =>
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   144
                "CRITICAL" ^ show name ^ show' name' ^ ": passed after " ^ Time.toString time);
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   145
            in () end;
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   146
        val _ = critical_thread := SOME (Thread.self ());
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   147
        val _ = critical_name := name;
26083
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   148
        val result = Exn.capture (restore_attributes e) ();
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   149
        val _ = critical_name := "";
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   150
        val _ = critical_thread := NONE;
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   151
        val _ = Mutex.unlock critical_lock;
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   152
      in Exn.release result end) ();
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   153
23991
d4417ba26706 renamed CRITICAL' to NAMED_CRITICAL;
wenzelm
parents: 23981
diff changeset
   154
fun CRITICAL e = NAMED_CRITICAL "" e;
23981
03b71bf91318 added trace flag, official tracing operation;
wenzelm
parents: 23973
diff changeset
   155
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   156
end;
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   157
23973
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
   158
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   159
(* scheduling -- multiple threads working on a queue of tasks *)
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   160
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   161
datatype 'a task =
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   162
  Task of {body: unit -> unit, cont: 'a -> 'a, fail: 'a -> 'a} | Wait | Terminate;
23973
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
   163
26083
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   164
fun schedule n next_task = uninterruptible (fn restore_attributes => fn tasks =>
23973
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
   165
  let
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
   166
    (*protected execution*)
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
   167
    val lock = Mutex.mutex ();
24672
wenzelm
parents: 24668
diff changeset
   168
    val protected_name = ref "";
24063
736c03ae92f5 more informative tracing;
wenzelm
parents: 24060
diff changeset
   169
    fun PROTECTED name e =
23973
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
   170
      let
24144
ec51a0f7eefe tuned tracing;
wenzelm
parents: 24119
diff changeset
   171
        val name' = ! protected_name;
23981
03b71bf91318 added trace flag, official tracing operation;
wenzelm
parents: 23973
diff changeset
   172
        val _ =
03b71bf91318 added trace flag, official tracing operation;
wenzelm
parents: 23973
diff changeset
   173
          if Mutex.trylock lock then ()
03b71bf91318 added trace flag, official tracing operation;
wenzelm
parents: 23973
diff changeset
   174
          else
24144
ec51a0f7eefe tuned tracing;
wenzelm
parents: 24119
diff changeset
   175
            let
ec51a0f7eefe tuned tracing;
wenzelm
parents: 24119
diff changeset
   176
              val _ = tracing 2 (fn () => "PROTECTED" ^ show name ^ show' name' ^ ": waiting");
ec51a0f7eefe tuned tracing;
wenzelm
parents: 24119
diff changeset
   177
              val _ = Mutex.lock lock;
ec51a0f7eefe tuned tracing;
wenzelm
parents: 24119
diff changeset
   178
              val _ = tracing 2 (fn () => "PROTECTED" ^ show name ^ show' name' ^ ": passed");
ec51a0f7eefe tuned tracing;
wenzelm
parents: 24119
diff changeset
   179
            in () end;
24069
8a15a04e36f6 tuned msgs;
wenzelm
parents: 24066
diff changeset
   180
        val _ = protected_name := name;
23973
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
   181
        val res = Exn.capture e ();
24069
8a15a04e36f6 tuned msgs;
wenzelm
parents: 24066
diff changeset
   182
        val _ = protected_name := "";
23973
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
   183
        val _ = Mutex.unlock lock;
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
   184
      in Exn.release res end;
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
   185
24072
8b9e5d776ef3 dequeue: wait loop while PROTECTED -- avoids race condition;
wenzelm
parents: 24069
diff changeset
   186
    (*wakeup condition*)
8b9e5d776ef3 dequeue: wait loop while PROTECTED -- avoids race condition;
wenzelm
parents: 24069
diff changeset
   187
    val wakeup = ConditionVar.conditionVar ();
8b9e5d776ef3 dequeue: wait loop while PROTECTED -- avoids race condition;
wenzelm
parents: 24069
diff changeset
   188
    fun wakeup_all () = ConditionVar.broadcast wakeup;
8b9e5d776ef3 dequeue: wait loop while PROTECTED -- avoids race condition;
wenzelm
parents: 24069
diff changeset
   189
    fun wait () = ConditionVar.wait (wakeup, lock);
24291
fa72aab966dc main: wait_timeout (1 second);
wenzelm
parents: 24214
diff changeset
   190
    fun wait_timeout () = ConditionVar.waitUntil (wakeup, lock, Time.now () + Time.fromSeconds 1);
24072
8b9e5d776ef3 dequeue: wait loop while PROTECTED -- avoids race condition;
wenzelm
parents: 24069
diff changeset
   191
24214
0482ecc4ef11 (un)interruptible: pass-through original thread attributes;
wenzelm
parents: 24208
diff changeset
   192
    (*queue of tasks*)
23973
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
   193
    val queue = ref tasks;
24072
8b9e5d776ef3 dequeue: wait loop while PROTECTED -- avoids race condition;
wenzelm
parents: 24069
diff changeset
   194
    val active = ref 0;
24119
06965b38c5e9 tracing: level;
wenzelm
parents: 24109
diff changeset
   195
    fun trace_active () = tracing 1 (fn () => "SCHEDULE: " ^ Int.toString (! active) ^ " active");
24072
8b9e5d776ef3 dequeue: wait loop while PROTECTED -- avoids race condition;
wenzelm
parents: 24069
diff changeset
   196
    fun dequeue () =
23973
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
   197
      let
23981
03b71bf91318 added trace flag, official tracing operation;
wenzelm
parents: 23973
diff changeset
   198
        val (next, tasks') = next_task (! queue);
23973
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
   199
        val _ = queue := tasks';
24072
8b9e5d776ef3 dequeue: wait loop while PROTECTED -- avoids race condition;
wenzelm
parents: 24069
diff changeset
   200
      in
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   201
        (case next of Wait =>
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   202
          (dec active; trace_active ();
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   203
            wait ();
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   204
            inc active; trace_active ();
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   205
            dequeue ())
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   206
        | _ => next)
24072
8b9e5d776ef3 dequeue: wait loop while PROTECTED -- avoids race condition;
wenzelm
parents: 24069
diff changeset
   207
      end;
23973
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
   208
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   209
    (*pool of running threads*)
23973
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
   210
    val status = ref ([]: exn list);
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   211
    val running = ref ([]: Thread.thread list);
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   212
    fun start f =
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   213
      (inc active;
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   214
       change running (cons (Thread.fork (f, [Thread.InterruptState Thread.InterruptDefer]))));
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   215
    fun stop () =
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   216
      (dec active;
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   217
       change running (List.filter (fn t => not (Thread.equal (t, Thread.self ())))));
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   218
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   219
   (*worker thread*)
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   220
    fun worker () =
24072
8b9e5d776ef3 dequeue: wait loop while PROTECTED -- avoids race condition;
wenzelm
parents: 24069
diff changeset
   221
      (case PROTECTED "dequeue" dequeue of
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   222
        Task {body, cont, fail} =>
26083
abb3f8dd66dc removed managed_process (cf. General/shell_process.ML);
wenzelm
parents: 26074
diff changeset
   223
          (case Exn.capture (restore_attributes body) () of
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   224
            Exn.Result () =>
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   225
              (PROTECTED "cont" (fn () => (change queue cont; wakeup_all ())); worker ())
23981
03b71bf91318 added trace flag, official tracing operation;
wenzelm
parents: 23973
diff changeset
   226
          | Exn.Exn exn =>
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   227
              PROTECTED "fail" (fn () =>
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   228
                (change status (cons exn); change queue fail; stop (); wakeup_all ())))
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   229
      | Terminate => PROTECTED "terminate" (fn () => (stop (); wakeup_all ())));
23973
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
   230
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
   231
    (*main control: fork and wait*)
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
   232
    fun fork 0 = ()
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   233
      | fork k = (start worker; fork (k - 1));
24063
736c03ae92f5 more informative tracing;
wenzelm
parents: 24060
diff changeset
   234
    val _ = PROTECTED "main" (fn () =>
736c03ae92f5 more informative tracing;
wenzelm
parents: 24060
diff changeset
   235
     (fork (Int.max (n, 1));
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   236
      while not (List.null (! running)) do
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   237
      (trace_active ();
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   238
       if not (List.null (! status)) then (List.app Thread.interrupt (! running)) else ();
24291
fa72aab966dc main: wait_timeout (1 second);
wenzelm
parents: 24214
diff changeset
   239
       wait_timeout ())));
23973
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
   240
24208
f4cafbaa05e4 schedule: more precise task model;
wenzelm
parents: 24144
diff changeset
   241
  in ! status end);
23973
b6ce6de5b700 renamed number_of_threads to max_threads;
wenzelm
parents: 23961
diff changeset
   242
25704
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   243
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   244
(* serial numbers *)
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   245
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   246
local
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   247
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   248
val serial_lock = Mutex.mutex ();
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   249
val serial_count = ref 0;
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   250
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   251
in
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   252
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   253
val serial = uninterruptible (fn _ => fn () =>
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   254
  let
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   255
    val _ = Mutex.lock serial_lock;
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   256
    val res = inc serial_count;
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   257
    val _ = Mutex.unlock serial_lock;
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   258
  in res end);
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   259
23961
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   260
end;
9e7e1e309ebd Multithreading in Poly/ML (version 5.1).
wenzelm
parents:
diff changeset
   261
25735
4d147263f71f added get/put_data;
wenzelm
parents: 25704
diff changeset
   262
4d147263f71f added get/put_data;
wenzelm
parents: 25704
diff changeset
   263
(* thread data *)
4d147263f71f added get/put_data;
wenzelm
parents: 25704
diff changeset
   264
4d147263f71f added get/put_data;
wenzelm
parents: 25704
diff changeset
   265
val get_data = Thread.getLocal;
4d147263f71f added get/put_data;
wenzelm
parents: 25704
diff changeset
   266
val put_data = Thread.setLocal;
4d147263f71f added get/put_data;
wenzelm
parents: 25704
diff changeset
   267
25704
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   268
end;
24688
a5754ca5c510 replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents: 24672
diff changeset
   269
25704
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   270
structure BasicMultithreading: BASIC_MULTITHREADING = Multithreading;
df9c8074ff09 signature BASIC_MULTITHREADING;
wenzelm
parents: 24688
diff changeset
   271
open BasicMultithreading;