[Haskell-cafe] Locking of threads in one OS thread

Nikolay Amiantov nikoamia at gmail.com
Tue Aug 19 21:36:59 UTC 2014


Hello Cafe,

I'm using FFI to interact with a library which calls, when fail, leave
the reason in some kind of "errno"-like variable which is retrived via
another call. AFAIU, this is not thread-safe in Haskell even if
thread-local storage is used inside the library, because Haskell uses
its own thread management and the Haskell thread in the same OS thread
might be switched between the actual call and the retrival of errno
value. This should be somehow handled already in Haskell (errno is
widely used with syscalls in Linux, for example), but the source of
Foreign.C.Error suggests that this is not handled in any way at all.
For example, throwErrnoIf is implemented as such:

throwErrno loc  = do
    errno <- getErrno
    ioError (errnoToIOError loc errno Nothing Nothing)

throwErrnoIf pred loc f  = do
    res <- f
    if pred res then throwErrno loc else return res

So, the question is: how is it ensured that this block is "atomic" in
sense that at most one Haskell thread computes this whole function at
every moment in every OS thread?

Thanks for any explanations! (And sorry if my English is poor)

Nikolay.


More information about the Haskell-Cafe mailing list