[commit: packages/stm] master: Add mkWeakTMVar to Control.Concurrent.STM.TMVar (ed5d29d)

git at git.haskell.org git at git.haskell.org
Mon Dec 15 15:32:35 UTC 2014


Repository : ssh://git@git.haskell.org/stm

On branch  : master
Link       : http://git.haskell.org/packages/stm.git/commitdiff/ed5d29d1e8c658427728ff1d9dece2ddd3bba8e9

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

commit ed5d29d1e8c658427728ff1d9dece2ddd3bba8e9
Author: Bas van Dijk <v.dijk.bas at gmail.com>
Date:   Mon Dec 15 09:33:17 2014 -0600

    Add mkWeakTMVar to Control.Concurrent.STM.TMVar


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

ed5d29d1e8c658427728ff1d9dece2ddd3bba8e9
 Control/Concurrent/STM/TMVar.hs | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/Control/Concurrent/STM/TMVar.hs b/Control/Concurrent/STM/TMVar.hs
index 8458d40..a861bb1 100644
--- a/Control/Concurrent/STM/TMVar.hs
+++ b/Control/Concurrent/STM/TMVar.hs
@@ -34,12 +34,15 @@ module Control.Concurrent.STM.TMVar (
         swapTMVar,
         tryTakeTMVar,
         tryPutTMVar,
-        isEmptyTMVar
+        isEmptyTMVar,
+        mkWeakTMVar
 #endif
   ) where
 
 #ifdef __GLASGOW_HASKELL__
+import GHC.Base
 import GHC.Conc
+import GHC.Weak
 
 import Data.Typeable (Typeable)
 
@@ -150,4 +153,10 @@ isEmptyTMVar (TMVar t) = do
   case m of
     Nothing -> return True
     Just _  -> return False
+
+-- | Make a 'Weak' pointer to a 'TMVar', using the second argument as
+-- a finalizer to run when the 'TMVar' is garbage-collected.
+mkWeakTMVar :: TMVar a -> IO () -> IO (Weak (TMVar a))
+mkWeakTMVar tmv@(TMVar (TVar t#)) f = IO $ \s ->
+    case mkWeak# t# tmv f s of (# s1, w #) -> (# s1, Weak w #)
 #endif



More information about the ghc-commits mailing list