Interrupts

SB-Prolog provides a facility for exception handling using user-definable interrupt handlers. This can be used both for external interrupts, e.g. those generated from the keyboard by the user or from signals other processes; or internal traps, e.g. those caused by stack overflows, encountering undefined predicates, etc. For example, the ``undefined predicate'' interrupt is handled, by default, by the predicate `_$undefined_pred'/1, which is defined in the files modlib/src/$init_sys.P and modlib/src/$readloop.P. The default action on encountering an undefined predicate is to attempt to dynamically load a file whose name matches that of the undefined predicate. However, the user may easily alter this behaviour by redefining the undefined predicate handler.

In general, interrupts are handled by the predicate `_$interrupt'/2: _$interrupt/2 (L) a call to this predicate is of the form `_$interrupt'(Call, Code), where Call is the call that generated the interrupt, and Code is an integer indicating the nature of the interrupt. For each interrupt code, the interrupt handler then calls a handler that is designed to handle that particular kind of interrupt. At this point, the following interrupt codes have predefined meanings:

0
undefined predicate;
1
keyboard interrupt (^C);
2
stack overflow.

Other interrupt codes may be incorporated by modifying the definition of the predicate `_$ interrupt'/2 in the file modlib/src/$readloop.P.

Interrupts during execution are signalled from within the WAM simulator. The general method for raising an interrupt is using the function set_intercode in the file sim/sub_inst.c: to raise an interrupt whose code is n, the line

lpcreg = set_intercode(n);
is added to the appropriate place in the main loop of the interpreter, defined in sim/main.c.