-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IORef
s for io-sim and io-sim-por
#145
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since IORefs
are STRef
s, 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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