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