2012-06-22 17:39:44 +00:00
|
|
|
{- Transactional sets
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Utility.TSet where
|
|
|
|
|
|
|
|
import Common
|
|
|
|
|
|
|
|
import Control.Concurrent.STM
|
|
|
|
|
|
|
|
type TSet = TChan
|
|
|
|
|
|
|
|
runTSet :: STM a -> IO a
|
|
|
|
runTSet = atomically
|
|
|
|
|
|
|
|
newTSet :: IO (TSet a)
|
|
|
|
newTSet = atomically newTChan
|
|
|
|
|
|
|
|
{- Gets the contents of the TSet. Blocks until at least one item is
|
|
|
|
- present. -}
|
|
|
|
getTSet :: TSet a -> IO [a]
|
|
|
|
getTSet tset = runTSet $ do
|
|
|
|
c <- readTChan tset
|
|
|
|
go [c]
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
go l = do
|
|
|
|
v <- tryReadTChan tset
|
|
|
|
case v of
|
|
|
|
Nothing -> return l
|
|
|
|
Just c -> go (c:l)
|
2012-06-22 17:39:44 +00:00
|
|
|
|
|
|
|
{- Puts items into a TSet. -}
|
|
|
|
putTSet :: TSet a -> [a] -> IO ()
|
|
|
|
putTSet tset vs = runTSet $ mapM_ (writeTChan tset) vs
|
|
|
|
|
|
|
|
{- Put a single item into a TSet. -}
|
|
|
|
putTSet1 :: TSet a -> a -> IO ()
|
|
|
|
putTSet1 tset v = void $ runTSet $ writeTChan tset v
|