According to GHC user guide foreign call can be marked interruptible, however, I cannot make it work. I'm using ghc 8.4.3 on GNU/Linux.
See for example this cbits.h:
/* cbits.h */
void loopForever();
cbits.c:
/* cbits.c */
#include <stdio.h>
#include <unistd.h>
#include "cbits.h"
void loopForever()
{
for (;;)
{
printf("Tick\n");
sleep(1);
}
}
And finally Test.hs:
-- Test.hs
{-# LANGUAGE InterruptibleFFI #-}
module Main where
import Control.Concurrent
import Control.Concurrent.Async
main :: IO ()
main = race_ loopForever $ do
threadDelay 2000000
putStrLn "Finished"
foreign import ccall interruptible "cbits.h"
loopForever :: IO ()
I compiled it all together with ghc -threaded -o a.out cbits.c Test.hs.
Now, I expected the code to stop after 2 seconds, however it keeps going even after "Finished" is printed. It did mention that This is **usually** enough to cause a blocking system call to return <...> in user guide, so is this the case that my c function is bad in particular, or am I doing something wrong on the Haskell side?