memory ordering

John Lato jwlato at gmail.com
Fri Dec 20 01:36:11 UTC 2013


Hello,

I'm working on a lock-free algorithm that's meant to be used in a
concurrent setting, and I've run into a possible issue.

The crux of the matter is that a particular function needs to perform the
following:

> x <- MVector.read vec ix
> position <- readIORef posRef

and the algorithm is only safe if these two reads are not reordered (both
the vector and IORef are written to by other threads).

My concern is, according to standard Haskell semantics this should be safe,
as IO sequencing should guarantee that the reads happen in-order.  Of
course this also relies upon the architecture's memory model, but x86 also
guarantees that reads happen in order.  However doubts remain; I do not
have confidence that the code generator will handle this properly.  In
particular, LLVM may freely re-order loads of NotAtomic and Unordered
values.

The one hope I have is that ghc will preserve IO semantics through the
entire pipeline.  This seems like it would be necessary for proper handling
of exceptions, for example.  So, can anyone tell me if my worries are
unfounded, or if there's any way to ensure the behavior I want?  I could
change the readIORef to an atomicModifyIORef, which should issue an mfence,
but that seems a bit heavy-handed as just a read fence would be sufficient
(although even that seems more than necessary).

Thanks,
John L.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/glasgow-haskell-users/attachments/20131219/2dc335e7/attachment.html>


More information about the Glasgow-haskell-users mailing list