From 956ef5fcd15bee4981eec3e666891b3f63c8b345 Mon Sep 17 00:00:00 2001 From: simonmar Date: Mon, 5 Dec 2005 11:42:47 +0000 Subject: [PATCH] [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. --- GHC/Conc.lhs | 10 ++++++++++ 1 file changed, 10 insertions(+) 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# -- 1.7.10.4