mirror of
https://github.com/barrucadu/dejafu.git
synced 2024-12-21 12:31:41 +03:00
35 lines
835 B
Haskell
35 lines
835 B
Haskell
|
-- | Transactional variables, for use with 'MonadSTM'.
|
||
|
module Control.Concurrent.STM.CTVar
|
||
|
( -- * @CTVar@s
|
||
|
CTVar
|
||
|
, newCTVar
|
||
|
, readCTVar
|
||
|
, writeCTVar
|
||
|
, modifyCTVar
|
||
|
, modifyCTVar'
|
||
|
, swapCTVar
|
||
|
) where
|
||
|
|
||
|
import Control.Monad.STM.Class
|
||
|
|
||
|
-- * @CTVar@s
|
||
|
|
||
|
-- | Mutate the contents of a 'CTVar'. This is non-strict.
|
||
|
modifyCTVar :: MonadSTM m => CTVar m a -> (a -> a) -> m ()
|
||
|
modifyCTVar ctvar f = do
|
||
|
a <- readCTVar ctvar
|
||
|
writeCTVar ctvar $ f a
|
||
|
|
||
|
-- | Mutate the contents of a 'CTVar' strictly.
|
||
|
modifyCTVar' :: MonadSTM m => CTVar m a -> (a -> a) -> m ()
|
||
|
modifyCTVar' ctvar f = do
|
||
|
a <- readCTVar ctvar
|
||
|
writeCTVar ctvar $! f a
|
||
|
|
||
|
-- | Swap the contents of a 'CTVar', returning the old value.
|
||
|
swapCTVar :: MonadSTM m => CTVar m a -> a -> m a
|
||
|
swapCTVar ctvar a = do
|
||
|
old <- readCTVar ctvar
|
||
|
writeCTVar ctvar a
|
||
|
return old
|