[Haskell-cafe] errno handling in concurrent haskell

Manlio Perillo manlio_perillo at libero.it
Tue Jan 13 06:04:43 EST 2009


Hi.

I have some doubts about errno handling in a Concurrent Haskell program.

Let's suppose that GHC non threaded runtime is used, so that each 
Haskell thread is bound to an OS thread.

Let's suppose there are two threads running (`A` and `B`).
Thread `A` calls a function `f`, that, in turn, calls via FFI a C 
function `c_f`.

Function `c_f` fails, settings errno; however the GHC scheduler suspends 
execution of thread `A` and switch to thread `B`, before the current 
value of errno is read.

Now, let's suppose thread `B` calls a function `g`, that, in turn, calls 
via FFI a C function `c_g`.
Function `c_g`, too, fails, setting errno.


Is this possible?


P.S.:
I have found this is C.Foreign.Error.hs, in base package (not the latest 
version):

throwErrnoIfRetry            :: (a -> Bool) -> String -> IO a -> IO a
throwErrnoIfRetry pred loc f  =
   do
     res <- f
     if pred res
       then do
         err <- getErrno
         if err == eINTR
           then throwErrnoIfRetry pred loc f
           else throwErrno loc
       else return res

This function calls getErrno two times.
Is this safe?

Why the throwErrno function does not accept errno as parameter?



Thanks  Manlio Perillo


More information about the Haskell-Cafe mailing list