Improve the API for TChan, TMVar, and TVar
Apply the patch for the proposal, reproduced at bottom. This proposal was discussed on the mailing list, http://www.haskell.org/pipermail/libraries/2011-March/016086.html. 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, http://www.haskell.org/pipermail/libraries/2011-March/016096.html, 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
Trac metadata
Trac field | Value |
---|---|
Version | 7.0.3 |
Type | Task |
TypeOfFailure | OtherFailure |
Priority | normal |
Resolution | Unresolved |
Component | libraries (other) |
Test case | |
Differential revisions | |
BlockedBy | |
Related | |
Blocking | |
CC | |
Operating system | |
Architecture |