|
| 1 | +{-# LANGUAGE BangPatterns #-} |
| 2 | +{-# LANGUAGE CPP #-} |
| 3 | +{-# LANGUAGE TupleSections #-} |
| 4 | +{-# LANGUAGE TypeFamilies #-} |
| 5 | +{-# LANGUAGE TypeOperators #-} |
| 6 | + |
| 7 | +-- | This module corresponds to 'Control.Concurrent.MVar' in "base" package |
| 8 | +-- |
| 9 | +module Control.Concurrent.Class.MonadMVar.Strict.Checked |
| 10 | + ( -- * StrictMVar |
| 11 | + StrictMVar |
| 12 | + , castStrictMVar |
| 13 | + , toLazyMVar |
| 14 | + , fromLazyMVar |
| 15 | + , newEmptyMVar |
| 16 | + , newEmptyMVarWithInvariant |
| 17 | + , newMVar |
| 18 | + , newMVarWithInvariant |
| 19 | + , takeMVar |
| 20 | + , putMVar |
| 21 | + , readMVar |
| 22 | + , swapMVar |
| 23 | + , tryTakeMVar |
| 24 | + , tryPutMVar |
| 25 | + , isEmptyMVar |
| 26 | + , withMVar |
| 27 | + , withMVarMasked |
| 28 | + , modifyMVar_ |
| 29 | + , modifyMVar |
| 30 | + , modifyMVarMasked_ |
| 31 | + , modifyMVarMasked |
| 32 | + , tryReadMVar |
| 33 | + -- * Re-exports |
| 34 | + , MonadMVar |
| 35 | + ) where |
| 36 | + |
| 37 | +import Control.Concurrent.Class.MonadMVar (MonadMVar) |
| 38 | +import qualified Control.Concurrent.Class.MonadMVar as Lazy |
| 39 | +import GHC.Stack (HasCallStack) |
| 40 | + |
| 41 | +-- |
| 42 | +-- StrictMVar |
| 43 | +-- |
| 44 | + |
| 45 | +type LazyMVar m = Lazy.MVar m |
| 46 | + |
| 47 | +-- | A strict MVar with invariant checking. |
| 48 | +-- |
| 49 | +-- There is a weaker invariant for a 'StrictMVar' than for a 'StrictTVar' (see |
| 50 | +-- the @strict-stm@ package): although all functions that modify the |
| 51 | +-- 'StrictMVar' check the invariant, we do /not/ guarantee that the value inside |
| 52 | +-- the 'StrictMVar' always satisfies the invariant. Instead, we /do/ guarantee |
| 53 | +-- that if the 'StrictMVar' is updated with a value that does not satisfy the |
| 54 | +-- invariant, an exception is thrown. The reason for this weaker guarantee is |
| 55 | +-- that leaving an 'MVar' empty can lead to very hard to debug "blocked |
| 56 | +-- indefinitely" problems. |
| 57 | +data StrictMVar m a = StrictMVar { |
| 58 | + -- | The invariant that is checked whenever the 'StrictMVar' is updated. |
| 59 | + invariant :: !(a -> Maybe String) |
| 60 | + , mvar :: !(LazyMVar m a) |
| 61 | + } |
| 62 | + |
| 63 | +castStrictMVar :: LazyMVar m ~ LazyMVar n |
| 64 | + => StrictMVar m a -> StrictMVar n a |
| 65 | +castStrictMVar v = StrictMVar (invariant v) (mvar v) |
| 66 | + |
| 67 | +-- | Get the underlying @MVar@ |
| 68 | +-- |
| 69 | +-- Since we obviously can not guarantee that updates to this 'LazyMVar' will be |
| 70 | +-- strict, this should be used with caution. |
| 71 | +-- |
| 72 | +-- Similarly, we can not guarantee that updates to this 'LazyMVar' do not break |
| 73 | +-- the original invariant that the 'StrictMVar' held. |
| 74 | +toLazyMVar :: StrictMVar m a -> LazyMVar m a |
| 75 | +toLazyMVar = mvar |
| 76 | + |
| 77 | +-- | Create a 'StrictMVar' from a 'LazyMVar' |
| 78 | +-- |
| 79 | +-- It is not guaranteed that the 'LazyMVar' contains a value that is in WHNF, so |
| 80 | +-- there is no guarantee that the resulting 'StrictMVar' contains a value that |
| 81 | +-- is in WHNF. This should be used with caution. |
| 82 | +-- |
| 83 | +-- The resulting 'StrictMVar' has a trivial invariant. |
| 84 | +fromLazyMVar :: Lazy.MVar m a -> StrictMVar m a |
| 85 | +fromLazyMVar = StrictMVar (const Nothing) |
| 86 | + |
| 87 | +newEmptyMVar :: MonadMVar m => m (StrictMVar m a) |
| 88 | +newEmptyMVar = fromLazyMVar <$> Lazy.newEmptyMVar |
| 89 | + |
| 90 | +newEmptyMVarWithInvariant :: |
| 91 | + MonadMVar m |
| 92 | + => (a -> Maybe String) -> m (StrictMVar m a) |
| 93 | +newEmptyMVarWithInvariant inv = StrictMVar inv <$> Lazy.newEmptyMVar |
| 94 | + |
| 95 | +newMVar :: MonadMVar m => a -> m (StrictMVar m a) |
| 96 | +newMVar !a = fromLazyMVar <$> Lazy.newMVar a |
| 97 | + |
| 98 | +newMVarWithInvariant :: |
| 99 | + MonadMVar m |
| 100 | + => (a -> Maybe String) -> a -> m (StrictMVar m a) |
| 101 | +newMVarWithInvariant inv !a = |
| 102 | + checkInvariant (inv a) $ |
| 103 | + StrictMVar inv <$> Lazy.newMVar a |
| 104 | + |
| 105 | +takeMVar :: MonadMVar m => StrictMVar m a -> m a |
| 106 | +takeMVar = Lazy.takeMVar . mvar |
| 107 | + |
| 108 | +putMVar :: MonadMVar m => StrictMVar m a -> a -> m () |
| 109 | +putMVar v !a = do |
| 110 | + Lazy.putMVar (mvar v) a |
| 111 | + checkInvariant (invariant v a) $ pure () |
| 112 | + |
| 113 | +readMVar :: MonadMVar m => StrictMVar m a -> m a |
| 114 | +readMVar v = Lazy.readMVar (mvar v) |
| 115 | + |
| 116 | +swapMVar :: MonadMVar m => StrictMVar m a -> a -> m a |
| 117 | +swapMVar v !a = do |
| 118 | + oldValue <- Lazy.swapMVar (mvar v) a |
| 119 | + checkInvariant (invariant v a) $ pure oldValue |
| 120 | + |
| 121 | +tryTakeMVar :: MonadMVar m => StrictMVar m a -> m (Maybe a) |
| 122 | +tryTakeMVar v = Lazy.tryTakeMVar (mvar v) |
| 123 | + |
| 124 | +tryPutMVar :: MonadMVar m => StrictMVar m a -> a -> m Bool |
| 125 | +tryPutMVar v !a = do |
| 126 | + didPut <- Lazy.tryPutMVar (mvar v) a |
| 127 | + checkInvariant (invariant v a) $ pure didPut |
| 128 | + |
| 129 | +isEmptyMVar :: MonadMVar m => StrictMVar m a -> m Bool |
| 130 | +isEmptyMVar v = Lazy.isEmptyMVar (mvar v) |
| 131 | + |
| 132 | +withMVar :: MonadMVar m => StrictMVar m a -> (a -> m b) -> m b |
| 133 | +withMVar v = Lazy.withMVar (mvar v) |
| 134 | + |
| 135 | +withMVarMasked :: MonadMVar m => StrictMVar m a -> (a -> m b) -> m b |
| 136 | +withMVarMasked v = Lazy.withMVarMasked (mvar v) |
| 137 | + |
| 138 | +-- | 'modifyMVar_' is defined in terms of 'modifyMVar'. |
| 139 | +modifyMVar_ :: MonadMVar m => StrictMVar m a -> (a -> m a) -> m () |
| 140 | +modifyMVar_ v io = modifyMVar v io' |
| 141 | + where io' a = (,()) <$> io a |
| 142 | + |
| 143 | +modifyMVar :: MonadMVar m => StrictMVar m a -> (a -> m (a,b)) -> m b |
| 144 | +modifyMVar v io = do |
| 145 | + (a', b) <- Lazy.modifyMVar (mvar v) io' |
| 146 | + checkInvariant (invariant v a') $ pure b |
| 147 | + where |
| 148 | + io' a = do |
| 149 | + (!a', b) <- io a |
| 150 | + -- Returning @a'@ along with @b@ allows us to check the invariant /after/ |
| 151 | + -- filling in the MVar. |
| 152 | + pure (a' , (a', b)) |
| 153 | + |
| 154 | +-- | 'modifyMVarMasked_' is defined in terms of 'modifyMVarMasked'. |
| 155 | +modifyMVarMasked_ :: MonadMVar m => StrictMVar m a -> (a -> m a) -> m () |
| 156 | +modifyMVarMasked_ v io = modifyMVar v io' |
| 157 | + where io' a = (,()) <$> io a |
| 158 | + |
| 159 | +modifyMVarMasked :: MonadMVar m => StrictMVar m a -> (a -> m (a,b)) -> m b |
| 160 | +modifyMVarMasked v io = do |
| 161 | + (a', b) <- Lazy.modifyMVar (mvar v) io' |
| 162 | + checkInvariant (invariant v a') $ pure b |
| 163 | + where |
| 164 | + io' a = do |
| 165 | + (!a', b) <- io a |
| 166 | + -- Returning @a'@ along with @b@ allows us to check the invariant /after/ |
| 167 | + -- filling in the MVar. |
| 168 | + pure (a', (a', b)) |
| 169 | + |
| 170 | +tryReadMVar :: MonadMVar m => StrictMVar m a -> m (Maybe a) |
| 171 | +tryReadMVar v = Lazy.tryReadMVar (mvar v) |
| 172 | + |
| 173 | +-- |
| 174 | +-- Dealing with invariants |
| 175 | +-- |
| 176 | + |
| 177 | +-- | Check invariant (if enabled) |
| 178 | +-- |
| 179 | +-- @checkInvariant mErr x@ is equal to @x@ if @mErr == Nothing@, and throws an |
| 180 | +-- error @err@ if @mErr == Just err@. |
| 181 | +checkInvariant :: HasCallStack => Maybe String -> a -> a |
| 182 | + |
| 183 | +#if CHECK_MVAR_INVARIANT |
| 184 | +checkInvariant Nothing k = k |
| 185 | +checkInvariant (Just err) _ = error $ "Invariant violation: " ++ err |
| 186 | +#else |
| 187 | +checkInvariant _err k = k |
| 188 | +#endif |
0 commit comments