Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell98 |
An MVar
variation that only blocks for modification.
Readers are never blocked but write access is carried out in sequence.
This is done with two MVar
s.
While modification is done, the readers use the old value.
When the modification is done, the old (unmodified) value is replaced with the new one.
For this to work, the writers have to block each other which is done with the second MVar
.
This process is encapsulated in modifyXMVar
and modifyXMVar_
.
Note: This may increase the memory usage since there may be two value present at a time. This is intended to be used with (big) data structures where small changes are made.
- data XMVar a
- newXMVar :: a -> IO (XMVar a)
- readXMVar :: XMVar a -> IO a
- modifyXMVar :: XMVar a -> (a -> IO (a, b)) -> IO b
- modifyXMVar_ :: XMVar a -> (a -> IO a) -> IO ()
- takeXMVarWrite :: XMVar a -> IO a
- putXMVarWrite :: XMVar a -> a -> IO ()
- takeXMVarLock :: XMVar a -> IO a
- putXMVarLock :: XMVar a -> a -> IO ()
Documentation
modifyXMVar :: XMVar a -> (a -> IO (a, b)) -> IO b Source
Modify the content.
modifyXMVar_ :: XMVar a -> (a -> IO a) -> IO () Source
Like modifyXMVar
but without a return value.
takeXMVarWrite :: XMVar a -> IO a Source
Locks for writes and reads the value. Readers do not block each other.
modifyXMVar
encapsulates takeXMVarWrite
and putXMVarWrite
and also handles exceptions.
putXMVarWrite :: XMVar a -> a -> IO () Source
Replaces the value (since it was locked for potential writers) and unlocks writers.
takeXMVarLock :: XMVar a -> IO a Source
Locks for both reads and writes (MVar
behaviour).
This may be useful to save space because the old a
does not have to be kept in memory for
read access. Note that references to the old a
might still lead to memory leaks/issues.
putXMVarLock :: XMVar a -> a -> IO () Source
Replaces the value (since it was locked for potential writers) and unlocks writers.