src/Pure/ML-Systems/multithreading_polyml.ML
author wenzelm
Mon Jul 30 19:22:27 2007 +0200 (2007-07-30 ago)
changeset 24072 8b9e5d776ef3
parent 24069 8a15a04e36f6
child 24108 24e5587603b4
permissions -rw-r--r--
dequeue: wait loop while PROTECTED -- avoids race condition;
wenzelm@23961
     1
(*  Title:      Pure/ML-Systems/multithreading_polyml.ML
wenzelm@23961
     2
    ID:         $Id$
wenzelm@23961
     3
    Author:     Makarius
wenzelm@23961
     4
wenzelm@23961
     5
Multithreading in Poly/ML (version 5.1).
wenzelm@23961
     6
*)
wenzelm@23961
     7
wenzelm@23961
     8
open Thread;
wenzelm@23961
     9
wenzelm@23961
    10
structure Multithreading: MULTITHREADING =
wenzelm@23961
    11
struct
wenzelm@23961
    12
wenzelm@24072
    13
(* options *)
wenzelm@24069
    14
wenzelm@23981
    15
val trace = ref false;
wenzelm@23981
    16
fun tracing msg =
wenzelm@23981
    17
  if ! trace
wenzelm@23981
    18
  then (TextIO.output (TextIO.stdErr, (">>> " ^ msg () ^ "\n")); TextIO.flushOut TextIO.stdErr)
wenzelm@23981
    19
  else ();
wenzelm@23961
    20
wenzelm@23981
    21
val available = true;
wenzelm@23973
    22
val max_threads = ref 1;
wenzelm@23973
    23
wenzelm@23973
    24
wenzelm@24069
    25
(* misc utils *)
wenzelm@24069
    26
wenzelm@24069
    27
fun show "" = ""
wenzelm@24069
    28
  | show name = " " ^ name;
wenzelm@24069
    29
wenzelm@24069
    30
fun show' "" = ""
wenzelm@24069
    31
  | show' name = " [" ^ name ^ "]";
wenzelm@24069
    32
wenzelm@24069
    33
fun inc i = (i := ! i + 1; ! i);
wenzelm@24069
    34
fun dec i = (i := ! i - 1; ! i);
wenzelm@24069
    35
wenzelm@24069
    36
wenzelm@23961
    37
(* critical section -- may be nested within the same thread *)
wenzelm@23961
    38
wenzelm@23961
    39
local
wenzelm@23961
    40
wenzelm@24063
    41
val critical_lock = Mutex.mutex ();
wenzelm@24063
    42
val critical_thread = ref (NONE: Thread.thread option);
wenzelm@24063
    43
val critical_name = ref "";
wenzelm@24063
    44
wenzelm@23961
    45
in
wenzelm@23961
    46
wenzelm@23961
    47
fun self_critical () =
wenzelm@23961
    48
  (case ! critical_thread of
wenzelm@23961
    49
    NONE => false
wenzelm@23961
    50
  | SOME id => Thread.equal (id, Thread.self ()));
wenzelm@23961
    51
wenzelm@23991
    52
fun NAMED_CRITICAL name e =
wenzelm@23961
    53
  if self_critical () then e ()
wenzelm@23961
    54
  else
wenzelm@23961
    55
    let
wenzelm@23961
    56
      val _ =
wenzelm@23961
    57
        if Mutex.trylock critical_lock then ()
wenzelm@23961
    58
        else
wenzelm@24060
    59
          let
wenzelm@24060
    60
            val timer = Timer.startRealTimer ();
