[Haskell-beginners] Signals and external bindings...

Brandon Allbery allbery.b at gmail.com
Fri May 4 20:19:37 CEST 2012


On Fri, May 4, 2012 at 1:26 PM, Mike Meyer <mwm at mired.org> wrote:

> But I realized I never got a more fundamental question answered: when
> does a signal handler written in Haskell run? Is it like C, in that it
> runs when the signal arrives, even if I'm currently executing some
> wrapped C code, and I have to deal with funky rules about what it can
> do? Or is it like Python, and it will run the first time the Haskell
> runtime gets control after the signal arrives?


I'm not sure it *does* run.  That is, I don't think a Haskell-based signal
handler is even possible.  When I talk about Haskell's signal handlers, I
mean C handlers within the GHC runtime.

1.  The GHC runtime only protects garbage collection from signals, it
doesn't protect anything else.  In particular, unless memory allocation is
somehow atomic, a signal arriving while something is doing an allocation
dare not itself do any allocation — which means it can do very nearly
nothing.

2.  Registering a Haskell-based signal handler would be a special case of
arranging for C to call into Haskell code (since that is in fact what it
would be doing; the real signal handler is C invoked from an assembler
trampoline).  As things currently work, this can only be done from calls to
C which suspend the Haskell runtime in a known safe state; asynchronous
signals don't provide any way to arrange this, any more than they insure
C's malloc() arena is consistent or etc.

If a Haskell-hosted signal handler mechanism were to be provided, it would
have to be done by invoking the handler from the next run of the event
loop.  The existing signal handlers work this way already, as I mentioned
already; the actual signal handler writes a byte doen a pipe, and the
runtime acts on it from its event loop.  This is because there is so little
that is safe to do from any signal handler, and even less that is safe
within the context of GHC's runtime.

-- 
brandon s allbery                                      allbery.b at gmail.com
wandering unix systems administrator (available)     (412) 475-9364 vm/sms
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120504/b019d623/attachment.htm>


More information about the Beginners mailing list