2004-01-23 02:23:51 +00: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: signals.pl *
|
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: signal handling in YAP *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
:- meta_predicate on_signal(+,?,:), alarm(+,:,-).
|
|
|
|
|
|
|
|
'$creep'(G) :-
|
|
|
|
% get the first signal from the mask
|
|
|
|
'$first_signal'(Sig), !,
|
|
|
|
% process it
|
|
|
|
'$do_signal'(Sig, G).
|
|
|
|
'$creep'([M|G]) :-
|
|
|
|
% noise, just go on with our life.
|
|
|
|
'$execute'(M:G).
|
|
|
|
|
|
|
|
'$do_signal'(sig_wake_up, G) :-
|
2004-06-05 04:37:01 +01:00
|
|
|
'$awoken_goals'(LG),
|
2004-01-23 02:23:51 +00:00
|
|
|
% if more signals alive, set creep flag
|
|
|
|
'$continue_signals',
|
|
|
|
'$wake_up_goal'(G, LG).
|
2008-08-30 02:39:36 +01:00
|
|
|
% never creep on entering system mode!!!
|
2008-09-22 18:07:50 +01:00
|
|
|
% don't creep on meta-call.
|
2005-02-08 04:05:39 +00:00
|
|
|
'$do_signal'(sig_creep, [M|G]) :-
|
2008-08-30 02:39:36 +01:00
|
|
|
'$creep_allowed', !,
|
2009-04-17 21:46:13 +01:00
|
|
|
(
|
2010-02-26 10:23:38 +00:00
|
|
|
( G = '$notrace'(G0) ; G = '$oncenotrace'(G0) ; G = '$execute0'(G0,M) ; '$system_module'(M), G = G0 )
|
2009-04-17 21:46:13 +01:00
|
|
|
->
|
|
|
|
(
|
|
|
|
'$execute_nonstop'(G0,M),
|
|
|
|
'$signal_creep'
|
|
|
|
;
|
|
|
|
'$signal_creep',
|
|
|
|
fail
|
|
|
|
)
|
|
|
|
;
|
|
|
|
'$start_creep'([M|G])
|
|
|
|
).
|
|
|
|
%
|
|
|
|
'$do_signal'(sig_creep, [M|G]) :-
|
2010-02-26 10:23:38 +00:00
|
|
|
( G = '$notrace'(G0) ; G = '$oncenotrace'(G0) ; G = '$execute0'(G0,M) ; '$system_module'(M), G = G0 ),
|
2009-04-17 21:46:13 +01:00
|
|
|
!,
|
|
|
|
(
|
|
|
|
'$execute_nonstop'(G0,M),
|
|
|
|
'$signal_creep'
|
|
|
|
;
|
|
|
|
'$signal_creep',
|
|
|
|
fail
|
|
|
|
).
|
|
|
|
%
|
2008-08-30 02:39:36 +01:00
|
|
|
'$do_signal'(sig_creep, [M|G]) :-
|
|
|
|
'$signal_creep',
|
|
|
|
'$execute_nonstop'(G,M).
|
2004-12-05 05:01:45 +00:00
|
|
|
'$do_signal'(sig_delay_creep, [M|G]) :-
|
|
|
|
'$execute'(M:G),
|
|
|
|
'$creep'.
|
2006-03-24 16:26:31 +00:00
|
|
|
'$do_signal'(sig_iti, [M|G]) :-
|
2004-02-05 16:57:02 +00:00
|
|
|
'$thread_gfetch'(Goal),
|
|
|
|
% if more signals alive, set creep flag
|
|
|
|
'$continue_signals',
|
|
|
|
'$current_module'(M0),
|
2006-03-24 16:26:31 +00:00
|
|
|
'$execute0'(Goal,M0),
|
|
|
|
'$execute'(M:G).
|
2005-11-09 18:02:53 +00:00
|
|
|
'$do_signal'(sig_trace, [M|G]) :-
|
2004-10-08 18:20:03 +01:00
|
|
|
'$continue_signals',
|
2005-11-09 18:02:53 +00:00
|
|
|
trace,
|
|
|
|
'$execute'(M:G).
|
|
|
|
'$do_signal'(sig_debug, [M|G]) :-
|
2004-10-08 18:20:03 +01:00
|
|
|
'$continue_signals',
|
|
|
|
debug,
|
2005-11-09 18:02:53 +00:00
|
|
|
'$execute'(M:G).
|
|
|
|
'$do_signal'(sig_break, [M|G]) :-
|
2004-10-08 18:20:03 +01:00
|
|
|
'$continue_signals',
|
|
|
|
break,
|
2005-11-09 18:02:53 +00:00
|
|
|
'$execute0'(G,M).
|
|
|
|
'$do_signal'(sig_statistics, [M|G]) :-
|
2004-10-08 18:20:03 +01:00
|
|
|
'$continue_signals',
|
|
|
|
statistics,
|
2005-11-09 18:02:53 +00:00
|
|
|
'$execute0'(G,M).
|
|
|
|
'$do_signal'(sig_stack_dump, [M|G]) :-
|
2004-10-08 18:20:03 +01:00
|
|
|
'$continue_signals',
|
|
|
|
'$stack_dump',
|
2005-11-09 18:02:53 +00:00
|
|
|
'$execute0'(G,M).
|
2004-01-23 02:23:51 +00:00
|
|
|
% Unix signals
|
|
|
|
'$do_signal'(sig_alarm, G) :-
|
|
|
|
'$signal_handler'(sig_alarm, G).
|
2009-06-03 16:09:14 +01:00
|
|
|
'$do_signal'(sig_vtalarm, G) :-
|
|
|
|
'$signal_handler'(sig_vtalarm, G).
|
2004-01-23 02:23:51 +00:00
|
|
|
'$do_signal'(sig_hup, G) :-
|
|
|
|
'$signal_handler'(sig_hup, G).
|
|
|
|
'$do_signal'(sig_usr1, G) :-
|
|
|
|
'$signal_handler'(sig_usr1, G).
|
|
|
|
'$do_signal'(sig_usr2, G) :-
|
|
|
|
'$signal_handler'(sig_usr2, G).
|
2008-05-23 11:02:13 +01:00
|
|
|
'$do_signal'(sig_pipe, G) :-
|
|
|
|
'$signal_handler'(sig_pipe, G).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
'$signal_handler'(Sig, [M|G]) :-
|
|
|
|
'$signal_do'(Sig, Goal),
|
|
|
|
% if more signals alive, set creep flag
|
|
|
|
'$continue_signals',
|
|
|
|
'$current_module'(M0),
|
|
|
|
'$execute0'((Goal,M:G),M0).
|
|
|
|
|
2008-09-23 22:50:25 +01:00
|
|
|
% '$execute0' should be ignored.
|
|
|
|
'$start_creep'([_|'$execute0'(G,M)]) :-
|
|
|
|
!,
|
|
|
|
'$start_creep'([M|G]).
|
|
|
|
% '$call'() is a complicated thing
|
|
|
|
'$start_creep'([M0|'$call'(G, CP, G0, M)]) :-
|
|
|
|
!,
|
|
|
|
'$creep',
|
|
|
|
'$execute_nonstop'('$call'(G, CP, G0, M),M0).
|
2008-09-23 23:43:01 +01:00
|
|
|
% donotrace: means do not trace! So,
|
|
|
|
% ignore and then put creep back for the continuation.
|
2008-09-24 00:13:02 +01:00
|
|
|
'$start_creep'([M0|'$notrace'(G)]) :-
|
2008-09-23 23:43:01 +01:00
|
|
|
!,
|
|
|
|
(
|
|
|
|
CP0 is '$last_choice_pt',
|
|
|
|
'$execute_nonstop'(G,M0),
|
|
|
|
CP1 is '$last_choice_pt',
|
|
|
|
% exit port: creep
|
|
|
|
'$creep',
|
|
|
|
(
|
|
|
|
% if deterministic just creep all you want.
|
|
|
|
CP0 = CP1 ->
|
|
|
|
!
|
|
|
|
;
|
|
|
|
% extra disjunction protects reentry into usergoal
|
|
|
|
(
|
|
|
|
% cannot cut here
|
|
|
|
true
|
|
|
|
;
|
|
|
|
% be sure to disable creep on redo port
|
|
|
|
'$disable_creep',
|
|
|
|
fail
|
|
|
|
)
|
|
|
|
)
|
|
|
|
;
|
|
|
|
% put it back again on fail
|
|
|
|
'$creep',
|
|
|
|
fail
|
|
|
|
).
|
|
|
|
'$start_creep'([M0|'$oncenotrace'(G)]) :-
|
|
|
|
!,
|
|
|
|
('$execute_nonstop'(G,M0),
|
|
|
|
CP1 is '$last_choice_pt',
|
|
|
|
% exit port: creep
|
|
|
|
'$creep',
|
|
|
|
!
|
|
|
|
;
|
|
|
|
% put it back again on fail
|
|
|
|
'$creep',
|
|
|
|
fail
|
|
|
|
).
|
2006-12-13 16:10:26 +00:00
|
|
|
% do not debug if we are not in debug mode.
|
|
|
|
'$start_creep'([Mod|G]) :-
|
2008-09-02 03:48:02 +01:00
|
|
|
'$debug_on'(DBON), DBON = false, !,
|
2006-12-13 16:10:26 +00:00
|
|
|
'$execute_nonstop'(G,Mod).
|
|
|
|
'$start_creep'([Mod|G]) :-
|
|
|
|
nb_getval('$system_mode',on), !,
|
|
|
|
'$execute_nonstop'(G,Mod).
|
2004-01-23 02:23:51 +00:00
|
|
|
% notice that the last signal to be processed must always be creep
|
|
|
|
'$start_creep'([_|'$cut_by'(CP)]) :- !,
|
2006-12-30 03:25:47 +00:00
|
|
|
'$$cut_by'(CP),
|
2004-01-23 02:23:51 +00:00
|
|
|
'$creep'.
|
|
|
|
'$start_creep'([_|true]) :- !,
|
|
|
|
'$creep'.
|
|
|
|
'$start_creep'([Mod|G]) :-
|
|
|
|
'$hidden_predicate'(G,Mod), !,
|
2008-08-30 02:39:36 +01:00
|
|
|
'$execute_nonstop'(G,Mod),
|
|
|
|
'$creep'.
|
2006-12-13 16:10:26 +00:00
|
|
|
% do not debug if we are zipping through.
|
|
|
|
'$start_creep'([Mod|G]) :-
|
2010-04-08 01:44:08 +01:00
|
|
|
nb_getval('$debug_run',Run),
|
|
|
|
Run \= off,
|
2006-12-13 16:10:26 +00:00
|
|
|
'$zip'(-1, G, Mod), !,
|
2008-08-30 02:39:36 +01:00
|
|
|
'$signal_creep',
|
2006-12-13 16:10:26 +00:00
|
|
|
'$execute_nonstop'(G,Mod).
|
|
|
|
'$start_creep'([Mod|G]) :-
|
2004-01-23 02:23:51 +00:00
|
|
|
CP is '$last_choice_pt',
|
2008-09-23 22:50:25 +01:00
|
|
|
'$do_spy'(G, Mod, CP, no).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
'$signal_do'(Sig, Goal) :-
|
|
|
|
recorded('$signal_handler', action(Sig,Goal), _), !.
|
|
|
|
'$signal_do'(Sig, Goal) :-
|
|
|
|
'$signal_def'(Sig, Goal).
|
|
|
|
|
|
|
|
% reconsult init files.
|
|
|
|
'$signal_def'(sig_hup, (( exists('~/.yaprc') -> [-'~/.yaprc'] ; true ),
|
|
|
|
( exists('~/.prologrc') -> [-'~/.prologrc'] ; true ),
|
|
|
|
( exists('~/prolog.ini') -> [-'~/prolog.ini'] ; true ))).
|
|
|
|
% die on signal default.
|
2008-05-23 11:02:13 +01:00
|
|
|
'$signal_def'(sig_usr1, throw(error(signal(usr1,[]),true))).
|
|
|
|
'$signal_def'(sig_usr2, throw(error(signal(usr2,[]),true))).
|
|
|
|
'$signal_def'(sig_pipe, throw(error(signal(pipe,[]),true))).
|
2004-01-23 02:23:51 +00:00
|
|
|
% ignore sig_alarm by default
|
|
|
|
'$signal_def'(sig_alarm, true).
|
|
|
|
|
|
|
|
|
|
|
|
on_signal(Signal,OldAction,default) :-
|
|
|
|
'$reset_signal'(Signal, OldAction).
|
|
|
|
on_signal(Signal,OldAction,Action) :-
|
2004-05-13 21:54:58 +01:00
|
|
|
var(Action), !,
|
2009-05-28 18:23:43 +01:00
|
|
|
throw(error(system_error,'Somehow the meta_predicate declarations of on_signal are subverted!')).
|
|
|
|
on_signal(Signal,OldAction,Action) :-
|
|
|
|
Action = (_:Goal),
|
|
|
|
var(Goal), !,
|
2006-03-24 16:26:31 +00:00
|
|
|
'$check_signal'(Signal, OldAction),
|
2004-01-23 02:23:51 +00:00
|
|
|
Action = OldAction.
|
|
|
|
on_signal(Signal,OldAction,Action) :-
|
|
|
|
'$reset_signal'(Signal, OldAction),
|
2009-05-28 18:08:29 +01:00
|
|
|
% 13211-2 speaks only about callable
|
|
|
|
( Action = M:Goal -> true ; throw(error(type_error(callable,Action),on_signal/3)) ),
|
|
|
|
% the following disagrees with 13211-2:6.7.1.4 which disagrees with 13211-1:7.12.2a
|
|
|
|
% but the following agrees with 13211-1:7.12.2a
|
|
|
|
( nonvar(M) -> true ; throw(error(instantiation_error,on_signal/3)) ),
|
|
|
|
( atom(M) -> true ; throw(error(type_error(callable,Action),on_signal/3)) ),
|
|
|
|
( nonvar(Goal) -> true ; throw(error(instantiation_error,on_signal/3)) ),
|
|
|
|
recordz('$signal_handler', action(Signal,Action), _).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
'$reset_signal'(Signal, OldAction) :-
|
2005-01-07 06:29:20 +00:00
|
|
|
recorded('$signal_handler', action(Signal,OldAction), Ref), !,
|
2004-01-23 02:23:51 +00:00
|
|
|
erase(Ref).
|
|
|
|
'$reset_signal'(_, default).
|
|
|
|
|
|
|
|
'$check_signal'(Signal, OldAction) :-
|
2005-01-07 06:29:20 +00:00
|
|
|
recorded('$signal_handler', action(Signal,OldAction), _), !.
|
2006-03-24 16:26:31 +00:00
|
|
|
'$check_signal'(_, default).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
|
2009-05-28 18:23:43 +01:00
|
|
|
alarm(Interval, Goal, Left) :-
|
|
|
|
Interval == 0, !,
|
2009-05-30 19:04:24 +01:00
|
|
|
'$alarm'(0, 0, Left0, _),
|
2009-05-28 18:23:43 +01:00
|
|
|
on_signal(sig_alarm, _, Goal),
|
2009-05-30 19:04:24 +01:00
|
|
|
Left = Left0.
|
2004-01-23 02:23:51 +00:00
|
|
|
alarm(Interval, Goal, Left) :-
|
2007-05-21 00:00:38 +01:00
|
|
|
integer(Interval), !,
|
2004-01-23 02:23:51 +00:00
|
|
|
on_signal(sig_alarm, _, Goal),
|
2007-05-21 00:00:38 +01:00
|
|
|
'$alarm'(Interval, 0, Left, _).
|
2010-02-27 10:10:23 +00:00
|
|
|
alarm(Number, Goal, Left) :-
|
|
|
|
float(Number), !,
|
|
|
|
Secs is integer(Number),
|
|
|
|
USecs is integer((Number-Secs)*1000000) mod 1000000,
|
|
|
|
on_signal(sig_alarm, _, Goal),
|
|
|
|
'$alarm'(Interval, 0, Left, _).
|
2007-05-21 00:00:38 +01:00
|
|
|
alarm(Interval.USecs, Goal, Left.LUSecs) :-
|
|
|
|
on_signal(sig_alarm, _, Goal),
|
|
|
|
'$alarm'(Interval, USecs, Left, LUSecs).
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
raise_exception(Ball) :- throw(Ball).
|
|
|
|
|
|
|
|
on_exception(Pat, G, H) :- catch(G, Pat, H).
|
|
|
|
|
2004-12-08 04:45:04 +00:00
|
|
|
read_sig :-
|
2005-01-07 06:29:20 +00:00
|
|
|
recorded('$signal_handler',X,_),
|
2004-12-08 04:45:04 +00:00
|
|
|
writeq(X),nl,
|
|
|
|
fail.
|
|
|
|
read_sig.
|
|
|
|
|
|
|
|
|
2006-12-13 16:10:26 +00:00
|
|
|
|