@@ -45,6 +45,7 @@ module Control.Concurrent.STM.TBQueue (
4545 ) where
4646
4747import Data.Typeable
48+ import Numeric.Natural
4849import GHC.Conc
4950
5051#define _UPK_(x) {-# UNPACK #-} !(x)
@@ -53,11 +54,11 @@ import GHC.Conc
5354--
5455-- @since 2.4
5556data TBQueue a
56- = TBQueue _UPK_ (TVar Int ) -- CR: read capacity
57- _UPK_ (TVar [a ]) -- R: elements waiting to be read
58- _UPK_ (TVar Int ) -- CW: write capacity
59- _UPK_ (TVar [a ]) -- W: elements written (head is most recent)
60- _UPK_ (Int ) -- CAP: initial capacity
57+ = TBQueue _UPK_ (TVar Natural ) -- CR: read capacity
58+ _UPK_ (TVar [a ]) -- R: elements waiting to be read
59+ _UPK_ (TVar Natural ) -- CW: write capacity
60+ _UPK_ (TVar [a ]) -- W: elements written (head is most recent)
61+ _UPK_ (Natural ) -- CAP: initial capacity
6162 deriving Typeable
6263
6364instance Eq (TBQueue a ) where
@@ -77,8 +78,8 @@ instance Eq (TBQueue a) where
7778-- then CW := CR - 1; CR := 0
7879-- else **FULL**
7980
80- -- | Build and returns a new instance of 'TBQueue'
81- newTBQueue :: Int -- ^ maximum number of elements the queue can hold
81+ -- | Builds and returns a new instance of 'TBQueue'.
82+ newTBQueue :: Natural -- ^ maximum number of elements the queue can hold
8283 -> STM (TBQueue a )
8384newTBQueue size = do
8485 read <- newTVar []
@@ -91,7 +92,7 @@ newTBQueue size = do
9192-- 'TBQueue's using 'System.IO.Unsafe.unsafePerformIO', because using
9293-- 'atomically' inside 'System.IO.Unsafe.unsafePerformIO' isn't
9394-- possible.
94- newTBQueueIO :: Int -> IO (TBQueue a )
95+ newTBQueueIO :: Natural -> IO (TBQueue a )
9596newTBQueueIO size = do
9697 read <- newTVarIO []
9798 write <- newTVarIO []
@@ -103,11 +104,11 @@ newTBQueueIO size = do
103104writeTBQueue :: TBQueue a -> a -> STM ()
104105writeTBQueue (TBQueue rsize _read wsize write _size) a = do
105106 w <- readTVar wsize
106- if (w /= 0 )
107+ if (w > 0 )
107108 then do writeTVar wsize $! w - 1
108109 else do
109110 r <- readTVar rsize
110- if (r /= 0 )
111+ if (r > 0 )
111112 then do writeTVar rsize 0
112113 writeTVar wsize $! r - 1
113114 else retry
@@ -194,7 +195,7 @@ unGetTBQueue (TBQueue rsize read wsize _write _size) a = do
194195-- | Return the length of a 'TBQueue'.
195196--
196197-- @since 2.5.0.0
197- lengthTBQueue :: TBQueue a -> STM Int
198+ lengthTBQueue :: TBQueue a -> STM Natural
198199lengthTBQueue (TBQueue rsize _read wsize _write size) = do
199200 r <- readTVar rsize
200201 w <- readTVar wsize
0 commit comments