module Control.Concurrent.STMSupply
(
STMSupply
, newSTMSupplyIO
, freshId
, splitSupply
) where
import Control.Applicative
import qualified Control.Concurrent.Supply as S
import GHC.Conc
newtype STMSupply = Supply {
unSupply :: TVar S.Supply
}
newSTMSupplyIO :: IO STMSupply
newSTMSupplyIO = Supply <$> (newTVarIO =<< S.newSupply)
freshId :: STMSupply -> STM Int
freshId Supply{unSupply = s} = do
(i, s') <- S.freshId <$> readTVar s
writeTVar s s' >> return i
splitSupply :: STMSupply -> STM STMSupply
splitSupply Supply{unSupply = s} = do
(s1, s2) <- S.splitSupply <$> readTVar s
writeTVar s s1 >> (Supply <$> newTVar s2)