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).
|
2005-02-08 04:05:39 +00:00
|
|
|
'$do_signal'(sig_creep, [M|G]) :-
|
|
|
|
( '$access_yap_flags'(10,0) ->
|
|
|
|
% we're not allowed to creep for now,
|
|
|
|
% maybe we're inside builtin.
|
|
|
|
'$late_creep',
|
|
|
|
'$execute'(M:G)
|
|
|
|
;
|
|
|
|
'$start_creep'([M|G])
|
|
|
|
).
|
2004-12-05 05:01:45 +00:00
|
|
|
'$do_signal'(sig_delay_creep, [M|G]) :-
|
|
|
|
'$execute'(M:G),
|
|
|
|
'$creep'.
|
2004-02-05 16:57:02 +00:00
|
|
|
'$do_signal'(sig_iti, G) :-
|
|
|
|
'$thread_gfetch'(Goal),
|
|
|
|
% if more signals alive, set creep flag
|
|
|
|
'$continue_signals',
|
|
|
|
'$current_module'(M0),
|
2004-10-08 18:20:03 +01:00
|
|
|
'$execute0'(G,M0).
|
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).
|
|
|
|
'$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).
|
|
|
|
|
|
|
|
'$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).
|
|
|
|
|
|
|
|
% notice that the last signal to be processed must always be creep
|
|
|
|
'$start_creep'([_|'$cut_by'(CP)]) :- !,
|
|
|
|
'$cut_by'(CP),
|
|
|
|
'$creep'.
|
|
|
|
'$start_creep'([_|true]) :- !,
|
|
|
|
'$creep'.
|
|
|
|
'$start_creep'([Mod|G]) :-
|
|
|
|
'$hidden_predicate'(G,Mod), !,
|
|
|
|
'$creep',
|
2004-05-13 21:54:58 +01:00
|
|
|
'$execute_nonstop'(G,Mod).
|
2004-01-23 02:23:51 +00:00
|
|
|
'$start_creep'([Mod|G]) :-
|
2005-02-08 04:05:39 +00:00
|
|
|
'$do_not_creep',
|
2004-01-23 02:23:51 +00:00
|
|
|
CP is '$last_choice_pt',
|
2004-07-15 16:47:08 +01:00
|
|
|
'$do_spy'(G, Mod, CP, yes).
|
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.
|
|
|
|
'$signal_def'(sig_usr1, (print_message(error, 'Received user signal 1'),halt)).
|
|
|
|
'$signal_def'(sig_usr2, (print_message(error, 'Received user signal 2'),halt)).
|
|
|
|
% 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), !,
|
2004-01-23 02:23:51 +00:00
|
|
|
'$check_signal'(OldAction),
|
|
|
|
Action = OldAction.
|
|
|
|
on_signal(Signal,OldAction,Action) :-
|
|
|
|
'$reset_signal'(Signal, OldAction),
|
|
|
|
'$current_module'(M),
|
2005-01-07 06:29:20 +00:00
|
|
|
recordz('$signal_handler', action(Signal,M: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), _), !.
|
2004-01-23 02:23:51 +00:00
|
|
|
'$reset_signal'(_, default).
|
|
|
|
|
|
|
|
|
|
|
|
alarm(Interval, Goal, Left) :-
|
|
|
|
on_signal(sig_alarm, _, Goal),
|
|
|
|
'$alarm'(Interval, Left).
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|