src/Pure/ML-Systems/polyml-5.1.ML
author wenzelm
Mon Jul 23 22:18:05 2007 +0200 (2007-07-23)
changeset 23947 5e396bcf749e
parent 23943 1b5f77bc146a
child 23966 25f34ff5eedf
permissions -rw-r--r--
added proper implementation of self_critical, CRITICAL;
wenzelm@23943
     1
(*  Title:      Pure/ML-Systems/polyml-5.0.ML
wenzelm@23943
     2
    ID:         $Id$
wenzelm@23943
     3
wenzelm@23943
     4
Compatibility wrapper for Poly/ML 5.1.
wenzelm@23943
     5
*)
wenzelm@23943
     6
wenzelm@23943
     7
use "ML-Systems/polyml.ML";
wenzelm@23943
     8
wenzelm@23943
     9
val pointer_eq = PolyML.pointerEq;
wenzelm@23943
    10
wenzelm@23943
    11
wenzelm@23943
    12
(* improved versions of use_text/file *)
wenzelm@23943
    13
wenzelm@23943
    14
fun use_text name (print, err) verbose txt =
wenzelm@23943
    15
  let
wenzelm@23943
    16
    val in_buffer = ref (explode txt);
wenzelm@23943
    17
    val out_buffer = ref ([]: string list);
wenzelm@23943
    18
    fun output () = implode (rev (case ! out_buffer of "\n" :: cs => cs | cs => cs));
wenzelm@23943
    19
wenzelm@23943
    20
    val line_no = ref 1;
wenzelm@23943
    21
    fun line () = ! line_no;
wenzelm@23943
    22
    fun get () =
wenzelm@23943
    23
      (case ! in_buffer of
wenzelm@23943
    24
        [] => ""
wenzelm@23943
    25
      | c :: cs => (in_buffer := cs; if c = "\n" then line_no := ! line_no + 1 else (); c));
wenzelm@23943
    26
    fun put s = out_buffer := s :: ! out_buffer;
wenzelm@23943
    27
wenzelm@23943
    28
    fun exec () =
wenzelm@23943
    29
      (case ! in_buffer of
wenzelm@23943
    30
        [] => ()
wenzelm@23943
    31
      | _ => (PolyML.compilerEx (get, put, line, name) (); exec ()));
wenzelm@23943
    32
  in
wenzelm@23943
    33
    exec () handle exn => (err (output ()); raise exn);
wenzelm@23943
    34
    if verbose then print (output ()) else ()
wenzelm@23943
    35
  end;
wenzelm@23943
    36
wenzelm@23943
    37
fun use_file output verbose name =
wenzelm@23943
    38
  let
wenzelm@23943
    39
    val instream = TextIO.openIn name;
wenzelm@23943
    40
    val txt = TextIO.inputAll instream before TextIO.closeIn instream;
wenzelm@23943
    41
  in use_text name output verbose txt end;
wenzelm@23947
    42
wenzelm@23947
    43
wenzelm@23947
    44
wenzelm@23947
    45
(** multithreading **)
wenzelm@23947
    46
wenzelm@23947
    47
open Thread;
wenzelm@23947
    48
wenzelm@23947
    49
local
wenzelm@23947
    50
wenzelm@23947
    51
datatype 'a result =
wenzelm@23947
    52
  Result of 'a |
wenzelm@23947
    53
  Exn of exn;
wenzelm@23947
    54
wenzelm@23947
    55
fun capture f x = Result (f x) handle e => Exn e;
wenzelm@23947
    56
wenzelm@23947
    57
fun release (Result y) = y
wenzelm@23947
    58
  | release (Exn e) = raise e;
wenzelm@23947
    59
wenzelm@23947
    60
fun message s =
wenzelm@23947
    61
  (TextIO.output (TextIO.stdErr, (">>> " ^ s ^ "\n")); TextIO.flushOut TextIO.stdErr);
wenzelm@23947
    62
wenzelm@23947
    63
wenzelm@23947
    64
val critical_lock = Mutex.mutex ();
wenzelm@23947
    65
val critical_thread = ref (NONE: Thread.thread option);
wenzelm@23947
    66
wenzelm@23947
    67
in
wenzelm@23947
    68
wenzelm@23947
    69
(* critical section -- may be nested within the same thread *)
wenzelm@23947
    70
wenzelm@23947
    71
fun self_critical () =
wenzelm@23947
    72
  (case ! critical_thread of
wenzelm@23947
    73
    NONE => false
wenzelm@23947
    74
  | SOME id => Thread.equal (id, Thread.self ()));
wenzelm@23947
    75
wenzelm@23947
    76
fun CRITICAL e =
wenzelm@23947
    77
  if self_critical () then e ()
wenzelm@23947
    78
  else
wenzelm@23947
    79
    let
wenzelm@23947
    80
      val _ =
wenzelm@23947
    81
        if Mutex.trylock critical_lock then ()
wenzelm@23947
    82
        else
wenzelm@23947
    83
          (message "Waiting for critical lock";
wenzelm@23947
    84
           Mutex.lock critical_lock;
wenzelm@23947
    85
           message "Obtained critical lock");
wenzelm@23947
    86
      val _ = critical_thread := SOME (Thread.self ());
wenzelm@23947
    87
      val res = capture e ();
wenzelm@23947
    88
      val _ = critical_thread := NONE;
wenzelm@23947
    89
      val _ = Mutex.unlock critical_lock;
wenzelm@23947
    90
    in release res end;
wenzelm@23947
    91
wenzelm@23947
    92
end;