[commit: stm] master: Control.Concurrent.STM.TVar: added modifyTVar, modifyTVar', swapTVar (1b7720f)

Simon Marlow marlowsd at gmail.com
Mon Apr 11 12:42:55 CEST 2011


Repository : ssh://darcs.haskell.org//srv/darcs/packages/stm

On branch  : master

http://hackage.haskell.org/trac/ghc/changeset/1b7720f73bd9c4ac48cd8a1b60b4f9ef97ba4544

>---------------------------------------------------------------

commit 1b7720f73bd9c4ac48cd8a1b60b4f9ef97ba4544
Author: Simon Marlow <marlowsd at gmail.com>
Date:   Mon Apr 11 11:06:24 2011 +0100

    Control.Concurrent.STM.TVar: added modifyTVar, modifyTVar', swapTVar

>---------------------------------------------------------------

 Control/Concurrent/STM/TVar.hs |   32 ++++++++++++++++++++++++++++++++
 1 files changed, 32 insertions(+), 0 deletions(-)

diff --git a/Control/Concurrent/STM/TVar.hs b/Control/Concurrent/STM/TVar.hs
index b3695b6..d445493 100644
--- a/Control/Concurrent/STM/TVar.hs
+++ b/Control/Concurrent/STM/TVar.hs
@@ -20,6 +20,9 @@ module Control.Concurrent.STM.TVar (
 	readTVar,
 	readTVarIO,
 	writeTVar,
+	modifyTVar,
+	modifyTVar',
+	swapTVar,
 #ifdef __GLASGOW_HASKELL__
 	registerDelay
 #endif
@@ -34,3 +37,32 @@ import Control.Sequential.STM
 #if ! (MIN_VERSION_base(4,2,0))
 readTVarIO = atomically . readTVar
 #endif
+
+
+-- Like 'modifyIORef' but for 'TVar'.
+-- | Mutate the contents of a 'TVar'. /N.B./, this version is
+-- non-strict.
+modifyTVar :: TVar a -> (a -> a) -> STM ()
+modifyTVar var f = do
+    x <- readTVar var
+    writeTVar var (f x)
+{-# INLINE modifyTVar #-}
+
+
+-- | Strict version of 'modifyTVar'.
+modifyTVar' :: TVar a -> (a -> a) -> STM ()
+modifyTVar' var f = do
+    x <- readTVar var
+    writeTVar var $! f x
+{-# INLINE modifyTVar' #-}
+
+
+-- Like 'swapTMVar' but for 'TVar'.
+-- | Swap the contents of a 'TVar' for a new value.
+swapTVar :: TVar a -> a -> STM a
+swapTVar var new = do
+    old <- readTVar var
+    writeTVar var new
+    return old
+{-# INLINE swapTVar #-}
+





More information about the Cvs-libraries mailing list