packages feed

bounded-tchan 0.1 → 0.2

raw patch · 2 files changed

+39/−23 lines, 2 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

+ Control.Concurrent.STM.BTChan: tryReadBTChan :: BTChan a -> STM (Maybe a)
+ Control.Concurrent.STM.BTChan: tryWriteBTChan :: BTChan a -> a -> STM Bool
+ Control.Concurrent.STM.BTChan: unGetBTChan :: BTChan a -> a -> STM ()

Files

Control/Concurrent/STM/BTChan.hs view
@@ -5,14 +5,18 @@         , newBTChanIO         , writeBTChan         , readBTChan-	, isEmptyBTChan-	, sizeOfBTChan-	, setMaxOfBTChan-	, maxOfBTChan+        , tryWriteBTChan+        , tryReadBTChan+        , unGetBTChan+        , isEmptyBTChan+        , sizeOfBTChan+        , setMaxOfBTChan+        , maxOfBTChan         ) where  import Control.Concurrent.STM-import Control.Monad (when)+import Control.Monad (when, liftM)+import Control.Applicative  -- |A 'BTChan' is a bounded 'TChan' - a FIFO channel using 'TChan' and -- a transactional variable to limit the number of elements on the channel.@@ -21,25 +25,30 @@ -- |An IO version of 'newBTChanIO'.  This should be useful with unsafePerformIO -- in the same manner as 'newTVarIO' and 'newTChanIO' are used. newBTChanIO :: Int -> IO (BTChan a)-newBTChanIO m = do-    szTV <- newTVarIO 0-    c    <- newTChanIO-    return (BTChan m c szTV)+newBTChanIO m = BTChan m <$> newTChanIO <*> newTVarIO 0 --- | `newBTChan m` make a new bounded TChan of max size `m`.+-- |@newBTChan m@ make a new bounded TChan of max size @m@. newBTChan :: Int -> STM (BTChan a)-newBTChan m = do-        szTV <- newTVar 0-        c    <- newTChan-        return (BTChan m c szTV)+newBTChan m = BTChan m <$> newTChan <*> newTVar 0  -- |Writes the value to the 'BTChan' or blocks if the channel is full. writeBTChan :: BTChan a -> a -> STM () writeBTChan (BTChan mx c szTV) x = do         sz <- readTVar szTV-	when (sz >= mx) retry-        writeTVar szTV (sz + 1) >> writeTChan c x+        when (sz >= mx) retry+        writeTVar szTV (sz + 1)+        writeTChan c x +-- |A non-blocking write that returns 'True' if the write succeeded, 'False' otherwise.+tryWriteBTChan :: BTChan a -> a -> STM Bool+tryWriteBTChan (BTChan mx c szTV) x = do+        sz <- readTVar szTV+        if (sz >= mx)+                then return False+                else do writeTVar szTV (sz + 1)+                        writeTChan c x+                        return True+ -- |Reads the next value from the 'BTChan' readBTChan :: BTChan a -> STM a readBTChan (BTChan _ c szTV) = do@@ -49,14 +58,21 @@         writeTVar szTV sz'         return x +-- |A non-blocking read that returns 'Just a' on success and 'Nothing'+-- when the channel is empty.+tryReadBTChan :: BTChan a -> STM (Maybe a)+tryReadBTChan bt = do+        e <- isEmptyBTChan bt+        if e then return Nothing else liftM Just (readBTChan bt)+ -- Put an element on the front of the queue so it will be the next item read. unGetBTChan :: BTChan a -> a -> STM () unGetBTChan (BTChan m c sTV) a = do-	s <- readTVar sTV-	when (s >= m) retry-	let !s' = s+1-	writeTVar sTV s'-	unGetTChan c a+        s <- readTVar sTV+        when (s >= m) retry+        let !s' = s+1+        writeTVar sTV s'+        unGetTChan c a  -- |Returns 'True' if the supplied 'TChan' is empty. isEmptyBTChan :: BTChan a -> STM Bool@@ -66,7 +82,7 @@ sizeOfBTChan :: BTChan a -> STM Int sizeOfBTChan (BTChan _ _ sTV) = readTVar sTV --- |@c2 = setMaxOfBTChan c1 mx@ Using the same underlying 'TChan',+-- |@let c2 = setMaxOfBTChan c1 mx@ Using the same underlying 'TChan', -- set a new maximum number of messages, @mx@.  If the current size -- is greater than @mx@ then no messages are dropped, but writes  -- will block till the size goes lower than @mx@.  Using @c2@ and
bounded-tchan.cabal view
@@ -1,5 +1,5 @@ Name:                bounded-tchan-Version:             0.1+Version:             0.2 Synopsis:            Bounded Transactional channels (queues) Description:         Bounded TChan's, or BTChan's, are a transactional queue with a limit to the number of elements (further calls to @writeBTChan@ or @unGetBTChan@ call STM retry).