2012-06-22 17:39:44 +00:00
|
|
|
{- Transactional sets
|
|
|
|
-
|
2013-03-11 01:36:13 +00:00
|
|
|
- Copyright 2012, 2013 Joey Hess <joey@kitenet.net>
|
2012-06-22 17:39:44 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Utility.TSet where
|
|
|
|
|
|
|
|
import Common
|
|
|
|
|
|
|
|
import Control.Concurrent.STM
|
|
|
|
|
|
|
|
type TSet = TChan
|
|
|
|
|
2013-03-11 01:36:13 +00:00
|
|
|
newTSet :: STM (TSet a)
|
|
|
|
newTSet = newTChan
|
2012-06-22 17:39:44 +00:00
|
|
|
|
|
|
|
{- Gets the contents of the TSet. Blocks until at least one item is
|
|
|
|
- present. -}
|
2013-03-11 01:36:13 +00:00
|
|
|
getTSet :: TSet a -> STM [a]
|
|
|
|
getTSet tset = do
|
2012-06-22 17:39:44 +00:00
|
|
|
c <- readTChan tset
|
2013-03-11 01:36:13 +00:00
|
|
|
l <- readTSet tset
|
|
|
|
return $ c:l
|
|
|
|
|
|
|
|
{- Gets anything currently in the TSet, without blocking. -}
|
|
|
|
readTSet :: TSet a -> STM [a]
|
|
|
|
readTSet tset = go []
|
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. -}
|
2013-03-11 01:36:13 +00:00
|
|
|
putTSet :: TSet a -> [a] -> STM ()
|
|
|
|
putTSet tset vs = mapM_ (writeTChan tset) vs
|
2012-06-22 17:39:44 +00:00
|
|
|
|
|
|
|
{- Put a single item into a TSet. -}
|
2013-03-11 01:36:13 +00:00
|
|
|
putTSet1 :: TSet a -> a -> STM ()
|
|
|
|
putTSet1 tset v = void $ writeTChan tset v
|