Opened 9 years ago

Closed 9 years ago

Last modified 8 years ago

#5104 closed task (fixed)

Improve the API for TChan, TMVar, and TVar

Reported by: WrenThornton Owned by: simonmar
Priority: normal Milestone: 7.2.1
Component: libraries (other) Version: 7.0.3
Keywords: Cc: bgamari@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


Apply the patch for the proposal, reproduced at bottom. This proposal was discussed on the mailing list, Supported by Edward Z. Yang, Bas van Dijk(*), Antoine Latter. No dissent.

Bas van Dijk's support is generally contingent on further improving the overall API by adding similar functions for the non-STM concurrency types. However, doing so requires a deprecation cycle in order to bring modifyMVar in sync with modifyTVar, modifyIORef, and the rest of the Haskell ecosystem. And therefore should be pursued in a separate proposal. He offered a patch,, which still requires verification of correctness. These changes to the base package are not included in the current proposal.

I've found the following functions helpful for working with STM. Some of them are just filling out the API so that TVars, TMVars, and IORefs match better. And all the non-TVar functions can be optimized considerably by including them in the STM library rather than defining them externally. The implementations are obvious, so I'll just include the types here; see the patch if you're interested.

    -- | A version of 'readTChan' which does not retry. Instead it
    -- returns @Nothing@ if no value is available.
    tryReadTChan :: TChan a -> STM (Maybe a)

    -- | Get the next value from the @TChan@ without removing it,
    -- retrying if the channel is empty.
    peekTChan :: TChan a -> STM a

    -- | A version of 'peekTChan' which does not retry. Instead it
    -- returns @Nothing@ if no value is available.
    tryPeekTChan :: TChan a -> STM (Maybe a)

    -- | A version of 'readTMVar' which does not retry. Instead it
    -- returns @Nothing@ if no value is available.
    tryReadTMVar :: TMVar a -> STM (Maybe a)

    -- Like 'modifyIORef' but for @TVar@.
    -- | Mutate the contents of a @TVar@. /N.B./, this version is
    -- non-strict.
    modifyTVar :: TVar a -> (a -> a) -> STM ()

    -- | Strict version of 'modifyTVar'.
    modifyTVar' :: TVar a -> (a -> a) -> STM ()

    -- Like 'swapTMVar' but for @TVar@.
    -- | Swap the contents of a @TVar@ for a new value.
    swapTVar :: TVar a -> a -> STM a

Attachments (1)

stm-api-extras.dpatch (8.7 KB) - added by WrenThornton 9 years ago.

Download all attachments as: .zip

Change History (4)

Changed 9 years ago by WrenThornton

Attachment: stm-api-extras.dpatch added

comment:1 Changed 9 years ago by simonmar

Owner: set to simonmar

comment:2 Changed 9 years ago by simonmar

Milestone: 7.2.1
Resolution: fixed
Status: newclosed

Pushed, thanks.

comment:3 Changed 8 years ago by bgamari

Cc: bgamari@… added
Note: See TracTickets for help on using tickets.