summaryrefslogtreecommitdiff
path: root/Utility/TSet.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2012-06-22 13:39:44 -0400
committerGravatar Joey Hess <joey@kitenet.net>2012-06-22 14:10:25 -0400
commit28e28bc0436cb0a33e570b1a1f678e80a770a21a (patch)
treed9acb7b66a19a64d6108f980c081f2537c9af353 /Utility/TSet.hs
parent3ee44cf8feb11fc439c02eb0eb8f12d290b01120 (diff)
stub syncer thread and commit channel
Diffstat (limited to 'Utility/TSet.hs')
-rw-r--r--Utility/TSet.hs39
1 files changed, 39 insertions, 0 deletions
diff --git a/Utility/TSet.hs b/Utility/TSet.hs
new file mode 100644
index 000000000..24d345477
--- /dev/null
+++ b/Utility/TSet.hs
@@ -0,0 +1,39 @@
+{- 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]
+ where
+ go l = do
+ v <- tryReadTChan tset
+ case v of
+ Nothing -> return l
+ Just c -> go (c:l)
+
+{- 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