View source with raw 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-2015, University of Amsterdam
    7                              VU University 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(time,
   37          [ alarm/3,                    % +Time, :Callable, -Id
   38            alarm/4,                    % +Time, :Callable, -Id, +Options
   39            alarm_at/3,                 % +Time, :Callable, -Id
   40            alarm_at/4,                 % +Time, :Callable, -Id, +Options
   41            remove_alarm/1,             % +Id
   42            install_alarm/1,            % +Id
   43            install_alarm/2,            % +Id, +Time
   44            uninstall_alarm/1,          % +Id
   45            current_alarm/4,            % ?At, ?:Goal, ?Id, ?Status
   46            call_with_time_limit/2      % +Time, :Callable
   47          ]).   48:- use_module(library(lists)).   49:- set_prolog_flag(generate_debug_info, false).   50
   51:- meta_predicate
   52    call_with_time_limit(+, 0),
   53    alarm(+, 0, -),
   54    alarm(+, 0, -, +),
   55    alarm_at(+, 0, -, +),
   56    current_alarm(?, :, ?, ?).   57
   58:- predicate_options(alarm/4, 4,
   59                     [ remove(boolean),
   60                       install(boolean)
   61                     ]).

Time and alarm library

*/

 alarm(+Time, :Callable, -Id) is det
 alarm(+Time, :Callable, -Id, +Options) is det
Set up an alarm to be signaled Time seconds from now. If the alarm expires, Callable is called asynchronously. Callable can be used to raise an exception using throw/1 to abort some execution.

Options is a list of Name(Value) options. Currently defined options are:

remove(Bool)
If true (default false), remove the alarm-event (as remove_alarm/1) after it has been fired.
install(Bool)
If false (default true) do not install the alarm. It must be installed separately using install_alarm/1.
 alarm_at(+Time, :Callable, -Id) is det
 alarm_at(+Time, :Callable, -Id, +Options) is det
As alarm/3 and alarm/4, but schedule the alarm at an absolute point in time.
See also
- date_time_stamp/2.
 install_alarm(+Id) is det
 install_alarm(+Id, +RelTime) is det
Install an alarm allocated using alarm/4 with the install(false) option or de-activated using uninstall_alarm/1. With a given RelTime, the alarm is scheduled at the RelTime from now. Otherwise it is scheduled on the same (absolute) time on which is was created.
 uninstall_alarm(+Id) is det
De-activate an alarm. This does not invalidate Id, but ensures that the alarm will not fire. The alarm can be rescheduled to the original time using install_alarm/1 or to a new time using install_alarm/2.
 remove_alarm(+Id) is det
Remove an alarm. If it has not yet been fired, it never will.
 current_alarm(?Time, :Goal, ?Id, ?Status) is nondet
Enumerate the alarms in the schedule. Time is the absolute time the event is scheduled for (see also get_time/1). Goal is the goal to execute, Id is the identifier and Status is the scheduling status. It takes the value done if the alarm has been fired, next if the event is the next to be executed and scheduled otherwise.
  122:- use_foreign_library(foreign(time)).  123:- public time_debug/1.                 % set debugging
 call_with_time_limit(+Time, :Goal) is semidet
Call Goal, while watching out for a (wall-time) limit. If this limit is exceeded, the exception time_limit_exceeded is raised. Goal is called as in once/1.
throws
- time_limit_exceeded
  133call_with_time_limit(Time, Goal) :-
  134    Time > 0,
  135    !,
  136    setup_call_cleanup(alarm(Time, time_limit_exceeded(Time),
  137                             Id, [install(false)]),
  138                       run_alarm_goal(Id, Goal),
  139                       remove_alarm_notrace(Id)).
  140call_with_time_limit(_Time, _Goal) :-
  141    throw(time_limit_exceeded).
  142
  143run_alarm_goal(AlarmID, Goal) :-
  144    install_alarm(AlarmID),
  145    Goal,
  146    !.
  147
  148time_limit_exceeded(_Time) :-
  149    throw(time_limit_exceeded).
  150
  151current_alarm(Time, Goal, Id, Status) :-
  152    current_alarms(Time, Goal, Id, Status, List),
  153    member(alarm(Time, Goal, Id, Status), List).
  154
  155                 /*******************************
  156                 *        HANDLE MESSAGES       *
  157                 *******************************/
  158
  159:- multifile
  160    prolog:message/3.  161
  162prolog:message(time_limit_exceeded) -->
  163    [ 'Time limit exceeded' ].
  164
  165                 /*******************************
  166                 *             ALARM            *
  167                 *******************************/
  168
  169:- multifile sandbox:safe_meta_predicate/1.  170
  171sandbox:safe_meta_predicate(time:call_with_time_limit/2)