YAP 7.1.0
timeout.yap
Go to the documentation of this file.
1/*************************************************************************
2* *
3* YAP Prolog *
4* *
5* Yap Prolog was developed at NCCUP - Universidade do Porto *
6* *
7* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
8* *
9**************************************************************************
10* *
11* File: timeout.yap *
12* Last rev: 5/12/99 *
13* mods: *
14* comments: Goal within timeout *
15* *
16*************************************************************************/
17
18/**
19 * @file timeout.yap
20 * @author VITOR SANTOS COSTA <vsc@VITORs-MBP.lan>
21 * @date Wed Nov 18 01:26:14 2015
22 *
23 * @brief Calls With Timeout
24 *
25 *
26*/
27
28
29:- module(timeout, [
30 time_out/3
31 ]).
32
33
34/** @defgroup timeout Calls With Timeout
35@ingroup YAPLibrary
36@{
37
38The <tt>time_out/3</tt> command relies on the <tt>alarm/3</tt> built-in to
39implement a call with a maximum time of execution. The command is
40available with the `use_module(library(timeout))` command.
41
42 */
43
44/*
45
46 @pred time_out(+ _Goal_, + _Timeout_, - _Result_)
47
48
49Execute goal _Goal_ with time limited _Timeout_, where
50 _Timeout_ is measured in milliseconds. If the goal succeeds, unify
51 _Result_ with success. If the timer expires before the goal
52terminates, unify _Result_ with <tt>time_out</tt>.
53
54This command is implemented by activating an alarm at procedure
55entry. If the timer expires before the goal completes, the alarm will
56throw an exception _timeout_.
57
58One should note that time_out/3 is not reentrant, that is, a goal
59called from `time_out` should never itself call
60time_out/3. Moreover, time_out/3 will deactivate any previous
61alarms set by alarm/3 and vice-versa, hence only one of these
62calls should be used in a program.
63
64Last, even though the timer is set in milliseconds, the current
65implementation relies on <tt>alarm/3</tt>, and therefore can only offer
66precision on the scale of seconds.
67
68 */
69
70
71:- meta_predicate time_out(0,+,-).
72
73:- virtual_alarm/3alarm/3use_module(library(hacks), [
74 ,
75
76 ]).
77
78%
79% not the nicest program I've ever seen.
80%
81
82
83time_out(Goal, Time, Result) :-
84 T is (Time div 1000),
85 UT is (Time*1000) mod 1000000,
86 gated_call(
87 alarm( T, UT, __OldT, _),
88 Goal,
89 Port,
90 exit_time_out(Port, Goal, Result)),
91 gated_call.
92
93
94exit_time_out(_,_,_) :-
95 alarm(0,0,_,_),
96 alarm.
97exit_time_out(Port, _, Result):-
98 time_out_rc(Port, Result).
99
100time_out_rc(exit, success).
101time_out_rc(answer, success).
102time_out_rc(fail, failure).
103time_out_rc(exception(_), failure).
104time_out_rc(external_exception(_), failure).
105time_out_rc(redo, failure).
106time_out_rc(!, success).
107
108 clean_goal((A,B),(CA,CB)) :-
109 clean_goal,
110 clean_goal(A,CA),
111 clean_goal(B,CB).
112 clean_goal((A;B),(CA;CB)) :-
113 clean_goal,
114 clean_goal(A,CA),
115 clean_goal(B,CB).
116 clean_goal((A->B),(CA->CB)) :-
117 clean_goal,
118 clean_goal(A,CA),
119 clean_goal(B,CB).
120 clean_goal((A *->B),(CA *->CB)) :-
121 clean_goal,
122 clean_goal(A,CA),
123 clean_goal(B,CB).
124 clean_goal(user:A,CA) :-
125 clean_goal,
126 clean_goal(A,CA).
127 clean_goal(prolog:A,CA) :-
128 clean_goal,
129 clean_goal(A,CA).
130 clean_goal(A,A).
131
132%% @}
133
134
alarm(+ Seconds,+ Callable,+ OldAlarm)
virtual_alarm(+Interval, 0:Goal, -Left)
use_module( +Files )