If anyone's interested, the latest GHC from CVS can now do signal handling
using the Posix library.  

Signal handlers now run in separate threads, instead of the old system which
replaced the entire computation with the signal handler.  This means you can
do I/O from a signal handler, and even raise exceptions in other threads.
eg.  the following program prints "Timer" every second, and quits with the
message "Ctrl-C" when you press ctrl-C.

  import IO
  import Posix
  import Concurrent
  import Exception

  timerHandler = do
    hPutStr stderr "Timer\n"

  ctrlCHandler thread = do
    raiseInThread thread (ErrorCall "Ctrl-C")

  main = do
    thread <- myThreadId
    installHandler keyboardSignal (Catch (ctrlCHandler thread)) Nothing
    installHandler realTimeAlarm  (Catch timerHandler) Nothing
    let loop = do
             scheduleAlarm 1
             awaitSignal Nothing
             yield
             loop
    loop


Unfortunately the 'yield' call is quite important to make sure the signal
handler threads get scheduled - otherwise the entire system ends up blocked
in awaitSignal all the time.  We're working on a solution to this.

Cheers,
        Simon

Reply via email to