Proposal: Add atomic IORef operations to Data.IORef

David Feuer david.feuer at
Fri Jan 15 20:05:13 UTC 2021

GHC.IORef currently exports

atomicModifyIORef2 :: IORef a -> (a -> (a,b)) -> IO (a, (a, b))
atomicSwapIORef :: IORef a -> a -> IO a

atomicModifyIORef2 is a lot like atomicModifyIORef, but it returns
both the old value in the IORef and the full result of applying the
user-supplied function to that. This is a pretty thin wrapper around
the newish atomicModifyMutVar2# primop, which has replaced the less
powerful atomicModifyMutVar#.

atomicSwapIORef atomically installs a user-supplied value in an IORef
and returns the old value. It is currently implemented using
atomicModifyIORef2, but it can and should be reimplemented using its
own, more efficient primop.

I propose to add both of these functions to Data.IORef.


More information about the Libraries mailing list