src/Pure/Concurrent/simple_thread.ML
author haftmann
Tue, 20 Jul 2010 06:35:29 +0200
changeset 37891 c26f9d06e82c
parent 37216 3165bc303f66
child 39232 69c6d3e87660
permissions -rw-r--r--
robustified metis proof
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Concurrent/simple_thread.ML
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
     3
28577
bd2456e0d944 added generic combinator for synchronized evaluation (formerly in future.ML);
wenzelm
parents: 28550
diff changeset
     4
Simplified thread operations.
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
     5
*)
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
     6
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
     7
signature SIMPLE_THREAD =
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
     8
sig
33602
d25e6bd6ca95 exported SimpleThread.attributes;
wenzelm
parents: 33220
diff changeset
     9
  val attributes: bool -> Thread.threadAttribute list
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
    10
  val fork: bool -> (unit -> unit) -> Thread.thread
28550
422e9bd169ac added fail-safe interrupt;
wenzelm
parents: 28241
diff changeset
    11
  val interrupt: Thread.thread -> unit
28577
bd2456e0d944 added generic combinator for synchronized evaluation (formerly in future.ML);
wenzelm
parents: 28550
diff changeset
    12
  val synchronized: string -> Mutex.mutex -> (unit -> 'a) -> 'a
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
    13
end;
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
    14
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 33605
diff changeset
    15
structure Simple_Thread: SIMPLE_THREAD =
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
    16
struct
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
    17
33602
d25e6bd6ca95 exported SimpleThread.attributes;
wenzelm
parents: 33220
diff changeset
    18
fun attributes interrupts =
d25e6bd6ca95 exported SimpleThread.attributes;
wenzelm
parents: 33220
diff changeset
    19
  if interrupts then Multithreading.public_interrupts else Multithreading.no_interrupts;
d25e6bd6ca95 exported SimpleThread.attributes;
wenzelm
parents: 33220
diff changeset
    20
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
    21
fun fork interrupts body =
33220
11a1af478dac SimpleThread.fork: uniform handling of outermost Interrupt, which is not an error and should not produce exception trace;
wenzelm
parents: 32295
diff changeset
    22
  Thread.fork (fn () => exception_trace (fn () => body () handle Exn.Interrupt => ()),
33602
d25e6bd6ca95 exported SimpleThread.attributes;
wenzelm
parents: 33220
diff changeset
    23
    attributes interrupts);
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
    24
28550
422e9bd169ac added fail-safe interrupt;
wenzelm
parents: 28241
diff changeset
    25
fun interrupt thread = Thread.interrupt thread handle Thread _ => ();
422e9bd169ac added fail-safe interrupt;
wenzelm
parents: 28241
diff changeset
    26
28577
bd2456e0d944 added generic combinator for synchronized evaluation (formerly in future.ML);
wenzelm
parents: 28550
diff changeset
    27
bd2456e0d944 added generic combinator for synchronized evaluation (formerly in future.ML);
wenzelm
parents: 28550
diff changeset
    28
(* basic synchronization *)
bd2456e0d944 added generic combinator for synchronized evaluation (formerly in future.ML);
wenzelm
parents: 28550
diff changeset
    29
33605
f91ec14e20b6 admit dummy implementation;
wenzelm
parents: 33602
diff changeset
    30
fun synchronized name lock e =
f91ec14e20b6 admit dummy implementation;
wenzelm
parents: 33602
diff changeset
    31
  if Multithreading.available then
f91ec14e20b6 admit dummy implementation;
wenzelm
parents: 33602
diff changeset
    32
    Exn.release (uninterruptible (fn restore_attributes => fn () =>
f91ec14e20b6 admit dummy implementation;
wenzelm
parents: 33602
diff changeset
    33
    let
f91ec14e20b6 admit dummy implementation;
wenzelm
parents: 33602
diff changeset
    34
      val immediate =
f91ec14e20b6 admit dummy implementation;
wenzelm
parents: 33602
diff changeset
    35
        if Mutex.trylock lock then true
f91ec14e20b6 admit dummy implementation;
wenzelm
parents: 33602
diff changeset
    36
        else
f91ec14e20b6 admit dummy implementation;
wenzelm
parents: 33602
diff changeset
    37
          let
f91ec14e20b6 admit dummy implementation;
wenzelm
parents: 33602
diff changeset
    38
            val _ = Multithreading.tracing 5 (fn () => name ^ ": locking ...");
f91ec14e20b6 admit dummy implementation;
wenzelm
parents: 33602
diff changeset
    39
            val time = Multithreading.real_time Mutex.lock lock;
f91ec14e20b6 admit dummy implementation;
wenzelm
parents: 33602
diff changeset
    40
            val _ = Multithreading.tracing_time true time
f91ec14e20b6 admit dummy implementation;
wenzelm
parents: 33602
diff changeset
    41
              (fn () => name ^ ": locked after " ^ Time.toString time);
f91ec14e20b6 admit dummy implementation;
wenzelm
parents: 33602
diff changeset
    42
          in false end;
f91ec14e20b6 admit dummy implementation;
wenzelm
parents: 33602
diff changeset
    43
      val result = Exn.capture (restore_attributes e) ();
f91ec14e20b6 admit dummy implementation;
wenzelm
parents: 33602
diff changeset
    44
      val _ =
f91ec14e20b6 admit dummy implementation;
wenzelm
parents: 33602
diff changeset
    45
        if immediate then () else Multithreading.tracing 5 (fn () => name ^ ": unlocking ...");
f91ec14e20b6 admit dummy implementation;
wenzelm
parents: 33602
diff changeset
    46
      val _ = Mutex.unlock lock;
f91ec14e20b6 admit dummy implementation;
wenzelm
parents: 33602
diff changeset
    47
    in result end) ())
f91ec14e20b6 admit dummy implementation;
wenzelm
parents: 33602
diff changeset
    48
  else e ();
28577
bd2456e0d944 added generic combinator for synchronized evaluation (formerly in future.ML);
wenzelm
parents: 28550
diff changeset
    49
28241
de20fccf6509 Simplified thread fork interface.
wenzelm
parents:
diff changeset
    50
end;