IORefs for io-sim and io-sim-por#145
Conversation
There was a problem hiding this comment.
Implementations in this module are based on Data.IORef and GHC.IORef from base-4.19.0.0
| instance MonadIORef m => MonadIORef (ReaderT r m) where | ||
| type IORef (ReaderT r m) = IORef m | ||
| newIORef = lift . newIORef | ||
| readIORef = lift . readIORef | ||
| writeIORef = lift .: writeIORef | ||
| modifyIORef = lift .: modifyIORef | ||
| modifyIORef' = lift .: modifyIORef' | ||
| atomicModifyIORef = lift .: atomicModifyIORef | ||
| atomicModifyIORef' = lift .: atomicModifyIORef' | ||
| atomicWriteIORef = lift .: atomicWriteIORef |
There was a problem hiding this comment.
Should this be moved to io-classes-mtl?
| atomicModifyIORef2Lazy :: IORef (IOSim s) a -> (a -> (a,b)) -> IOSim s (a, (a, b)) | ||
| atomicModifyIORef2Lazy (IORef (STRef r#)) f = stToIO $ | ||
| ST (\s -> case atomicModifyMutVar2# r# f s of | ||
| (# s', old, res #) -> (# s', (old, res) #)) |
There was a problem hiding this comment.
Since IORefs are STRefs, we can copy the code from base. However, this is probably overkill -- @coot, do you have pointers for how I should proceed?
There was a problem hiding this comment.
Also, these implementations might change slightly between GHC releases. I see that GHA is failing because GHCs before 9.8 don't have access to an atomicSwapMutVar# function
dcoutts
left a comment
There was a problem hiding this comment.
The iosim implementations could be simpler because there's no genuine concurrency, even in the POR case. No primitive can be interrupted. In particular anything lifted using stToIO is a single LiftST action in the interpreter. So that means "atomic" modify can be implemented just as a read and write, wrapped in stToIO.
|
Arguably, these should be implemented as "proper" simulation primitives for the benefit of the POR case. Otherwise the POR interpreter cannot see that they might be interesting to reorder. |
First stab at #112