module Data.StateRef.Instances.STM
( STM
, TVar
#ifdef useTMVar
, TMVar
#endif
, atomically
) where
import Data.StateRef.Classes
import Control.Monad.Trans
import Control.Concurrent.STM
instance ReadRef (STM a) STM a where
readRef = id
instance MonadIO m => ReadRef (STM a) m a where
readRef = liftIO . atomically
instance DefaultStateRef (TVar a) STM a
instance NewRef (TVar a) STM a where
newRef = newTVar
instance ReadRef (TVar a) STM a where
readRef = readTVar
instance WriteRef (TVar a) STM a where
writeRef = writeTVar
instance ModifyRef (TVar a) STM a
instance MonadIO m => NewRef (TVar a) m a where
newRef = liftIO . newTVarIO
instance MonadIO m => ReadRef (TVar a) m a where
readRef = liftIO . atomically . readRef
instance MonadIO m => WriteRef (TVar a) m a where
writeRef ref = liftIO . atomically . writeRef ref
instance MonadIO m => ModifyRef (TVar a) m a where
modifyRef ref = liftIO . atomically . modifyRef ref
#ifdef useTMVar
instance NewRef (TMVar a) STM (Maybe a) where
newRef Nothing = newEmptyTMVar
newRef (Just x) = newTMVar x
instance ReadRef (TMVar a) STM (Maybe a) where
readRef tmv = fmap Just (readTMVar tmv) `orElse` return Nothing
instance MonadIO m => NewRef (TMVar a) m (Maybe a) where
newRef Nothing = liftIO newEmptyTMVarIO
newRef (Just x) = liftIO (newTMVarIO x)
instance MonadIO m => ReadRef (TMVar a) m (Maybe a) where
readRef = liftIO . atomically . readRef
#endif