Skip to content

Commit

Permalink
quasar: Add documentation about mapFinalizeTOnce being reentrant-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
queezle42 committed Apr 4, 2024
1 parent a7972d5 commit 0ccd36f
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions quasar/src/Quasar/Utils/TOnce.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ newTOnceIO initial = liftIO do
registry <- newCallbackRegistryIO
TOnce <$> newTVarIO (Left (initial, registry))

-- | Finalizes the `TOnce` by replacing the content, if not already finalized.
finalizeTOnce :: MonadSTMc NoRetry '[TOnceAlreadyFinalized] m => TOnce a b -> b -> m ()
finalizeTOnce (TOnce var) value = liftSTMc @NoRetry @'[TOnceAlreadyFinalized] do
readTVar var >>= \case
Expand All @@ -56,6 +57,9 @@ finalizeTOnce (TOnce var) value = liftSTMc @NoRetry @'[TOnceAlreadyFinalized] do
readTOnce :: MonadSTMc NoRetry '[] m => TOnce a b -> m (Either a b)
readTOnce (TOnce var) = Bifunctor.first fst <$> readTVar var

-- | Finalizes the `TOnce` by running an STM action.
--
-- Reentrant-safe.
mapFinalizeTOnce :: MonadSTMc NoRetry '[] m => TOnce a (Future b) -> (a -> m (Future b)) -> m (Future b)
mapFinalizeTOnce (TOnce var) fn = do
readTVar var >>= \case
Expand Down

0 comments on commit 0ccd36f

Please sign in to comment.