mirror of
https://github.com/barrucadu/dejafu.git
synced 2024-12-21 04:21:30 +03:00
f79f7fd245
- 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.
35 lines
835 B
Haskell
Executable File
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
|