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)  2002-2020, University of Amsterdam
    7                              VU University Amsterdam
    8                              CWI, Amsterdam
    9    All rights reserved.
   10
   11    Redistribution and use in source and binary forms, with or without
   12    modification, are permitted provided that the following conditions
   13    are met:
   14
   15    1. Redistributions of source code must retain the above copyright
   16       notice, this list of conditions and the following disclaimer.
   17
   18    2. Redistributions in binary form must reproduce the above copyright
   19       notice, this list of conditions and the following disclaimer in
   20       the documentation and/or other materials provided with the
   21       distribution.
   22
   23    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   24    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   25    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   26    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   27    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   28    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   29    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   30    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   31    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   32    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   33    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   34    POSSIBILITY OF SUCH DAMAGE.
   35*/
   36
   37:- module(time,
   38          [ alarm/3,                    % +Time, :Callable, -Id
   39            alarm/4,                    % +Time, :Callable, -Id, +Options
   40            alarm_at/3,                 % +Time, :Callable, -Id
   41            alarm_at/4,                 % +Time, :Callable, -Id, +Options
   42            remove_alarm/1,             % +Id
   43            install_alarm/1,            % +Id
   44            install_alarm/2,            % +Id, +Time
   45            uninstall_alarm/1,          % +Id
   46            current_alarm/4,            % ?At, ?:Goal, ?Id, ?Status
   47            call_with_time_limit/2      % +Time, :Callable
   48          ]).   49:- autoload(library(lists),[member/2]).   50
   51:- set_prolog_flag(generate_debug_info, false).   52
   53:- meta_predicate
   54    call_with_time_limit(+, 0),
   55    alarm(+, 0, -),
   56    alarm(+, 0, -, +),
   57    alarm_at(+, 0, -, +),
   58    current_alarm(?, :, ?, ?).   59
   60:- predicate_options(alarm/4, 4,
   61                     [ remove(boolean),
   62                       install(boolean)
   63                     ]).   64
   65
   66/** <module> Time and alarm library
   67*/
   68
   69%!  alarm(+Time, :Callable, -Id) is det.
   70%!  alarm(+Time, :Callable, -Id, +Options) is det.
   71%
   72%   Set up an alarm to be  signaled   Time  seconds from now. If the
   73%   alarm expires, Callable is called   asynchronously. Callable can
   74%   be used to raise  an  exception   using  throw/1  to  abort some
   75%   execution.
   76%
   77%   Options is a list of Name(Value) options.  Currently defined
   78%   options are:
   79%
   80%           * remove(Bool)
   81%           If =true= (default =false=), remove the alarm-event (as
   82%           remove_alarm/1) after it has been fired.
   83%           * install(Bool)
   84%           If =false= (default =true=) do not install the alarm.
   85%           It must be installed separately using install_alarm/1.
   86
   87%!  alarm_at(+Time, :Callable, -Id) is det.
   88%!  alarm_at(+Time, :Callable, -Id, +Options) is det.
   89%
   90%   As alarm/3 and alarm/4, but schedule   the  alarm at an absolute
   91%   point in time.
   92%
   93%   @see date_time_stamp/2.
   94
   95%!  install_alarm(+Id) is det.
   96%!  install_alarm(+Id, +RelTime) is det.
   97%
   98%   Install an alarm allocated using alarm/4 with the install(false)
   99%   option or de-activated using  uninstall_alarm/1.   With  a given
  100%   RelTime, the alarm  is  scheduled  at   the  RelTime  from  now.
  101%   Otherwise it is scheduled on the   same (absolute) time on which
  102%   is was created.
  103
  104%!  uninstall_alarm(+Id) is det.
  105%
  106%   De-activate an alarm. This does _not_ invalidate Id, but ensures
  107%   that the alarm will not fire. The alarm can be rescheduled to
  108%   the original time using install_alarm/1 or to a new time using
  109%   install_alarm/2.
  110
  111%!  remove_alarm(+Id) is det.
  112%
  113%   Remove an alarm.  If it has not yet been fired, it never will.
  114
  115%!  current_alarm(?Time, :Goal, ?Id, ?Status) is nondet.
  116%
  117%   Enumerate the alarms in the schedule.  Time is the absolute time
  118%   the event is scheduled for (see   also  get_time/1). Goal is the
  119%   goal to execute,  Id  is  the   identifier  and  Status  is  the
  120%   scheduling status. It takes the value   =done=  if the alarm has
  121%   been fired, =next= if the event is   the next to be executed and
  122%   =scheduled= otherwise.
  123
  124:- use_foreign_library(foreign(time)).  125:- public time_debug/1.                 % set debugging
  126
  127%!  call_with_time_limit(+Time, :Goal) is semidet.
  128%
  129%   Call Goal, while watching out for   a (wall-time) limit. If this
  130%   limit  is  exceeded,  the   exception  =time_limit_exceeded=  is
  131%   raised. Goal is called as in once/1.
  132%
  133%   @throws =time_limit_exceeded=
  134
  135call_with_time_limit(Time, Goal) :-
  136    Time > 0,
  137    !,
  138    setup_call_cleanup(alarm(Time, time_limit_exceeded(Time),
  139                             Id, [install(false)]),
  140                       run_alarm_goal(Id, Goal),
  141                       remove_alarm_notrace(Id)).
  142call_with_time_limit(_Time, _Goal) :-
  143    throw(time_limit_exceeded).
  144
  145run_alarm_goal(AlarmID, Goal) :-
  146    install_alarm(AlarmID),
  147    Goal,
  148    !.
  149
  150time_limit_exceeded(_Time) :-
  151    throw(time_limit_exceeded).
  152
  153current_alarm(Time, Goal, Id, Status) :-
  154    current_alarms(Time, Goal, Id, Status, List),
  155    member(alarm(Time, Goal, Id, Status), List).
  156
  157                 /*******************************
  158                 *        HANDLE MESSAGES       *
  159                 *******************************/
  160
  161:- multifile
  162    prolog:message/3.  163
  164prolog:message(time_limit_exceeded) -->
  165    [ 'Time limit exceeded' ].
  166
  167                 /*******************************
  168                 *             ALARM            *
  169                 *******************************/
  170
  171:- multifile sandbox:safe_meta_predicate/1.  172
  173sandbox:safe_meta_predicate(time:call_with_time_limit/2)