wenzelm@24060
    61
            val _ = tracing (fn () =>
wenzelm@24069
    62
              "CRITICAL" ^ show name ^ show' (! critical_name) ^ ": waiting");
wenzelm@24060
    63
            val _ = Mutex.lock critical_lock;
wenzelm@24060
    64
            val _ = tracing (fn () =>
wenzelm@24069
    65
              "CRITICAL" ^ show name ^ show' (! critical_name) ^ ": passed after " ^
wenzelm@24060
    66
              Time.toString (Timer.checkRealTimer timer));
wenzelm@24060
    67
          in () end;
wenzelm@23961
    68
      val _ = critical_thread := SOME (Thread.self ());
wenzelm@24060
    69
      val _ = critical_name := name;
wenzelm@23961
    70
      val result = Exn.capture e ();
wenzelm@24060
    71
      val _ = critical_name := "";
wenzelm@23961
    72
      val _ = critical_thread := NONE;
wenzelm@23961
    73
      val _ = Mutex.unlock critical_lock;
wenzelm@23961
    74
    in Exn.release result end;
wenzelm@23961
    75
wenzelm@23991
    76
fun CRITICAL e = NAMED_CRITICAL "" e;
wenzelm@23981
    77
wenzelm@23961
    78
end;
wenzelm@23961
    79
wenzelm@23973
    80
wenzelm@23973
    81
(* scheduling -- non-interruptible threads working on a queue of tasks *)
wenzelm@23973
    82
wenzelm@24069
    83
local
wenzelm@24069
    84
wenzelm@24069
    85
val protected_name = ref "";
wenzelm@24069
    86
wenzelm@24069
    87
in
wenzelm@24063
    88
wenzelm@23973
    89
fun schedule n next_task tasks =
wenzelm@23973
    90
  let
wenzelm@23973
    91
    (*protected execution*)
wenzelm@23973
    92
    val lock = Mutex.mutex ();
wenzelm@24063
    93
    fun PROTECTED name e =
wenzelm@23973
    94
      let
wenzelm@23981
    95
        val _ =
wenzelm@23981
    96
          if Mutex.trylock lock then ()
wenzelm@23981
    97
          else
wenzelm@24069
    98
           (tracing (fn () => "PROTECTED" ^ show name ^ show' (! protected_name) ^ ": waiting");
wenzelm@23981
    99
            Mutex.lock lock;
wenzelm@24069
   100
            tracing (fn () => "PROTECTED" ^ show name ^ show' (! protected_name) ^ ": passed"));
wenzelm@24069
   101
        val _ = protected_name := name;
wenzelm@23973
   102
        val res = Exn.capture e ();
wenzelm@24069
   103
        val _ = protected_name := "";
wenzelm@23973
   104
        val _ = Mutex.unlock lock;
wenzelm@23973
   105
      in Exn.release res end;
wenzelm@23973
   106
wenzelm@24072
   107
    (*wakeup condition*)
wenzelm@24072
   108
    val wakeup = ConditionVar.conditionVar ();
wenzelm@24072
   109
    fun wakeup_all () = ConditionVar.broadcast wakeup;
wenzelm@24072
   110
    fun wait () = ConditionVar.wait (wakeup, lock);
wenzelm@24072
   111
wenzelm@23981
   112
    (*the queue of tasks*)
wenzelm@23973
   113
    val queue = ref tasks;
wenzelm@24072
   114
    val active = ref 0;
wenzelm@24072
   115
    fun trace_active () = tracing (fn () => "SCHEDULE: " ^ Int.toString (! active) ^ " active");
wenzelm@24072
   116
    fun dequeue () =
wenzelm@23973
   117
      let
wenzelm@23981
   118
        val (next, tasks') = next_task (! queue);
wenzelm@23973
   119
        val _ = queue := tasks';
wenzelm@24072
   120
      in
wenzelm@24072
   121
        if Task.is_running (#1 next) then
wenzelm@24072
   122
         (dec active; trace_active ();
wenzelm@24072
   123
          wait ();
wenzelm@24072
   124
          inc active; trace_active ();
wenzelm@24072
   125
          dequeue ())
wenzelm@24072
   126
        else next
wenzelm@24072
   127
      end;
wenzelm@23973
   128
wenzelm@23973
   129
    (*worker threads*)
wenzelm@23973
   130
    val running = ref 0;
wenzelm@23973
   131
    val status = ref ([]: exn list);
wenzelm@24066
   132
    fun work () =
wenzelm@24072
   133
      (case PROTECTED "dequeue" dequeue of
wenzelm@23981
   134
        (Task.Task f, cont) =>
wenzelm@24063
   135
          (case Exn.capture f () of
wenzelm@24066
   136
            Exn.Result () => continue cont
wenzelm@23981
   137
          | Exn.Exn exn =>
wenzelm@24066
   138
              (PROTECTED "status" (fn () => status := exn :: ! status); continue cont))
wenzelm@23981
   139
      | (Task.Finished, _) =>
wenzelm@24063
   140
         (PROTECTED "running" (fn () => (dec active; dec running));
wenzelm@24072
   141
          wakeup_all ()))
wenzelm@24066
   142
    and continue cont =
wenzelm@24072
   143
      (PROTECTED "cont" (fn () => queue := cont (! queue)); wakeup_all; work ());
wenzelm@23973
   144
wenzelm@23973
   145
    (*main control: fork and wait*)
wenzelm@23973
   146
    fun fork 0 = ()
wenzelm@23973
   147
      | fork k =
wenzelm@24063
   148
         (inc running; inc active;
wenzelm@24066
   149
          Thread.fork (work, [Thread.InterruptState Thread.InterruptDefer]);
wenzelm@23973
   150
          fork (k - 1));
wenzelm@24063
   151
    val _ = PROTECTED "main" (fn () =>
wenzelm@24063
   152
     (fork (Int.max (n, 1));
wenzelm@24066
   153
      while ! running <> 0 do (trace_active (); wait ())));
wenzelm@23973
   154
wenzelm@23973
   155
  in ! status end;
wenzelm@23973
   156
wenzelm@23961
   157
end;
wenzelm@23961
   158
wenzelm@24069
   159
end;
wenzelm@24069
   160
wenzelm@23991
   161
val NAMED_CRITICAL = Multithreading.NAMED_CRITICAL;
wenzelm@23961
   162
val CRITICAL = Multithreading.CRITICAL;