From: simonmar Date: Mon, 5 Dec 2005 11:42:47 +0000 (+0000) Subject: [project @ 2005-12-05 11:42:47 by simonmar] X-Git-Tag: Initial_conversion_from_CVS_complete~10 X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=956ef5fcd15bee4981eec3e666891b3f63c8b345;p=haskell-directory.git [project @ 2005-12-05 11:42:47 by simonmar] Add IO versions of the STM primitives that allocate new transactional variables: newTVarIO :: a -> IO (TVar a) newTChanIO :: IO (TChan a) newTMVarIO :: a -> IO (TMVar a) These can be used inside unsafePerformIO, unlike the STM versions. --- diff --git a/GHC/Conc.lhs b/GHC/Conc.lhs index 6c21cf4..d83a2c3 100644 --- a/GHC/Conc.lhs +++ b/GHC/Conc.lhs @@ -59,6 +59,7 @@ module GHC.Conc , catchSTM -- :: STM a -> (Exception -> STM a) -> STM a , TVar -- abstract , newTVar -- :: a -> STM (TVar a) + , newTVarIO -- :: a -> STM (TVar a) , readTVar -- :: TVar a -> STM a , writeTVar -- :: a -> TVar a -> STM () , unsafeIOToSTM -- :: IO a -> STM a @@ -321,6 +322,15 @@ newTVar val = STM $ \s1# -> case newTVar# val s1# of (# s2#, tvar# #) -> (# s2#, TVar tvar# #) +-- |@IO@ version of 'newTVar'. This is useful for creating top-level +-- 'TVar's using 'System.IO.Unsafe.unsafePerformIO', because using +-- 'atomically' inside 'System.IO.Unsafe.unsafePerformIO' isn't +-- possible. +newTVarIO :: a -> IO (TVar a) +newTVarIO val = IO $ \s1# -> + case newTVar# val s1# of + (# s2#, tvar# #) -> (# s2#, TVar tvar# #) + -- |Return the current value stored in a TVar readTVar :: TVar a -> STM a readTVar (TVar tvar#) = STM $ \s# -> readTVar# tvar# s#