Skip to content

Commit b2d53b0

Browse files
committed
Modify withTMVar to use generalBracket
1 parent 3f16a57 commit b2d53b0

File tree

1 file changed

+13
-10
lines changed
  • ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus

1 file changed

+13
-10
lines changed

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Util.hs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -487,19 +487,22 @@ withTMVarAnd ::
487487
IOLike m
488488
=> StrictTMVar m a
489489
-> (a -> STM m b) -- ^ Additional STM action to run in the same atomically
490-
-- block as the TMVar is acquired
490+
-- block as the TMVar is acquired
491491
-> (a -> b -> m (c, a)) -- ^ Action
492492
-> m c
493493
withTMVarAnd tv guard f =
494-
bracketOnError
494+
fst . fst <$> generalBracket
495495
(atomically $ do
496-
i <- takeTMVar tv
497-
g <- guard i
498-
pure (i, g)
496+
istate <- takeTMVar tv
497+
guarded <- guard istate
498+
pure (istate, guarded)
499499
)
500-
(atomically . putTMVar tv . fst)
501-
(\(s, g) -> do
502-
(x, s') <- f s g
503-
atomically $ putTMVar tv s'
504-
return x
500+
(\(origState, _) -> \case
501+
ExitCaseSuccess (_, newState)
502+
-> atomically $ putTMVar tv newState
503+
ExitCaseException _
504+
-> atomically $ putTMVar tv origState
505+
ExitCaseAbort
506+
-> atomically $ putTMVar tv origState
505507
)
508+
(uncurry f)

0 commit comments

Comments
 (0)