[Openmcl-devel] Unix signal handling
Ron Garret
ron at flownet.com
Tue Jul 6 22:00:27 PDT 2010
On Jul 6, 2010, at 9:18 PM, Gary Byers wrote:
>
>
> On Tue, 6 Jul 2010, Ron Garret wrote:
>
>> Actually, on thinking about this some more, a message queue isn't necessary
>> because signals are already segregated by the OS. So something like this
>> should work:
>> (defmacro set-signal-handler (signo &body body)
>> (let ((sem (make-semaphore))
>> (handler (gensym "HANDLER")))
>> `(progn
>> (defcallback ,handler (:int signo :void)
>> (declare (ignore signo))
>> (signal-semaphore ',sem))
>> (#_signal ,signo ,handler)
>> (process-run-function ,(format nil "SIGNAL ~A HANDLER" signo)
>> (lambda ()
>> (loop
>> (wait-on-semaphore ',sem)
>> , at body))))))
>> I tried it and it actually does seem to work. To really make this
>> bulletproof you'd want to tweak it so that calling set-signal-handler
>> multiple times on the same signals didn't leave garbage processes lying
>> around.
>> rg
>
> [I realize that you're just thinking out loud here; sorry if this reply
> sounds like an overreaction.]
>
> A few messages ago in this thread, I think that I said something to the
> effect that you can't just define signal handlers via the FFI like this:
> that it'd work some of the time, but that there were GC issues. (If
> the GC runs in some thread at around the time that the signal handler
> runs in another thread, the GC has no way of seeing the state of the
> interrupted thread at the time that the signal occurred.)
>
> I did in fact say that, so my conscience is clear in this case.
Indeed you did say that. And I actually read it. This solution was specifically designed with your caveats in mind. The signal handler only does one thing: call signal-semaphore, which is itself just an FFI call. All the Lispy stuff happens in a separate thread. Is there a reason that would not work reliably? The kind of GC interaction you describe would seem to me to be impossible if the signal handler thread doesn't cons, and doesn't reference anything that might become garbage.
rg
More information about the Openmcl-devel
mailing list