View source with formatted comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2010-2019, University of Amsterdam
    7			      CWI, Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(timeout,
   37	  [ time_out/3			% :Goal, +Time, -Result
   38	  ]).   39:- use_module(library(time)).   40
   41/** <module> SICStus 3-compatible library(timeout).
   42
   43@author Ulrich Neumerkel
   44@author Jan Wielemaker
   45
   46@see https://sicstus.sics.se/sicstus/docs/3.12.11/html/sicstus/Timeout.html
   47*/
   48
   49:- meta_predicate
   50    time_out(0, +, -).   51
   52%!   time_out(:Goal, +Time_ms, -Result) is nondet.
   53%
   54%    This  library  provides  a  SICStus  compatible  implementation  of
   55%    time-outs. This predicate runs Goal as   call/1 and binds Result to
   56%    either `success` (the  answer  was   produced  within  Time_ms)  or
   57%    `time_out`  (Goal  did  not  terminate  within  Time_ms).  If  Goal
   58%    succeeds with a choice point, backtracking   into it re-applies the
   59%    time limit, i.e., each solution gets a Time_ms time limit.
   60%
   61%    Calls to time_out/3 can  be  nested.  If   an  outer  time  out  is
   62%    triggered  first,  the  inner  time  out    is  cancelled  using  a
   63%    time_out(Id)  exception  and  the  outer    one   binds  Result  to
   64%    `time_out`.
   65%
   66%    @bug Unfortunately, our emulation is not  fully compatible with the
   67%    SICStus original. Notably, Time is  measured in __wall-time instead
   68%    of  virtual  CPU   time__.   Virtual   CPU    time   is   hard   in
   69%    threaded-environments. On most systems, you  probably need a thread
   70%    that measures the CPU usage of the monitored thread.
   71%
   72%    @see alarm/3, call_with_time_limit/2,   call_with_inference_limit/3
   73%    and   thread-based   primitives   such   as   thread_signal/2   and
   74%    first_solution/3.
   75
   76time_out(Goal, Time_ms, Result) :-
   77    Time_s is (Time_ms//1)/1000,
   78    prolog_current_frame(Fid),                  % Unique id for this call.
   79    catch( ( Result0 = success,
   80             setup_call_cleanup(
   81                 alarm(Time_s, throw(time_out(Fid)), Id),
   82                 Goal,
   83                 ( Removed = true, remove_alarm(Id) )),
   84             (   var(Removed)
   85             ->  uninstall_alarm(Id),
   86                 ( true ; install_alarm(Id,Time_s), fail )
   87             ;   true
   88             )
   89           ),
   90           time_out(Fid),
   91           Result0 = time_out ),
   92    Result = Result0