dejafu/Control/Concurrent/STM/CTVar.hs
Michael Walker f79f7fd245 Add a MonadSTM
- Each MonadConc has an associated MonadSTM, transactions of which
   it can run atomically.
 - The MonadSTM for IO is STM.
 - Conc and ConcIO do not yet have a MonadSTM.
2015-02-09 15:30:54 +00:00

35 lines
835 B
Haskell
Executable File

-- | 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