You can not call libguile functions from handlers for POSIX signals, but
you can register Scheme handlers for POSIX signals such as
SIGINT
. These handlers do not run during the actual signal
delivery. Instead, they are run when the program (more precisely, the
thread that the handler has been registered for) reaches the next
safe point.
The libguile functions themselves have many such safe points.
Consequently, you must be prepared for arbitrary actions anytime you
call a libguile function. For example, even scm_cons
can contain
a safe point and when a signal handler is pending for your thread,
calling scm_cons
will run this handler and anything might happen,
including a non-local exit although scm_cons
would not ordinarily
do such a thing on its own.
If you do not want to allow the running of asynchronous signal handlers,
you can block them temporarily with scm_dynwind_block_asyncs
, for
example. See Asynchronous Interrupts.
Since signal handling in Guile relies on safe points, you need to make sure that your functions do offer enough of them. Normally, calling libguile functions in the normal course of action is all that is needed. But when a thread might spent a long time in a code section that calls no libguile function, it is good to include explicit safe points. This can allow the user to interrupt your code with C-c, for example.
You can do this with the macro SCM_TICK
. This macro is
syntactically a statement. That is, you could use it like this:
while (1) { SCM_TICK; do_some_work (); }
Frequent execution of a safe point is even more important in multi threaded programs, See Multi-Threading.