pure-borrow
Safe HaskellNone
LanguageGHC2021

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

The TMDeque type

data TMDeque a Source #

A closable, STM-backed double-ended queue with amortized O(1) operations.

Construction

newTMDeque :: STM (TMDeque a) Source #

Create a new empty TMDeque.

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.

isClosedTMDeque :: TMDeque a -> STM Bool Source #

Check whether the deque has been closed.

isClosedTMDequeIO :: TMDeque a -> IO Bool Source #

Check whether the deque has been closed.

isEmptyTMDeque :: TMDeque a -> STM Bool Source #

Check whether the deque is currently empty.

sizeTMDeque :: TMDeque a -> STM Int Source #

Return the number of elements currently in the deque. O(1).

countTMDequeIO :: TMDeque a -> IO Int Source #

IO variant of countTMDeque. O(1).