src/Pure/Concurrent/timeout.ML
author wenzelm
Sun, 05 Apr 2020 13:05:40 +0200
changeset 71692 f8e52c0152fe
parent 69826 1bea05713dde
child 73387 3b5196dac4c8
permissions -rw-r--r--
clarified names;

(*  Title:      Pure/Concurrent/timeout.ML
    Author:     Makarius

Execution with (relative) timeout.
*)

signature TIMEOUT =
sig
  exception TIMEOUT of Time.time
  val apply: Time.time -> ('a -> 'b) -> 'a -> 'b
  val print: Time.time -> string
end;

structure Timeout: TIMEOUT =
struct

exception TIMEOUT of Time.time;

fun apply timeout f x =
  Thread_Attributes.with_attributes Thread_Attributes.no_interrupts (fn orig_atts =>
    let
      val self = Thread.self ();
      val start = Time.now ();

      val request =
        Event_Timer.request {physical = false} (start + timeout)
          (fn () => Isabelle_Thread.interrupt_unsynchronized self);
      val result =
        Exn.capture (fn () => Thread_Attributes.with_attributes orig_atts (fn _ => f x)) ();

      val stop = Time.now ();
      val was_timeout = not (Event_Timer.cancel request);
      val test = Exn.capture Thread_Attributes.expose_interrupt ();
    in
      if was_timeout andalso (Exn.is_interrupt_exn result orelse Exn.is_interrupt_exn test)
      then raise TIMEOUT (stop - start)
      else (Exn.release test; Exn.release result)
    end);

fun print t = "Timeout after " ^ Value.print_time t ^ "s";

end;