2001-04-09 20:54:03 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: timeout.yap *
|
|
|
|
* Last rev: 5/12/99 *
|
|
|
|
* mods: *
|
|
|
|
* comments: Goal within timeout *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
:- module(timeout, [
|
|
|
|
time_out/3
|
|
|
|
]).
|
|
|
|
|
|
|
|
:- meta_predicate time_out(:,+,-).
|
|
|
|
|
|
|
|
%
|
|
|
|
% not the nicest program I've ever seen.
|
|
|
|
%
|
2009-05-21 06:39:57 +01:00
|
|
|
%
|
|
|
|
% I cannot use setup_call_cleanup because I can only take interrupts *after* I set up
|
|
|
|
% the catcher, so I have to do the dirty deed myself.
|
|
|
|
%
|
2001-04-09 20:54:03 +01:00
|
|
|
time_out(Goal, Time, Result) :-
|
|
|
|
T is Time//1000,
|
2007-05-21 00:00:38 +01:00
|
|
|
UT is (Time mod 1000)*1000,
|
2009-05-21 06:39:57 +01:00
|
|
|
yap_hacks:disable_interrupts,
|
2001-04-09 20:54:03 +01:00
|
|
|
% enable alarm
|
2007-05-21 00:00:38 +01:00
|
|
|
alarm(T.UT,throw(time_out),_),
|
2001-04-09 20:54:03 +01:00
|
|
|
% launch goal and wait for signal
|
2009-05-21 06:39:57 +01:00
|
|
|
catch( run_goal(Goal, Result0),
|
|
|
|
time_out,
|
|
|
|
Result0 = time_out ),
|
2009-05-19 17:10:51 +01:00
|
|
|
Result = Result0.
|
2009-05-21 06:39:57 +01:00
|
|
|
|
|
|
|
run_goal(Goal, Result0) :-
|
|
|
|
% we can only enable interrupts after alarm was been enabled.
|
|
|
|
yap_hacks:enable_interrupts,
|
|
|
|
Result0 = success,
|
2009-05-21 07:30:24 +01:00
|
|
|
yap_hacks:current_choice_point(CP0),
|
2009-05-21 06:39:57 +01:00
|
|
|
call(Goal),
|
2009-05-21 07:30:24 +01:00
|
|
|
yap_hacks:current_choice_point(CP1),
|
2009-05-21 06:39:57 +01:00
|
|
|
% make sure we're not getting an extraneous interrupt if we terminate early....
|
2009-05-21 07:30:24 +01:00
|
|
|
alarm(0,_,RT),
|
|
|
|
(
|
|
|
|
CP0 == CP1
|
|
|
|
->
|
|
|
|
true
|
|
|
|
;
|
|
|
|
(
|
|
|
|
true
|
|
|
|
;
|
|
|
|
alarm(RT,throw(time_out),_),
|
|
|
|
fail
|
|
|
|
)
|
|
|
|
).
|
|
|
|
|
2009-05-21 06:39:57 +01:00
|
|
|
run_goal(Goal, Result0) :-
|
|
|
|
yap_hacks:disable_interrupts,
|
|
|
|
% make sure we're not getting an extraneous interrupt if we terminate early....
|
|
|
|
alarm(0,_,_),
|
|
|
|
fail.
|
|
|
|
|
|
|
|
complete_time_out :-
|
|
|
|
alarm(0,_,_),
|
|
|
|
yap_hacks:enable_interrupts.
|