From a80b05631cc0a1e103651b9fb847a0e98693a252 Mon Sep 17 00:00:00 2001 From: Jens Nolte Date: Tue, 14 May 2024 23:39:18 +0200 Subject: [PATCH] quasar: Add throwing variants of readRc and duplicateRc --- quasar/src/Quasar/Resources/Rc.hs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/quasar/src/Quasar/Resources/Rc.hs b/quasar/src/Quasar/Resources/Rc.hs index 01fd94a..9134b3e 100644 --- a/quasar/src/Quasar/Resources/Rc.hs +++ b/quasar/src/Quasar/Resources/Rc.hs @@ -4,7 +4,10 @@ module Quasar.Resources.Rc ( newRcIO, tryReadRc, tryReadRcIO, + readRc, + readRcIO, tryDuplicateRc, + duplicateRc, tryExtractRc, consumeRc, ) where @@ -12,7 +15,7 @@ module Quasar.Resources.Rc ( import Quasar.Prelude import Quasar.Resources import Quasar.Resources.DisposableVar -import Quasar.Exceptions (mkDisposedException) +import Quasar.Exceptions (mkDisposedException, DisposedException(..)) import Control.Exception (finally) -- | A Rc is a disposable readonly data structure that can be cloned. Every copy @@ -90,6 +93,14 @@ tryReadRcIO :: MonadIO m => Rc a -> m (Maybe a) tryReadRcIO (Rc var) = liftIO do (.content) <<$>> tryReadDisposableVarIO var +readRc :: MonadSTMc NoRetry '[DisposedException] m => Rc a -> m a +readRc rc = liftSTMc @NoRetry @'[DisposedException] do + maybe (throwC mkDisposedException) pure =<< tryReadRc rc + +readRcIO :: MonadIO m => Rc a -> m a +readRcIO rc = liftIO do + maybe (throwIO mkDisposedException) pure =<< tryReadRcIO rc + -- | Produces a _new_ lock that points to the same content, but has an -- independent lifetime. The caller has to ensure the new lock is disposed. -- @@ -100,6 +111,12 @@ tryDuplicateRc (Rc var) = liftSTMc @NoRetry @'[] do modifyTVar rc.lockCount succ Rc <$> newSpecialDisposableVar decrementRc rc +duplicateRc :: + (HasCallStack, MonadSTMc NoRetry '[DisposedException] m) => + Rc a -> m (Rc a) +duplicateRc rc = liftSTMc @NoRetry @'[DisposedException] do + maybe (throwC mkDisposedException) pure =<< tryDuplicateRc rc + consumeRc :: Rc a -> (a -> IO b) -> IO b consumeRc rc fn = do flip finally (dispose rc) do