| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
Control.Concurrent.STM.TMDeque
Description
A closable, concurrent double-ended queue backed by STM, with amortized
O(1) operations. The underlying implementation uses a two-stack design with
separate TVars for the front and rear, reducing STM contention: in the
common case, pushFront and popBack touch disjoint variables and do not
conflict.
Closing semantics follow stm-chans conventions:
- Closed + empty → read returns
Nothing(end-of-stream) - Closed + non-empty → read returns
Just a(drain remaining) - Open + empty → read blocks (
retry) - Open + non-empty → read returns
Just a - Write to closed → silently ignored
Synopsis
- data TMDeque a
- newTMDeque :: STM (TMDeque a)
- newTMDequeIO :: IO (TMDeque a)
- pushFrontTMDeque :: TMDeque a -> a -> STM ()
- popFrontTMDeque :: TMDeque a -> STM (Maybe a)
- popBackTMDeque :: TMDeque a -> STM (Maybe a)
- tryPopFrontTMDeque :: TMDeque a -> STM (Maybe (Maybe a))
- tryPopBackTMDeque :: TMDeque a -> STM (Maybe (Maybe a))
- closeTMDeque :: TMDeque a -> STM ()
- isClosedTMDeque :: TMDeque a -> STM Bool
- isClosedTMDequeIO :: TMDeque a -> IO Bool
- isEmptyTMDeque :: TMDeque a -> STM Bool
- sizeTMDeque :: TMDeque a -> STM Int
- countTMDequeIO :: TMDeque a -> IO Int
The TMDeque type
Construction
newTMDequeIO :: IO (TMDeque a) Source #
IO variant of newTMDeque.
Push operations
pushFrontTMDeque :: TMDeque a -> a -> STM () Source #
Push an element to the front of the deque. Silently ignored if the deque is closed.
Pop operations (blocking)
popFrontTMDeque :: TMDeque a -> STM (Maybe a) Source #
Pop an element from the front. Blocks if the deque is open and empty.
Returns Nothing when the deque is closed and empty (end-of-stream).
popBackTMDeque :: TMDeque a -> STM (Maybe a) Source #
Pop an element from the back. Blocks if the deque is open and empty.
Returns Nothing when the deque is closed and empty (end-of-stream).
Pop operations (non-blocking)
tryPopFrontTMDeque :: TMDeque a -> STM (Maybe (Maybe a)) Source #
Non-blocking pop from the front.
Nothing— closed (end-of-stream)Just Nothing— open and empty (would block)Just (Just a)— got an element
tryPopBackTMDeque :: TMDeque a -> STM (Maybe (Maybe a)) Source #
Non-blocking pop from the back.
Nothing— closed (end-of-stream)Just Nothing— open and empty (would block)Just (Just a)— got an element
Closing & queries
closeTMDeque :: TMDeque a -> STM () Source #
Close the deque. After closing, writes are silently ignored and reads will drain remaining elements before signalling end-of-stream. Closing is idempotent.